Skip to content

Commit

Permalink
Merge pull request #16 from samuelralak/identity-context
Browse files Browse the repository at this point in the history
Problem: logic not totally clear
  • Loading branch information
gsovereignty authored Oct 20, 2023
2 parents a1adfb5 + d299491 commit 1c1ffea
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 152 deletions.
1 change: 0 additions & 1 deletion src/components/modals/AddIdentity.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
let buttonDisabled = true;
const profileData = writable<NDKUser | undefined>(undefined);
const _ndk_profiles = get(ndk_profiles);
function getProfile(pubkey) {
if (pubkey.length == 64) {
Expand Down
60 changes: 19 additions & 41 deletions src/components/views/Identity.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,33 @@
import Profile from "../Profile.svelte";
import AddIdentity from "../modals/AddIdentity.svelte";
import IdentityContext from "$lib/contexts/IdentityContext.svelte";
import IdentityList from "$lib/components/identity/IdentityList.svelte";
import ndk from "$lib/stores/ndk";
import IdentityResource from "$lib/resources/IdentityResource";
import {getContext, onDestroy} from "svelte";
import {ignitionPubkey} from "$lib/settings";
import softState from "$lib/stores/states/soft-state";
import {writable} from "svelte/store";
import SoftStateContext from "$lib/contexts/SoftStateContext.svelte";
const rocketMap = getContext('rocketMap')
const participants = writable([])
// TODO: Rename this to profiles
const identities = $ndk.storeSubscribe(
{kinds: [0], authors: [ignitionPubkey]},
{closeOnEose: false},
IdentityResource
)
// const identities = $ndk.storeSubscribe(
// {kinds: [0], authors: [ignitionPubkey]},
// {closeOnEose: false},
// IdentityResource
// )
// subscribe to the rocketMap context
// we can use this to get list of participants as well
rocketMap.subscribe((entries) => {
entries.forEach((entry) => {
console.log({entry})
})
})
onDestroy(() => {
identities.unsubscribe()
})
// rocketMap.subscribe((entries) => {
// entries.forEach((entry) => {
// console.log({entry})
// })
// })
</script>

<IdentityContext>
<SoftStateContext>
<h2>These people have joined Nostrocket</h2>
<Row>
<Column>
Expand All @@ -47,27 +46,6 @@
</InlineNotification>
</Column>
</Row>
<Row>
<Column max={4} lg={4} md={4} sm={16} aspectRatio="2x1">
<Row style="height:99%;padding:2px;">
<Tile light style="width:100%; height:100%;overflow:hidden;">
<Row>
<Column>
<AspectRatio ratio="1x1" style="width:100%;">
todo: get user image from profile if we have pubkey, or use
silhouette if none.
</AspectRatio>
</Column>
<Column>
<Button icon={User}>REQUEST TO JOIN</Button>
<p>#{$nostrocketParticipantProfiles.length}</p>
</Column>
</Row>
</Tile>
</Row>
</Column>
{#each $nostrocketParticipantProfiles as p, i (p.profile.pubkey)}
<Profile profile={p.profile} num={p.index}/>
{/each}
</Row>
</IdentityContext>

<IdentityList/>
</SoftStateContext>
26 changes: 3 additions & 23 deletions src/lib/components/identity/IdentityList.svelte
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
<script lang="ts">
import {getContext, onMount} from "svelte";
import type {Readable, Stores} from "svelte/store";
import {NDKUser} from "@nostr-dev-kit/ndk";
import type {Account} from "$lib/types";
import {derived, get} from "svelte/store";
import {getContext} from "svelte";
import {User} from "carbon-pictograms-svelte";
import {AspectRatio, Button, Column, InlineNotification, Row, Tile,} from "carbon-components-svelte";
import {AspectRatio, Button, Column, Row, Tile,} from "carbon-components-svelte";
import Profile from "../../../components/Profile.svelte";
let nostrocketParticipantProfiles: Readable<{ profile: NDKUser; index: number }[]>
const nostrocketParticipants = getContext<Readable<Account[]>>('nostrocketParticipants')
const participantProfiles = getContext<Map<Account, NDKUser>>('participantProfiles')
nostrocketParticipantProfiles = derived(<Stores>participantProfiles, ($participantProfile) => {
let orderedProfiles: { profile: NDKUser; index: number }[] = [];
get(nostrocketParticipants).forEach((pk, i) => {
let profile = $participantProfile.get(pk);
if (profile) {
orderedProfiles.push({ profile: profile, index: i });
}
});
return orderedProfiles.reverse();
});
const nostrocketParticipantProfiles = getContext('nostrocketParticipantProfiles')
</script>

<Row>
Expand Down
8 changes: 4 additions & 4 deletions src/lib/consensus/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const nostrocketParticipants = derived(consensusTipState, ($cts) => {
return orderedList;
});

function recursiveList(
export function recursiveList(
rocket: string,
rootAccount: Account,
state: Nostrocket,
Expand Down Expand Up @@ -254,7 +254,7 @@ async function watchMempool() {
});
}

function processMempool(currentState: Nostrocket): Nostrocket {
export function processMempool(currentState: Nostrocket): Nostrocket {
let handled: NDKEvent[] = [];
//let newState:Nostrocket = clone(currentState)
mempool.singleIterator().forEach((e) => {
Expand Down Expand Up @@ -292,7 +292,7 @@ function processMempool(currentState: Nostrocket): Nostrocket {
return currentState;
}

function handleProblemEvent(e: NDKEvent, c: Nostrocket): [Nostrocket, boolean] {
export function handleProblemEvent(e: NDKEvent, c: Nostrocket): [Nostrocket, boolean] {
switch (e.kind) {
case 15171971:
//Problem ANCHOR
Expand All @@ -304,7 +304,7 @@ function handleProblemEvent(e: NDKEvent, c: Nostrocket): [Nostrocket, boolean] {
return [c, false];
}

function handleIdentityEvent(
export function handleIdentityEvent(
e: NDKEvent,
c: Nostrocket
): [Nostrocket, boolean] {
Expand Down
88 changes: 5 additions & 83 deletions src/lib/contexts/NDKContext.svelte
Original file line number Diff line number Diff line change
@@ -1,88 +1,10 @@
<script lang="ts">
import {ignitionPubkey, rootEventID} from "$lib/settings";
import NostrocketEvent from "$lib/events/NostrocketEvent";
import {onDestroy, onMount, setContext} from "svelte";
import ndk from "$lib/stores/ndk";
import type {Identity, Rocket, RocketID} from "$lib/types";
import {derived, writable} from "svelte/store";
import {identity, rocket} from "$lib/types";
import {allNostrocketEventKinds} from "$lib/kinds";
import {labelledTag} from "$lib/consensus/state";
import {setContext} from "svelte";
import {consensusTipState, eventsInState, mempool} from "$lib/consensus/state";
type IdentityList = Identity[]
type IdentityMap = Map<string, Identity>
type EventPool = Map<string, NostrocketEvent>
type RocketMap = Map<string, Rocket>
const identityList = writable<IdentityList>([])
const identityMap = writable<IdentityMap>(new Map())
const eventsNotInState = writable<EventPool>(new Map()) // all state change requests that are potentially valid but not included in the current state.
const validatedEvents = writable<EventPool>(new Map())
const rocketMap = writable<RocketMap>(new Map())
// Subscribe to all events that have a e tag, pointing to the ignition event with label root.
const subscriptionOpts = {"#e": [rootEventID], kinds: [30000, 15172008, 15171031]}
const nostrocketEvents = $ndk.storeSubscribe<NostrocketEvent>(subscriptionOpts, {closeOnEose: false}, NostrocketEvent)
nostrocketEvents.subscribe((events) => {
events.forEach((nostrocketEvent: NostrocketEvent) => {
switch (nostrocketEvent.kind) {
case 30000:
nostrocketEvent.getMatchingTags('d').forEach((ndkTag) => {
if (ndkTag[1].length === 64) {
const exisitingRocket = $rocketMap.get(ndkTag[1])
if (exisitingRocket) {
exisitingRocket.updateParticipants(nostrocketEvent)
rocketMap.update((current) => {
current.delete(exisitingRocket.UID)
current.set(exisitingRocket.UID, exisitingRocket)
return current
})
}
}
})
break;
//15171031 is a hard state change event, it cannot be handled this way. It must only be handled from mempool if and only if the current logged in user has the consensus lead, otherwise the only time it can be handled is if it is included in a kind 15172008 consensus event signed by someone with votepower in our current state.
// case 15171031:
// nostrocketEvent.getMatchingTags('n').forEach((ndkTag) => {
// const newRocket = new rocket(undefined)
// newRocket.fromEvent(nostrocketEvent, ndkTag[1], undefined)
// rocketMap.update((current) => {
// current.set(newRocket.UID, newRocket)
// return current
// })
// })
// break;
default:
console.log('Not yet implemented')
}
})
})
identityMap.subscribe(console.log)
eventsNotInState.subscribe((some) => console.log({some}))
setContext('rocketMap', rocketMap)
onMount(() => {
if (!($identityMap.get(ignitionPubkey))) {
identityMap.set((new Map()).set(ignitionPubkey, new identity({
Account: ignitionPubkey,
Name: "Ignition Account",
MaintainerBy: "1Humanityrvhus5mFWRRzuJjtAbjk2qwww",
Order: 0,
UniqueSovereignBy: "1Humanityrvhus5mFWRRzuJjtAbjk2qwww",
})))
}
})
onDestroy(() => {
nostrocketEvents.unsubscribe()
})
setContext('consensusTipState', consensusTipState)
setContext('mempool', mempool)
setContext('eventsInState', eventsInState)
</script>

<main>
Expand Down
51 changes: 51 additions & 0 deletions src/lib/contexts/SoftStateContext.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<script lang="ts">
import {getContext, setContext} from "svelte";
import {consensusTipState, recursiveList} from "$lib/consensus/state";
import {derived, get, type Stores} from "svelte/store";
import {type Account} from "$lib/types";
import {ignitionPubkey, nostrocketIgnitionEvent} from "$lib/settings";
import {profiles} from "$lib/stores/profiles";
import ndk from "$lib/stores/ndk";
import {NDKUser} from "@nostr-dev-kit/ndk";
const currentTipState = getContext('consensusTipState')
const nostrocketParticipants = derived(consensusTipState, ($cts) => {
let orderedList: Account[] = [];
recursiveList(nostrocketIgnitionEvent, ignitionPubkey, $cts, orderedList);
return orderedList;
});
nostrocketParticipants.subscribe((pkList) => {
pkList.forEach((pk) => {
let user = $ndk.getUser({hexpubkey: pk})
user.fetchProfile().then((profile) => {
if (user.profile) {
profiles.update((data) => {
data.set(user.pubkey, user);
return data;
});
}
});
});
});
const nostrocketParticipantProfiles = derived(profiles, ($p) => {
let orderedProfiles: { profile: NDKUser; index: number }[] = [];
get(nostrocketParticipants).forEach((pk, i) => {
let profile = $p.get(pk);
if (profile) {
orderedProfiles.push({profile: profile, index: i});
}
});
return orderedProfiles.reverse();
});
setContext('nostrocketParticipants', nostrocketParticipants)
setContext('nostrocketParticipantProfiles', nostrocketParticipantProfiles)
</script>

<main>
<slot/>
</main>
21 changes: 21 additions & 0 deletions src/lib/stores/base-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {type Writable, writable} from 'svelte/store';

export default class BaseStore {
protected static instance: BaseStore;
protected data: Writable<{}>;

protected constructor(input: {}) {
this.data = writable(input); // Initialize Svelte store
}

static getInstance(input): BaseStore {
if (!BaseStore.instance) {
BaseStore.instance = new BaseStore(input);
}
return BaseStore.instance;
}

get store() {
return this.data;
}
}
28 changes: 28 additions & 0 deletions src/lib/stores/states/soft-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {type Writable} from "svelte/store";
import BaseStore from "$lib/stores/base-store";
import {ignitionPubkey} from "$lib/settings";

interface Identity {
publicKeys: string[]
}

interface ISoftState {
identity: Identity
}

class SoftState extends BaseStore {
// override protected allows us to enforce type
protected data: Writable<ISoftState>

protected constructor(input: ISoftState) {
super(input)
}
}

const _default: ISoftState = {
identity: {
publicKeys: [ignitionPubkey]
}
}

export default SoftState.getInstance(_default).store

0 comments on commit 1c1ffea

Please sign in to comment.