From a69f7d59506d5e8c219f4b66354f411304099639 Mon Sep 17 00:00:00 2001 From: Bojan Mojsilovic Date: Mon, 10 Jun 2024 15:44:26 +0200 Subject: [PATCH] Proper subs from profile and article --- src/pages/Longform.tsx | 30 ++++++++++++++++++++++++++---- src/pages/Profile.tsx | 29 +++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/pages/Longform.tsx b/src/pages/Longform.tsx index 69dff5d..c029e2c 100644 --- a/src/pages/Longform.tsx +++ b/src/pages/Longform.tsx @@ -390,7 +390,7 @@ const Longform: Component< { naddr: string } > = (props) => { getAuthorSubscriptionTiers(author.pubkey, subId) } - const doSubscription = async (tier: Tier, cost: TierCost) => { + const doSubscription = async (tier: Tier, cost: TierCost, exchangeRate?: Record>) => { const a = store.article?.user; if (!a || !account || !cost) return; @@ -412,10 +412,34 @@ const Longform: Component< { naddr: string } > = (props) => { const { success, note } = await sendEvent(subEvent, account.relays, account.relaySettings); if (success && note) { - await zapSubscription(note, a, account.publicKey, account.relays); + const isZapped = await zapSubscription(note, a, account.publicKey, account.relays, exchangeRate); + + if (!isZapped) { + unsubscribe(note.id); + } } } + const unsubscribe = async (eventId: string) => { + const a = store.article?.user; + + if (!a || !account) return; + + const unsubEvent = { + kind: Kind.Unsubscribe, + content: '', + created_at: Math.floor((new Date()).getTime() / 1_000), + + tags: [ + ['p', a.pubkey], + ['e', eventId], + ], + }; + + await sendEvent(unsubEvent, account.relays, account.relaySettings); + + } + const openSubscribe = () => { app?.actions.openAuthorSubscribeModal(store.article?.user, doSubscription); }; @@ -602,8 +626,6 @@ const Longform: Component< { naddr: string } > = (props) => { getArticleThread(account?.publicKey, pubkey, identifier, kind, subId); } - - const updatePage = (content: NostrEventContent) => { if (content.kind === Kind.Metadata) { const user = content as NostrUserContent; diff --git a/src/pages/Profile.tsx b/src/pages/Profile.tsx index b68bf5b..19db9cf 100644 --- a/src/pages/Profile.tsx +++ b/src/pages/Profile.tsx @@ -547,7 +547,7 @@ const Profile: Component = () => { getAuthorSubscriptionTiers(author.pubkey, subId); } - const doSubscription = async (tier: Tier, cost: TierCost) => { + const doSubscription = async (tier: Tier, cost: TierCost, exchangeRate?: Record>) => { const a = profile?.userProfile; if (!a || !account || !cost) return; @@ -569,10 +569,35 @@ const Profile: Component = () => { const { success, note } = await sendEvent(subEvent, account.relays, account.relaySettings); if (success && note) { - await zapSubscription(note, a, account.publicKey, account.relays); + const isZapped = await zapSubscription(note, a, account.publicKey, account.relays, exchangeRate); + + if (!isZapped) { + unsubscribe(note.id); + } } } + const unsubscribe = async (eventId: string) => { + const a = profile?.userProfile;; + + if (!a || !account) return; + + const unsubEvent = { + kind: Kind.Unsubscribe, + content: '', + created_at: Math.floor((new Date()).getTime() / 1_000), + + tags: [ + ['p', a.pubkey], + ['e', eventId], + ], + }; + + await sendEvent(unsubEvent, account.relays, account.relaySettings); + + } + + const openSubscribe = () => { app?.actions.openAuthorSubscribeModal(profile?.userProfile, doSubscription); };