forked from Kieran/snort
NostrLink.encode(prefix: NostrPrefix)
This commit is contained in:
parent
700db8f62c
commit
2e663dcb4c
@ -9,10 +9,7 @@ import { DeckContext } from "Pages/DeckLayout";
|
|||||||
export default function Articles() {
|
export default function Articles() {
|
||||||
const data = useArticles();
|
const data = useArticles();
|
||||||
const deck = useContext(DeckContext);
|
const deck = useContext(DeckContext);
|
||||||
const related = useReactions(
|
const related = useReactions("articles:reactions", data.data?.map(v => NostrLink.fromEvent(v)) ?? []);
|
||||||
"articles:reactions",
|
|
||||||
data.data?.map(v => NostrLink.fromEvent(v, CONFIG.eventLinkPrefix)) ?? [],
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -12,7 +12,7 @@ export default function ZapstrEmbed({ ev }: { ev: NostrEvent }) {
|
|||||||
const subject = ev.tags.find(a => a[0] === "subject");
|
const subject = ev.tags.find(a => a[0] === "subject");
|
||||||
const refPersons = ev.tags.filter(a => a[0] === "p");
|
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 (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="flex zapstr mb10 card">
|
<div className="flex zapstr mb10 card">
|
||||||
|
@ -45,7 +45,7 @@ export function NoteContextMenu({ ev, ...props }: NosteContextMenuProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function share() {
|
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}`;
|
const url = `${window.location.protocol}//${window.location.host}/${link}`;
|
||||||
if ("share" in window.navigator) {
|
if ("share" in window.navigator) {
|
||||||
await window.navigator.share({
|
await window.navigator.share({
|
||||||
@ -81,7 +81,7 @@ export function NoteContextMenu({ ev, ...props }: NosteContextMenuProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function copyId() {
|
async function copyId() {
|
||||||
const link = NostrLink.fromEvent(ev).encode();
|
const link = NostrLink.fromEvent(ev).encode(CONFIG.eventLinkPrefix);
|
||||||
await navigator.clipboard.writeText(link);
|
await navigator.clipboard.writeText(link);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +104,7 @@ export function NoteCreator() {
|
|||||||
note.note += "\n";
|
note.note += "\n";
|
||||||
}
|
}
|
||||||
const link = NostrLink.fromEvent(note.quote);
|
const link = NostrLink.fromEvent(note.quote);
|
||||||
note.note += `nostr:${link.encode()}`;
|
note.note += `nostr:${link.encode(CONFIG.eventLinkPrefix)}`;
|
||||||
const quoteTag = link.toEventTag();
|
const quoteTag = link.toEventTag();
|
||||||
if (quoteTag) {
|
if (quoteTag) {
|
||||||
extraTags ??= [];
|
extraTags ??= [];
|
||||||
|
@ -123,7 +123,7 @@ export default function NoteFooter(props: NoteFooterProps) {
|
|||||||
name: getDisplayName(author, ev.pubkey),
|
name: getDisplayName(author, ev.pubkey),
|
||||||
zap: {
|
zap: {
|
||||||
pubkey: ev.pubkey,
|
pubkey: ev.pubkey,
|
||||||
event: NostrLink.fromEvent(ev, CONFIG.eventLinkPrefix),
|
event: NostrLink.fromEvent(ev),
|
||||||
},
|
},
|
||||||
} as ZapTarget,
|
} as ZapTarget,
|
||||||
];
|
];
|
||||||
|
@ -151,12 +151,12 @@ export function NoteInner(props: NoteProps) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const link = NostrLink.fromEvent(eTarget, CONFIG.eventLinkPrefix);
|
const link = NostrLink.fromEvent(eTarget);
|
||||||
// detect cmd key and open in new tab
|
// detect cmd key and open in new tab
|
||||||
if (e.metaKey) {
|
if (e.metaKey) {
|
||||||
window.open(`/${link.encode()}`, "_blank");
|
window.open(`/${link.encode(CONFIG.eventLinkPrefix)}`, "_blank");
|
||||||
} else {
|
} else {
|
||||||
navigate(`/${link.encode()}`, {
|
navigate(`/${link.encode(CONFIG.eventLinkPrefix)}`, {
|
||||||
state: eTarget,
|
state: eTarget,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -58,13 +58,8 @@ export default function Poll(props: PollProps) {
|
|||||||
|
|
||||||
setVoting(opt);
|
setVoting(opt);
|
||||||
const r = Object.keys(relays.item);
|
const r = Object.keys(relays.item);
|
||||||
const zap = await publisher.zap(
|
const zap = await publisher.zap(amount * 1000, props.ev.pubkey, r, NostrLink.fromEvent(props.ev), undefined, eb =>
|
||||||
amount * 1000,
|
eb.tag(["poll_option", opt.toString()]),
|
||||||
props.ev.pubkey,
|
|
||||||
r,
|
|
||||||
NostrLink.fromEvent(props.ev, CONFIG.eventLinkPrefix),
|
|
||||||
undefined,
|
|
||||||
eb => eb.tag(["poll_option", opt.toString()]),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const lnurl = props.ev.tags.find(a => a[0] === "zap")?.[1] || pollerProfile?.lud16 || pollerProfile?.lud06;
|
const lnurl = props.ev.tags.find(a => a[0] === "zap")?.[1] || pollerProfile?.lud16 || pollerProfile?.lud06;
|
||||||
|
@ -283,7 +283,7 @@ export function Thread(props: { onBack?: () => void; disableSpotlight?: boolean
|
|||||||
notes={replies}
|
notes={replies}
|
||||||
related={getAllLinkReactions(
|
related={getAllLinkReactions(
|
||||||
thread.reactions,
|
thread.reactions,
|
||||||
replies.map(a => NostrLink.fromEvent(a, CONFIG.eventLinkPrefix)),
|
replies.map(a => NostrLink.fromEvent(a)),
|
||||||
)}
|
)}
|
||||||
chains={thread.chains}
|
chains={thread.chains}
|
||||||
onNavigate={navigateThread}
|
onNavigate={navigateThread}
|
||||||
|
@ -12,7 +12,7 @@ import { FormattedNumber } from "react-intl";
|
|||||||
|
|
||||||
export function ZapGoal({ ev }: { ev: NostrEvent }) {
|
export function ZapGoal({ ev }: { ev: NostrEvent }) {
|
||||||
const [zap, setZap] = useState(false);
|
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 target = Number(findTag(ev, "amount"));
|
||||||
const amount = zaps.reduce((acc, v) => (acc += v.amount * 1000), 0);
|
const amount = zaps.reduce((acc, v) => (acc += v.amount * 1000), 0);
|
||||||
const progress = amount / target;
|
const progress = amount / target;
|
||||||
|
@ -44,7 +44,7 @@ export function LiveEvent({ ev }: { ev: NostrEvent }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function cta() {
|
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) {
|
switch (status) {
|
||||||
case "live": {
|
case "live": {
|
||||||
return (
|
return (
|
||||||
|
@ -32,7 +32,7 @@ function LiveStreamEvent({ ev }: { ev: NostrEvent }) {
|
|||||||
const image = findTag(ev, "image");
|
const image = findTag(ev, "image");
|
||||||
const status = findTag(ev, "status");
|
const status = findTag(ev, "status");
|
||||||
|
|
||||||
const link = NostrLink.fromEvent(ev).encode();
|
const link = NostrLink.fromEvent(ev).encode(CONFIG.eventLinkPrefix);
|
||||||
const imageProxy = proxy(image ?? "");
|
const imageProxy = proxy(image ?? "");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -185,7 +185,7 @@ function MediaCol({ setThread }: { setThread: (e: NostrLink) => void }) {
|
|||||||
"--img": `url(${proxy(images[0].content)})`,
|
"--img": `url(${proxy(images[0].content)})`,
|
||||||
} as CSSProperties
|
} as CSSProperties
|
||||||
}
|
}
|
||||||
onClick={() => setThread(NostrLink.fromEvent(e, CONFIG.eventLinkPrefix))}></div>
|
onClick={() => setThread(NostrLink.fromEvent(e))}></div>
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
@ -238,7 +238,7 @@ export default function ProfilePage({ id: propId, state }: ProfilePageProps) {
|
|||||||
<Note
|
<Note
|
||||||
key={`pinned-${n.id}`}
|
key={`pinned-${n.id}`}
|
||||||
data={n}
|
data={n}
|
||||||
related={getLinkReactions(pinned, NostrLink.fromEvent(n, CONFIG.eventLinkPrefix))}
|
related={getLinkReactions(pinned, NostrLink.fromEvent(n))}
|
||||||
options={{ showTime: false, showPinned: true, canUnpin: id === loginPubKey }}
|
options={{ showTime: false, showPinned: true, canUnpin: id === loginPubKey }}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
@ -148,7 +148,7 @@ export const NotesTab = () => {
|
|||||||
noteOnClick={
|
noteOnClick={
|
||||||
deckContext
|
deckContext
|
||||||
? ev => {
|
? ev => {
|
||||||
deckContext.setThread(NostrLink.fromEvent(ev, CONFIG.eventLinkPrefix));
|
deckContext.setThread(NostrLink.fromEvent(ev));
|
||||||
}
|
}
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
|
@ -110,16 +110,6 @@ export function eventLink(hex: u256, relays?: Array<string> | string) {
|
|||||||
return `/${encoded}`;
|
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
|
* Convert hex to bech32
|
||||||
*/
|
*/
|
||||||
|
@ -56,7 +56,7 @@ export class Zapper {
|
|||||||
weight: Number(v[3] ?? 0),
|
weight: Number(v[3] ?? 0),
|
||||||
zap: {
|
zap: {
|
||||||
pubkey: v[1],
|
pubkey: v[1],
|
||||||
event: NostrLink.fromEvent(ev, CONFIG.eventLinkPrefix),
|
event: NostrLink.fromEvent(ev),
|
||||||
},
|
},
|
||||||
} as ZapTarget;
|
} as ZapTarget;
|
||||||
} else {
|
} else {
|
||||||
@ -67,7 +67,7 @@ export class Zapper {
|
|||||||
weight: 1,
|
weight: 1,
|
||||||
zap: {
|
zap: {
|
||||||
pubkey: ev.pubkey,
|
pubkey: ev.pubkey,
|
||||||
event: NostrLink.fromEvent(ev, CONFIG.eventLinkPrefix),
|
event: NostrLink.fromEvent(ev),
|
||||||
},
|
},
|
||||||
} as ZapTarget;
|
} as ZapTarget;
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
import { bech32ToHex, hexToBech32, unwrap } from "@snort/shared";
|
import { bech32ToHex, hexToBech32, unwrap } from "@snort/shared";
|
||||||
import {
|
import {
|
||||||
NostrPrefix,
|
|
||||||
decodeTLV,
|
decodeTLV,
|
||||||
TLVEntryType,
|
|
||||||
encodeTLV,
|
encodeTLV,
|
||||||
NostrEvent,
|
|
||||||
TaggedNostrEvent,
|
|
||||||
EventExt,
|
EventExt,
|
||||||
Tag,
|
|
||||||
EventKind,
|
EventKind,
|
||||||
|
NostrEvent,
|
||||||
|
NostrPrefix,
|
||||||
|
Tag,
|
||||||
|
TaggedNostrEvent,
|
||||||
|
TLVEntryType,
|
||||||
} from ".";
|
} from ".";
|
||||||
import { findTag } from "./utils";
|
import { findTag } from "./utils";
|
||||||
|
|
||||||
@ -21,11 +21,11 @@ export class NostrLink {
|
|||||||
readonly relays?: Array<string>,
|
readonly relays?: Array<string>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
encode(): string {
|
encode(type: NostrPrefix = this.type): string {
|
||||||
if (this.type === NostrPrefix.Note || this.type === NostrPrefix.PrivateKey || this.type === NostrPrefix.PublicKey) {
|
if (type === NostrPrefix.Note || type === NostrPrefix.PrivateKey || type === NostrPrefix.PublicKey) {
|
||||||
return hexToBech32(this.type, this.id);
|
return hexToBech32(type, this.id);
|
||||||
} else {
|
} 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]}`);
|
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;
|
const relays = "relays" in ev ? ev.relays : undefined;
|
||||||
|
|
||||||
if (ev.kind >= 30_000 && ev.kind < 40_000) {
|
if (ev.kind >= 30_000 && ev.kind < 40_000) {
|
||||||
const dTag = unwrap(findTag(ev, "d"));
|
const dTag = unwrap(findTag(ev, "d"));
|
||||||
return new NostrLink(NostrPrefix.Address, dTag, ev.kind, ev.pubkey, relays);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user