auto add self

This commit is contained in:
zmeyer44 2023-10-18 15:05:36 -04:00
parent 66a6a983ed
commit 9a1ee11a97
4 changed files with 8 additions and 109 deletions

View File

@ -10,6 +10,7 @@ import { getTwoLetters, truncateText } from "@/lib/utils";
import ProfileFeed from "./_components/Feed";
import Subscriptions from "./_components/Subscriptions";
import { nip19 } from "nostr-tools";
import useLists from "@/lib/hooks/useLists";
export default function ProfilePage({
params: { npub },
@ -19,7 +20,6 @@ export default function ProfilePage({
};
}) {
const [activeTab, setActiveTab] = useState("feed");
console.log("calling with ", npub);
if (npub === "service-worker.js") {
throw new Error("Invalid list");
}
@ -30,18 +30,8 @@ export default function ProfilePage({
}
const pubkey = data.toString();
const { profile } = useProfile(pubkey);
const { init, lists } = useLists();
const demo = [
{
id: "1",
title: "BTC Radio",
description:
"BTC Radio is the best fuking show ever. you should sub to it. now",
picture:
"https://assets.whop.com/cdn-cgi/image/width=1080/https://assets.whop.com/images/images/51602.original.png?1693358530",
tags: ["music", "crypto", "art"],
},
];
return (
<div className="relative mx-auto max-w-5xl space-y-6">
<div className="relative @container">
@ -116,9 +106,9 @@ export default function ProfilePage({
</div>
<div className="mx-auto max-w-[800px] space-y-6">
<div className="flex max-w-2xl flex-col gap-4 px-4">
{demo.map((e) => (
{/* {[].map((e) => (
<SubscriptionCard key={e.id} {...e} />
))}
))} */}
</div>
<div className="">
<Tabs

View File

@ -13,6 +13,7 @@ import { getTagValues } from "@/lib/nostr/utils";
import { NDKList } from "@nostr-dev-kit/ndk";
import { saveEphemeralSigner } from "@/lib/actions/ephemeral";
import { useRouter } from "next/navigation";
const CreateListSchema = z.object({
title: z.string(),
image: z.string().optional(),
@ -49,6 +50,7 @@ export default function CreateList() {
}
if (data.subscriptions) {
tags.push(["subscriptions", "true"]);
tags.push(["p", currentUser!.pubkey, "", "self", "4000000000"]);
}
if (data.price) {
tags.push(["price", satsToBtc(data.price).toString(), "btc", "year"]);

View File

@ -68,20 +68,14 @@ export async function createEventHandler(
let publishedEvent: NDKEvent | null = null;
// Check if is private event
if (isPrivate) {
console.log("isPrivate");
const rawEventString = JSON.stringify(eventToPublish.rawEvent());
console.log("rawEventString", rawEventString);
const passphrase = generateRandomString();
console.log("passphrase", passphrase);
const encryptedRawEventString = await encryptMessage(
rawEventString,
passphrase,
);
console.log("encryptedRawEventString", encryptedRawEventString);
console.log("delegateSigner", delegateSigner);
const signer = delegateSigner ?? ndk.signer!;
const user = await signer.user();
console.log("signer user", user);
const newEvent = new NDKEvent(ndk, {
content: encryptedRawEventString,
@ -132,80 +126,7 @@ export async function createEventHandler(
}
return true;
}
export async function createEncryptedEventOnPrivateList(
ndk: NDK,
event: {
content: string;
kind: number;
tags: string[][];
},
list: NDKList,
delegateSigner?: NDKPrivateKeySigner,
) {
const pubkey = await window.nostr?.getPublicKey();
if (!pubkey || !window.nostr) {
throw new Error("No public key provided!");
}
const eventToPublish = new NDKEvent(ndk, {
...event,
tags: [...event.tags, ["client", "flockstr"]],
pubkey,
created_at: unixTimeNowInSeconds(),
} as NostrEvent);
await eventToPublish.sign();
const rawEventString = JSON.stringify(eventToPublish.rawEvent());
const passphrase = generateRandomString();
const encryptedRawEventString = await encryptMessage(
rawEventString,
passphrase,
);
const signer = delegateSigner ?? ndk.signer!;
const user = await signer.user();
const newEvent = new NDKEvent(ndk, {
content: encryptedRawEventString,
kind: 3745,
tags: [
["kind", event.kind.toString()],
["client", "flockstr"],
],
pubkey: user.pubkey,
} as NostrEvent);
await newEvent.sign(signer);
await newEvent.publish();
const tag = newEvent.tagReference();
if (!tag) return;
// Add event to list
await list.addItem(tag, undefined, false);
await list.sign();
await list.publish();
// Send DMs to subscribers
const subscribers = getTagsValues("p", list.tags);
for (const subscriber of subscribers) {
const messageEvent = new NDKEvent(ndk, {
content: passphrase,
kind: 4,
tags: [
["p", subscriber],
["e", newEvent.id],
["client", "flockstr"],
],
pubkey: user.hexpubkey,
} as NostrEvent);
console.log("message to create", messageEvent);
await messageEvent.encrypt(new NDKUser({ hexpubkey: subscriber }), signer);
console.log("Encrypted message", messageEvent);
await messageEvent.sign(signer);
await messageEvent.publish();
}
return true;
}
export async function createReaction(
ndk: NDK,
content: "+" | "-",

View File

@ -146,22 +146,8 @@ export async function updateListUsersFromZaps(
// Add self
console.log("Adding self");
const selfIndex = validUsers.findIndex(([vu]) => vu === event.pubkey);
if (selfIndex !== -1) {
console.log("Already there");
validUsers[selfIndex] = [
event.pubkey,
"",
"self",
(unixTimeNowInSeconds() + SECONDS_IN_YEAR).toString(),
];
} else {
validUsers.push([
event.pubkey,
"",
"self",
(unixTimeNowInSeconds() + SECONDS_IN_YEAR).toString(),
]);
if (selfIndex === -1) {
validUsers.push([event.pubkey, "", "self", "4000000000"]);
}
console.log("Valid users", validUsers);
return createEvent(ndk, {