Simplify comment tags

This commit is contained in:
Jon Staab
2025-03-03 12:53:52 -08:00
parent 100f80d4b8
commit 3e289e9eac
2 changed files with 22 additions and 29 deletions
+18 -16
View File
@@ -262,10 +262,14 @@ describe("tags", () => {
}
const result = tagEventForComment(eventWithMultipleRoots)
// First root should be uppercase
expect(result.some(tag => tag[0] === "E" && tag[1] === id1)).toBe(true)
// Subsequent roots should be lowercase
expect(result.some(tag => tag[0] === "e" && tag[1] === id2)).toBe(true)
expect(result).toEqual([
["K", String(NOTE)],
["P", pubkey, "pubkey-relay-url"],
["E", id, "event-relay-url", pubkey],
["k", String(NOTE)],
["p", pubkey, "pubkey-relay-url"],
["e", id, "event-relay-url", pubkey],
])
})
it("should handle events with mixed tag types", () => {
@@ -282,18 +286,16 @@ describe("tags", () => {
}
const result = tagEventForComment(eventWithMixedTags)
// Should propagate root tags (e, p, i, a) to uppercase
expect(result.some(tag => tag[0] === "E" && tag[1] === id)).toBe(true)
expect(result.some(tag => tag[0] === "P" && tag[1] === pubkey1)).toBe(true)
expect(result.some(tag => tag[0] === "I" && tag[1] === id1)).toBe(true)
expect(result.some(tag => tag[0] === "A" && tag[1] === "some-address")).toBe(true)
// Should include parent variants in lowercase
expect(result.some(tag => tag[0] === "e" && tag[1] === id)).toBe(true)
expect(result.some(tag => tag[0] === "p" && tag[1] === pubkey1)).toBe(true)
// Should not include non-relevant tags
expect(result.some(tag => tag[0] === "custom")).toBe(false)
expect(result).toEqual([
["K", String(MUTES)],
["P", pubkey, "pubkey-relay-url"],
["E", id, "event-relay-url", pubkey],
["A", getAddress(eventWithMixedTags), "event-relay-url", pubkey],
["k", String(MUTES)],
["p", pubkey, "pubkey-relay-url"],
["e", id, "event-relay-url", pubkey],
["a", getAddress(eventWithMixedTags), "event-relay-url", pubkey],
])
})
it("should add event metadata tags when no root tags exist", () => {
+4 -13
View File
@@ -97,22 +97,13 @@ export const tagEventForComment = (event: TrustedEvent) => {
const pubkeyHint = ctx.app.router.FromPubkey(event.pubkey).getUrl()
const eventHint = ctx.app.router.Event(event).getUrl()
const address = getAddress(event)
const tags = tagEventPubkeys(event)
const seenRoots = new Set<string>()
const tags: string[][] = []
for (const [raw, ...tag] of event.tags) {
const T = raw.toUpperCase()
const t = raw.toLowerCase()
if (!["k", "e", "a", "i", "p"].includes(t)) {
continue
}
if (seenRoots.has(T)) {
for (const [t, ...tag] of event.tags) {
if (["K", "E", "A", "I", "P"].includes(t)) {
tags.push([t, ...tag])
} else {
tags.push([T, ...tag])
seenRoots.add(T)
seenRoots.add(t)
}
}