@pmate/chat-sdk 0.13.0-profile.1 → 0.13.0-profile.10
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/README.md +26 -0
- package/lib/profile.d.ts +4 -1
- package/lib/profile.js +4 -1
- package/lib/runtime/profileChat.d.ts +118 -1
- package/lib/runtime/profileChat.js +98 -30
- package/lib/runtime/profileCommitQueue.d.ts +2 -1
- package/lib/runtime/profileCommitQueue.js +26 -7
- package/lib/runtime/profileCoordinator.d.ts +19 -19
- package/lib/runtime/profileCoordinator.js +127 -94
- package/lib/runtime/profileDatabase.d.ts +7 -7
- package/lib/runtime/profileDatabase.js +57 -42
- package/lib/runtime/profileGenerationBuilder.d.ts +2 -3
- package/lib/runtime/profileGenerationBuilder.js +60 -26
- package/lib/runtime/profileInboxFeed.d.ts +1 -0
- package/lib/runtime/profileInboxFeed.js +12 -2
- package/lib/runtime/profileLegacyInbox.d.ts +8 -0
- package/lib/runtime/profileLegacyInbox.js +114 -0
- package/lib/runtime/profileLegacyOutbox.d.ts +7 -0
- package/lib/runtime/profileLegacyOutbox.js +106 -0
- package/lib/runtime/profileLegacyReading.d.ts +7 -0
- package/lib/runtime/profileLegacyReading.js +110 -0
- package/lib/runtime/profileMemoryStore.d.ts +22 -2
- package/lib/runtime/profileMemoryStore.js +140 -12
- package/lib/runtime/profileOutboxDispatcher.d.ts +3 -0
- package/lib/runtime/profileOutboxDispatcher.js +41 -5
- package/lib/runtime/profileOwner.d.ts +11 -6
- package/lib/runtime/profileOwner.js +71 -46
- package/lib/runtime/profileProjection.d.ts +54 -5
- package/lib/runtime/profileProjection.js +336 -51
- package/lib/runtime/profileReadingIndex.d.ts +1 -0
- package/lib/runtime/profileReadingIndex.js +17 -1
- package/lib/runtime/profileStoreTypes.d.ts +15 -9
- package/lib/runtime/{profileReplica.d.ts → profileWorkerClient.d.ts} +10 -26
- package/lib/runtime/{profileReplica.js → profileWorkerClient.js} +55 -102
- package/lib/runtime/profileWorkerHost.d.ts +4 -6
- package/lib/runtime/profileWorkerHost.js +58 -21
- package/package.json +2 -2
|
@@ -7,6 +7,13 @@ import { ProfileReadingIndex } from "./profileReadingIndex.js";
|
|
|
7
7
|
import { compareProfileOrder, ProfileWindowDirectory } from "./profileWindows.js";
|
|
8
8
|
import { projectMessageContent } from "../messageView.js";
|
|
9
9
|
export const profileDiscussionId = (parent, root) => `discussion:${JSON.stringify([parent, root])}`;
|
|
10
|
+
function mergeAssignedLists(source, destination) {
|
|
11
|
+
const merged = { ...source };
|
|
12
|
+
for (const [key, values] of Object.entries(destination ?? {}))
|
|
13
|
+
merged[key] = [...new Map([...(merged[key] ?? []), ...values].map(value => [JSON.stringify(value), value])).values()];
|
|
14
|
+
return merged;
|
|
15
|
+
}
|
|
16
|
+
const supportedLocalOperations = new Set(["restoreReadingSources", "restoreMessageReadingFacts", "migrationCompleted", "restoreReadingCommand", "restoreReadingIntent", "restoreEvent", "restoreInvitation", "register", "teamAccess", "send", "commandPayload", "observe", "commandResult", "restoreReading", "restoreReceipt", "receiptsReady"]);
|
|
10
17
|
/** Committed authority for the replay reducer. Drafts contain only changed rows;
|
|
11
18
|
* failure discards the draft, leaving both entities and indexes untouched. */
|
|
12
19
|
export class ProfileProjection {
|
|
@@ -51,13 +58,20 @@ export class ProfileProjection {
|
|
|
51
58
|
}
|
|
52
59
|
prepare(events) {
|
|
53
60
|
const draft = new ProfileProjectionDraft(this);
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
const rejected = [];
|
|
62
|
+
for (const [index, event] of events.entries()) {
|
|
63
|
+
if ("type" in event) {
|
|
64
|
+
const error = draft.localAccessError(event);
|
|
65
|
+
if (error) {
|
|
66
|
+
rejected[index] = error;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
56
69
|
draft.local(event);
|
|
70
|
+
}
|
|
57
71
|
else
|
|
58
72
|
draft.event(event);
|
|
59
73
|
}
|
|
60
|
-
return draft.finish();
|
|
74
|
+
return { ...draft.finish(), rejected };
|
|
61
75
|
}
|
|
62
76
|
prepareBasis(bootstrap) {
|
|
63
77
|
if (bootstrap.appId !== this.identity.appId || bootstrap.profileId !== this.identity.profileId || bootstrap.epoch !== this.current.epoch)
|
|
@@ -117,12 +131,49 @@ class ProfileProjectionDraft {
|
|
|
117
131
|
seq;
|
|
118
132
|
revision;
|
|
119
133
|
receiptsReady;
|
|
134
|
+
localMigrations;
|
|
135
|
+
revokedTeamIds;
|
|
136
|
+
teamAccessRevision;
|
|
137
|
+
readingCommands = new Map();
|
|
120
138
|
constructor(base) {
|
|
121
139
|
this.base = base;
|
|
122
140
|
this.seq = inboxSequence(base.current.committedSeq);
|
|
123
141
|
this.revision = base.current.commitRevision + 1;
|
|
124
142
|
this.receiptsReady = base.current.receiptsReady;
|
|
125
143
|
}
|
|
144
|
+
localAccessError(operation) {
|
|
145
|
+
if (!supportedLocalOperations.has(operation.type))
|
|
146
|
+
return Error("PROFILE_OPERATION_UNSUPPORTED");
|
|
147
|
+
if (operation.type === "teamAccess" && operation.allowed && operation.expectedAccessRevision !== (this.teamAccessRevision ?? this.base.current.teamAccessRevision ?? 0))
|
|
148
|
+
return Error("PROFILE_ACCESS_CHECK_STALE");
|
|
149
|
+
if (operation.type !== "send" && operation.type !== "observe")
|
|
150
|
+
return;
|
|
151
|
+
const thread = this.thread(operation.threadId);
|
|
152
|
+
const parentId = thread?.parentThreadId ?? (operation.type === "send" ? operation.parentThreadId : undefined);
|
|
153
|
+
const parent = parentId ? this.thread(parentId) : undefined;
|
|
154
|
+
if ((this.revokedTeamIds ?? this.base.current.revokedTeamIds ?? []).includes((parent ?? thread)?.teamId ?? ""))
|
|
155
|
+
return Error("PROFILE_TEAM_ACCESS_REVOKED");
|
|
156
|
+
if (thread?.status !== "ready" && !(operation.type === "send" && !thread && parent?.status === "ready") || parentId && parent?.status !== "ready")
|
|
157
|
+
return Error("PROFILE_THREAD_NOT_READY");
|
|
158
|
+
if (operation.type === "send") {
|
|
159
|
+
const wire = operation.message, command = operation.command;
|
|
160
|
+
if (!wire || !command || !wire.hash || wire.from !== this.base.identity.profileId || command.messageId !== wire.hash || command.commandId !== wire.hash || command.threadId !== operation.threadId)
|
|
161
|
+
return Error("OUTGOING_MESSAGE_IDENTITY_MISMATCH");
|
|
162
|
+
const root = messageThreadRoot(wire.body);
|
|
163
|
+
if (!thread && (!parentId || !root || profileDiscussionId(parentId, root) !== operation.threadId))
|
|
164
|
+
return Error("INVALID_DISCUSSION_DESTINATION");
|
|
165
|
+
const destination = parent ?? thread;
|
|
166
|
+
if (destination?.internal?.targetId && destination.internal.targetId !== wire.to)
|
|
167
|
+
return Error("OUTGOING_MESSAGE_DESTINATION_MISMATCH");
|
|
168
|
+
const body = wire.body;
|
|
169
|
+
const scope = body?.content ?? body?.payload;
|
|
170
|
+
if (scope?.appId !== undefined && scope.appId !== this.base.identity.appId || scope?.teamId !== undefined && scope.teamId !== destination?.teamId)
|
|
171
|
+
return Error("OUTGOING_MESSAGE_SCOPE_MISMATCH");
|
|
172
|
+
const reaction = body?.kind === "message" ? body.content?.reaction : undefined;
|
|
173
|
+
if (reaction && (reaction.version !== 1 || typeof reaction.messageId !== "string" || !reaction.messageId || typeof reaction.emojiId !== "string" || !reaction.emojiId || typeof reaction.active !== "boolean" || operation.preserveWire && (!Number.isSafeInteger(reaction.revision) || reaction.revision < 1)))
|
|
174
|
+
return Error("INVALID_MESSAGE_REACTION");
|
|
175
|
+
}
|
|
176
|
+
}
|
|
126
177
|
message(id) { return this.messages.get(id) ?? this.base.messages.get(id); }
|
|
127
178
|
thread(id) { return this.threads.get(id) ?? this.base.threads.get(id); }
|
|
128
179
|
ensure(id, kind = "group", extra = {}) {
|
|
@@ -171,31 +222,55 @@ class ProfileProjectionDraft {
|
|
|
171
222
|
return first?.messageId;
|
|
172
223
|
}
|
|
173
224
|
read(message, thread = this.thread(message.threadId)) {
|
|
174
|
-
if (message.
|
|
225
|
+
if (message.recalled)
|
|
175
226
|
return true;
|
|
176
227
|
if (message.readPolicy === "discussion")
|
|
177
228
|
return this.thread(message.discussionThreadId)?.readingStatus === "read";
|
|
178
|
-
if (
|
|
179
|
-
return
|
|
229
|
+
if (message.from === this.base.identity.profileId)
|
|
230
|
+
return true;
|
|
180
231
|
if (message.explicitReadingFacts.some(fact => fact.profileId === this.base.identity.profileId))
|
|
181
232
|
return true;
|
|
182
233
|
const source = thread && this.source(thread, message.sourceThreadId);
|
|
183
234
|
if (source?.preservedReadMessageIds.includes(message.messageId) || source?.explicitReadMessageIds.includes(message.messageId) || source?.pendingExplicitReadMessageIds?.includes(message.messageId))
|
|
184
235
|
return true;
|
|
185
|
-
return message.readPolicy !== "explicit" && message.sourceSeq <= Math.max(source?.readThroughSeq ?? 0, source?.pendingReadThroughSeq ?? 0);
|
|
236
|
+
return !!message.sourceSeq && message.readPolicy !== "explicit" && message.sourceSeq <= Math.max(source?.readThroughSeq ?? 0, source?.pendingReadThroughSeq ?? 0);
|
|
237
|
+
}
|
|
238
|
+
unknownReading(message, thread = message && this.thread(message.threadId)) {
|
|
239
|
+
return message && !message.sourceSeq && message.from !== this.base.identity.profileId && !this.read(message, thread) ? 1 : 0;
|
|
186
240
|
}
|
|
187
241
|
contribution(message, thread) {
|
|
188
|
-
return message && message.readPolicy !== "discussion" && !this.read(message, thread) ? { unread: 1, mentions: message.mention ? 1 : 0 } : { unread: 0, mentions: 0 };
|
|
242
|
+
return message && !this.unknownReading(message, thread) && message.readPolicy !== "discussion" && !this.read(message, thread) ? { unread: 1, mentions: message.mention ? 1 : 0 } : { unread: 0, mentions: 0 };
|
|
189
243
|
}
|
|
190
|
-
updateSummary(id, unreadDelta, mentionDelta) {
|
|
191
|
-
if (!unreadDelta && !mentionDelta)
|
|
244
|
+
updateSummary(id, unreadDelta, mentionDelta, unknownDelta = 0) {
|
|
245
|
+
if (!unreadDelta && !mentionDelta && !unknownDelta)
|
|
192
246
|
return;
|
|
193
247
|
const thread = this.thread(id);
|
|
194
|
-
const
|
|
195
|
-
|
|
248
|
+
const unknownReadingCount = Math.max(0, (thread.internal?.unknownReadingCount ?? 0) + unknownDelta);
|
|
249
|
+
let unreadCount = null, mentionCount = null;
|
|
250
|
+
if (!unknownReadingCount) {
|
|
251
|
+
if (thread.unreadCount === null || thread.mentionCount === null) {
|
|
252
|
+
const summary = this.readingIndex(id).summary(thread.readingSources);
|
|
253
|
+
unreadCount = summary.unread;
|
|
254
|
+
mentionCount = summary.mentions;
|
|
255
|
+
const children = new Set(this.base.children.get(id));
|
|
256
|
+
for (const row of this.threads.values())
|
|
257
|
+
if (row.parentThreadId === id)
|
|
258
|
+
children.add(row.threadId);
|
|
259
|
+
for (const childId of children) {
|
|
260
|
+
const child = this.thread(childId);
|
|
261
|
+
unreadCount += child?.unreadCount ?? 0;
|
|
262
|
+
mentionCount += child?.mentionCount ?? 0;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
unreadCount = Math.max(0, thread.unreadCount + unreadDelta);
|
|
267
|
+
mentionCount = Math.max(0, thread.mentionCount + mentionDelta);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
this.putThread({ ...thread, unreadCount, mentionCount, internal: { ...thread.internal, unknownReadingCount },
|
|
196
271
|
firstUnreadMessageId: this.firstUnread(id), readingStatus: unreadCount === null ? "unknown" : unreadCount > 0 ? "unread" : "read" });
|
|
197
272
|
if (thread.parentThreadId)
|
|
198
|
-
this.updateSummary(thread.parentThreadId, unreadDelta, mentionDelta);
|
|
273
|
+
this.updateSummary(thread.parentThreadId, unreadDelta, mentionDelta, unknownDelta);
|
|
199
274
|
}
|
|
200
275
|
setMessage(next) {
|
|
201
276
|
const previous = this.message(next.messageId);
|
|
@@ -205,11 +280,11 @@ class ProfileProjectionDraft {
|
|
|
205
280
|
this.readingIndex(next.threadId).upsert(next, this.base.identity.profileId, this.source(this.thread(next.threadId), next.sourceThreadId));
|
|
206
281
|
this.messages.set(next.messageId, { ...next, revision: this.revision });
|
|
207
282
|
if (previous?.threadId === next.threadId)
|
|
208
|
-
this.updateSummary(next.threadId, after.unread - before.unread, after.mentions - before.mentions);
|
|
283
|
+
this.updateSummary(next.threadId, after.unread - before.unread, after.mentions - before.mentions, this.unknownReading(next) - this.unknownReading(previous));
|
|
209
284
|
else {
|
|
210
285
|
if (previous)
|
|
211
|
-
this.updateSummary(previous.threadId, -before.unread, -before.mentions);
|
|
212
|
-
this.updateSummary(next.threadId, after.unread, after.mentions);
|
|
286
|
+
this.updateSummary(previous.threadId, -before.unread, -before.mentions, -this.unknownReading(previous));
|
|
287
|
+
this.updateSummary(next.threadId, after.unread, after.mentions, this.unknownReading(next));
|
|
213
288
|
}
|
|
214
289
|
}
|
|
215
290
|
insert(message, historical = false) {
|
|
@@ -277,7 +352,7 @@ class ProfileProjectionDraft {
|
|
|
277
352
|
this.assign(event);
|
|
278
353
|
break;
|
|
279
354
|
case "relationshipUpdated":
|
|
280
|
-
this.relationship(event);
|
|
355
|
+
this.relationship(event, basis);
|
|
281
356
|
break;
|
|
282
357
|
default: throw Error(`UNSUPPORTED_INBOX_EVENT:${event.kind}`);
|
|
283
358
|
}
|
|
@@ -285,6 +360,66 @@ class ProfileProjectionDraft {
|
|
|
285
360
|
this.seq = seq;
|
|
286
361
|
}
|
|
287
362
|
local(operation) {
|
|
363
|
+
if (operation.type === "restoreReadingSources") {
|
|
364
|
+
if (this.thread(operation.threadId))
|
|
365
|
+
this.mergeAssignedReading(operation.threadId, operation.sources);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
if (operation.type === "restoreMessageReadingFacts") {
|
|
369
|
+
const message = this.message(operation.messageId);
|
|
370
|
+
if (message)
|
|
371
|
+
this.setMessage({ ...message, explicitReadingFacts: [...new Map([...message.explicitReadingFacts, ...operation.facts.filter(fact => fact.profileId === this.base.identity.profileId)]
|
|
372
|
+
.map(fact => [JSON.stringify(fact), fact])).values()] });
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (operation.type === "restoreInvitation") {
|
|
376
|
+
const wire = operation.message, body = wire.body;
|
|
377
|
+
if (wire.appId !== this.base.identity.appId || wire.seq || body?.kind !== "contact" || body.action !== "friendRequest" || ![wire.from, wire.to].includes(this.base.identity.profileId))
|
|
378
|
+
throw Error("INVALID_LEGACY_INVITATION");
|
|
379
|
+
this.invitation({ threadId: wire.threadId, messageId: wire.messageId, actorId: wire.from, targetId: wire.to, teamId: wire.teamId, createdAt: wire.serverCreatedAt }, operation.acceptedAt);
|
|
380
|
+
const message = operation.recalled && this.message(wire.messageId);
|
|
381
|
+
if (message && !message.recalled)
|
|
382
|
+
this.setMessage({ ...message, recalled: true });
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
if (operation.type === "teamAccess") {
|
|
386
|
+
if (!operation.teamId)
|
|
387
|
+
throw Error("INVALID_PROFILE_TEAM");
|
|
388
|
+
const revoked = new Set(this.revokedTeamIds ?? this.base.current.revokedTeamIds ?? []);
|
|
389
|
+
if (operation.allowed && !revoked.has(operation.teamId))
|
|
390
|
+
return;
|
|
391
|
+
if (operation.allowed)
|
|
392
|
+
revoked.delete(operation.teamId);
|
|
393
|
+
else
|
|
394
|
+
revoked.add(operation.teamId);
|
|
395
|
+
this.revokedTeamIds = [...revoked].sort();
|
|
396
|
+
this.teamAccessRevision = (this.teamAccessRevision ?? this.base.current.teamAccessRevision ?? 0) + 1;
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if (operation.type === "restoreReadingCommand") {
|
|
400
|
+
const command = operation.command, data = command.payload;
|
|
401
|
+
if (!["reading", "played"].includes(command.kind) || !command.commandId || data.appId !== this.base.identity.appId || data.profileId !== this.base.identity.profileId)
|
|
402
|
+
throw Error("INVALID_READING_COMMAND");
|
|
403
|
+
this.reading({ inboxSeq: "0", sourceId: command.commandId, acceptedAt: command.createdAt, kind: "readingUpdated", fact: data }, false);
|
|
404
|
+
if (!this.outbox.has(command.commandId) && !this.base.outbox.has(command.commandId))
|
|
405
|
+
this.outbox.set(command.commandId, command);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
if (operation.type === "restoreReadingIntent") {
|
|
409
|
+
this.restoreReadingIntent(operation);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
if (operation.type === "migrationCompleted") {
|
|
413
|
+
this.localMigrations = [...new Set([...(this.localMigrations ?? this.base.current.localMigrations ?? []), operation.id])];
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (operation.type === "restoreEvent") {
|
|
417
|
+
this.event(operation.event, true);
|
|
418
|
+
const message = operation.recalled && this.message(operation.event.fact.messageId);
|
|
419
|
+
if (message && !message.recalled)
|
|
420
|
+
this.setMessage({ ...message, recalled: true });
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
288
423
|
if (operation.type === "commandPayload") {
|
|
289
424
|
const command = this.outbox.get(operation.commandId) ?? this.base.outbox.get(operation.commandId);
|
|
290
425
|
if (!command || command.kind !== "message" || operation.message.hash !== command.messageId || operation.message.from !== this.base.identity.profileId)
|
|
@@ -301,12 +436,6 @@ class ProfileProjectionDraft {
|
|
|
301
436
|
this.ensure(operation.threadId, operation.kind, { teamId: operation.teamId, title: operation.title ?? "", internal: { targetId: operation.targetId } });
|
|
302
437
|
return;
|
|
303
438
|
}
|
|
304
|
-
if (operation.type === "revoke") {
|
|
305
|
-
const thread = this.thread(operation.threadId);
|
|
306
|
-
if (thread && thread.status !== "unavailable")
|
|
307
|
-
this.putThread({ ...thread, status: "unavailable", readingStatus: "unknown", unreadCount: null, mentionCount: null });
|
|
308
|
-
return;
|
|
309
|
-
}
|
|
310
439
|
if (operation.type === "restoreReceipt") {
|
|
311
440
|
this.reading({ inboxSeq: "0", sourceId: "receipt-baseline", acceptedAt: 0, kind: "readingUpdated", fact: { ...operation.data, receiptOnly: true } });
|
|
312
441
|
return;
|
|
@@ -350,7 +479,7 @@ class ProfileProjectionDraft {
|
|
|
350
479
|
const reaction = body?.kind === "message" ? body.content?.reaction : undefined;
|
|
351
480
|
const recall = body?.kind === "recall" ? body.targetMessageId : body?.content?.recall?.messageId;
|
|
352
481
|
if (reaction || recall || body?.kind === "group") {
|
|
353
|
-
if (reaction) {
|
|
482
|
+
if (reaction && !operation.preserveWire) {
|
|
354
483
|
const target = this.message(reaction.messageId), key = JSON.stringify(["reaction", wire.from, reaction.emojiId]);
|
|
355
484
|
const previous = target?.controlVersions[key];
|
|
356
485
|
const floor = typeof previous === "string" ? JSON.parse(previous)[0] : 0;
|
|
@@ -368,7 +497,7 @@ class ProfileProjectionDraft {
|
|
|
368
497
|
const message = { generation: this.base.current.activeGeneration, messageId: wire.hash,
|
|
369
498
|
sourceThreadId: thread.parentThreadId ?? thread.threadId, threadId: thread.threadId,
|
|
370
499
|
orderKey: [wire.t, thread.parentThreadId ?? thread.threadId, Number.MAX_SAFE_INTEGER, wire.hash], from: wire.from,
|
|
371
|
-
recipients: wire.recipients, content: wire.body, attachmentRefs: [], reactions: [], sendStatus: "sending",
|
|
500
|
+
recipients: wire.recipients, content: wire.body, attachmentRefs: [], reactions: [], sendStatus: command.state === "failed" ? "failed" : "sending",
|
|
372
501
|
readPolicy: messageRequiresPlayback(wire.body) ? "explicit" : "visible", readBy: [], receiptStatus: "unknown",
|
|
373
502
|
explicitReadingFacts: [], controlVersions: {}, revision: this.revision };
|
|
374
503
|
message.presentation = projectMessageContent({ id: wire.hash, messageId: wire.hash, threadId: thread.threadId,
|
|
@@ -381,7 +510,7 @@ class ProfileProjectionDraft {
|
|
|
381
510
|
}
|
|
382
511
|
if (operation.type === "commandResult") {
|
|
383
512
|
const command = this.outbox.get(operation.commandId) ?? this.base.outbox.get(operation.commandId);
|
|
384
|
-
if (!command)
|
|
513
|
+
if (!command || operation.state === command.state && operation.state !== "sending")
|
|
385
514
|
return;
|
|
386
515
|
if (operation.state === "confirmed")
|
|
387
516
|
this.deleteOutbox.add(command.commandId);
|
|
@@ -396,12 +525,20 @@ class ProfileProjectionDraft {
|
|
|
396
525
|
if (!thread || thread.status !== "ready")
|
|
397
526
|
throw Error("PROFILE_THREAD_NOT_READY");
|
|
398
527
|
const grouped = new Map();
|
|
399
|
-
|
|
528
|
+
const ids = operation.allVisible && operation.policy === "visible"
|
|
529
|
+
? this.readingIndex(thread.threadId).latestVisible().map(ref => ref.messageId) : operation.messageIds;
|
|
530
|
+
for (const id of new Set(ids)) {
|
|
400
531
|
const message = this.message(id);
|
|
401
532
|
if (!message || message.threadId !== thread.threadId || message.readPolicy === "discussion" || this.read(message))
|
|
402
533
|
continue;
|
|
403
534
|
if (operation.policy === "visible" && message.readPolicy === "explicit" || operation.policy === "played" && message.readPolicy !== "explicit")
|
|
404
535
|
continue;
|
|
536
|
+
if (!message.sourceSeq) {
|
|
537
|
+
if (!thread.internal?.pendingReadingIntents?.[id]?.some(intent => intent.policy === operation.policy))
|
|
538
|
+
this.restoreReadingIntent({ type: "restoreReadingIntent", threadId: message.sourceThreadId, messageId: id,
|
|
539
|
+
commandId: `${operation.commandId}:${encodeURIComponent(id)}`, policy: operation.policy, createdAt: operation.createdAt });
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
405
542
|
const group = grouped.get(message.sourceThreadId) ?? { readThroughSeq: 0, explicitReadMessageIds: [] };
|
|
406
543
|
if (operation.policy === "played" || !message.sourceSeq)
|
|
407
544
|
group.explicitReadMessageIds.push(id);
|
|
@@ -412,10 +549,19 @@ class ProfileProjectionDraft {
|
|
|
412
549
|
grouped.set(message.sourceThreadId, group);
|
|
413
550
|
}
|
|
414
551
|
for (const [sourceId, values] of grouped) {
|
|
415
|
-
const
|
|
552
|
+
const key = JSON.stringify([sourceId, thread.rootMessageId ?? null]);
|
|
553
|
+
const earlierId = operation.policy === "visible" ? this.readingCommands.get(key) : undefined;
|
|
554
|
+
const earlier = earlierId && !this.deleteOutbox.has(earlierId) ? this.outbox.get(earlierId) : undefined;
|
|
555
|
+
const commandId = earlier?.commandId ?? `${operation.commandId}:${encodeURIComponent(sourceId)}`;
|
|
556
|
+
const previous = earlier?.payload;
|
|
557
|
+
const merged = previous ? { ...values, readThroughSeq: Math.max(previous.readThroughSeq, values.readThroughSeq),
|
|
558
|
+
readThroughMessageId: previous.readThroughSeq >= values.readThroughSeq ? previous.readThroughMessageId : values.readThroughMessageId,
|
|
559
|
+
explicitReadMessageIds: [...new Set([...previous.explicitReadMessageIds, ...values.explicitReadMessageIds])] } : values;
|
|
560
|
+
if (operation.policy === "visible")
|
|
561
|
+
this.readingCommands.set(key, commandId);
|
|
416
562
|
this.outbox.set(commandId, { commandId, kind: operation.policy === "played" ? "played" : "reading", threadId: thread.threadId,
|
|
417
|
-
createdAt: operation.createdAt, payload: { appId: this.base.identity.appId, profileId: this.base.identity.profileId,
|
|
418
|
-
teamId: thread.teamId, threadId: sourceId, ...(thread.rootMessageId ? { threadRootId: thread.rootMessageId } : {}), ...
|
|
563
|
+
createdAt: earlier?.createdAt ?? operation.createdAt, payload: { appId: this.base.identity.appId, profileId: this.base.identity.profileId,
|
|
564
|
+
teamId: thread.teamId, threadId: sourceId, ...(thread.rootMessageId ? { threadRootId: thread.rootMessageId } : {}), ...merged }, state: "pending", attempts: 0 });
|
|
419
565
|
this.reading(eventForLocalReading(this.base.identity, sourceId, thread.rootMessageId, values), false);
|
|
420
566
|
}
|
|
421
567
|
}
|
|
@@ -435,10 +581,12 @@ class ProfileProjectionDraft {
|
|
|
435
581
|
const parent = canonical ? this.ensure(canonical, wire.threadKind, { teamId: source.teamId }) : source;
|
|
436
582
|
const body = wire.body;
|
|
437
583
|
if (body?.kind === "group") {
|
|
438
|
-
const
|
|
439
|
-
const
|
|
584
|
+
const control = canonical ? { ...wire, threadId: parent.threadId, teamId: parent.teamId } : wire;
|
|
585
|
+
const controls = { ...parent.internal?.groupControls, [wire.messageId]: control };
|
|
586
|
+
const group = projectInboxGroup(this.base.identity.appId, wire.to, parent.teamId, Object.values(controls));
|
|
440
587
|
this.putThread({ ...parent, internal: { ...parent.internal, groupControls: controls },
|
|
441
|
-
...(group ? { group, title: group.title, avatar: group.avatar ?? undefined,
|
|
588
|
+
...(group ? { group, title: group.title, avatar: group.avatar ?? undefined,
|
|
589
|
+
status: group.status === "dissolved" || !group.members?.includes(this.base.identity.profileId) ? "unavailable" : "ready" } : {}) });
|
|
442
590
|
return;
|
|
443
591
|
}
|
|
444
592
|
const reaction = body?.kind === "message" ? body.content?.reaction : undefined;
|
|
@@ -483,6 +631,43 @@ class ProfileProjectionDraft {
|
|
|
483
631
|
const saved = entry;
|
|
484
632
|
this.control(parent.threadId, saved.wire, saved.reaction, saved.recall);
|
|
485
633
|
}
|
|
634
|
+
for (const origin of new Set([source.threadId, parent.threadId])) {
|
|
635
|
+
const intents = this.thread(origin)?.internal?.pendingReadingIntents?.[wire.messageId] ?? [];
|
|
636
|
+
for (const intent of intents)
|
|
637
|
+
this.restoreReadingIntent({ type: "restoreReadingIntent", threadId: origin, messageId: wire.messageId, ...intent });
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
restoreReadingIntent(operation) {
|
|
641
|
+
const source = this.ensure(operation.threadId), message = this.message(operation.messageId);
|
|
642
|
+
const thread = message && this.thread(message.threadId);
|
|
643
|
+
const canonical = source.internal?.assignments?.[source.threadId] ?? source.threadId;
|
|
644
|
+
const known = message && message.sourceSeq && (message.sourceThreadId === source.threadId || (thread?.parentThreadId ?? thread?.threadId) === canonical);
|
|
645
|
+
const intents = { ...source.internal?.pendingReadingIntents }, previous = intents[operation.messageId] ?? [];
|
|
646
|
+
if (!known || operation.defer) {
|
|
647
|
+
if (!previous.some(intent => intent.commandId === operation.commandId)) {
|
|
648
|
+
intents[operation.messageId] = [...previous, { commandId: operation.commandId, policy: operation.policy, createdAt: operation.createdAt }];
|
|
649
|
+
this.putThread({ ...source, internal: { ...source.internal, pendingReadingIntents: intents } });
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
else if (previous.some(intent => intent.commandId === operation.commandId)) {
|
|
653
|
+
const remaining = previous.filter(intent => intent.commandId !== operation.commandId);
|
|
654
|
+
if (remaining.length)
|
|
655
|
+
intents[operation.messageId] = remaining;
|
|
656
|
+
else
|
|
657
|
+
delete intents[operation.messageId];
|
|
658
|
+
this.putThread({ ...source, internal: { ...source.internal, pendingReadingIntents: intents } });
|
|
659
|
+
}
|
|
660
|
+
if (!known || message.from === this.base.identity.profileId || message.recalled || message.readPolicy === "discussion" ||
|
|
661
|
+
(operation.policy === "played") !== (message.readPolicy === "explicit"))
|
|
662
|
+
return;
|
|
663
|
+
const values = operation.policy === "played" ? { readThroughSeq: 0, explicitReadMessageIds: [message.messageId] }
|
|
664
|
+
: { readThroughSeq: message.sourceSeq, readThroughMessageId: message.messageId, explicitReadMessageIds: [] };
|
|
665
|
+
const fact = eventForLocalReading(this.base.identity, message.sourceThreadId, thread?.rootMessageId, values).fact;
|
|
666
|
+
if (operation.defer)
|
|
667
|
+
this.reading({ inboxSeq: "0", sourceId: operation.commandId, acceptedAt: operation.createdAt, kind: "readingUpdated", fact }, false);
|
|
668
|
+
else
|
|
669
|
+
this.local({ type: "restoreReadingCommand", command: { commandId: operation.commandId, kind: operation.policy === "played" ? "played" : "reading",
|
|
670
|
+
threadId: message.threadId, createdAt: operation.createdAt, payload: fact, state: "pending", attempts: 0 } });
|
|
486
671
|
}
|
|
487
672
|
control(threadId, wire, reaction, recall) {
|
|
488
673
|
const targetId = recall ?? reaction?.messageId;
|
|
@@ -522,7 +707,10 @@ class ProfileProjectionDraft {
|
|
|
522
707
|
reading(event, confirmed = true) {
|
|
523
708
|
const data = event.fact;
|
|
524
709
|
if (data.receiptOnly === true) {
|
|
525
|
-
const receipt = validateInboxReceipt(data, this.base.identity.profileId);
|
|
710
|
+
const receipt = validateInboxReceipt(data.receipt, this.base.identity.profileId);
|
|
711
|
+
if (data.appId !== this.base.identity.appId || data.profileId !== this.base.identity.profileId || data.threadId !== receipt.threadId ||
|
|
712
|
+
data.readThroughSeq !== 0 || !Array.isArray(data.explicitReadMessageIds) || data.explicitReadMessageIds.length || data.threadRootId !== undefined)
|
|
713
|
+
throw Error("INVALID_INBOX_RECEIPT_ENVELOPE");
|
|
526
714
|
for (const id of receipt.messageIds) {
|
|
527
715
|
const message = this.message(id);
|
|
528
716
|
if (message && message.from !== this.base.identity.profileId)
|
|
@@ -541,8 +729,9 @@ class ProfileProjectionDraft {
|
|
|
541
729
|
}
|
|
542
730
|
if (data.profileId !== this.base.identity.profileId || !data.threadId || !Number.isSafeInteger(data.readThroughSeq) || data.readThroughSeq < 0 || !Array.isArray(data.explicitReadMessageIds))
|
|
543
731
|
throw Error("INVALID_INBOX_READING");
|
|
544
|
-
const
|
|
545
|
-
const
|
|
732
|
+
const canonical = this.thread(data.threadId)?.internal?.assignments?.[data.threadId] ?? data.threadId;
|
|
733
|
+
const id = data.threadRootId ? profileDiscussionId(canonical, data.threadRootId) : canonical;
|
|
734
|
+
const thread = this.ensure(id, data.threadRootId ? "discussion" : "group", data.threadRootId ? { parentThreadId: canonical, rootMessageId: data.threadRootId } : {});
|
|
546
735
|
if (thread.parentThreadId)
|
|
547
736
|
this.ensure(thread.parentThreadId);
|
|
548
737
|
const sourceId = data.threadId, previous = this.source(thread, sourceId);
|
|
@@ -565,14 +754,20 @@ class ProfileProjectionDraft {
|
|
|
565
754
|
index.remove(message);
|
|
566
755
|
}
|
|
567
756
|
const after = index.summary(next.readingSources);
|
|
757
|
+
const unknownDelta = [...new Set(data.explicitReadMessageIds)].reduce((delta, messageId) => {
|
|
758
|
+
const message = this.message(messageId);
|
|
759
|
+
return message?.threadId === id ? delta + this.unknownReading(message, next) - this.unknownReading(message, thread) : delta;
|
|
760
|
+
}, 0);
|
|
568
761
|
this.putThread(next);
|
|
569
|
-
this.updateSummary(id, after.unread - before.unread, after.mentions - before.mentions);
|
|
762
|
+
this.updateSummary(id, after.unread - before.unread, after.mentions - before.mentions, unknownDelta);
|
|
570
763
|
}
|
|
571
764
|
organize(event) {
|
|
572
765
|
const data = event.fact;
|
|
573
766
|
if (!data.threadId || !data.rootMessageId || !Array.isArray(data.messageIds) || !Number.isSafeInteger(data.revision))
|
|
574
767
|
throw Error("INVALID_INBOX_ORGANIZATION");
|
|
575
|
-
const
|
|
768
|
+
const source = this.ensure(data.threadId);
|
|
769
|
+
const parent = source.internal?.assignments?.[source.threadId] ? this.ensure(source.internal.assignments[source.threadId]) : source;
|
|
770
|
+
const old = parent.internal?.organizationRevisions?.[data.rootMessageId] ?? -1;
|
|
576
771
|
if (old >= data.revision)
|
|
577
772
|
return;
|
|
578
773
|
const messageRoots = { ...parent.internal?.messageRoots };
|
|
@@ -580,8 +775,9 @@ class ProfileProjectionDraft {
|
|
|
580
775
|
if (messageId !== data.rootMessageId)
|
|
581
776
|
messageRoots[messageId] = data.rootMessageId;
|
|
582
777
|
this.putThread({ ...parent, internal: { ...parent.internal, messageRoots, organizationRevisions: { ...parent.internal?.organizationRevisions, [data.rootMessageId]: data.revision } } });
|
|
583
|
-
const id = profileDiscussionId(
|
|
584
|
-
this.ensure(id, "discussion", { parentThreadId:
|
|
778
|
+
const id = profileDiscussionId(parent.threadId, data.rootMessageId);
|
|
779
|
+
const discussion = this.ensure(id, "discussion", { parentThreadId: parent.threadId, rootMessageId: data.rootMessageId, teamId: parent.teamId });
|
|
780
|
+
this.putThread({ ...discussion, ownerAgentId: typeof data.ownerAgentId === "string" ? data.ownerAgentId : undefined });
|
|
585
781
|
const root = this.message(data.rootMessageId);
|
|
586
782
|
if (root)
|
|
587
783
|
this.setMessage({ ...root, readPolicy: "discussion", discussionThreadId: id });
|
|
@@ -597,24 +793,113 @@ class ProfileProjectionDraft {
|
|
|
597
793
|
const data = event.fact;
|
|
598
794
|
if (!data.sourceThreadId || !data.threadId || !data.teamId)
|
|
599
795
|
throw Error("INVALID_INBOX_ASSIGNMENT");
|
|
600
|
-
|
|
796
|
+
if (data.sourceThreadId === data.threadId)
|
|
797
|
+
return;
|
|
601
798
|
const source = this.ensure(data.sourceThreadId);
|
|
602
|
-
this.
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
799
|
+
const destination = this.ensure(data.threadId, source.kind === "dm" ? "dm" : "group", { teamId: data.teamId });
|
|
800
|
+
const targetId = destination.internal?.targetId ?? source.internal?.targetId;
|
|
801
|
+
const groupControls = Object.fromEntries(Object.entries({ ...source.internal?.groupControls, ...destination.internal?.groupControls })
|
|
802
|
+
.map(([id, value]) => [id, { ...value, threadId: destination.threadId, teamId: data.teamId }]));
|
|
803
|
+
const group = targetId ? projectInboxGroup(this.base.identity.appId, targetId, data.teamId, Object.values(groupControls)) : undefined;
|
|
804
|
+
const organizationRevisions = { ...source.internal?.organizationRevisions };
|
|
805
|
+
for (const [root, revision] of Object.entries(destination.internal?.organizationRevisions ?? {}))
|
|
806
|
+
organizationRevisions[root] = Math.max(organizationRevisions[root] ?? -1, revision);
|
|
807
|
+
this.putThread({ ...destination, teamId: data.teamId, title: group?.title ?? (destination.title || source.title), avatar: group?.avatar ?? destination.avatar ?? source.avatar,
|
|
808
|
+
group: group ?? destination.group ?? (source.group ? { ...source.group, teamId: data.teamId } : undefined),
|
|
809
|
+
...(group ? { status: group.status === "dissolved" || !group.members?.includes(this.base.identity.profileId) ? "unavailable" : "ready" } : {}),
|
|
810
|
+
internal: { ...source.internal, ...destination.internal, targetId, groupControls, organizationRevisions,
|
|
811
|
+
messageRoots: { ...source.internal?.messageRoots, ...destination.internal?.messageRoots },
|
|
812
|
+
pendingControls: mergeAssignedLists(source.internal?.pendingControls, destination.internal?.pendingControls),
|
|
813
|
+
pendingReceipts: mergeAssignedLists(source.internal?.pendingReceipts, destination.internal?.pendingReceipts),
|
|
814
|
+
pendingReadingIntents: mergeAssignedLists(source.internal?.pendingReadingIntents, destination.internal?.pendingReadingIntents),
|
|
815
|
+
participantCounts: destination.internal?.participantCounts, unknownReadingCount: destination.internal?.unknownReadingCount } });
|
|
816
|
+
this.mergeAssignedReading(destination.threadId, source.readingSources);
|
|
817
|
+
const children = new Set(this.base.children.get(source.threadId));
|
|
818
|
+
for (const row of this.threads.values())
|
|
819
|
+
if (row.parentThreadId === source.threadId)
|
|
820
|
+
children.add(row.threadId);
|
|
821
|
+
for (const childId of children) {
|
|
822
|
+
const child = this.thread(childId);
|
|
823
|
+
if (!child.rootMessageId)
|
|
824
|
+
continue;
|
|
825
|
+
const id = profileDiscussionId(destination.threadId, child.rootMessageId);
|
|
826
|
+
this.ensure(id, "discussion", { parentThreadId: destination.threadId, rootMessageId: child.rootMessageId, teamId: data.teamId,
|
|
827
|
+
title: child.title, ownerAgentId: child.ownerAgentId });
|
|
828
|
+
this.mergeAssignedReading(id, child.readingSources);
|
|
829
|
+
const root = this.message(child.rootMessageId);
|
|
830
|
+
if (root)
|
|
831
|
+
this.setMessage({ ...root, discussionThreadId: id });
|
|
832
|
+
for (const ref of this.directory(childId).blocks.flatMap(window => window.refs))
|
|
833
|
+
this.move(this.message(ref.messageId), id);
|
|
834
|
+
this.putThread({ ...this.thread(childId), status: "unavailable" });
|
|
835
|
+
}
|
|
836
|
+
for (const ref of this.directory(source.threadId).blocks.flatMap(window => window.refs))
|
|
837
|
+
this.move(this.message(ref.messageId), destination.threadId);
|
|
838
|
+
this.putThread({ ...this.thread(source.threadId), status: "unavailable", internal: { ...this.thread(source.threadId)?.internal, assignments: { ...source.internal?.assignments, [source.threadId]: destination.threadId } } });
|
|
839
|
+
}
|
|
840
|
+
mergeAssignedReading(id, incoming) {
|
|
841
|
+
const thread = this.thread(id), sources = new Map(thread.readingSources.map(source => [source.sourceThreadId, source]));
|
|
842
|
+
for (const source of incoming) {
|
|
843
|
+
const previous = sources.get(source.sourceThreadId);
|
|
844
|
+
if (!previous) {
|
|
845
|
+
sources.set(source.sourceThreadId, source);
|
|
846
|
+
continue;
|
|
847
|
+
}
|
|
848
|
+
sources.set(source.sourceThreadId, { ...previous, readThroughSeq: Math.max(previous.readThroughSeq, source.readThroughSeq),
|
|
849
|
+
pendingReadThroughSeq: Math.max(previous.pendingReadThroughSeq ?? 0, source.pendingReadThroughSeq ?? 0),
|
|
850
|
+
explicitReadMessageIds: [...new Set([...previous.explicitReadMessageIds, ...source.explicitReadMessageIds])],
|
|
851
|
+
pendingExplicitReadMessageIds: [...new Set([...(previous.pendingExplicitReadMessageIds ?? []), ...(source.pendingExplicitReadMessageIds ?? [])])],
|
|
852
|
+
preservedReadMessageIds: [...new Set([...previous.preservedReadMessageIds, ...source.preservedReadMessageIds])] });
|
|
853
|
+
}
|
|
854
|
+
const index = this.readingIndex(id), before = index.summary(thread.readingSources), next = { ...thread, readingSources: [...sources.values()] };
|
|
855
|
+
this.putThread(next);
|
|
856
|
+
for (const window of this.directory(id).blocks)
|
|
857
|
+
for (const ref of window.refs) {
|
|
858
|
+
const message = this.message(ref.messageId);
|
|
859
|
+
index.upsert(message, this.base.identity.profileId, sources.get(message.sourceThreadId));
|
|
860
|
+
}
|
|
861
|
+
const after = index.summary(next.readingSources);
|
|
862
|
+
this.updateSummary(id, after.unread - before.unread, after.mentions - before.mentions);
|
|
608
863
|
}
|
|
609
|
-
relationship(event) {
|
|
864
|
+
relationship(event, basis = false) {
|
|
610
865
|
const data = event.fact;
|
|
611
866
|
if (!data.pairId || ![data.actorId, data.targetId].includes(this.base.identity.profileId))
|
|
612
867
|
throw Error("INVALID_INBOX_RELATIONSHIP");
|
|
613
|
-
if (data.threadId)
|
|
614
|
-
|
|
868
|
+
if (!data.threadId) {
|
|
869
|
+
if (data.action === "friendRequest" && data.messageId && !basis && event.acceptedAt >= this.base.windowStart)
|
|
870
|
+
throw Error("INVALID_INBOX_INVITATION");
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
const peer = data.actorId === this.base.identity.profileId ? data.targetId : data.actorId;
|
|
874
|
+
this.ensureDirectThread(data.threadId, peer, data.teamId);
|
|
875
|
+
if (data.action !== "friendRequest" || typeof data.messageId !== "string" || basis || event.acceptedAt < this.base.windowStart)
|
|
876
|
+
return;
|
|
877
|
+
this.invitation(data, event.acceptedAt);
|
|
878
|
+
}
|
|
879
|
+
ensureDirectThread(id, peer, teamId) {
|
|
880
|
+
const thread = this.ensure(id, "dm", { teamId, internal: { targetId: peer } });
|
|
881
|
+
if (thread.kind !== "dm" || thread.teamId !== teamId || thread.internal?.targetId !== peer)
|
|
882
|
+
this.putThread({ ...thread, kind: "dm", teamId, internal: { ...thread.internal, targetId: peer } });
|
|
883
|
+
}
|
|
884
|
+
invitation(data, acceptedAt) {
|
|
885
|
+
if (acceptedAt < this.base.windowStart)
|
|
886
|
+
return;
|
|
887
|
+
if (!data.threadId || !data.messageId || !data.actorId || !data.targetId || !Number.isSafeInteger(data.createdAt))
|
|
888
|
+
throw Error("INVALID_INBOX_INVITATION");
|
|
889
|
+
this.ensureDirectThread(data.threadId, data.actorId === this.base.identity.profileId ? data.targetId : data.actorId, data.teamId);
|
|
890
|
+
if (this.message(data.messageId))
|
|
891
|
+
return;
|
|
892
|
+
const body = { kind: "contact", threadKind: "dm", action: "friendRequest", payload: { appId: this.base.identity.appId, ...(data.teamId ? { teamId: data.teamId } : {}) } };
|
|
893
|
+
const message = { generation: this.base.current.activeGeneration, messageId: data.messageId, threadId: data.threadId, sourceThreadId: data.threadId,
|
|
894
|
+
orderKey: [data.createdAt, data.threadId, 0, data.messageId], from: data.actorId, recipients: [data.targetId], content: body, attachmentRefs: [], reactions: [], readBy: [], receiptStatus: "unknown",
|
|
895
|
+
explicitReadingFacts: [], controlVersions: {}, readPolicy: "visible", revision: this.revision,
|
|
896
|
+
presentation: projectMessageContent({ id: data.messageId, threadId: data.threadId, threadKind: "dm", teamId: data.teamId, timestamp: data.createdAt, from: data.actorId, recipients: [data.targetId], body }) };
|
|
897
|
+
this.setMessage(message);
|
|
898
|
+
this.insert(message, true);
|
|
615
899
|
}
|
|
616
900
|
finish() {
|
|
617
|
-
return { current: { ...this.base.current, committedSeq: this.seq.toString(), commitRevision: this.revision, receiptsReady: this.receiptsReady
|
|
901
|
+
return { current: { ...this.base.current, committedSeq: this.seq.toString(), commitRevision: this.revision, receiptsReady: this.receiptsReady,
|
|
902
|
+
localMigrations: this.localMigrations ?? this.base.current.localMigrations, revokedTeamIds: this.revokedTeamIds ?? this.base.current.revokedTeamIds, teamAccessRevision: this.teamAccessRevision ?? this.base.current.teamAccessRevision },
|
|
618
903
|
threads: [...this.threads.values()], windows: [...this.windows.values()], messages: [...this.messages.values()], readingIndices: this.readingIndices,
|
|
619
904
|
outbox: [...this.outbox.values()].filter(command => !this.deleteOutbox.has(command.commandId)), deleteOutbox: [...this.deleteOutbox] };
|
|
620
905
|
}
|
|
@@ -13,5 +13,6 @@ export declare class ProfileReadingIndex {
|
|
|
13
13
|
upsert(message: ProfileMessage, profileId: string, source?: ProfileReadingSource): void;
|
|
14
14
|
summary(positions: readonly ProfileReadingSource[]): Summary;
|
|
15
15
|
visibleBetween(sourceId: string, low: number, high: number): ProfileMessageRef[];
|
|
16
|
+
latestVisible(): ProfileMessageRef[];
|
|
16
17
|
}
|
|
17
18
|
export {};
|
|
@@ -64,6 +64,13 @@ function between(root, low, high, result) {
|
|
|
64
64
|
if (root.seq <= high)
|
|
65
65
|
between(root.right, low, high, result);
|
|
66
66
|
}
|
|
67
|
+
function lastVisible(root) {
|
|
68
|
+
if (!root?.visible.unread)
|
|
69
|
+
return;
|
|
70
|
+
if (root.right?.visible.unread)
|
|
71
|
+
return lastVisible(root.right);
|
|
72
|
+
return root.explicit ? lastVisible(root.left) : root.ref;
|
|
73
|
+
}
|
|
67
74
|
/** Persistent per-source range trees. Cursor advances aggregate whole ranges;
|
|
68
75
|
* they neither scan history nor rewrite a read flag on each Message. */
|
|
69
76
|
export class ProfileReadingIndex {
|
|
@@ -75,7 +82,7 @@ export class ProfileReadingIndex {
|
|
|
75
82
|
}
|
|
76
83
|
upsert(message, profileId, source) {
|
|
77
84
|
this.remove(message);
|
|
78
|
-
if (message.from === profileId || message.recalled || message.readPolicy === "discussion" ||
|
|
85
|
+
if (!message.sourceSeq || message.from === profileId || message.recalled || message.readPolicy === "discussion" ||
|
|
79
86
|
message.explicitReadingFacts.some(fact => fact.profileId === profileId) ||
|
|
80
87
|
source?.explicitReadMessageIds.includes(message.messageId) || source?.pendingExplicitReadMessageIds?.includes(message.messageId) || source?.preservedReadMessageIds.includes(message.messageId))
|
|
81
88
|
return;
|
|
@@ -96,4 +103,13 @@ export class ProfileReadingIndex {
|
|
|
96
103
|
between(this.sources.get(sourceId), low, high, result);
|
|
97
104
|
return result;
|
|
98
105
|
}
|
|
106
|
+
latestVisible() {
|
|
107
|
+
const result = [];
|
|
108
|
+
for (const root of this.sources.values()) {
|
|
109
|
+
const ref = lastVisible(root);
|
|
110
|
+
if (ref)
|
|
111
|
+
result.push(ref);
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
99
115
|
}
|