@estiva-app/conversation 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -0
- package/README.md +23 -4
- package/dist/builders.d.ts.map +1 -1
- package/dist/builders.js +1 -2
- package/dist/builders.js.map +1 -1
- package/dist/compose.d.ts +53 -0
- package/dist/compose.d.ts.map +1 -0
- package/dist/compose.js +62 -0
- package/dist/compose.js.map +1 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/kinds.d.ts +8 -0
- package/dist/kinds.d.ts.map +1 -1
- package/dist/kinds.js +8 -0
- package/dist/kinds.js.map +1 -1
- package/dist/membership.d.ts +132 -0
- package/dist/membership.d.ts.map +1 -0
- package/dist/membership.js +271 -0
- package/dist/membership.js.map +1 -0
- package/dist/readState.d.ts +214 -0
- package/dist/readState.d.ts.map +1 -0
- package/dist/readState.js +345 -0
- package/dist/readState.js.map +1 -0
- package/dist/strength.d.ts +2 -0
- package/dist/strength.d.ts.map +1 -1
- package/dist/strength.js +7 -3
- package/dist/strength.js.map +1 -1
- package/dist/unread.d.ts +54 -0
- package/dist/unread.d.ts.map +1 -0
- package/dist/unread.js +50 -0
- package/dist/unread.js.map +1 -0
- package/package.json +2 -2
- package/src/builders.ts +1 -3
- package/src/compose.ts +63 -0
- package/src/index.ts +71 -1
- package/src/kinds.ts +10 -0
- package/src/membership.ts +328 -0
- package/src/readState.ts +486 -0
- package/src/strength.ts +8 -3
- package/src/unread.ts +85 -0
package/src/index.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* apps: it imports no app's data layer, opens no socket, and takes every
|
|
13
13
|
* environment touch — the relay read, the clock, storage — as a parameter.
|
|
14
14
|
*/
|
|
15
|
-
export const CONVERSATION_VERSION = '0.
|
|
15
|
+
export const CONVERSATION_VERSION = '0.3.0'
|
|
16
16
|
|
|
17
17
|
export {
|
|
18
18
|
KIND_MESSAGE,
|
|
@@ -37,6 +37,7 @@ export {
|
|
|
37
37
|
threadStrength,
|
|
38
38
|
referenceTagsFor,
|
|
39
39
|
mentionTagsFor,
|
|
40
|
+
peopleNamedInBody,
|
|
40
41
|
MAX_MENTIONED_PEOPLE,
|
|
41
42
|
type Strength,
|
|
42
43
|
} from './strength.js'
|
|
@@ -63,6 +64,75 @@ export { decorationsOf, type Decoration, type Decorations, type Resolution, type
|
|
|
63
64
|
|
|
64
65
|
export { buildComment, buildReply, offersEditAndDelete, type CommentRef } from './builders.js'
|
|
65
66
|
|
|
67
|
+
export { mentionText, messageReference, URGENT_TAG, urgentTagsFor } from './compose.js'
|
|
68
|
+
|
|
69
|
+
export {
|
|
70
|
+
KIND_CHANGE,
|
|
71
|
+
MEMBER_FIELD_PREFIX,
|
|
72
|
+
memberField,
|
|
73
|
+
memberOfField,
|
|
74
|
+
mentions,
|
|
75
|
+
streamOf,
|
|
76
|
+
membershipOf,
|
|
77
|
+
membersOf,
|
|
78
|
+
membershipFilters,
|
|
79
|
+
candidateFilesOf,
|
|
80
|
+
buildMembershipChange,
|
|
81
|
+
MUTED_D_TAG,
|
|
82
|
+
APPDATA_TAG,
|
|
83
|
+
parseMutedBlob,
|
|
84
|
+
serializeMutedBlob,
|
|
85
|
+
buildMutedEvent,
|
|
86
|
+
mutedFilter,
|
|
87
|
+
mutedFromFollowed,
|
|
88
|
+
type Membership,
|
|
89
|
+
type MutedList,
|
|
90
|
+
} from './membership.js'
|
|
91
|
+
|
|
92
|
+
export {
|
|
93
|
+
READ_STATE_TAG,
|
|
94
|
+
READ_STATE_D_PREFIX,
|
|
95
|
+
MAX_CONTEXTS,
|
|
96
|
+
MAX_CONTEXT_ID_BYTES,
|
|
97
|
+
MAX_TIMESTAMP,
|
|
98
|
+
MAX_CLIENT_ID_BYTES,
|
|
99
|
+
MAX_BLOB_BYTES,
|
|
100
|
+
MAX_CONTEXTS_BYTES,
|
|
101
|
+
READ_STATE_HORIZON_DAYS,
|
|
102
|
+
channelContext,
|
|
103
|
+
fileContext,
|
|
104
|
+
threadContext,
|
|
105
|
+
messageContext,
|
|
106
|
+
toContextSeconds,
|
|
107
|
+
isPublishableContextId,
|
|
108
|
+
loadSlotIdentity,
|
|
109
|
+
rotateSlotId,
|
|
110
|
+
readStateDTag,
|
|
111
|
+
parseReadStateBlob,
|
|
112
|
+
serializeReadStateBlob,
|
|
113
|
+
capContextsToBytes,
|
|
114
|
+
advanceContexts,
|
|
115
|
+
buildReadStateEvent,
|
|
116
|
+
allSlotsFilter,
|
|
117
|
+
mergeSlots,
|
|
118
|
+
classifyOwnSlots,
|
|
119
|
+
effectiveReadAt,
|
|
120
|
+
fetchReadState,
|
|
121
|
+
publishReadState,
|
|
122
|
+
type SlotIdentity,
|
|
123
|
+
type SlotStorage,
|
|
124
|
+
type SlotSource,
|
|
125
|
+
type RandomBytes,
|
|
126
|
+
type ReadStateBlob,
|
|
127
|
+
type CappedContexts,
|
|
128
|
+
type SlotBlob,
|
|
129
|
+
type OwnSlotVerdict,
|
|
130
|
+
type Nip44,
|
|
131
|
+
type FetchedReadState,
|
|
132
|
+
} from './readState.js'
|
|
133
|
+
|
|
134
|
+
export { unreadIn, isUnread, type StreamJudge } from './unread.js'
|
|
135
|
+
|
|
66
136
|
export {
|
|
67
137
|
createDraftStore,
|
|
68
138
|
draftKeys,
|
package/src/kinds.ts
CHANGED
|
@@ -17,6 +17,16 @@ export const KIND_DELETION = KIND.DELETION
|
|
|
17
17
|
/** An assertion; with `t=resolution` it resolves or reopens a comment (PEEK-128). */
|
|
18
18
|
export const KIND_ASSERTION = KIND.ASSERTION
|
|
19
19
|
|
|
20
|
+
/** A field change (SPEC §6.2). Not in protocol's `KIND`; the number is the SPEC's. */
|
|
21
|
+
export const KIND_CHANGE = 1851
|
|
22
|
+
/** NIP-78 app data: read state (§11.6) and app-private storage (§12). */
|
|
23
|
+
export const KIND_APP_DATA = KIND.APP_DATA
|
|
24
|
+
|
|
25
|
+
/** A Folder's channel id: a lowercase UUID v4. */
|
|
26
|
+
export const FOLDER_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
|
|
27
|
+
/** A file's address, `<kind>:<pubkey>:<d>`: kind 30000–39999, 64 lowercase hex, `d` verbatim and without whitespace. */
|
|
28
|
+
export const FILE_ADDRESS = /^3[0-9]{4}:[0-9a-f]{64}:[^\s]*$/
|
|
29
|
+
|
|
20
30
|
/** Whether the event is a message or a comment — what a conversation lists. */
|
|
21
31
|
export const isConversationKind = (kind: number): boolean => kind === KIND_MESSAGE || kind === KIND_COMMENT
|
|
22
32
|
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Membership — SPEC §11.8: whose unread a file's conversation is.
|
|
3
|
+
*
|
|
4
|
+
* A person is told about a file's conversation when they are a **member** of
|
|
5
|
+
* the file, and every app computes that the same way, from public events: the
|
|
6
|
+
* file's root, the messages in its stream (§11.1) and its `kind:1851`
|
|
7
|
+
* changes. A Folder's members are its channel roster, which is the relay's
|
|
8
|
+
* and not folded here.
|
|
9
|
+
*
|
|
10
|
+
* | join | the event |
|
|
11
|
+
* | --- | --- |
|
|
12
|
+
* | created it | the root, ordered before every other event for the file |
|
|
13
|
+
* | took part | a message in the stream, by `P` |
|
|
14
|
+
* | was mentioned | a message in the stream whose content names `P` (`nostr:npub…`) |
|
|
15
|
+
* | was placed | a `kind:1851` on the file, not a membership field, with a non-empty `value`, carrying `["p", P]` |
|
|
16
|
+
* | was added, or joined | a `kind:1851` setting `member:<P>` to `true`, by anyone |
|
|
17
|
+
*
|
|
18
|
+
* A **leave** is a `member:<P>` = `false` signed by `P`; one signed by anybody
|
|
19
|
+
* else is ignored. Joins and leaves order as §6.3 orders changes, `P` is a
|
|
20
|
+
* member when the last is a join, and a member **since** the earliest join
|
|
21
|
+
* after the last leave.
|
|
22
|
+
*
|
|
23
|
+
* *Placed* is the `p` alone, so membership and the `#p` discovery query are
|
|
24
|
+
* the same set. A placement written without one does not count (on production,
|
|
25
|
+
* most assignee changes before 2026-10-01).
|
|
26
|
+
*
|
|
27
|
+
* Muting is private and separate (§11.8 *Muting is private*): see
|
|
28
|
+
* {@link parseMutedBlob}.
|
|
29
|
+
*/
|
|
30
|
+
import type { SignedEvent, UnsignedEvent } from '@estiva-app/protocol'
|
|
31
|
+
import { FILE_ADDRESS as ADDRESS, FOLDER_ID, KIND_APP_DATA, KIND_CHANGE, KIND_COMMENT, KIND_MESSAGE } from './kinds.js'
|
|
32
|
+
import { byOrder } from './order.js'
|
|
33
|
+
import { anchorsOf, isCommentOn, peopleNamedInBody } from './strength.js'
|
|
34
|
+
import { groupThreads } from './threads.js'
|
|
35
|
+
|
|
36
|
+
export { KIND_CHANGE }
|
|
37
|
+
|
|
38
|
+
/** The field prefix of a membership change: `member:<pubkey>`. */
|
|
39
|
+
export const MEMBER_FIELD_PREFIX = 'member:'
|
|
40
|
+
|
|
41
|
+
const PUBKEY = /^[0-9a-f]{64}$/
|
|
42
|
+
|
|
43
|
+
type Event = Pick<SignedEvent, 'id' | 'pubkey' | 'kind' | 'created_at' | 'tags' | 'content'>
|
|
44
|
+
|
|
45
|
+
/** A relay filter, as `QueryFn` takes one. */
|
|
46
|
+
type Filter = Record<string, unknown>
|
|
47
|
+
|
|
48
|
+
const tag = (event: Pick<SignedEvent, 'tags'>, name: string): string | undefined => event.tags.find((t) => t[0] === name)?.[1]
|
|
49
|
+
|
|
50
|
+
/** The membership field for a person: `member:<pubkey>`. */
|
|
51
|
+
export function memberField(pubkey: string): string {
|
|
52
|
+
if (!PUBKEY.test(pubkey)) throw new Error(`not a lowercase hex pubkey: ${JSON.stringify(pubkey)}`)
|
|
53
|
+
return `${MEMBER_FIELD_PREFIX}${pubkey}`
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The person a `member:<pubkey>` field names, or undefined when it is not one. */
|
|
57
|
+
export function memberOfField(field: string | undefined): string | undefined {
|
|
58
|
+
if (!field?.startsWith(MEMBER_FIELD_PREFIX)) return undefined
|
|
59
|
+
const pubkey = field.slice(MEMBER_FIELD_PREFIX.length)
|
|
60
|
+
return PUBKEY.test(pubkey) ? pubkey : undefined
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Does the message's content mention the person — a `nostr:npub…` in its body.
|
|
65
|
+
* A `p` tag alone is not a mention for membership: the comment builders tag
|
|
66
|
+
* the file's author and the parent's on every comment (§6.4), so a `p` would
|
|
67
|
+
* re-join an author who left.
|
|
68
|
+
*/
|
|
69
|
+
export function mentions(event: Pick<SignedEvent, 'content'>, pubkey: string): boolean {
|
|
70
|
+
return peopleNamedInBody(event.content).has(pubkey)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The address of an addressable event, `<kind>:<pubkey>:<d>`. */
|
|
74
|
+
function addressOf(event: Pick<SignedEvent, 'kind' | 'pubkey' | 'tags'>): string | undefined {
|
|
75
|
+
if (event.kind < 30000 || event.kind > 39999) return undefined
|
|
76
|
+
return `${event.kind}:${event.pubkey}:${tag(event, 'd') ?? ''}`
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The messages in a file's stream (§11.1): every root that is a comment on it,
|
|
81
|
+
* and every reply in those threads. A `kind:1111` reply whose root the read
|
|
82
|
+
* does not hold still counts when its `A` is the file — a reply copies its
|
|
83
|
+
* thread's object. A `kind:9` never roots a file's stream (§6.4, CON-20).
|
|
84
|
+
*/
|
|
85
|
+
export function streamOf<E extends Event>(file: string, events: readonly E[]): E[] {
|
|
86
|
+
const conversation = events.filter((e) => e.kind === KIND_COMMENT || e.kind === KIND_MESSAGE)
|
|
87
|
+
const { roots, replies, missingRoots } = groupThreads(conversation)
|
|
88
|
+
const missing = new Set(missingRoots)
|
|
89
|
+
const out: E[] = []
|
|
90
|
+
for (const root of roots) {
|
|
91
|
+
if (!isCommentOn(root, file)) continue
|
|
92
|
+
out.push(root, ...(replies[root.id] ?? []))
|
|
93
|
+
}
|
|
94
|
+
for (const id of missing) {
|
|
95
|
+
for (const reply of replies[id] ?? []) if (reply.kind === KIND_COMMENT && anchorsOf(reply).includes(file)) out.push(reply)
|
|
96
|
+
}
|
|
97
|
+
return out.sort(byOrder)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Whether a person is a member of a file, and since which second. */
|
|
101
|
+
export interface Membership {
|
|
102
|
+
member: boolean
|
|
103
|
+
/** Unix seconds: the `created_at` of the first join after the last leave. Undefined when not a member. */
|
|
104
|
+
since?: number
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const NOT_A_MEMBER: Membership = { member: false }
|
|
108
|
+
|
|
109
|
+
/** `creation` is the root: it orders before everything else for the file, whichever version the reader holds. */
|
|
110
|
+
type Step = { event: Event; join: boolean; creation?: boolean }
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Every join and leave in a file's events, per person. `events` may hold
|
|
114
|
+
* anything; only the file's root versions, its stream and its `kind:1851`
|
|
115
|
+
* changes are read.
|
|
116
|
+
*/
|
|
117
|
+
function stepsOf(file: string, events: readonly Event[]): Map<string, Step[]> {
|
|
118
|
+
const steps = new Map<string, Step[]>()
|
|
119
|
+
const add = (person: string, event: Event, join: boolean, creation = false) => {
|
|
120
|
+
if (!PUBKEY.test(person)) return
|
|
121
|
+
const list = steps.get(person) ?? []
|
|
122
|
+
list.push({ event, join, creation })
|
|
123
|
+
steps.set(person, list)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// A relay keeps only the latest version of an addressable event, so the root
|
|
127
|
+
// the reader holds is usually the last edit. Its join is ordered first, so
|
|
128
|
+
// an edit after a leave does not make the author a member again.
|
|
129
|
+
const versions = events.filter((e) => addressOf(e) === file).sort(byOrder)
|
|
130
|
+
if (versions[0]) add(versions[0].pubkey, versions[0], true, true)
|
|
131
|
+
|
|
132
|
+
for (const message of streamOf(file, events)) {
|
|
133
|
+
add(message.pubkey, message, true)
|
|
134
|
+
for (const person of peopleNamedInBody(message.content)) add(person, message, true)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
for (const change of events) {
|
|
138
|
+
if (change.kind !== KIND_CHANGE || !change.tags.some((t) => t[0] === 'a' && t[1] === file)) continue
|
|
139
|
+
const field = tag(change, 'field')
|
|
140
|
+
const value = tag(change, 'value')
|
|
141
|
+
const member = memberOfField(field)
|
|
142
|
+
if (member !== undefined) {
|
|
143
|
+
if (value === 'true') add(member, change, true)
|
|
144
|
+
else if (value === 'false' && change.pubkey === member) add(member, change, false)
|
|
145
|
+
continue
|
|
146
|
+
}
|
|
147
|
+
// A change with an empty value takes someone off (an unassign) and places nobody, whatever `p` it carries.
|
|
148
|
+
if (field?.startsWith(MEMBER_FIELD_PREFIX) || !value) continue
|
|
149
|
+
for (const t of change.tags) if (t[0] === 'p' && typeof t[1] === 'string') add(t[1], change, true)
|
|
150
|
+
}
|
|
151
|
+
return steps
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function fold(steps: readonly Step[] | undefined): Membership {
|
|
155
|
+
if (!steps || steps.length === 0) return NOT_A_MEMBER
|
|
156
|
+
const ordered = [...steps].sort(
|
|
157
|
+
(a, b) => Number(b.creation ?? false) - Number(a.creation ?? false) || byOrder(a.event, b.event) || Number(a.join) - Number(b.join),
|
|
158
|
+
)
|
|
159
|
+
if (!ordered[ordered.length - 1].join) return NOT_A_MEMBER
|
|
160
|
+
// Since: the earliest second among the joins after the last leave — the creation's
|
|
161
|
+
// held version can be a later edit than the author's first comment.
|
|
162
|
+
let since: number | undefined
|
|
163
|
+
for (const step of ordered) {
|
|
164
|
+
if (!step.join) since = undefined
|
|
165
|
+
else since = since === undefined ? step.event.created_at : Math.min(since, step.event.created_at)
|
|
166
|
+
}
|
|
167
|
+
return { member: true, since }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* One person's membership of one file (§11.8 *The fold*), from the file's own
|
|
172
|
+
* events: its root, its stream and its changes.
|
|
173
|
+
*/
|
|
174
|
+
export function membershipOf(file: string, person: string, events: readonly Event[]): Membership {
|
|
175
|
+
return fold(stepsOf(file, events).get(person))
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Every member of a file and since when, from the file's own events. People who left are absent. */
|
|
179
|
+
export function membersOf(file: string, events: readonly Event[]): Map<string, Membership> {
|
|
180
|
+
const members = new Map<string, Membership>()
|
|
181
|
+
for (const [person, steps] of stepsOf(file, events)) {
|
|
182
|
+
const membership = fold(steps)
|
|
183
|
+
if (membership.member) members.set(person, membership)
|
|
184
|
+
}
|
|
185
|
+
return members
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The filters that find every file a person may be a member of (§11.8): the
|
|
190
|
+
* changes that name them, what they wrote, and what mentions them. Each
|
|
191
|
+
* candidate file is then read and folded with {@link membershipOf}. The files
|
|
192
|
+
* they created are the app's own query; `since` bounds all three.
|
|
193
|
+
*/
|
|
194
|
+
export function membershipFilters(person: string, since?: number): Filter[] {
|
|
195
|
+
if (!PUBKEY.test(person)) throw new Error(`not a lowercase hex pubkey: ${JSON.stringify(person)}`)
|
|
196
|
+
const bound = since === undefined ? {} : { since }
|
|
197
|
+
return [
|
|
198
|
+
{ kinds: [KIND_CHANGE], '#p': [person], ...bound },
|
|
199
|
+
{ kinds: [KIND_COMMENT, KIND_MESSAGE], authors: [person], ...bound },
|
|
200
|
+
{ kinds: [KIND_COMMENT, KIND_MESSAGE], '#p': [person], ...bound },
|
|
201
|
+
]
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* The files an event can make a person a member of: a change's `a`, a
|
|
206
|
+
* comment's `A` (or every `a` when it has none). A `kind:9` names none — its
|
|
207
|
+
* file, if any, is its root's, which the app resolves by reading the thread.
|
|
208
|
+
*/
|
|
209
|
+
export function candidateFilesOf(event: Pick<SignedEvent, 'kind' | 'tags'>): string[] {
|
|
210
|
+
if (event.kind === KIND_CHANGE) return event.tags.filter((t) => t[0] === 'a' && ADDRESS.test(t[1] ?? '')).map((t) => t[1])
|
|
211
|
+
return anchorsOf(event).filter((a) => ADDRESS.test(a))
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* A membership change (§11.8 *The membership change*): `true` adds a person,
|
|
216
|
+
* by anyone; `false` is a leave and counts only when the person signs it
|
|
217
|
+
* themselves. Tag order: `a`, `field`, `value`, `h`, `ts`, `p`.
|
|
218
|
+
*
|
|
219
|
+
* Sign one only when the person chose it — joining, leaving, or adding
|
|
220
|
+
* somebody. Never to migrate a private follow (§11.8 *Following, retired*).
|
|
221
|
+
*
|
|
222
|
+
* `content` is the note an activity feed shows, as for every change: a change
|
|
223
|
+
* with none renders as a blank line in Ship's feed. "Join", "Leave" or "Add"
|
|
224
|
+
* by default.
|
|
225
|
+
*/
|
|
226
|
+
export function buildMembershipChange(
|
|
227
|
+
pubkey: string,
|
|
228
|
+
createdAtMs: number,
|
|
229
|
+
args: { file: string; folder: string; person: string; member: boolean; note?: string },
|
|
230
|
+
): UnsignedEvent {
|
|
231
|
+
const note = args.note ?? (!args.member ? 'Leave' : args.person === pubkey ? 'Join' : 'Add')
|
|
232
|
+
if (!ADDRESS.test(args.file)) throw new Error(`not a file address: ${JSON.stringify(args.file)}`)
|
|
233
|
+
if (!FOLDER_ID.test(args.folder)) throw new Error(`not a Folder id (lowercase UUID v4): ${JSON.stringify(args.folder)}`)
|
|
234
|
+
if (!Number.isSafeInteger(createdAtMs) || createdAtMs < 0) throw new Error(`not epoch milliseconds: ${createdAtMs}`)
|
|
235
|
+
return {
|
|
236
|
+
pubkey,
|
|
237
|
+
created_at: Math.floor(createdAtMs / 1000),
|
|
238
|
+
kind: KIND_CHANGE,
|
|
239
|
+
tags: [
|
|
240
|
+
['a', args.file],
|
|
241
|
+
['field', memberField(args.person)],
|
|
242
|
+
['value', args.member ? 'true' : 'false'],
|
|
243
|
+
['h', args.folder],
|
|
244
|
+
['ts', String(createdAtMs)],
|
|
245
|
+
['p', args.person],
|
|
246
|
+
],
|
|
247
|
+
content: note,
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ── Muting: private, suite-wide (§11.8 *Muting is private*, §12) ────────────
|
|
252
|
+
|
|
253
|
+
/** The `d` of the person's mute list. */
|
|
254
|
+
export const MUTED_D_TAG = 'estiva:muted:v1'
|
|
255
|
+
|
|
256
|
+
/** The `t` every §12 app-private blob carries. */
|
|
257
|
+
export const APPDATA_TAG = 'estiva-appdata'
|
|
258
|
+
|
|
259
|
+
/** The decrypted mute list: file addresses and channel uuids. */
|
|
260
|
+
export interface MutedList {
|
|
261
|
+
v: 1
|
|
262
|
+
updatedAt: number
|
|
263
|
+
keys: string[]
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const isMuteKey = (key: unknown): key is string => typeof key === 'string' && (ADDRESS.test(key) || FOLDER_ID.test(key))
|
|
267
|
+
|
|
268
|
+
/** The mute list from its decrypted content, or undefined when it is not a v1 list. Unknown keys are dropped. */
|
|
269
|
+
export function parseMutedBlob(plaintext: string): MutedList | undefined {
|
|
270
|
+
let value: unknown
|
|
271
|
+
try {
|
|
272
|
+
value = JSON.parse(plaintext)
|
|
273
|
+
} catch {
|
|
274
|
+
return undefined
|
|
275
|
+
}
|
|
276
|
+
if (!value || typeof value !== 'object') return undefined
|
|
277
|
+
const { v, updatedAt, keys } = value as Record<string, unknown>
|
|
278
|
+
if (v !== 1 || typeof updatedAt !== 'number' || !Number.isFinite(updatedAt) || !Array.isArray(keys)) return undefined
|
|
279
|
+
return { v: 1, updatedAt, keys: [...new Set(keys.filter(isMuteKey))].sort() }
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** The plaintext to encrypt to self: keys de-duplicated and sorted, so equal lists are equal bytes. */
|
|
283
|
+
export function serializeMutedBlob(keys: Iterable<string>, updatedAt: number): string {
|
|
284
|
+
return JSON.stringify({ v: 1, updatedAt, keys: [...new Set([...keys].filter(isMuteKey))].sort() })
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* The unsigned `kind:30078` holding the encrypted mute list. Whole blob, last
|
|
289
|
+
* write wins (§12.3): a writer reads the current list, changes it, and writes
|
|
290
|
+
* all of it.
|
|
291
|
+
*/
|
|
292
|
+
export function buildMutedEvent(pubkey: string, createdAtMs: number, ciphertext: string): UnsignedEvent {
|
|
293
|
+
return {
|
|
294
|
+
pubkey,
|
|
295
|
+
created_at: Math.floor(createdAtMs / 1000),
|
|
296
|
+
kind: KIND_APP_DATA,
|
|
297
|
+
tags: [
|
|
298
|
+
['d', MUTED_D_TAG],
|
|
299
|
+
['t', APPDATA_TAG],
|
|
300
|
+
],
|
|
301
|
+
content: ciphertext,
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** The filter for the person's mute list. */
|
|
306
|
+
export function mutedFilter(pubkey: string): Filter {
|
|
307
|
+
return { kinds: [KIND_APP_DATA], authors: [pubkey], '#d': [MUTED_D_TAG], limit: 1 }
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* The one migration from a retired `estiva:followed:v1` blob: its `muted`
|
|
312
|
+
* keys, to merge into the mute list. Peek wrote `muted` as a map of key to
|
|
313
|
+
* epoch ms, and for one day as a plain list; both read. Its followed `keys`
|
|
314
|
+
* are **not** returned — the list was private, so nothing may publish them as
|
|
315
|
+
* memberships — and a followed Folder is dropped, since the roster decides a
|
|
316
|
+
* general stream.
|
|
317
|
+
*/
|
|
318
|
+
export function mutedFromFollowed(followedPlaintext: string): string[] {
|
|
319
|
+
let value: unknown
|
|
320
|
+
try {
|
|
321
|
+
value = JSON.parse(followedPlaintext)
|
|
322
|
+
} catch {
|
|
323
|
+
return []
|
|
324
|
+
}
|
|
325
|
+
const muted = value && typeof value === 'object' ? (value as Record<string, unknown>).muted : undefined
|
|
326
|
+
const keys = Array.isArray(muted) ? muted : muted && typeof muted === 'object' ? Object.keys(muted) : []
|
|
327
|
+
return [...new Set(keys.filter(isMuteKey))].sort()
|
|
328
|
+
}
|