feat: long form rendering

This commit is contained in:
2023-10-11 11:44:53 +01:00
parent 3b505f6c3e
commit 6eca5a632d
25 changed files with 508 additions and 191 deletions

View File

@ -105,7 +105,6 @@ export abstract class EventExt {
}
static extractThread(ev: NostrEvent) {
const shouldWriteMarkers = ev.kind === EventKind.TextNote;
const ret = {
mentions: [],
pubKeys: [],
@ -115,16 +114,14 @@ export abstract class EventExt {
const marked = replyTags.some(a => a.marker);
if (!marked) {
ret.root = replyTags[0];
ret.root.marker = shouldWriteMarkers ? "root" : undefined;
ret.root.marker = "root";
if (replyTags.length > 1) {
ret.replyTo = replyTags[replyTags.length - 1];
ret.replyTo.marker = shouldWriteMarkers ? "reply" : undefined;
ret.replyTo.marker = "reply";
}
if (replyTags.length > 2) {
ret.mentions = replyTags.slice(1, -1);
if (shouldWriteMarkers) {
ret.mentions.forEach(a => (a.marker = "mention"));
}
ret.mentions.forEach(a => (a.marker = "mention"));
}
} else {
const root = replyTags.find(a => a.marker === "root");

View File

@ -165,14 +165,14 @@ export class EventPublisher {
amount: number,
author: HexKey,
relays: Array<string>,
note?: HexKey,
note?: NostrLink,
msg?: string,
fnExtra?: EventBuilderHook,
) {
const eb = this.#eb(EventKind.ZapRequest);
eb.content(msg ?? "");
if (note) {
eb.tag(["e", note]);
eb.tag(unwrap(note.toEventTag()));
}
eb.tag(["p", author]);
eb.tag(["relays", ...relays.map(a => a.trim())]);
@ -205,7 +205,7 @@ export class EventPublisher {
eb.tag(["p", pk]);
}
} else {
eb.tag([...(NostrLink.fromEvent(replyTo).toEventTag() ?? []), "reply"]);
eb.tag([...(NostrLink.fromEvent(replyTo).toEventTag() ?? []), "root"]);
// dont tag self in replies
if (replyTo.pubkey !== this.#pubKey) {
eb.tag(["p", replyTo.pubkey]);
@ -219,7 +219,7 @@ export class EventPublisher {
async react(evRef: NostrEvent, content = "+") {
const eb = this.#eb(EventKind.Reaction);
eb.content(content);
eb.tag(["e", evRef.id]);
eb.tag(unwrap(NostrLink.fromEvent(evRef).toEventTag()));
eb.tag(["p", evRef.pubkey]);
return await this.#sign(eb);
}
@ -269,7 +269,7 @@ export class EventPublisher {
*/
async repost(note: NostrEvent) {
const eb = this.#eb(EventKind.Repost);
eb.tag(["e", note.id, ""]);
eb.tag(unwrap(NostrLink.fromEvent(note).toEventTag()));
eb.tag(["p", note.pubkey]);
return await this.#sign(eb);
}

View File

@ -1,5 +1,5 @@
import { bech32ToHex, hexToBech32, unwrap } from "@snort/shared";
import { NostrPrefix, decodeTLV, TLVEntryType, encodeTLV, NostrEvent, TaggedNostrEvent } from ".";
import { NostrPrefix, decodeTLV, TLVEntryType, encodeTLV, NostrEvent, TaggedNostrEvent, EventExt, Tag } from ".";
import { findTag } from "./utils";
export class NostrLink {
@ -43,6 +43,79 @@ export class NostrLink {
return false;
}
/**
* Is the supplied event a reply to this link
*/
isReplyToThis(ev: NostrEvent) {
const thread = EventExt.extractThread(ev);
if (!thread) return false; // non-thread events are not replies
if (!thread.root) return false; // must have root marker or positional e/a tag in position 0
if (
thread.root.key === "e" &&
thread.root.value === this.id &&
(this.type === NostrPrefix.Event || this.type === NostrPrefix.Note)
) {
return true;
}
if (thread.root.key === "a" && this.type === NostrPrefix.Address) {
const [kind, author, dTag] = unwrap(thread.root.value).split(":");
if (Number(kind) === this.kind && author === this.author && dTag === this.id) {
return true;
}
}
return false;
}
/**
* Does the supplied event contain a tag matching this link
*/
referencesThis(ev: NostrEvent) {
for (const t of ev.tags) {
if (t[0] === "e" && t[1] === this.id && (this.type === NostrPrefix.Event || this.type === NostrPrefix.Note)) {
return true;
}
if (t[0] === "a" && this.type === NostrPrefix.Address) {
const [kind, author, dTag] = t[1].split(":");
if (Number(kind) === this.kind && author === this.author && dTag === this.id) {
return true;
}
}
if (
t[0] === "p" &&
(this.type === NostrPrefix.Profile || this.type === NostrPrefix.PublicKey) &&
this.id === t[1]
) {
return true;
}
}
return false;
}
equals(other: NostrLink) {
if (other.type === this.type && this.type === NostrPrefix.Address) {
}
}
static fromThreadTag(tag: Tag) {
const relay = tag.relay ? [tag.relay] : undefined;
switch (tag.key) {
case "e": {
return new NostrLink(NostrPrefix.Event, unwrap(tag.value), undefined, undefined, relay);
}
case "p": {
return new NostrLink(NostrPrefix.Profile, unwrap(tag.value), undefined, undefined, relay);
}
case "a": {
const [kind, author, dTag] = unwrap(tag.value).split(":");
return new NostrLink(NostrPrefix.Address, dTag, Number(kind), author, relay);
}
}
throw new Error(`Unknown tag kind ${tag.key}`);
}
static fromTag(tag: Array<string>) {
const relays = tag.length > 2 ? [tag[2]] : undefined;
switch (tag[0]) {