From d70e4ed4bff04000fd6c2108e2884e37dda960c9 Mon Sep 17 00:00:00 2001 From: cmd Date: Tue, 27 Feb 2024 23:13:09 -0600 Subject: [PATCH] update --- demo/draft/alice/list.ts | 2 + src/client/class/draft.ts | 96 +++++++++++++++++++++++---------------- src/client/class/store.ts | 2 +- src/client/types.ts | 1 + src/lib/policy.ts | 2 +- 5 files changed, 62 insertions(+), 41 deletions(-) diff --git a/demo/draft/alice/list.ts b/demo/draft/alice/list.ts index c902d18d..9c48c7fd 100644 --- a/demo/draft/alice/list.ts +++ b/demo/draft/alice/list.ts @@ -17,3 +17,5 @@ const session = new DraftSession(signer, { const drafts = await session.list('wss://relay.damus.io') console.dir(drafts, { depth : null }) + +session.delete(drafts[0].store_id) diff --git a/src/client/class/draft.ts b/src/client/class/draft.ts index 9ead3a40..f09adb7d 100644 --- a/src/client/class/draft.ts +++ b/src/client/class/draft.ts @@ -23,7 +23,8 @@ import { import { has_full_enrollment, join_role, - rem_enrollment + rem_enrollment, + tabulate_enrollment } from '@/lib/policy.js' import { @@ -144,6 +145,22 @@ export class DraftSession extends EventEmitter <{ get approvals () { return this.data.approvals } + + get available () { + const map = tabulate_enrollment(this.members, this.roles) + return this.roles.filter(e => { + const score = map.get(e.id) ?? e.max_slots + return score >= e.max_slots + }) + } + + get data () { + return this._store.data + } + + get id () { + return this._store._socket.topic_id + } get is_approved () { const mship = this.membership.data @@ -162,43 +179,44 @@ export class DraftSession extends EventEmitter <{ }) } - get data () { - return this._store.data - } - - get members () { - return this.data.members + get is_endorsed () { + const sig = this.signatures.find(e => { + return e.slice(0, 64) === this.pubkey + }) + return (sig !== undefined) } - get opt () { - return this._opt + get is_full () { + return has_full_enrollment(this.members, this.roles) } - get proposal () { - return this.data.proposal + get is_member () { + return this.signer.credential.exists(this.members) } - get prop_id () { - return get_proposal_id(this.proposal) + get is_moderated () { + return this.proposal.moderator !== undefined } - get pubkey () { - return this.signer.pubkey + get is_moderator () { + return this.proposal.moderator === this.pubkey } - get is_endorsed () { - const sig = this.signatures.find(e => { - return e.slice(0, 64) === this.pubkey - }) - return (sig !== undefined) + get is_ready () { + return this._init } - get is_full () { - return has_full_enrollment(this.members, this.roles) + get is_valid () { + try { + this.verify() + return true + } catch { + return false + } } - get is_member () { - return this.signer.credential.exists(this.members) + get members () { + return this.data.members } get member_idx () { @@ -215,31 +233,30 @@ export class DraftSession extends EventEmitter <{ return this.signer.credential.claim(this.members) } - get is_moderated () { - return this.proposal.moderator !== undefined + get opt () { + return this._opt } - get is_moderator () { - return this.proposal.moderator === this.pubkey + get proposal () { + return this.data.proposal } - get is_ready () { - return this._init + get prop_id () { + return get_proposal_id(this.proposal) } - get is_valid () { - try { - this.verify() - return true - } catch { - return false - } + get pubkey () { + return this.signer.pubkey } get roles () { return this.data.roles } + get secret () { + return this._store._socket.secret + } + get signatures () { return this.data.signatures } @@ -451,7 +468,7 @@ export class DraftSession extends EventEmitter <{ } delete (store_id : string) { - this._socket.delete(store_id) + this._store._socket.delete(store_id) } endorse () { @@ -555,11 +572,12 @@ export class DraftSession extends EventEmitter <{ const events = await socket.query(address, filter) socket.close() events.filter(e => socket.can_recover(e)).forEach(e => { + const pubkey = e.pubkey const updated_at = e.created_at const store_id = e.id try { const session_id = socket.recover(e) - sessions.push({ session_id, store_id, updated_at }) + sessions.push({ pubkey, session_id, store_id, updated_at }) } catch { return } }) return sessions diff --git a/src/client/class/store.ts b/src/client/class/store.ts index c46f04af..a2c7cd92 100644 --- a/src/client/class/store.ts +++ b/src/client/class/store.ts @@ -237,8 +237,8 @@ export class NostrStore > extends EventEmitter<{ // Emit ready message. this.emit('ready', this) } else { - this.emit('update', this) this._send('post', data) + this.emit('update', this) } // Print debug message to console. this.log.debug('update data:', this.data) diff --git a/src/client/types.ts b/src/client/types.ts index 67168b3e..9b731b9a 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -48,6 +48,7 @@ export type EventFilter = { } & { [ key : string ] : any | undefined } export interface DraftItem { + pubkey : string, session_id : string, store_id : string, updated_at : number diff --git a/src/lib/policy.ts b/src/lib/policy.ts index 9f8784b4..770b550e 100644 --- a/src/lib/policy.ts +++ b/src/lib/policy.ts @@ -66,7 +66,7 @@ export function tabulate_enrollment ( if (mbr.pol === pol.id) { scores.set(pol.id, tab + 1) } - } + } } return scores }