change to adressable events

This commit is contained in:
@s.roertgen 2025-04-30 12:05:31 +02:00
parent d3fb4275ba
commit 99c0f580f3
4 changed files with 46 additions and 17 deletions

View file

@ -1,5 +1,5 @@
<script> <script>
let { event } = $props(); let { event, showReactions } = $props();
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { NDKEvent } from '@nostr-dev-kit/ndk'; import { NDKEvent } from '@nostr-dev-kit/ndk';
@ -7,6 +7,9 @@
import { writable } from 'svelte/store'; import { writable } from 'svelte/store';
import { login } from '$lib'; import { login } from '$lib';
console.log("show reactions", showReactions)
let reactions = writable([]); let reactions = writable([]);
let reacted = writable(window.localStorage.getItem(event.id)); let reacted = writable(window.localStorage.getItem(event.id));
let reaction = writable({}) let reaction = writable({})
@ -53,7 +56,7 @@
<span>👍 {$reactions.length}</span> <span>👍 {$reactions.length}</span>
<span class="thanks">Danke für deinen Vote!</span> <span class="thanks">Danke für deinen Vote!</span>
<!-- <button onclick={() => deleteVote()} class="btn">Vote zurückziehen</button> --> <!-- <button onclick={() => deleteVote()} class="btn">Vote zurückziehen</button> -->
{:else} {:else if showReactions === "true"}
<button onclick={() => sendReaction()} class="like">👍</button> <button onclick={() => sendReaction()} class="like">👍</button>
<span>{$reactions.length}</span> <span>{$reactions.length}</span>
{/if} {/if}

View file

@ -32,6 +32,9 @@ export async function login() {
userStore.set(signedUser) userStore.set(signedUser)
window.localStorage.setItem('nostrPrivateKey', JSON.stringify(privateKey.privateKey)); window.localStorage.setItem('nostrPrivateKey', JSON.stringify(privateKey.privateKey));
ndk.signer = signer;
console.log("ndk signer", ndk)
ndkStore.set(ndk)
} }
} }
} }

View file

@ -13,6 +13,7 @@
let timer = 0; let timer = 0;
let votingEnabled = false; let votingEnabled = false;
let sessionId = ''; let sessionId = '';
let event = writable();
async function joinSession() { async function joinSession() {
console.log('join ' + sessionId); console.log('join ' + sessionId);
@ -28,14 +29,14 @@
} }
async function postQuestion() { async function postQuestion() {
questionShortId = 10000000 + Math.floor(Math.random() * 90000000); questionShortId = 10000000 + Math.floor(Math.random() * 90000000);
const event = new NDKEvent($ndk, { $event = new NDKEvent($ndk, {
kind: 1342, kind: 30342,
content: question, content: question,
tags: [['d', questionShortId + '']] tags: [['d', questionShortId + '']]
}); });
await event.publish(); await $event.publishReplaceable();
console.log("event id", event.id) console.log("event id", $event.id)
$questionId = event.id; $questionId = `${$event.kind}:${$event.pubkey}:${$event.dTag}`;
$qrCodeUrl = await QRCode.toDataURL(`${window.location.origin}/q/${$questionId}`, { $qrCodeUrl = await QRCode.toDataURL(`${window.location.origin}/q/${$questionId}`, {
width: 800, width: 800,
}); });
@ -52,7 +53,6 @@
}, 1000); }, 1000);
} }
$effect(() => { $effect(() => {
if ($ndkReady) { if ($ndkReady) {
if ($ndk.activeUser) { if ($ndk.activeUser) {

View file

@ -21,6 +21,9 @@
let comment = ''; let comment = '';
let comments = writable([]); let comments = writable([]);
let question = writable()
const [kind, pubkey, d] = data.id.split(":")
const showReactions = writable(false);
$effect(() => { $effect(() => {
if ($ndkReady) { if ($ndkReady) {
@ -30,16 +33,38 @@
$comments = [...$comments, event]; $comments = [...$comments, event];
console.log(`${event.content}`); console.log(`${event.content}`);
}); });
const questionSub = $ndk.subscribe({ kinds: [Number(kind)], authors: [pubkey], "#d": [d] });
questionSub.on('event', (event) => {
$question = event;
if ($question?.tags.find(t => t[0] === "reactions")[1] === "true") {
console.log("reactions enabled")
$showReactions = "true"
}
});
} }
}); });
async function startReactions(event) {
console.log("start reactions", event)
let updatedEvent = new NDKEvent($ndk, event )
console.log("updated event", updatedEvent)
updatedEvent.tags.push(['reactions', "true"])
await updatedEvent.publishReplaceable();
console.log("sent!", updatedEvent)
$showReactions = "true"
}
</script> </script>
<div class="main-layout mx-auto flex w-3/4 flex-col items-center justify-center"> <div class="main-layout mx-auto flex w-3/4 flex-col items-center justify-center">
{#key $ndkReady} {#if $question}
{#await $ndk.fetchEvent(data.id) then question} <button class="btn" onclick={() => startReactions($question)}>Start Reaktionen</button>
{#if question}
<div class="question mb-4 w-full rounded border p-4 text-xl"> <div class="question mb-4 w-full rounded border p-4 text-xl">
<h2>Question:<br />{question.content}</h2> <h2>Question:<br />{$question.content}</h2>
</div> </div>
<div class="mb-2 flex w-full flex-col items-center justify-center gap-2"> <div class="mb-2 flex w-full flex-col items-center justify-center gap-2">
@ -51,15 +76,13 @@
{:else} {:else}
<p>Loading...</p> <p>Loading...</p>
{/if} {/if}
{:catch error}
<p>Error fetching question: {error.message}</p>
{/await}
{/key}
<div class="mx-auto flex w-full flex-col items-center justify-center gap-5"> <div class="mx-auto flex w-full flex-col items-center justify-center gap-5">
{#key $showReactions}
{#each $comments.sort((a, b) => a.created_at - b.created_at).reverse() as event} {#each $comments.sort((a, b) => a.created_at - b.created_at).reverse() as event}
<Comment {event} /> <Comment event={event} showReactions={$showReactions} />
{/each} {/each}
{/key}
</div> </div>
</div> </div>