Casual refactor of entire eventBuilder

This commit is contained in:
2023-04-14 16:02:15 +01:00
parent 914fa759a9
commit 36926d4346
33 changed files with 648 additions and 580 deletions

View File

@ -28,10 +28,12 @@ export default function DM(props: DMProps) {
const otherPubkey = isMe ? pubKey : unwrap(props.data.tags.find(a => a[0] === "p")?.[1]);
async function decrypt() {
const decrypted = await publisher.decryptDm(props.data);
setContent(decrypted || "<ERROR>");
if (!isMe) {
setLastReadDm(props.data.pubkey);
if (publisher) {
const decrypted = await publisher.decryptDm(props.data);
setContent(decrypted || "<ERROR>");
if (!isMe) {
setLastReadDm(props.data.pubkey);
}
}
}

View File

@ -14,18 +14,26 @@ export interface FollowButtonProps {
}
export default function FollowButton(props: FollowButtonProps) {
const pubkey = parseId(props.pubkey);
const publiser = useEventPublisher();
const isFollowing = useLogin().follows.item.includes(pubkey);
const publisher = useEventPublisher();
const { follows, relays } = useLogin();
const isFollowing = follows.item.includes(pubkey);
const baseClassname = `${props.className} follow-button`;
async function follow(pubkey: HexKey) {
const ev = await publiser.addFollow(pubkey);
publiser.broadcast(ev);
if (publisher) {
const ev = await publisher.contactList([pubkey, ...follows.item], relays.item);
publisher.broadcast(ev);
}
}
async function unfollow(pubkey: HexKey) {
const ev = await publiser.removeFollow(pubkey);
publiser.broadcast(ev);
if (publisher) {
const ev = await publisher.contactList(
follows.item.filter(a => a !== pubkey),
relays.item
);
publisher.broadcast(ev);
}
}
return (

View File

@ -6,6 +6,7 @@ import { HexKey } from "@snort/nostr";
import ProfilePreview from "Element/ProfilePreview";
import messages from "./messages";
import useLogin from "Hooks/useLogin";
export interface FollowListBaseProps {
pubkeys: HexKey[];
@ -15,10 +16,13 @@ export interface FollowListBaseProps {
}
export default function FollowListBase({ pubkeys, title, showFollowAll, showAbout }: FollowListBaseProps) {
const publisher = useEventPublisher();
const { follows, relays } = useLogin();
async function followAll() {
const ev = await publisher.addFollow(pubkeys);
publisher.broadcast(ev);
if (publisher) {
const ev = await publisher.contactList([...pubkeys, ...follows.item], relays.item);
publisher.broadcast(ev);
}
}
return (

View File

@ -189,7 +189,7 @@ export default function Nip5Service(props: Nip05ServiceProps) {
}
async function updateProfile(handle: string, domain: string) {
if (user) {
if (user && publisher) {
const nip05 = `${handle}@${domain}`;
const newProfile = {
...user,

View File

@ -3,7 +3,7 @@ import React, { useMemo, useState, useLayoutEffect, ReactNode } from "react";
import { useNavigate, Link } from "react-router-dom";
import { useInView } from "react-intersection-observer";
import { useIntl, FormattedMessage } from "react-intl";
import { TaggedRawEvent, HexKey, EventKind, NostrPrefix } from "@snort/nostr";
import { TaggedRawEvent, HexKey, EventKind, NostrPrefix, Lists } from "@snort/nostr";
import useEventPublisher from "Feed/EventPublisher";
import Icon from "Icons/Icon";
@ -132,27 +132,23 @@ export default function Note(props: NoteProps) {
};
async function unpin(id: HexKey) {
if (options.canUnpin) {
if (options.canUnpin && publisher) {
if (window.confirm(formatMessage(messages.ConfirmUnpin))) {
const es = pinned.item.filter(e => e !== id);
const ev = await publisher.pinned(es);
if (ev) {
publisher.broadcast(ev);
setPinned(login, es, ev.created_at * 1000);
}
const ev = await publisher.noteList(es, Lists.Pinned);
publisher.broadcast(ev);
setPinned(login, es, ev.created_at * 1000);
}
}
}
async function unbookmark(id: HexKey) {
if (options.canUnbookmark) {
if (options.canUnbookmark && publisher) {
if (window.confirm(formatMessage(messages.ConfirmUnbookmark))) {
const es = bookmarked.item.filter(e => e !== id);
const ev = await publisher.bookmarked(es);
if (ev) {
publisher.broadcast(ev);
setBookmarked(login, es, ev.created_at * 1000);
}
const ev = await publisher.noteList(es, Lists.Bookmarked);
publisher.broadcast(ev);
setBookmarked(login, es, ev.created_at * 1000);
}
}
}

View File

@ -29,6 +29,7 @@ import { LNURL } from "LNURL";
import messages from "./messages";
import { ClipboardEventHandler, useState } from "react";
import Spinner from "Icons/Spinner";
import { EventBuilder } from "System";
interface NotePreviewProps {
note: TaggedRawEvent;
@ -64,7 +65,7 @@ export function NoteCreator() {
const dispatch = useDispatch();
async function sendNote() {
if (note) {
if (note && publisher) {
let extraTags: Array<Array<string>> | undefined;
if (zapForward) {
try {
@ -91,9 +92,12 @@ export function NoteCreator() {
extraTags ??= [];
extraTags.push(...pollOptions.map((a, i) => ["poll_option", i.toString(), a]));
}
const ev = replyTo
? await publisher.reply(replyTo, note, extraTags, kind)
: await publisher.note(note, extraTags, kind);
const hk = (eb: EventBuilder) => {
extraTags?.forEach(t => eb.tag(t));
eb.kind(kind);
return eb;
};
const ev = replyTo ? await publisher.reply(replyTo, note, hk) : await publisher.note(note, hk);
publisher.broadcast(ev);
dispatch(reset());
}
@ -154,7 +158,7 @@ export function NoteCreator() {
async function loadPreview() {
if (preview) {
dispatch(setPreview(undefined));
} else {
} else if (publisher) {
const tmpNote = await publisher.note(note);
if (tmpNote) {
dispatch(setPreview(tmpNote));

View File

@ -3,7 +3,7 @@ import { useSelector, useDispatch } from "react-redux";
import { useIntl, FormattedMessage } from "react-intl";
import { Menu, MenuItem } from "@szhsin/react-menu";
import { useLongPress } from "use-long-press";
import { TaggedRawEvent, HexKey, u256, encodeTLV, NostrPrefix } from "@snort/nostr";
import { TaggedRawEvent, HexKey, u256, encodeTLV, NostrPrefix, Lists } from "@snort/nostr";
import Icon from "Icons/Icon";
import Spinner from "Icons/Spinner";
@ -96,7 +96,7 @@ export default function NoteFooter(props: NoteFooterProps) {
const dispatch = useDispatch();
const { formatMessage } = useIntl();
const login = useLogin();
const { pinned, bookmarked, publicKey, preferences: prefs } = login;
const { pinned, bookmarked, publicKey, preferences: prefs, relays } = login;
const { mute, block } = useModeration();
const author = useUserProfile(ev.pubkey);
const publisher = useEventPublisher();
@ -134,21 +134,21 @@ export default function NoteFooter(props: NoteFooterProps) {
}
async function react(content: string) {
if (!hasReacted(content)) {
if (!hasReacted(content) && publisher) {
const evLike = await publisher.react(ev, content);
publisher.broadcast(evLike);
}
}
async function deleteEvent() {
if (window.confirm(formatMessage(messages.ConfirmDeletion, { id: ev.id.substring(0, 8) }))) {
if (window.confirm(formatMessage(messages.ConfirmDeletion, { id: ev.id.substring(0, 8) })) && publisher) {
const evDelete = await publisher.delete(ev.id);
publisher.broadcast(evDelete);
}
}
async function repost() {
if (!hasReposted()) {
if (!hasReposted() && publisher) {
if (!prefs.confirmReposts || window.confirm(formatMessage(messages.ConfirmRepost, { id: ev.id }))) {
const evRepost = await publisher.repost(ev);
publisher.broadcast(evRepost);
@ -196,7 +196,9 @@ export default function NoteFooter(props: NoteFooterProps) {
await barrierZapper(async () => {
const handler = new LNURL(lnurl);
await handler.load();
const zap = handler.canZap ? await publisher.zap(amount * 1000, key, id) : undefined;
const zr = Object.keys(relays.item);
const zap = handler.canZap && publisher ? await publisher.zap(amount * 1000, key, zr, id) : undefined;
const invoice = await handler.getInvoice(amount, undefined, zap);
await wallet?.payInvoice(unwrap(invoice.pr));
});
@ -320,18 +322,18 @@ export default function NoteFooter(props: NoteFooterProps) {
}
async function pin(id: HexKey) {
const es = [...pinned.item, id];
const ev = await publisher.pinned(es);
if (ev) {
if (publisher) {
const es = [...pinned.item, id];
const ev = await publisher.noteList(es, Lists.Pinned);
publisher.broadcast(ev);
setPinned(login, es, ev.created_at * 1000);
}
}
async function bookmark(id: HexKey) {
const es = [...bookmarked.item, id];
const ev = await publisher.bookmarked(es);
if (ev) {
if (publisher) {
const es = [...bookmarked.item, id];
const ev = await publisher.noteList(es, Lists.Bookmarked);
publisher.broadcast(ev);
setBookmarked(login, es, ev.created_at * 1000);
}

View File

@ -23,7 +23,7 @@ export default function Poll(props: PollProps) {
const { formatMessage } = useIntl();
const publisher = useEventPublisher();
const { wallet } = useWallet();
const { preferences: prefs, publicKey: myPubKey } = useLogin();
const { preferences: prefs, publicKey: myPubKey, relays } = useLogin();
const pollerProfile = useUserProfile(props.ev.pubkey);
const [error, setError] = useState("");
const [invoice, setInvoice] = useState("");
@ -35,7 +35,7 @@ export default function Poll(props: PollProps) {
const options = props.ev.tags.filter(a => a[0] === "poll_option").sort((a, b) => Number(a[1]) - Number(b[1]));
async function zapVote(ev: React.MouseEvent, opt: number) {
ev.stopPropagation();
if (voting) return;
if (voting || !publisher) return;
const amount = prefs.defaultZapAmount;
try {
@ -53,17 +53,10 @@ export default function Poll(props: PollProps) {
}
setVoting(opt);
const zap = await publisher.zap(amount * 1000, props.ev.pubkey, props.ev.id, undefined, [
["poll_option", opt.toString()],
]);
if (!zap) {
throw new Error(
formatMessage({
defaultMessage: "Can't create vote, maybe you're not logged in?",
})
);
}
const r = Object.keys(relays.item);
const zap = await publisher.zap(amount * 1000, props.ev.pubkey, r, props.ev.id, undefined, eb =>
eb.tag(["poll_option", opt.toString()])
);
const lnurl = props.ev.tags.find(a => a[0] === "zap")?.[1] || pollerProfile?.lud16 || pollerProfile?.lud06;
if (!lnurl) return;

View File

@ -13,10 +13,11 @@ import Copy from "Element/Copy";
import { LNURL, LNURLError, LNURLErrorCode, LNURLInvoice, LNURLSuccessAction } from "LNURL";
import { chunks, debounce } from "Util";
import { useWallet } from "Wallet";
import { EventExt } from "System/EventExt";
import useLogin from "Hooks/useLogin";
import { generateRandomKey } from "Login";
import { EventPublisher } from "System/EventPublisher";
import messages from "./messages";
import useLogin from "Hooks/useLogin";
enum ZapType {
PublicZap = 1,
@ -40,7 +41,8 @@ export interface SendSatsProps {
export default function SendSats(props: SendSatsProps) {
const onClose = props.onClose || (() => undefined);
const { note, author, target } = props;
const defaultZapAmount = useLogin().preferences.defaultZapAmount;
const login = useLogin();
const defaultZapAmount = login.preferences.defaultZapAmount;
const amounts = [defaultZapAmount, 1_000, 5_000, 10_000, 20_000, 50_000, 100_000, 1_000_000];
const emojis: Record<number, string> = {
1_000: "👍",
@ -118,22 +120,21 @@ export default function SendSats(props: SendSatsProps) {
};
async function loadInvoice() {
if (!amount || !handler) return null;
if (!amount || !handler || !publisher) return null;
let zap: RawEvent | undefined;
if (author && zapType !== ZapType.NonZap) {
const ev = await publisher.zap(amount * 1000, author, note, comment);
if (ev) {
// replace sig for anon-zap
if (zapType === ZapType.AnonZap) {
const randomKey = publisher.newKey();
console.debug("Generated new key for zap: ", randomKey);
ev.pubkey = randomKey.publicKey;
ev.id = "";
ev.tags.push(["anon", ""]);
await EventExt.sign(ev, randomKey.privateKey);
}
zap = ev;
const relays = Object.keys(login.relays.item);
// use random key for anon zaps
if (zapType === ZapType.AnonZap) {
const randomKey = generateRandomKey();
console.debug("Generated new key for zap: ", randomKey);
const publisher = new EventPublisher(randomKey.publicKey, randomKey.privateKey);
zap = await publisher.zap(amount * 1000, author, relays, note, comment);
} else {
zap = await publisher.zap(amount * 1000, author, relays, note, comment);
}
}