problem: voting and rocket logic is spread out and messy

This commit is contained in:
gsovereignty
2024-07-16 12:30:33 +08:00
parent a84ec12a27
commit f18c31ad35
6 changed files with 309 additions and 110 deletions

View File

@@ -6,54 +6,21 @@
import { Input } from '$lib/components/ui/input/index.js';
import { Label } from '$lib/components/ui/label/index.js';
import * as Alert from '@/components/ui/alert';
import { getRocketURL } from '@/helpers';
import { ndk } from '@/ndk';
import { currentUser } from '@/stores/session';
import { NDKEvent } from '@nostr-dev-kit/ndk';
import { Terminal } from 'lucide-svelte';
import Todo from './Todo.svelte';
import { Rocket } from '@/event_helpers/rockets';
export let product: NDKEvent;
export let rocket: NDKEvent;
let parsedRocket: Rocket = new Rocket(rocket);
let price: number = 0;
let max: number = 0;
function updateIgnitionAndParentTag(rocket: NDKEvent) {
let existingIgnition = rocket.getMatchingTags('ignition');
//let existingParent = rocket.getMatchingTags("parent")
removeIgnitionAndParentTag(rocket);
if (existingIgnition.length > 1) {
throw new Error('too many ignition tags!');
}
if (existingIgnition.length == 0) {
rocket.tags.push(['ignition', rocket.id]);
}
if (existingIgnition.length == 1) {
if (existingIgnition[0][1].length == 64) {
rocket.tags.push(existingIgnition[0]);
}
if (existingIgnition[0][1] == 'this') {
rocket.tags.push(['ignition', rocket.id]);
}
}
rocket.tags.push(['parent', rocket.id]);
}
function removeIgnitionAndParentTag(rocket: NDKEvent) {
let existing = [];
for (let t of rocket.tags) {
existing.push(t);
}
rocket.tags = [];
for (let t of existing) {
if (t[0] !== 'ignition' && t[0] !== 'parent') {
rocket.tags.push(t);
}
}
}
function publish() {
if (!$ndk.signer) {
throw new Error('no ndk signer found');
@@ -67,16 +34,8 @@
console.log(rocket.author, author);
throw new Error('you are not the creator of this rocket');
}
rocket.created_at = Math.floor(new Date().getTime() / 1000);
//todo validate d tag
rocket.tags.push([
'product',
`${product.id}:${price}:${rocket.created_at}:${max}`,
'wss://relay.nostrocket.org',
JSON.stringify([])
]);
updateIgnitionAndParentTag(rocket)
rocket.publish().then((x) => {
let event = parsedRocket.UpsertProduct(product.id, price, max);
event.publish().then((x) => {
console.log(x);
goto(`${base}/products`);
});

View File

@@ -58,7 +58,6 @@
{#each $merits as [id, merit], _ (id)}
<Table.Row
on:click={() => {
console.log(merit.Event.rawEvent());
goto(`${base}/rockets/merits/${merit.ID}`);
}}
class="cursor-pointer bg-accent"

View File

@@ -1,15 +1,13 @@
<script lang="ts">
import * as Card from '$lib/components/ui/card/index.js';
import { Vote, type MeritRequest } from '@/event_helpers/merits';
import { Vote, Votes, type MeritRequest } from '@/event_helpers/merits';
import { ndk } from '@/ndk';
import type { NDKEvent, NDKKind } from '@nostr-dev-kit/ndk';
import { NDKEvent, type NDKKind } from '@nostr-dev-kit/ndk';
import { Avatar, Name } from '@nostr-dev-kit/ndk-svelte-components';
import { ExternalLink, Info } from 'lucide-svelte';
import { onDestroy } from 'svelte';
import VoteOnMeritRequest from './VoteOnMeritRequest.svelte';
import { goto } from '$app/navigation';
import { base } from '$app/paths';
import { Separator } from '$lib/components/ui/separator/index.js';
import * as Table from '@/components/ui/table';
import { Rocket, RocketATagFilter } from '@/event_helpers/rockets';
@@ -19,12 +17,15 @@
import { Button } from '$lib/components/ui/button/index.js';
import { Label } from '$lib/components/ui/label/index.js';
import { Textarea } from '$lib/components/ui/textarea/index.js';
import CornerDownLeft from 'lucide-svelte/icons/corner-down-left';
import Alert from '@/components/ui/alert/alert.svelte';
import { currentUser } from '@/stores/session';
import CornerDownLeft from 'lucide-svelte/icons/corner-down-left';
export let merit: MeritRequest;
export let rocket: NDKEvent;
let parsedRocket = new Rocket(rocket);
let _votes = $ndk.storeSubscribe(
{ '#a': [RocketATagFilter(rocket)], '#e': [merit.ID], kinds: [1410 as NDKKind] },
{
@@ -52,11 +53,36 @@
for (let [_, v] of vMap) {
let existing = pMap.get(v.Pubkey);
if (!existing || (existing && existing.TimeStamp < v.TimeStamp)) {
//todo: check if this merit request has already been included in the rocket. If not, and if we have enough votes to approve it, update the rocket.
pMap.set(v.Pubkey, v);
}
}
return pMap;
vMap = new Map<string, Vote>();
for (let [_, v] of pMap) {
vMap.set(v.ID, v);
}
return vMap;
});
let rocketUpdates = derived([votes, currentUser], ([$votes, $currentUser]) => {
let events: NDKEvent[] = [];
if (
$currentUser &&
parsedRocket &&
parsedRocket.VotePowerForPubkey($currentUser.pubkey) > 0
) {
let votes = new Votes(Array.from($votes, ([_, v])=>(v)))
let result = votes.Results().Result(parsedRocket)
if (result && result == "ratify" && !parsedRocket.ApprovedMeritRequests().get(votes.Request)) {
//todo: parsedRocket.AppendAMR(votes.ConstructProof())
//
}
}
return events;
});
rocketUpdates.subscribe((c)=>{if (c.length > 0){console.log(c)}})
</script>
<Card.Root class="sm:col-span-2">
@@ -116,14 +142,17 @@
</div>
<Separator class="my-4" />
<div class="font-semibold">Votes</div>
{#if $votes.size == 0}<Alert><Info />Waiting for existing <span class="italic">{new Rocket(rocket).Name()}</span> Merit holders to vote</Alert> {/if}
{#if $votes.size == 0}<Alert
><Info />Waiting for existing <span class="italic">{new Rocket(rocket).Name()}</span> Merit
holders to vote</Alert
>
{/if}
<Table.Root>
<Table.Body>
{#each $votes as [id, vote], _ (id)}
<Table.Row
on:click={() => {
console.log(vote.Event.rawEvent());
goto(`${base}/rockets/merits/${vote.ID}`);
}}
class="cursor-pointer {vote.VoteDirection == 'ratify'
? 'bg-lime-600'