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) const result = tagEventForComment(eventWithMultipleRoots)
// First root should be uppercase expect(result).toEqual([
expect(result.some(tag => tag[0] === "E" && tag[1] === id1)).toBe(true) ["K", String(NOTE)],
// Subsequent roots should be lowercase ["P", pubkey, "pubkey-relay-url"],
expect(result.some(tag => tag[0] === "e" && tag[1] === id2)).toBe(true) ["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", () => { it("should handle events with mixed tag types", () => {
@@ -282,18 +286,16 @@ describe("tags", () => {
} }
const result = tagEventForComment(eventWithMixedTags) const result = tagEventForComment(eventWithMixedTags)
// Should propagate root tags (e, p, i, a) to uppercase expect(result).toEqual([
expect(result.some(tag => tag[0] === "E" && tag[1] === id)).toBe(true) ["K", String(MUTES)],
expect(result.some(tag => tag[0] === "P" && tag[1] === pubkey1)).toBe(true) ["P", pubkey, "pubkey-relay-url"],
expect(result.some(tag => tag[0] === "I" && tag[1] === id1)).toBe(true) ["E", id, "event-relay-url", pubkey],
expect(result.some(tag => tag[0] === "A" && tag[1] === "some-address")).toBe(true) ["A", getAddress(eventWithMixedTags), "event-relay-url", pubkey],
["k", String(MUTES)],
// Should include parent variants in lowercase ["p", pubkey, "pubkey-relay-url"],
expect(result.some(tag => tag[0] === "e" && tag[1] === id)).toBe(true) ["e", id, "event-relay-url", pubkey],
expect(result.some(tag => tag[0] === "p" && tag[1] === pubkey1)).toBe(true) ["a", getAddress(eventWithMixedTags), "event-relay-url", pubkey],
])
// Should not include non-relevant tags
expect(result.some(tag => tag[0] === "custom")).toBe(false)
}) })
it("should add event metadata tags when no root tags exist", () => { 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 pubkeyHint = ctx.app.router.FromPubkey(event.pubkey).getUrl()
const eventHint = ctx.app.router.Event(event).getUrl() const eventHint = ctx.app.router.Event(event).getUrl()
const address = getAddress(event) const address = getAddress(event)
const tags = tagEventPubkeys(event)
const seenRoots = new Set<string>() const seenRoots = new Set<string>()
const tags: string[][] = []
for (const [raw, ...tag] of event.tags) { for (const [t, ...tag] of event.tags) {
const T = raw.toUpperCase() if (["K", "E", "A", "I", "P"].includes(t)) {
const t = raw.toLowerCase()
if (!["k", "e", "a", "i", "p"].includes(t)) {
continue
}
if (seenRoots.has(T)) {
tags.push([t, ...tag]) tags.push([t, ...tag])
} else { seenRoots.add(t)
tags.push([T, ...tag])
seenRoots.add(T)
} }
} }