fix: use last e/a tag for reply context

This commit is contained in:
2023-10-13 11:10:55 +01:00
parent 85faf528c5
commit 430763478f

View File

@ -1,5 +1,15 @@
import { bech32ToHex, hexToBech32, unwrap } from "@snort/shared";
import { NostrPrefix, decodeTLV, TLVEntryType, encodeTLV, NostrEvent, TaggedNostrEvent, EventExt, Tag } from ".";
import {
NostrPrefix,
decodeTLV,
TLVEntryType,
encodeTLV,
NostrEvent,
TaggedNostrEvent,
EventExt,
Tag,
EventKind,
} from ".";
import { findTag } from "./utils";
export class NostrLink {
@ -47,6 +57,25 @@ export class NostrLink {
* Is the supplied event a reply to this link
*/
isReplyToThis(ev: NostrEvent) {
const NonNip10Kinds = [EventKind.Reaction, EventKind.Repost, EventKind.ZapReceipt];
if (NonNip10Kinds.includes(ev.kind)) {
const lastRef = ev.tags.findLast(a => a[0] === "e" || a[1] === "a");
if (!lastRef) return false;
if (
lastRef[0] === "e" &&
lastRef[1] === this.id &&
(this.type === NostrPrefix.Event || this.type === NostrPrefix.Note)
) {
return true;
}
if (lastRef[0] === "a" && this.type === NostrPrefix.Address) {
const [kind, author, dTag] = lastRef[1].split(":");
if (Number(kind) === this.kind && author === this.author && dTag === this.id) {
return true;
}
}
} else {
const thread = EventExt.extractThread(ev);
if (!thread) return false; // non-thread events are not replies
@ -65,6 +94,7 @@ export class NostrLink {
return true;
}
}
}
return false;
}