Get rid of domain module, allow app to override default event type

This commit is contained in:
Jon Staab
2024-08-13 15:45:20 -07:00
parent 5a63273b9d
commit 149c29472c
33 changed files with 201 additions and 285 deletions
+49
View File
@@ -0,0 +1,49 @@
import {parseJson, nth, nthEq} from "@welshman/lib"
import {Address} from "./Address"
import {isShareableRelayUrl} from "./Relay"
import {Encryptable, DecryptedEvent} from "./Encryptable"
export type ListParams = {
kind: number
}
export type List = ListParams & {
publicTags: string[][]
privateTags: string[][]
event?: DecryptedEvent
}
export type PublishedList = Omit<List, "event"> & {
event: DecryptedEvent
}
export const makeList = (list: ListParams & Partial<List>): List =>
({publicTags: [], privateTags: [], ...list})
const isValidTag = (tag: string[]) => {
if (tag[0] === "p") return tag[1]?.length === 64
if (tag[0] === "e") return tag[1]?.length === 64
if (tag[0] === "a") return Address.isAddress(tag[1] || "")
if (tag[0] === "t") return tag[1]?.length > 0
if (tag[0] === "r") return isShareableRelayUrl(tag[1])
if (tag[0] === "relay") return isShareableRelayUrl(tag[1])
return true
}
export const readList = (event: DecryptedEvent): PublishedList => {
const getTags = (tags: string[][]) => (Array.isArray(tags) ? tags.filter(isValidTag) : [])
const privateTags = getTags(parseJson(event.plaintext?.content))
const publicTags = getTags(event.tags)
return {event, kind: event.kind, publicTags, privateTags}
}
export const createList = ({kind, publicTags = [], privateTags = []}: List) =>
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
export const editList = ({kind, publicTags = [], privateTags = []}: PublishedList) =>
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
export const getListValues = (tagName: string, list: List | undefined) =>
[...list?.publicTags || [], ...list?.privateTags || []].filter(nthEq(0, tagName)).map(nth(1))