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 Icon from "Icons/Icon";
import Spinner from "Icons/Spinner";
@ -36,7 +36,9 @@ export default function WriteMessage({ chat }: { chat: Chat }) {
if (file) {
const rx = await uploader.upload(file, file.name);
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}`);
setOtherEvents([...otherEvents, rx.header]);
} else if (rx.url) {

View File

@ -168,7 +168,9 @@ export function NoteCreator() {
const rx = await uploader.upload(file, file.name);
note.update(v => {
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.otherEvents = [...(v.otherEvents ?? []), rx.header];
} else if (rx.url) {

View File

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

View File

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

View File

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

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;
switch (tag.key) {
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": {
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}`);
}
static fromTag(tag: Array<string>, eventLinkPrefix = NostrPrefix.Event) {
static fromTag(tag: Array<string>) {
const relays = tag.length > 2 ? [tag[2]] : undefined;
switch (tag[0]) {
case "e": {
return new NostrLink(eventLinkPrefix, tag[1], undefined, undefined, relays);
return new NostrLink(NostrPrefix.Event, tag[1], undefined, undefined, relays);
}
case "p": {
return new NostrLink(NostrPrefix.Profile, tag[1], undefined, undefined, relays);