@estiva-app/conversation 0.40.0 → 0.42.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 +54 -0
- package/README.md +2 -1
- package/dist/builders.d.ts +2 -1
- package/dist/builders.d.ts.map +1 -1
- package/dist/builders.js +2 -1
- package/dist/builders.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/places.d.ts +61 -3
- package/dist/places.d.ts.map +1 -1
- package/dist/places.js +112 -9
- package/dist/places.js.map +1 -1
- package/dist/reactions.d.ts +80 -1
- package/dist/reactions.d.ts.map +1 -1
- package/dist/reactions.js +146 -1
- package/dist/reactions.js.map +1 -1
- package/dist/readState.d.ts +79 -12
- package/dist/readState.d.ts.map +1 -1
- package/dist/readState.js +196 -53
- package/dist/readState.js.map +1 -1
- package/dist/readStateQueue.d.ts.map +1 -1
- package/dist/readStateQueue.js +8 -4
- package/dist/readStateQueue.js.map +1 -1
- package/package.json +3 -3
- package/src/builders.ts +2 -1
- package/src/index.ts +15 -1
- package/src/places.ts +172 -20
- package/src/reactions.ts +144 -2
- package/src/readState.ts +197 -49
- package/src/readStateQueue.ts +7 -3
package/src/readState.ts
CHANGED
|
@@ -78,6 +78,37 @@ export const THREAD_RULE_FROM = 1791226800
|
|
|
78
78
|
*/
|
|
79
79
|
export const REPLY_FLOOR_CONTEXT = 'reply-floor'
|
|
80
80
|
|
|
81
|
+
/**
|
|
82
|
+
* The reserved context of the reactions floor (§11.1, §11.6, ACT-7): every
|
|
83
|
+
* reaction to `P`'s message at or before it counts as seen. Raised, never
|
|
84
|
+
* lowered, when a cap drops a `reactions:` key — to that key's value — as
|
|
85
|
+
* {@link REPLY_FLOOR_CONTEXT} is for a `thread:` key, so a reaction somebody
|
|
86
|
+
* saw never turns a place orange again.
|
|
87
|
+
*
|
|
88
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-reaction-is-seen-once-in-its-conversation.md
|
|
89
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/reactions-seen-is-kept-under-reactions-and-opening-it-sees-it.md
|
|
90
|
+
*/
|
|
91
|
+
export const REACTIONS_FLOOR_CONTEXT = 'reactions-floor'
|
|
92
|
+
|
|
93
|
+
const REACTIONS_PREFIX = 'reactions:'
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Where "seen" is kept for the reactions to `P`'s messages in one conversation
|
|
97
|
+
* (§11.1, ACT-7): `reactions:` + the stream's context — `reactions:file:<d>`,
|
|
98
|
+
* `reactions:<file address>`, `reactions:<channel uuid>`. A reaction on a reply
|
|
99
|
+
* counts in its root's stream. It has no parent (§11.3): no stream, thread or
|
|
100
|
+
* floor marker reaches it, and it reaches none of them, so opening a file
|
|
101
|
+
* clears none of its reactions and seeing them reads none of its messages.
|
|
102
|
+
*
|
|
103
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-reaction-is-seen-once-in-its-conversation.md
|
|
104
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/reactions-seen-is-kept-under-reactions-and-opening-it-sees-it.md
|
|
105
|
+
*/
|
|
106
|
+
export function reactionsContext(context: string): string {
|
|
107
|
+
return `${REACTIONS_PREFIX}${context}`
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const isReactionsKey = (key: string) => key.startsWith(REACTIONS_PREFIX)
|
|
111
|
+
|
|
81
112
|
const EVENT_ID_RE = /^[0-9a-f]{64}$/
|
|
82
113
|
const SLOT_ID_RE = /^[0-9a-f]{32}$/
|
|
83
114
|
|
|
@@ -156,6 +187,30 @@ export function streamMarkerOf(merged: Readonly<Record<string, number>>, stream:
|
|
|
156
187
|
return out
|
|
157
188
|
}
|
|
158
189
|
|
|
190
|
+
/**
|
|
191
|
+
* Until when the reactions to `P`'s messages in one conversation are seen
|
|
192
|
+
* (§11.3, §11.6, ACT-7): the later of `reactions:<context>` over the stream's
|
|
193
|
+
* contexts and the reactions floor. A file is named both ways — pass its
|
|
194
|
+
* address and `file:<d>`; an address whose `d` is a file id reads
|
|
195
|
+
* `reactions:file:<d>` as well. Nothing else reaches it: no stream, thread or
|
|
196
|
+
* reply-floor marker. Unix seconds; 0 when nothing is held, which is earlier
|
|
197
|
+
* than every reaction that counts.
|
|
198
|
+
*
|
|
199
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-reaction-is-seen-once-in-its-conversation.md
|
|
200
|
+
*/
|
|
201
|
+
export function reactionsSeenOf(merged: Readonly<Record<string, number>>, streamContexts: readonly string[]): number {
|
|
202
|
+
let out = merged[REACTIONS_FLOOR_CONTEXT] ?? 0
|
|
203
|
+
const read = (context: string | undefined) => {
|
|
204
|
+
const at = context === undefined ? undefined : merged[reactionsContext(context)]
|
|
205
|
+
if (at !== undefined && at > out) out = at
|
|
206
|
+
}
|
|
207
|
+
for (const context of streamContexts) {
|
|
208
|
+
read(context)
|
|
209
|
+
if (ADDRESS_RE.test(context)) read(fileIdContext(fileIdOf(context)))
|
|
210
|
+
}
|
|
211
|
+
return out
|
|
212
|
+
}
|
|
213
|
+
|
|
159
214
|
/** `thread:<root-event-id>`, NIP-RS's own scheme. */
|
|
160
215
|
export function threadContext(rootEventId: string | null | undefined): string | undefined {
|
|
161
216
|
if (!rootEventId) return undefined
|
|
@@ -178,13 +233,25 @@ export function toContextSeconds(atMs: number): number {
|
|
|
178
233
|
}
|
|
179
234
|
|
|
180
235
|
/**
|
|
181
|
-
* Is this a context id
|
|
182
|
-
*
|
|
183
|
-
* The reserved `folder:` is
|
|
236
|
+
* Is this a context id this code **writes and judges**: a channel uuid, a file
|
|
237
|
+
* address, `file:<d>`, `thread:`/`msg:` with 64 lowercase hex, `reactions:`
|
|
238
|
+
* before any of those, or one of the two floors. The reserved `folder:` is
|
|
239
|
+
* refused until §11.5 specifies it.
|
|
240
|
+
*
|
|
241
|
+
* Only what an app writes must pass it ({@link advanceContexts}' updates).
|
|
242
|
+
* What a blob already holds is carried by {@link isCarriedContextId}.
|
|
184
243
|
*/
|
|
185
244
|
export function isPublishableContextId(id: string): boolean {
|
|
186
|
-
if (id === REPLY_FLOOR_CONTEXT) return true
|
|
245
|
+
if (id === REPLY_FLOOR_CONTEXT || id === REACTIONS_FLOOR_CONTEXT) return true
|
|
187
246
|
if (id.length === 0 || utf8(id) > MAX_CONTEXT_ID_BYTES) return false
|
|
247
|
+
if (!isReactionsKey(id)) return isConversationContextId(id)
|
|
248
|
+
// A reaction on a reply or a message counts in its root's stream, so there is no `reactions:thread:` or `reactions:msg:` (§11.1).
|
|
249
|
+
const rest = id.slice(REACTIONS_PREFIX.length)
|
|
250
|
+
return !rest.startsWith('thread:') && !rest.startsWith('msg:') && isConversationContextId(rest)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** A context of one conversation's own marker (§11.1): not a floor, not a `reactions:` key. */
|
|
254
|
+
function isConversationContextId(id: string): boolean {
|
|
188
255
|
if (id.startsWith('thread:')) return EVENT_ID_RE.test(id.slice('thread:'.length))
|
|
189
256
|
if (id.startsWith('msg:')) return EVENT_ID_RE.test(id.slice('msg:'.length))
|
|
190
257
|
// A key beginning `file:` whose remainder is not a lowercase UUID is not a file context (§11.1).
|
|
@@ -192,6 +259,39 @@ export function isPublishableContextId(id: string): boolean {
|
|
|
192
259
|
return UUID_RE.test(id) || ADDRESS_RE.test(id)
|
|
193
260
|
}
|
|
194
261
|
|
|
262
|
+
/**
|
|
263
|
+
* Is this a key a blob **carries**, known or not (§11.1, ACT-7): any non-empty
|
|
264
|
+
* string of at most {@link MAX_CONTEXT_ID_BYTES} bytes. A key this code does not
|
|
265
|
+
* know — another app's, a later version's — is kept verbatim through
|
|
266
|
+
* {@link parseReadStateBlob}, {@link mergeSlots}, {@link advanceContexts} and
|
|
267
|
+
* {@link publishReadState}, and never judged: dropping it would erase what a
|
|
268
|
+
* newer app wrote every time an older one republished its slot. Judging is
|
|
269
|
+
* {@link isPublishableContextId}'s.
|
|
270
|
+
*/
|
|
271
|
+
export function isCarriedContextId(id: string): boolean {
|
|
272
|
+
return typeof id === 'string' && id.length > 0 && id !== '__proto__' && utf8(id) <= MAX_CONTEXT_ID_BYTES
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** A blob's carried entries: every {@link isCarriedContextId} key with a valid timestamp. */
|
|
276
|
+
function carried(contexts: Readonly<Record<string, unknown>>): Record<string, number> {
|
|
277
|
+
const out = contextMap()
|
|
278
|
+
for (const [key, value] of Object.entries(contexts)) if (isCarriedContextId(key) && isTimestamp(value)) out[key] = value
|
|
279
|
+
return out
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* An empty context map. A carried key may be named like a member of `Object.prototype` (`constructor`, `toString`,
|
|
284
|
+
* `valueOf`): it is an own property here, so every lookup of a key read from a blob checks {@link has}, never
|
|
285
|
+
* `map[key] === undefined`. A non-enumerable `Symbol.toPrimitive` keeps the map printable (`String(map)`) when an own
|
|
286
|
+
* `toString` or `valueOf` key shadows the prototype's; it is not a key, so nothing serialises or compares it.
|
|
287
|
+
*/
|
|
288
|
+
function contextMap(): Record<string, number> {
|
|
289
|
+
return Object.defineProperty({}, Symbol.toPrimitive, { value: () => '[object Object]', enumerable: false })
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** Whether the map holds `key` itself, not by its prototype. */
|
|
293
|
+
const has = (map: Readonly<Record<string, number>>, key: string): boolean => Object.prototype.hasOwnProperty.call(map, key)
|
|
294
|
+
|
|
195
295
|
const isTimestamp = (value: unknown): value is number =>
|
|
196
296
|
typeof value === 'number' && Number.isInteger(value) && value >= 0 && value <= MAX_TIMESTAMP
|
|
197
297
|
|
|
@@ -315,17 +415,14 @@ export function parseReadStateBlob(plaintext: string | undefined): ReadStateBlob
|
|
|
315
415
|
if (blob.v !== 1) return undefined
|
|
316
416
|
const clientId =
|
|
317
417
|
typeof blob.client_id === 'string' && blob.client_id.length >= 1 && utf8(blob.client_id) <= MAX_CLIENT_ID_BYTES ? blob.client_id : ''
|
|
318
|
-
const contexts: Record<string, number> = {}
|
|
319
418
|
const source = typeof blob.contexts === 'object' && blob.contexts !== null ? blob.contexts : {}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
}
|
|
323
|
-
return { v: 1, client_id: clientId, contexts }
|
|
419
|
+
// Every well-formed key is kept, known or not (§11.1): an unknown one is carried, never judged.
|
|
420
|
+
return { v: 1, client_id: clientId, contexts: carried(source as Record<string, unknown>) }
|
|
324
421
|
}
|
|
325
422
|
|
|
326
423
|
/** Serialize for encryption, keys sorted, so an unchanged set is an unchanged string. */
|
|
327
424
|
export function serializeReadStateBlob(blob: ReadStateBlob): string {
|
|
328
|
-
const contexts
|
|
425
|
+
const contexts = contextMap()
|
|
329
426
|
for (const key of Object.keys(blob.contexts).sort()) contexts[key] = blob.contexts[key]
|
|
330
427
|
return JSON.stringify({ v: blob.v, client_id: blob.client_id, contexts })
|
|
331
428
|
}
|
|
@@ -351,7 +448,7 @@ const byNewestThenKey = (contexts: Readonly<Record<string, number>>) => (a: stri
|
|
|
351
448
|
export function capContextsToBytes(contexts: Readonly<Record<string, number>>, budget: number = MAX_CONTEXTS_BYTES): CappedContexts {
|
|
352
449
|
const cost = (key: string, value: number) => utf8(JSON.stringify(key)) + 1 + String(value).length
|
|
353
450
|
const keys = Object.keys(contexts).sort(byNewestThenKey(contexts))
|
|
354
|
-
const kept
|
|
451
|
+
const kept = contextMap()
|
|
355
452
|
let bytes = 2
|
|
356
453
|
for (let i = 0; i < keys.length; i++) {
|
|
357
454
|
const key = keys[i]
|
|
@@ -363,40 +460,73 @@ export function capContextsToBytes(contexts: Readonly<Record<string, number>>, b
|
|
|
363
460
|
return { contexts: kept, bytes, evicted: [] }
|
|
364
461
|
}
|
|
365
462
|
|
|
366
|
-
const isThreadKey = (key: string) => key.startsWith('thread:')
|
|
463
|
+
const isThreadKey = (key: string) => key.startsWith('thread:') && isPublishableContextId(key)
|
|
367
464
|
|
|
368
465
|
/**
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
*
|
|
373
|
-
|
|
466
|
+
* The two floors (§11.6), and the keys each pays for when a cap drops them: the
|
|
467
|
+
* reply floor for a `thread:` marker, the reactions floor for a `reactions:`
|
|
468
|
+
* key. A key nobody knows pays for nothing: it is never judged, so dropping it
|
|
469
|
+
* costs nothing that a floor would have to hold.
|
|
470
|
+
*/
|
|
471
|
+
export const FLOORS: readonly { context: string; pays: (key: string) => boolean }[] = [
|
|
472
|
+
{ context: REPLY_FLOOR_CONTEXT, pays: isThreadKey },
|
|
473
|
+
{ context: REACTIONS_FLOOR_CONTEXT, pays: (key) => isReactionsKey(key) && isPublishableContextId(key) },
|
|
474
|
+
]
|
|
475
|
+
|
|
476
|
+
const isFloor = (key: string) => key === REPLY_FLOOR_CONTEXT || key === REACTIONS_FLOOR_CONTEXT
|
|
477
|
+
|
|
478
|
+
/** A context map split into its floors and everything else. */
|
|
479
|
+
export function splitFloors(contexts: Readonly<Record<string, number>>): { floors: Record<string, number>; markers: Record<string, number> } {
|
|
480
|
+
const floors = contextMap()
|
|
481
|
+
const markers = contextMap()
|
|
482
|
+
for (const [key, value] of Object.entries(contexts)) (isFloor(key) ? floors : markers)[key] = value
|
|
483
|
+
return { floors, markers }
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* {@link capContextsToBytes} for a read-state blob (§11.6): a floor is never
|
|
488
|
+
* evicted, and dropping a `thread:` marker first raises the reply floor to its
|
|
489
|
+
* value, dropping a `reactions:` key the reactions floor. Under the thread rule
|
|
490
|
+
* a `thread:` marker is never made redundant by its stream, and a `reactions:`
|
|
491
|
+
* key has no parent at all, so without the floors every eviction would light
|
|
492
|
+
* what the person already read or saw. Deterministic and idempotent, like the
|
|
493
|
+
* cap it wraps.
|
|
374
494
|
*/
|
|
375
495
|
export function capReadStateContexts(contexts: Readonly<Record<string, number>>, budget: number = MAX_CONTEXTS_BYTES): CappedContexts {
|
|
376
|
-
const {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
496
|
+
const { floors: held, markers: rest } = splitFloors(contexts)
|
|
497
|
+
// A floor's slot is set aside first, at the widest value it can hold, so raising it never overflows. Only the floors
|
|
498
|
+
// held or needed are reserved, so a blob with no thread or reactions key evicted is capped as before.
|
|
499
|
+
const needed = new Set(Object.keys(held))
|
|
500
|
+
for (;;) {
|
|
501
|
+
const reserve = [...needed].reduce((sum, floor) => sum + utf8(JSON.stringify(floor)) + 1 + String(MAX_TIMESTAMP).length + 1, 0)
|
|
502
|
+
const capped = capContextsToBytes(rest, budget - reserve)
|
|
503
|
+
const more = FLOORS.filter((f) => !needed.has(f.context) && capped.evicted.some(f.pays))
|
|
504
|
+
if (more.length > 0) {
|
|
505
|
+
for (const f of more) needed.add(f.context)
|
|
506
|
+
continue
|
|
507
|
+
}
|
|
508
|
+
if (needed.size === 0) return capped
|
|
509
|
+
const kept = Object.assign(contextMap(), capped.contexts)
|
|
510
|
+
for (const f of FLOORS) {
|
|
511
|
+
if (!needed.has(f.context)) continue
|
|
512
|
+
kept[f.context] = capped.evicted.reduce((max, key) => (f.pays(key) && rest[key] > max ? rest[key] : max), held[f.context] ?? 0)
|
|
513
|
+
}
|
|
514
|
+
return { contexts: kept, bytes: utf8(JSON.stringify(kept)), evicted: capped.evicted }
|
|
380
515
|
}
|
|
381
|
-
// The floor's slot is set aside first, at the widest value it can hold, so raising it never overflows.
|
|
382
|
-
const reserve = utf8(JSON.stringify(REPLY_FLOOR_CONTEXT)) + 1 + String(MAX_TIMESTAMP).length + 1
|
|
383
|
-
const capped = capContextsToBytes(rest, budget - reserve)
|
|
384
|
-
const raised = capped.evicted.reduce((max, key) => (isThreadKey(key) && rest[key] > max ? rest[key] : max), floor ?? 0)
|
|
385
|
-
const kept = { ...capped.contexts, [REPLY_FLOOR_CONTEXT]: raised }
|
|
386
|
-
return { contexts: kept, bytes: utf8(JSON.stringify(kept)), evicted: capped.evicted }
|
|
387
516
|
}
|
|
388
517
|
|
|
389
518
|
/**
|
|
390
519
|
* Apply markers, taking the later of each (§11.4: never lower one), then cap.
|
|
391
|
-
* Unpublishable ids and out-of-range timestamps are dropped,
|
|
392
|
-
*
|
|
393
|
-
*
|
|
520
|
+
* Unpublishable ids and out-of-range timestamps among the updates are dropped,
|
|
521
|
+
* and so is a floor: only the cap raises one (§11.6), so a stray value cannot
|
|
522
|
+
* mark every reply read or every reaction seen. What `current` holds is
|
|
523
|
+
* carried, a key this code does not know and the floors included (§11.1).
|
|
394
524
|
*/
|
|
395
525
|
export function advanceContexts(current: Readonly<Record<string, number>>, updates: Readonly<Record<string, number>>): CappedContexts {
|
|
396
|
-
const next: Record<string, number> =
|
|
526
|
+
const next: Record<string, number> = carried(current)
|
|
397
527
|
for (const [id, seconds] of Object.entries(updates)) {
|
|
398
|
-
if (id
|
|
399
|
-
if (next
|
|
528
|
+
if (isFloor(id) || !isPublishableContextId(id) || !isTimestamp(seconds)) continue
|
|
529
|
+
if (!has(next, id) || seconds > next[id]) next[id] = seconds
|
|
400
530
|
}
|
|
401
531
|
return capReadStateContexts(next)
|
|
402
532
|
}
|
|
@@ -442,24 +572,35 @@ export interface SlotBlob {
|
|
|
442
572
|
|
|
443
573
|
/** Every slot merged by the max rule, capped at {@link MAX_CONTEXTS} newest first, ties by key. */
|
|
444
574
|
export function mergeSlots(slots: readonly (Pick<SlotBlob, 'blob'> & { createdAt?: number })[]): Record<string, number> {
|
|
445
|
-
const merged
|
|
575
|
+
const merged = contextMap()
|
|
446
576
|
for (const { blob, createdAt } of slots) {
|
|
447
|
-
|
|
448
|
-
|
|
577
|
+
// Every well-formed key, known or not, is carried (§11.1); only the known ones are ever judged.
|
|
578
|
+
for (const [id, value] of Object.entries(carried(blob.contexts))) {
|
|
449
579
|
// A floor is the value of a marker the cap dropped, so never later than the blob that holds it (§11.6).
|
|
450
|
-
const seconds = id
|
|
451
|
-
if (merged
|
|
580
|
+
const seconds = isFloor(id) && createdAt !== undefined ? Math.min(value, createdAt) : value
|
|
581
|
+
if (!has(merged, id) || seconds > merged[id]) merged[id] = seconds
|
|
452
582
|
}
|
|
453
583
|
}
|
|
454
|
-
const {
|
|
584
|
+
const { floors, markers: rest } = splitFloors(merged)
|
|
455
585
|
const keys = Object.keys(rest)
|
|
456
|
-
if (keys.length + (
|
|
457
|
-
// Over the count cap, as over the byte cap: the
|
|
586
|
+
if (keys.length + Object.keys(floors).length <= MAX_CONTEXTS) return merged
|
|
587
|
+
// Over the count cap, as over the byte cap: the floors stay, each paying for the keys it covers that are dropped.
|
|
458
588
|
const sorted = keys.sort(byNewestThenKey(rest))
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
589
|
+
// Room is kept for each floor held or needed, so the merged view is at most MAX_CONTEXTS keys, floors included.
|
|
590
|
+
const needed = new Set(Object.keys(floors))
|
|
591
|
+
let room = MAX_CONTEXTS - needed.size
|
|
592
|
+
for (const f of FLOORS) {
|
|
593
|
+
if (needed.has(f.context) || !sorted.slice(room - 1).some(f.pays)) continue
|
|
594
|
+
needed.add(f.context)
|
|
595
|
+
room -= 1
|
|
596
|
+
}
|
|
597
|
+
const kept = contextMap()
|
|
598
|
+
for (const id of sorted.slice(0, room)) kept[id] = rest[id]
|
|
599
|
+
const dropped = sorted.slice(room)
|
|
600
|
+
for (const f of FLOORS) {
|
|
601
|
+
if (!needed.has(f.context)) continue
|
|
602
|
+
kept[f.context] = dropped.reduce((max, key) => (f.pays(key) && rest[key] > max ? rest[key] : max), floors[f.context] ?? 0)
|
|
603
|
+
}
|
|
463
604
|
return kept
|
|
464
605
|
}
|
|
465
606
|
|
|
@@ -503,6 +644,8 @@ export function effectiveReadAt(
|
|
|
503
644
|
from: number = THREAD_RULE_FROM,
|
|
504
645
|
): number | undefined {
|
|
505
646
|
const own = merged[context]
|
|
647
|
+
// A `reactions:` key has no parent (§11.3): no stream's marker reaches it, only the reactions floor.
|
|
648
|
+
if (isReactionsKey(context)) return later(own, merged[REACTIONS_FLOOR_CONTEXT])
|
|
506
649
|
const parent = stream === undefined ? undefined : streamMarkerOf(merged, stream)
|
|
507
650
|
if (isThreadKey(context)) return threadReadAt({ thread: own, stream: parent, floor: merged[REPLY_FLOOR_CONTEXT] }, from)
|
|
508
651
|
return later(own, parent)
|
|
@@ -733,11 +876,16 @@ export async function publishReadState(
|
|
|
733
876
|
identity: SlotIdentity,
|
|
734
877
|
contexts: Readonly<Record<string, number>>,
|
|
735
878
|
nowMs: number,
|
|
879
|
+
slot?: Readonly<Record<string, number>>,
|
|
736
880
|
): Promise<{ ok: boolean; reason?: string }> {
|
|
737
|
-
// Filtered here as well: the blob is grow-only, and a map built outside `advanceContexts` must not put an id into
|
|
738
|
-
//
|
|
739
|
-
|
|
740
|
-
|
|
881
|
+
// Filtered here as well: the blob is grow-only, and a map built outside `advanceContexts` must not put an id into
|
|
882
|
+
// it, so the caller's markers go in as updates, through the gate. The floors go in as `current`, since
|
|
883
|
+
// `advanceContexts` refuses one among the updates. What `slot` — this slot as read back — already holds is carried
|
|
884
|
+
// verbatim, a key this code does not know included: another app or a later version wrote it, and republishing the
|
|
885
|
+
// slot must not erase it (§11.1). A key that is in neither is never written.
|
|
886
|
+
const { floors, markers } = splitFloors(contexts)
|
|
887
|
+
const held = carried(slot ?? {})
|
|
888
|
+
for (const [floor, at] of Object.entries(carried(floors))) held[floor] = has(held, floor) ? Math.max(at, held[floor]) : at
|
|
741
889
|
const plaintext = serializeReadStateBlob({ v: 1, client_id: identity.clientId, contexts: advanceContexts(held, markers).contexts })
|
|
742
890
|
if (utf8(plaintext) > MAX_BLOB_BYTES) return { ok: false, reason: `read state is ${utf8(plaintext)} bytes, over ${MAX_BLOB_BYTES}` }
|
|
743
891
|
const ciphertext = await deps.nip44.encrypt(plaintext)
|
package/src/readStateQueue.ts
CHANGED
|
@@ -24,8 +24,8 @@ import type { SignedEvent, UnsignedEvent } from '@estiva-app/protocol'
|
|
|
24
24
|
import type { QueryFn } from './decorations.js'
|
|
25
25
|
import { KIND_APP_DATA } from './kinds.js'
|
|
26
26
|
import {
|
|
27
|
-
REPLY_FLOOR_CONTEXT,
|
|
28
27
|
advanceContexts as advance,
|
|
28
|
+
splitFloors,
|
|
29
29
|
loadSlotIdentity,
|
|
30
30
|
parseReadStateBlob,
|
|
31
31
|
publishReadState,
|
|
@@ -197,8 +197,9 @@ export function createReadStateQueue(options: ReadStateQueueOptions): ReadStateQ
|
|
|
197
197
|
// Seeded from our own slot, never the merge: a slot is this installation's account of what it read, and seeding
|
|
198
198
|
// from the merge grows every slot to everything the person has read. The floor goes in as `current`, since
|
|
199
199
|
// `advanceContexts` refuses one among the updates.
|
|
200
|
-
const {
|
|
201
|
-
const held =
|
|
200
|
+
const { floors, markers: marked } = splitFloors(known)
|
|
201
|
+
const held = { ...own }
|
|
202
|
+
for (const [floor, at] of Object.entries(floors)) held[floor] = Math.max(at, own[floor] ?? 0)
|
|
202
203
|
known = advanceContexts(held, marked)
|
|
203
204
|
published = own
|
|
204
205
|
hydrated = true
|
|
@@ -244,6 +245,8 @@ export function createReadStateQueue(options: ReadStateQueueOptions): ReadStateQ
|
|
|
244
245
|
}
|
|
245
246
|
const sending = pending
|
|
246
247
|
const contexts = { ...known }
|
|
248
|
+
// Our slot as read back: what it holds is carried verbatim, keys this version does not know included (§11.1).
|
|
249
|
+
const slot = ownCopy?.contexts
|
|
247
250
|
pending = {}
|
|
248
251
|
if (published && sameContexts(published, contexts)) return attempt('unchanged')
|
|
249
252
|
try {
|
|
@@ -266,6 +269,7 @@ export function createReadStateQueue(options: ReadStateQueueOptions): ReadStateQ
|
|
|
266
269
|
// "duplicate" as accepted, so a larger blob published within the second (a flush on hide right after the
|
|
267
270
|
// debounced one) could lose to the smaller one with nothing to say so.
|
|
268
271
|
Math.max(Date.now(), (slotAt + 1) * 1000),
|
|
272
|
+
slot,
|
|
269
273
|
)
|
|
270
274
|
if (result.ok) {
|
|
271
275
|
published = contexts
|