NostrLink.encode(prefix: NostrPrefix)
All checks were successful
continuous-integration/drone/pr Build is passing

This commit is contained in:
Martti Malmi 2023-10-18 10:01:25 +03:00
parent 700db8f62c
commit 2e663dcb4c
17 changed files with 31 additions and 49 deletions

View File

@ -9,10 +9,7 @@ import { DeckContext } from "Pages/DeckLayout";
export default function Articles() {
const data = useArticles();
const deck = useContext(DeckContext);
const related = useReactions(
"articles:reactions",
data.data?.map(v => NostrLink.fromEvent(v, CONFIG.eventLinkPrefix)) ?? [],
);
const related = useReactions("articles:reactions", data.data?.map(v => NostrLink.fromEvent(v)) ?? []);
return (
<>

View File

@ -12,7 +12,7 @@ export default function ZapstrEmbed({ ev }: { ev: NostrEvent }) {
const subject = ev.tags.find(a => a[0] === "subject");
const refPersons = ev.tags.filter(a => a[0] === "p");
const link = NostrLink.fromEvent(ev, CONFIG.eventLinkPrefix).encode();
const link = NostrLink.fromEvent(ev).encode(CONFIG.eventLinkPrefix);
return (
<>
<div className="flex zapstr mb10 card">

View File

@ -45,7 +45,7 @@ export function NoteContextMenu({ ev, ...props }: NosteContextMenuProps) {
}
async function share() {
const link = NostrLink.fromEvent(ev, CONFIG.eventLinkPrefix).encode();
const link = NostrLink.fromEvent(ev).encode(CONFIG.eventLinkPrefix);
const url = `${window.location.protocol}//${window.location.host}/${link}`;
if ("share" in window.navigator) {
await window.navigator.share({
@ -81,7 +81,7 @@ export function NoteContextMenu({ ev, ...props }: NosteContextMenuProps) {
}
async function copyId() {
const link = NostrLink.fromEvent(ev).encode();
const link = NostrLink.fromEvent(ev).encode(CONFIG.eventLinkPrefix);
await navigator.clipboard.writeText(link);
}

View File

@ -104,7 +104,7 @@ export function NoteCreator() {
note.note += "\n";
}
const link = NostrLink.fromEvent(note.quote);
note.note += `nostr:${link.encode()}`;
note.note += `nostr:${link.encode(CONFIG.eventLinkPrefix)}`;
const quoteTag = link.toEventTag();
if (quoteTag) {
extraTags ??= [];

View File

@ -123,7 +123,7 @@ export default function NoteFooter(props: NoteFooterProps) {
name: getDisplayName(author, ev.pubkey),
zap: {
pubkey: ev.pubkey,
event: NostrLink.fromEvent(ev, CONFIG.eventLinkPrefix),
event: NostrLink.fromEvent(ev),
},
} as ZapTarget,
];

View File

@ -151,12 +151,12 @@ export function NoteInner(props: NoteProps) {
return;
}
const link = NostrLink.fromEvent(eTarget, CONFIG.eventLinkPrefix);
const link = NostrLink.fromEvent(eTarget);
// detect cmd key and open in new tab
if (e.metaKey) {
window.open(`/${link.encode()}`, "_blank");
window.open(`/${link.encode(CONFIG.eventLinkPrefix)}`, "_blank");
} else {
navigate(`/${link.encode()}`, {
navigate(`/${link.encode(CONFIG.eventLinkPrefix)}`, {
state: eTarget,
});
}

View File

@ -58,13 +58,8 @@ export default function Poll(props: PollProps) {
setVoting(opt);
const r = Object.keys(relays.item);
const zap = await publisher.zap(
amount * 1000,
props.ev.pubkey,
r,
NostrLink.fromEvent(props.ev, CONFIG.eventLinkPrefix),
undefined,
eb => eb.tag(["poll_option", opt.toString()]),
const zap = await publisher.zap(amount * 1000, props.ev.pubkey, r, NostrLink.fromEvent(props.ev), undefined, eb =>
eb.tag(["poll_option", opt.toString()]),
);
const lnurl = props.ev.tags.find(a => a[0] === "zap")?.[1] || pollerProfile?.lud16 || pollerProfile?.lud06;

View File

@ -283,7 +283,7 @@ export function Thread(props: { onBack?: () => void; disableSpotlight?: boolean
notes={replies}
related={getAllLinkReactions(
thread.reactions,
replies.map(a => NostrLink.fromEvent(a, CONFIG.eventLinkPrefix)),
replies.map(a => NostrLink.fromEvent(a)),
)}
chains={thread.chains}
onNavigate={navigateThread}

View File

@ -12,7 +12,7 @@ import { FormattedNumber } from "react-intl";
export function ZapGoal({ ev }: { ev: NostrEvent }) {
const [zap, setZap] = useState(false);
const zaps = useZapsFeed(NostrLink.fromEvent(ev, CONFIG.eventLinkPrefix));
const zaps = useZapsFeed(NostrLink.fromEvent(ev));
const target = Number(findTag(ev, "amount"));
const amount = zaps.reduce((acc, v) => (acc += v.amount * 1000), 0);
const progress = amount / target;

View File

@ -44,7 +44,7 @@ export function LiveEvent({ ev }: { ev: NostrEvent }) {
}
function cta() {
const link = `https://zap.stream/${NostrLink.fromEvent(ev, CONFIG.eventLinkPrefix).encode()}`;
const link = `https://zap.stream/${NostrLink.fromEvent(ev).encode(CONFIG.eventLinkPrefix)}`;
switch (status) {
case "live": {
return (

View File

@ -32,7 +32,7 @@ function LiveStreamEvent({ ev }: { ev: NostrEvent }) {
const image = findTag(ev, "image");
const status = findTag(ev, "status");
const link = NostrLink.fromEvent(ev).encode();
const link = NostrLink.fromEvent(ev).encode(CONFIG.eventLinkPrefix);
const imageProxy = proxy(image ?? "");
return (

View File

@ -185,7 +185,7 @@ function MediaCol({ setThread }: { setThread: (e: NostrLink) => void }) {
"--img": `url(${proxy(images[0].content)})`,
} as CSSProperties
}
onClick={() => setThread(NostrLink.fromEvent(e, CONFIG.eventLinkPrefix))}></div>
onClick={() => setThread(NostrLink.fromEvent(e))}></div>
);
}}
/>

View File

@ -238,7 +238,7 @@ export default function ProfilePage({ id: propId, state }: ProfilePageProps) {
<Note
key={`pinned-${n.id}`}
data={n}
related={getLinkReactions(pinned, NostrLink.fromEvent(n, CONFIG.eventLinkPrefix))}
related={getLinkReactions(pinned, NostrLink.fromEvent(n))}
options={{ showTime: false, showPinned: true, canUnpin: id === loginPubKey }}
/>
);

View File

@ -148,7 +148,7 @@ export const NotesTab = () => {
noteOnClick={
deckContext
? ev => {
deckContext.setThread(NostrLink.fromEvent(ev, CONFIG.eventLinkPrefix));
deckContext.setThread(NostrLink.fromEvent(ev));
}
: undefined
}

View File

@ -110,16 +110,6 @@ export function eventLink(hex: u256, relays?: Array<string> | string) {
return `/${encoded}`;
}
/**
* Convert hex pubkey to bech32 link url
*/
export function profileLink(hex: HexKey, relays?: Array<string> | string) {
const encoded = relays
? encodeTLV(NostrPrefix.Profile, hex, Array.isArray(relays) ? relays : [relays])
: hexToBech32(NostrPrefix.PublicKey, hex);
return `/${encoded}`;
}
/**
* Convert hex to bech32
*/

View File

@ -56,7 +56,7 @@ export class Zapper {
weight: Number(v[3] ?? 0),
zap: {
pubkey: v[1],
event: NostrLink.fromEvent(ev, CONFIG.eventLinkPrefix),
event: NostrLink.fromEvent(ev),
},
} as ZapTarget;
} else {
@ -67,7 +67,7 @@ export class Zapper {
weight: 1,
zap: {
pubkey: ev.pubkey,
event: NostrLink.fromEvent(ev, CONFIG.eventLinkPrefix),
event: NostrLink.fromEvent(ev),
},
} as ZapTarget;
}

View File

@ -1,14 +1,14 @@
import { bech32ToHex, hexToBech32, unwrap } from "@snort/shared";
import {
NostrPrefix,
decodeTLV,
TLVEntryType,
encodeTLV,
NostrEvent,
TaggedNostrEvent,
EventExt,
Tag,
EventKind,
NostrEvent,
NostrPrefix,
Tag,
TaggedNostrEvent,
TLVEntryType,
} from ".";
import { findTag } from "./utils";
@ -21,11 +21,11 @@ export class NostrLink {
readonly relays?: Array<string>,
) {}
encode(): string {
if (this.type === NostrPrefix.Note || this.type === NostrPrefix.PrivateKey || this.type === NostrPrefix.PublicKey) {
return hexToBech32(this.type, this.id);
encode(type: NostrPrefix = this.type): string {
if (type === NostrPrefix.Note || type === NostrPrefix.PrivateKey || type === NostrPrefix.PublicKey) {
return hexToBech32(type, this.id);
} else {
return encodeTLV(this.type, this.id, this.relays, this.kind, this.author);
return encodeTLV(type, this.id, this.relays, this.kind, this.author);
}
}
@ -163,14 +163,14 @@ export class NostrLink {
throw new Error(`Unknown tag kind ${tag[0]}`);
}
static fromEvent(ev: TaggedNostrEvent | NostrEvent, prefixHint = NostrPrefix.Event) {
static fromEvent(ev: TaggedNostrEvent | NostrEvent) {
const relays = "relays" in ev ? ev.relays : undefined;
if (ev.kind >= 30_000 && ev.kind < 40_000) {
const dTag = unwrap(findTag(ev, "d"));
return new NostrLink(NostrPrefix.Address, dTag, ev.kind, ev.pubkey, relays);
}
return new NostrLink(prefixHint, ev.id, ev.kind, ev.pubkey, relays);
return new NostrLink(NostrPrefix.Event, ev.id, ev.kind, ev.pubkey, relays);
}
}