Proper subs from profile and article

This commit is contained in:
Bojan Mojsilovic 2024-06-10 15:44:26 +02:00
parent 4440e0d499
commit a69f7d5950
2 changed files with 53 additions and 6 deletions

View File

@ -390,7 +390,7 @@ const Longform: Component< { naddr: string } > = (props) => {
getAuthorSubscriptionTiers(author.pubkey, subId) getAuthorSubscriptionTiers(author.pubkey, subId)
} }
const doSubscription = async (tier: Tier, cost: TierCost) => { const doSubscription = async (tier: Tier, cost: TierCost, exchangeRate?: Record<string, Record<string, number>>) => {
const a = store.article?.user; const a = store.article?.user;
if (!a || !account || !cost) return; 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); const { success, note } = await sendEvent(subEvent, account.relays, account.relaySettings);
if (success && note) { 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 = () => { const openSubscribe = () => {
app?.actions.openAuthorSubscribeModal(store.article?.user, doSubscription); app?.actions.openAuthorSubscribeModal(store.article?.user, doSubscription);
}; };
@ -602,8 +626,6 @@ const Longform: Component< { naddr: string } > = (props) => {
getArticleThread(account?.publicKey, pubkey, identifier, kind, subId); getArticleThread(account?.publicKey, pubkey, identifier, kind, subId);
} }
const updatePage = (content: NostrEventContent) => { const updatePage = (content: NostrEventContent) => {
if (content.kind === Kind.Metadata) { if (content.kind === Kind.Metadata) {
const user = content as NostrUserContent; const user = content as NostrUserContent;

View File

@ -547,7 +547,7 @@ const Profile: Component = () => {
getAuthorSubscriptionTiers(author.pubkey, subId); getAuthorSubscriptionTiers(author.pubkey, subId);
} }
const doSubscription = async (tier: Tier, cost: TierCost) => { const doSubscription = async (tier: Tier, cost: TierCost, exchangeRate?: Record<string, Record<string, number>>) => {
const a = profile?.userProfile; const a = profile?.userProfile;
if (!a || !account || !cost) return; if (!a || !account || !cost) return;
@ -569,10 +569,35 @@ const Profile: Component = () => {
const { success, note } = await sendEvent(subEvent, account.relays, account.relaySettings); const { success, note } = await sendEvent(subEvent, account.relays, account.relaySettings);
if (success && note) { 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 = () => { const openSubscribe = () => {
app?.actions.openAuthorSubscribeModal(profile?.userProfile, doSubscription); app?.actions.openAuthorSubscribeModal(profile?.userProfile, doSubscription);
}; };