import {parseJson} from "@welshman/lib" import {Address, isShareableRelayUrl, TrustedEvent} from "@welshman/util" import {Encryptable, DecryptedEvent} from "./util" export type ListParams = { kind: number } export type List = ListParams & { publicTags: string[][] privateTags: string[][] event?: DecryptedEvent } export type PublishedList = Omit, "event"> & { event: DecryptedEvent } export const makeList = (list: ListParams & Partial>): 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)})