Add support for nip 22 tags

This commit is contained in:
Ticruz
2025-01-17 16:53:16 +01:00
committed by Jon Staab
parent b440fdc84a
commit 856055f99c
11 changed files with 148 additions and 58 deletions
+40 -7
View File
@@ -1,8 +1,9 @@
import {verifiedSymbol, getEventHash, verifyEvent} from "nostr-tools/pure"
import {cached, pick, now} from "@welshman/lib"
import {getAncestorTagValues} from "./Tags.js"
import {getAddress} from "./Address.js"
import {cached, mapVals, first, pick, now} from "@welshman/lib"
import {getReplyTagValues, getCommentTagValues} from "./Tags.js"
import {getAddress, Address} from "./Address.js"
import {
COMMENT,
isEphemeralKind,
isReplaceableKind,
isPlainReplaceableKind,
@@ -135,9 +136,41 @@ export const isPlainReplaceable = (e: EventTemplate) => isPlainReplaceableKind(e
export const isParameterizedReplaceable = (e: EventTemplate) =>
isParameterizedReplaceableKind(e.kind)
export const isChildOf = (child: EventContent, parent: HashedEvent) => {
const {roots, replies} = getAncestorTagValues(child.tags)
const parentIds = replies.length > 0 ? replies : roots
export const getAncestors = ({kind, tags}: EventTemplate) =>
kind === COMMENT ? getCommentTagValues(tags) : getReplyTagValues(tags)
return getIdAndAddress(parent).some(x => parentIds.includes(x))
export const getParentIdsAndAddrs = (event: EventTemplate) => {
const {roots, replies} = getAncestors(event)
return replies.length > 0 ? replies : roots
}
export const getParentIdOrAddr = (event: EventTemplate) => first(getParentIdsAndAddrs(event))
export const getParentIds = (event: EventTemplate) => {
const {roots, replies} = mapVals(
ids => ids.filter(id => !Address.isAddress(id)),
getAncestors(event),
)
return replies.length > 0 ? replies : roots
}
export const getParentId = (event: EventTemplate) => first(getParentIds(event))
export const getParentAddrs = (event: EventTemplate) => {
const {roots, replies} = mapVals(
ids => ids.filter(id => Address.isAddress(id)),
getAncestors(event),
)
return replies.length > 0 ? replies : roots
}
export const getParentAddr = (event: EventTemplate) => first(getParentAddrs(event))
export const isChildOf = (child: EventTemplate, parent: HashedEvent) => {
const idsAndAddrs = getParentIdsAndAddrs(child)
return getIdAndAddress(parent).some(x => idsAndAddrs.includes(x))
}
+13 -3
View File
@@ -54,7 +54,17 @@ export const getKindTags = (tags: string[][]) =>
export const getKindTagValues = (tags: string[][]) => getKindTags(tags).map(t => parseInt(t[1]))
export const getAncestorTags = (tags: string[][]) => {
export const getCommentTags = (tags: string[][]) => {
const roots = tags.filter(t => ["A", "E", "P", "K"].includes(t[0]))
const replies = tags.filter(t => ["a", "e", "p", "k"].includes(t[0]))
return {roots, replies}
}
export const getCommentTagValues = (tags: string[][]) =>
mapVals(tags => tags.map(nth(1)), getCommentTags(tags))
export const getReplyTags = (tags: string[][]) => {
const validTags = tags.filter(t => ["a", "e", "q"].includes(t[0]))
const mentionTags = validTags.filter(nthEq(0, "q"))
const roots: string[][] = []
@@ -90,8 +100,8 @@ export const getAncestorTags = (tags: string[][]) => {
return {roots, replies, mentions}
}
export const getAncestorTagValues = (tags: string[][]) =>
mapVals(tags => tags.map(nth(1)), getAncestorTags(tags))
export const getReplyTagValues = (tags: string[][]) =>
mapVals(tags => tags.map(nth(1)), getReplyTags(tags))
export const uniqTags = (tags: string[][]) => uniqBy(t => t.join(":"), tags)