Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Feb 28, 2024
1 parent e8f0d12 commit d70e4ed
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 41 deletions.
2 changes: 2 additions & 0 deletions demo/draft/alice/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
96 changes: 57 additions & 39 deletions src/client/class/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import {
import {
has_full_enrollment,
join_role,
rem_enrollment
rem_enrollment,
tabulate_enrollment
} from '@/lib/policy.js'

import {
Expand Down Expand Up @@ -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
Expand All @@ -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 () {
Expand All @@ -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
}
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/client/class/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ export class NostrStore <T extends Record<string, any>> 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)
Expand Down
1 change: 1 addition & 0 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lib/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function tabulate_enrollment (
if (mbr.pol === pol.id) {
scores.set(pol.id, tab + 1)
}
}
}
}
return scores
}
Expand Down

0 comments on commit d70e4ed

Please sign in to comment.