chore: always add prefix on encode

This commit is contained in:
Kieran 2023-10-18 09:50:26 +01:00
parent 81df18ea4e
commit a081f9655e
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
6 changed files with 22 additions and 20 deletions

View File

@ -1,4 +1,4 @@
import { NostrEvent, NostrLink } from "@snort/system"; import { NostrEvent, NostrLink, NostrPrefix } from "@snort/system";
import useEventPublisher from "Hooks/useEventPublisher"; import useEventPublisher from "Hooks/useEventPublisher";
import Icon from "Icons/Icon"; import Icon from "Icons/Icon";
import Spinner from "Icons/Spinner"; import Spinner from "Icons/Spinner";
@ -36,7 +36,9 @@ export default function WriteMessage({ chat }: { chat: Chat }) {
if (file) { if (file) {
const rx = await uploader.upload(file, file.name); const rx = await uploader.upload(file, file.name);
if (rx.header) { if (rx.header) {
const link = `nostr:${new NostrLink(CONFIG.eventLinkPrefix, rx.header.id, rx.header.kind).encode()}`; const link = `nostr:${new NostrLink(NostrPrefix.Event, rx.header.id, rx.header.kind).encode(
CONFIG.eventLinkPrefix,
)}`;
setMsg(`${msg ? `${msg}\n` : ""}${link}`); setMsg(`${msg ? `${msg}\n` : ""}${link}`);
setOtherEvents([...otherEvents, rx.header]); setOtherEvents([...otherEvents, rx.header]);
} else if (rx.url) { } else if (rx.url) {

View File

@ -168,7 +168,9 @@ export function NoteCreator() {
const rx = await uploader.upload(file, file.name); const rx = await uploader.upload(file, file.name);
note.update(v => { note.update(v => {
if (rx.header) { if (rx.header) {
const link = `nostr:${new NostrLink(CONFIG.eventLinkPrefix, rx.header.id, rx.header.kind).encode()}`; const link = `nostr:${new NostrLink(NostrPrefix.Event, rx.header.id, rx.header.kind).encode(
CONFIG.eventLinkPrefix,
)}`;
v.note = `${v.note ? `${v.note}\n` : ""}${link}`; v.note = `${v.note ? `${v.note}\n` : ""}${link}`;
v.otherEvents = [...(v.otherEvents ?? []), rx.header]; v.otherEvents = [...(v.otherEvents ?? []), rx.header];
} else if (rx.url) { } else if (rx.url) {

View File

@ -203,6 +203,7 @@ export function NoteInner(props: NoteProps) {
const pubMentions = const pubMentions =
mentions.length > maxMentions ? mentions?.slice(0, maxMentions).map(renderMention) : mentions?.map(renderMention); mentions.length > maxMentions ? mentions?.slice(0, maxMentions).map(renderMention) : mentions?.map(renderMention);
const others = mentions.length > maxMentions ? formatMessage(messages.Others, { n: othersLength }) : ""; const others = mentions.length > maxMentions ? formatMessage(messages.Others, { n: othersLength }) : "";
const link = replyLink?.encode(CONFIG.eventLinkPrefix);
return ( return (
<div className="reply"> <div className="reply">
re:&nbsp; re:&nbsp;
@ -211,7 +212,7 @@ export function NoteInner(props: NoteProps) {
{pubMentions} {others} {pubMentions} {others}
</> </>
) : ( ) : (
replyLink && <Link to={`/${replyLink.encode()}`}>{replyLink.encode().substring(0, 12)}</Link> replyLink && <Link to={`/${link}`}>{link?.substring(0, 12)}</Link>
)} )}
</div> </div>
); );

View File

@ -37,12 +37,12 @@ export function ProfileLink({
return `/${username}`; return `/${username}`;
} }
return `/${new NostrLink( return `/${new NostrLink(
CONFIG.profileLinkPrefix, NostrPrefix.Profile,
pubkey, pubkey,
undefined, undefined,
undefined, undefined,
relays ? randomSample(relays, 3) : undefined, relays ? randomSample(relays, 3) : undefined,
).encode()}`; ).encode(CONFIG.profileLinkPrefix)}`;
} }
if (link && (link.type === NostrPrefix.Profile || link.type === NostrPrefix.PublicKey)) { if (link && (link.type === NostrPrefix.Profile || link.type === NostrPrefix.PublicKey)) {
return `/${link.encode()}`; return `/${link.encode()}`;

View File

@ -39,7 +39,7 @@ export default function useThreadFeed(link: NostrLink) {
const links = store.data const links = store.data
.map(a => [ .map(a => [
NostrLink.fromEvent(a), NostrLink.fromEvent(a),
...a.tags.filter(a => a[0] === "e" || a[0] === "a").map(v => NostrLink.fromTag(v, CONFIG.eventLinkPrefix)), ...a.tags.filter(a => a[0] === "e" || a[0] === "a").map(v => NostrLink.fromTag(v)),
]) ])
.flat(); .flat();
setAllEvents(links); setAllEvents(links);
@ -51,15 +51,12 @@ export default function useThreadFeed(link: NostrLink) {
const rootOrReplyAsRoot = t?.root ?? t?.replyTo; const rootOrReplyAsRoot = t?.root ?? t?.replyTo;
if (rootOrReplyAsRoot) { if (rootOrReplyAsRoot) {
setRoot( setRoot(
NostrLink.fromTag( NostrLink.fromTag([
[ rootOrReplyAsRoot.key,
rootOrReplyAsRoot.key, rootOrReplyAsRoot.value ?? "",
rootOrReplyAsRoot.value ?? "", rootOrReplyAsRoot.relay ?? "",
rootOrReplyAsRoot.relay ?? "", ...(rootOrReplyAsRoot.marker ?? []),
...(rootOrReplyAsRoot.marker ?? []), ]),
],
CONFIG.eventLinkPrefix,
),
); );
} }
} }

View File

@ -128,12 +128,12 @@ export class NostrLink {
} }
} }
static fromThreadTag(tag: Tag, eventLinkPrefix = NostrPrefix.Event) { static fromThreadTag(tag: Tag) {
const relay = tag.relay ? [tag.relay] : undefined; const relay = tag.relay ? [tag.relay] : undefined;
switch (tag.key) { switch (tag.key) {
case "e": { case "e": {
return new NostrLink(eventLinkPrefix, unwrap(tag.value), undefined, undefined, relay); return new NostrLink(NostrPrefix.Event, unwrap(tag.value), undefined, undefined, relay);
} }
case "p": { case "p": {
return new NostrLink(NostrPrefix.Profile, unwrap(tag.value), undefined, undefined, relay); return new NostrLink(NostrPrefix.Profile, unwrap(tag.value), undefined, undefined, relay);
@ -146,11 +146,11 @@ export class NostrLink {
throw new Error(`Unknown tag kind ${tag.key}`); throw new Error(`Unknown tag kind ${tag.key}`);
} }
static fromTag(tag: Array<string>, eventLinkPrefix = NostrPrefix.Event) { static fromTag(tag: Array<string>) {
const relays = tag.length > 2 ? [tag[2]] : undefined; const relays = tag.length > 2 ? [tag[2]] : undefined;
switch (tag[0]) { switch (tag[0]) {
case "e": { case "e": {
return new NostrLink(eventLinkPrefix, tag[1], undefined, undefined, relays); return new NostrLink(NostrPrefix.Event, tag[1], undefined, undefined, relays);
} }
case "p": { case "p": {
return new NostrLink(NostrPrefix.Profile, tag[1], undefined, undefined, relays); return new NostrLink(NostrPrefix.Profile, tag[1], undefined, undefined, relays);