new users sync
This commit is contained in:
parent
c7f6f73ae7
commit
f9c0f35862
@ -1,5 +1,6 @@
|
|||||||
import NDK, {
|
import NDK, {
|
||||||
NDKEvent,
|
NDKEvent,
|
||||||
|
NDKUser,
|
||||||
zapInvoiceFromEvent,
|
zapInvoiceFromEvent,
|
||||||
type NostrEvent,
|
type NostrEvent,
|
||||||
} from "@nostr-dev-kit/ndk";
|
} from "@nostr-dev-kit/ndk";
|
||||||
@ -10,7 +11,7 @@ import { createZodFetcher } from "zod-fetch";
|
|||||||
import { getTagValues, getTagsAllValues } from "../nostr/utils";
|
import { getTagValues, getTagsAllValues } from "../nostr/utils";
|
||||||
import { unixTimeNowInSeconds } from "../nostr/dates";
|
import { unixTimeNowInSeconds } from "../nostr/dates";
|
||||||
import { createEvent } from "./create";
|
import { createEvent } from "./create";
|
||||||
|
import { findEphemeralSigner } from "@/lib/actions/ephemeral";
|
||||||
const fetchWithZod = createZodFetcher();
|
const fetchWithZod = createZodFetcher();
|
||||||
const ZapEndpointResponseSchema = z.object({
|
const ZapEndpointResponseSchema = z.object({
|
||||||
nostrPubkey: z.string(),
|
nostrPubkey: z.string(),
|
||||||
@ -40,7 +41,6 @@ export async function checkPayment(
|
|||||||
pubkey: string,
|
pubkey: string,
|
||||||
event: NostrEvent,
|
event: NostrEvent,
|
||||||
) {
|
) {
|
||||||
console.log("CHeck payment called", tagId);
|
|
||||||
const paymentEvents = await ndk.fetchEvents({
|
const paymentEvents = await ndk.fetchEvents({
|
||||||
kinds: [9735],
|
kinds: [9735],
|
||||||
["#a"]: [tagId],
|
["#a"]: [tagId],
|
||||||
@ -49,10 +49,8 @@ export async function checkPayment(
|
|||||||
const paymentEvent = Array.from(paymentEvents).find(
|
const paymentEvent = Array.from(paymentEvents).find(
|
||||||
(e) => zapInvoiceFromEvent(e)?.zappee === pubkey,
|
(e) => zapInvoiceFromEvent(e)?.zappee === pubkey,
|
||||||
);
|
);
|
||||||
console.log("paymentEvent", paymentEvent);
|
|
||||||
if (!paymentEvent) return;
|
if (!paymentEvent) return;
|
||||||
const invoice = zapInvoiceFromEvent(paymentEvent);
|
const invoice = zapInvoiceFromEvent(paymentEvent);
|
||||||
console.log("invoice", invoice);
|
|
||||||
if (!invoice) {
|
if (!invoice) {
|
||||||
console.log("No invoice");
|
console.log("No invoice");
|
||||||
return;
|
return;
|
||||||
@ -137,6 +135,7 @@ export async function updateListUsersFromZaps(
|
|||||||
event,
|
event,
|
||||||
);
|
);
|
||||||
console.log("Is valid?", isValid);
|
console.log("Is valid?", isValid);
|
||||||
|
const newUsers: string[] = [];
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
validUsers.push([
|
validUsers.push([
|
||||||
paymentInvoice.zappee,
|
paymentInvoice.zappee,
|
||||||
@ -144,7 +143,10 @@ export async function updateListUsersFromZaps(
|
|||||||
"",
|
"",
|
||||||
(unixTimeNowInSeconds() + SECONDS_IN_YEAR).toString(),
|
(unixTimeNowInSeconds() + SECONDS_IN_YEAR).toString(),
|
||||||
]);
|
]);
|
||||||
|
newUsers.push(paymentInvoice.zappee);
|
||||||
|
// Send old codes to user
|
||||||
}
|
}
|
||||||
|
await sendCodesToNewUsers(ndk, newUsers, tagId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add self
|
// Add self
|
||||||
@ -163,3 +165,39 @@ export async function updateListUsersFromZaps(
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function sendCodesToNewUsers(ndk: NDK, users: string[], tagId: string) {
|
||||||
|
const signer = await findEphemeralSigner(ndk, ndk!.signer!, {
|
||||||
|
associatedEventNip19: tagId,
|
||||||
|
});
|
||||||
|
if (!signer) return;
|
||||||
|
const delegate = await signer.user();
|
||||||
|
const messages = await ndk.fetchEvents({
|
||||||
|
authors: [delegate.pubkey],
|
||||||
|
kinds: [4],
|
||||||
|
["#p"]: [tagId.split(":")?.[1] ?? ""],
|
||||||
|
});
|
||||||
|
const codes: [string, string][] = [];
|
||||||
|
for (const message of Array.from(messages)) {
|
||||||
|
await message.decrypt(delegate, signer);
|
||||||
|
codes.push([getTagValues("e", message.tags) ?? "", message.content]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const user of users) {
|
||||||
|
for (const [event, code] of codes) {
|
||||||
|
const messageEvent = new NDKEvent(ndk, {
|
||||||
|
content: code,
|
||||||
|
kind: 4,
|
||||||
|
tags: [
|
||||||
|
["p", user],
|
||||||
|
["e", event],
|
||||||
|
["client", "flockstr"],
|
||||||
|
],
|
||||||
|
pubkey: delegate.pubkey,
|
||||||
|
} as NostrEvent);
|
||||||
|
await messageEvent.encrypt(new NDKUser({ hexpubkey: user }), signer);
|
||||||
|
await messageEvent.sign(signer);
|
||||||
|
await messageEvent.publish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user