@pmate/chat-sdk 0.13.0-profile.4 → 0.13.0-profile.5

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.
@@ -135,6 +135,8 @@ export declare class ProfileChat implements Chat {
135
135
  synchronizeReading(): Promise<void>;
136
136
  revokeThread(id: string): Promise<void>;
137
137
  revokeAccess(): Promise<void>;
138
+ /** Call only after a fresh authoritative Team membership check succeeds. */
139
+ grantAccess(expectedAccessRevision: number): Promise<void>;
138
140
  firstUnread(id: string): string | undefined;
139
141
  readPage(id: string, anchor: string | undefined, limit: number, direction?: "older" | "newer"): readonly Readonly<{
140
142
  id: string;
@@ -68,7 +68,7 @@ export class ProfileChat {
68
68
  const parent = this.parent(id), thread = this.thread(id);
69
69
  if (parent && parent.teamId !== this.teamId || thread && thread.teamId !== this.teamId)
70
70
  return new ChatError("invalid-input", "Conversation belongs to another Team");
71
- if (parent?.status === "unavailable" || thread?.status === "unavailable") {
71
+ if (!this.host.memory.isTeamAvailable(this.teamId) || parent?.status === "unavailable" || thread?.status === "unavailable") {
72
72
  const error = new ChatError("access-denied", "Conversation unavailable", { threadId: id, status: 403 });
73
73
  return parent?.kind === "group" ? scopedGroupDenial(error) : error;
74
74
  }
@@ -353,9 +353,9 @@ export class ProfileChat {
353
353
  async dissolveGroup(id, options = {}) { const previous = await this.getGroup(id); await this.sendControl(id, { groupDissolve: { expectedRevision: options.expectedRevision ?? previous.revision } }); return this.waitGroup(id, previous.revision); }
354
354
  synchronizeReading() { return this.host.synchronize(); }
355
355
  revokeThread(id) { return this.host.submit({ type: "revoke", threadId: this.key(id) }, true); }
356
- async revokeAccess() { for (const thread of this.host.memory.getThreadList())
357
- if (thread.teamId === this.teamId)
358
- await this.revokeThread(thread.threadId); }
356
+ revokeAccess() { return this.host.submit({ type: "teamAccess", teamId: this.teamId, allowed: false }, true); }
357
+ /** Call only after a fresh authoritative Team membership check succeeds. */
358
+ grantAccess(expectedAccessRevision) { return this.host.submit({ type: "teamAccess", teamId: this.teamId, allowed: true, expectedAccessRevision }, true); }
359
359
  firstUnread(id) { this.assertAccessible(id); return this.thread(id)?.firstUnreadMessageId; }
360
360
  readPage(id, anchor, limit, direction = "older") {
361
361
  this.assertAccessible(id);
@@ -81,7 +81,7 @@ export class ProfileDatabase extends Dexie {
81
81
  throw Error("PROFILE_CURSOR_REGRESSION");
82
82
  const changed = [batch.threads, batch.windows, batch.messages, batch.outbox, batch.deleteMessages, batch.deleteWindows, batch.deleteOutbox].some(rows => rows?.length);
83
83
  const localMigrations = batch.localMigrations ? [...new Set([...(current.localMigrations ?? []), ...batch.localMigrations])] : current.localMigrations;
84
- if (!changed && batch.committedSeq === current.committedSeq && (batch.receiptsReady === undefined || batch.receiptsReady === current.receiptsReady) && JSON.stringify(localMigrations) === JSON.stringify(current.localMigrations))
84
+ if (!changed && batch.committedSeq === current.committedSeq && (batch.receiptsReady === undefined || batch.receiptsReady === current.receiptsReady) && JSON.stringify(localMigrations) === JSON.stringify(current.localMigrations) && JSON.stringify(batch.revokedTeamIds ?? current.revokedTeamIds) === JSON.stringify(current.revokedTeamIds) && (batch.teamAccessRevision ?? current.teamAccessRevision) === current.teamAccessRevision)
85
85
  return current;
86
86
  if (batch.messages?.length)
87
87
  await this.messages.bulkPut(batch.messages);
@@ -99,7 +99,7 @@ export class ProfileDatabase extends Dexie {
99
99
  await this.outbox.bulkDelete(batch.deleteOutbox);
100
100
  if (!Number.isSafeInteger(current.commitRevision + 1))
101
101
  throw Error("PROFILE_REVISION_EXHAUSTED");
102
- const next = { ...current, ...(batch.receiptsReady === undefined ? {} : { receiptsReady: batch.receiptsReady }), localMigrations, committedSeq: batch.committedSeq, commitRevision: current.commitRevision + 1 };
102
+ const next = { ...current, ...(batch.receiptsReady === undefined ? {} : { receiptsReady: batch.receiptsReady }), localMigrations, revokedTeamIds: batch.revokedTeamIds ?? current.revokedTeamIds, teamAccessRevision: batch.teamAccessRevision ?? current.teamAccessRevision, committedSeq: batch.committedSeq, commitRevision: current.commitRevision + 1 };
103
103
  await this.inboxState.put(next);
104
104
  return next;
105
105
  });
@@ -56,6 +56,8 @@ export declare class ProfileMemoryStore {
56
56
  };
57
57
  getThread(threadId: string): ProfileThread | undefined;
58
58
  getMessage(messageId: string): ProfileMessage | undefined;
59
+ getAccessRevision(): number;
60
+ isTeamAvailable(teamId: string | undefined): boolean;
59
61
  isThreadAvailable(threadId: string): boolean;
60
62
  getPresentedMessage(messageId: string): Readonly<{
61
63
  id: string;
@@ -47,9 +47,11 @@ export class ProfileMemoryStore {
47
47
  getStatus() { return { phase: this.phase, generation: this.current?.activeGeneration, revision: this.current?.commitRevision }; }
48
48
  getThread(threadId) { return this.threads.get(threadId); }
49
49
  getMessage(messageId) { return this.entities.get(messageId); }
50
+ getAccessRevision() { return this.current?.teamAccessRevision ?? 0; }
51
+ isTeamAvailable(teamId) { return !teamId || !this.current?.revokedTeamIds?.includes(teamId); }
50
52
  isThreadAvailable(threadId) {
51
53
  const thread = this.threads.get(threadId);
52
- return thread?.status === "ready" && (!thread.parentThreadId || this.threads.get(thread.parentThreadId)?.status === "ready");
54
+ return thread?.status === "ready" && this.isTeamAvailable(thread.teamId) && (!thread.parentThreadId || this.threads.get(thread.parentThreadId)?.status === "ready" && this.isTeamAvailable(this.threads.get(thread.parentThreadId)?.teamId));
53
55
  }
54
56
  getPresentedMessage(messageId) {
55
57
  const message = this.entities.get(messageId);
@@ -236,6 +238,11 @@ export class ProfileMemoryStore {
236
238
  directory.accept({ windows: [window], windowCount: 0 });
237
239
  affected.add(window.threadId);
238
240
  }
241
+ const accessChanged = new Set([...(this.current.revokedTeamIds ?? []), ...(delta.current.revokedTeamIds ?? [])].filter(id => this.current.revokedTeamIds?.includes(id) !== delta.current.revokedTeamIds?.includes(id)));
242
+ if (accessChanged.size)
243
+ for (const thread of this.threads.values())
244
+ if (thread.teamId && accessChanged.has(thread.teamId))
245
+ affected.add(thread.threadId);
239
246
  this.current = delta.current;
240
247
  for (const message of delta.messages) {
241
248
  this.indexMessage(message);
@@ -273,7 +280,7 @@ export class ProfileMemoryStore {
273
280
  }
274
281
  }
275
282
  }
276
- this.publish(affected, delta.threads.length > 0);
283
+ this.publish(affected, delta.threads.length > 0 || accessChanged.size > 0);
277
284
  if (added.length)
278
285
  for (const listener of this.messageListeners)
279
286
  this.notify(() => listener(added));
@@ -343,7 +350,7 @@ export class ProfileMemoryStore {
343
350
  if (affected.has(JSON.parse(key)[0]))
344
351
  this.pageCache.delete(key);
345
352
  if (directoryChanged) {
346
- this.list = [...this.threads.values()].filter(thread => thread.kind !== "discussion")
353
+ this.list = [...this.threads.values()].filter(thread => thread.kind !== "discussion" && this.isTeamAvailable(thread.teamId))
347
354
  .sort((a, b) => b.lastActivityAt - a.lastActivityAt || a.threadId.localeCompare(b.threadId));
348
355
  }
349
356
  for (const thread of affected)
@@ -218,7 +218,7 @@ export class ProfileOwner {
218
218
  const prepared = this.projection.prepare(events);
219
219
  const committed = await this.options.database.commit({ ...prepared, ownerId: this.lease.ownerId, fence: this.lease.fence,
220
220
  generation: this.current.activeGeneration, expectedRevision: fromRevision, epoch: this.current.epoch,
221
- committedSeq: prepared.current.committedSeq, receiptsReady: prepared.current.receiptsReady, localMigrations: prepared.current.localMigrations });
221
+ committedSeq: prepared.current.committedSeq, receiptsReady: prepared.current.receiptsReady, localMigrations: prepared.current.localMigrations, revokedTeamIds: prepared.current.revokedTeamIds, teamAccessRevision: prepared.current.teamAccessRevision });
222
222
  if (committed.commitRevision === fromRevision)
223
223
  return prepared.rejected;
224
224
  prepared.current = committed;
@@ -33,6 +33,15 @@ export type ProfileLocalOperation = {
33
33
  } | {
34
34
  type: "revoke";
35
35
  threadId: string;
36
+ } | {
37
+ type: "teamAccess";
38
+ teamId: string;
39
+ allowed: false;
40
+ } | {
41
+ type: "teamAccess";
42
+ teamId: string;
43
+ allowed: true;
44
+ expectedAccessRevision: number;
36
45
  } | {
37
46
  type: "send";
38
47
  threadId: string;
@@ -125,6 +125,8 @@ class ProfileProjectionDraft {
125
125
  revision;
126
126
  receiptsReady;
127
127
  localMigrations;
128
+ revokedTeamIds;
129
+ teamAccessRevision;
128
130
  readingCommands = new Map();
129
131
  constructor(base) {
130
132
  this.base = base;
@@ -133,11 +135,15 @@ class ProfileProjectionDraft {
133
135
  this.receiptsReady = base.current.receiptsReady;
134
136
  }
135
137
  localAccessError(operation) {
138
+ if (operation.type === "teamAccess" && operation.allowed && operation.expectedAccessRevision !== (this.teamAccessRevision ?? this.base.current.teamAccessRevision ?? 0))
139
+ return Error("PROFILE_ACCESS_CHECK_STALE");
136
140
  if (operation.type !== "send" && operation.type !== "observe")
137
141
  return;
138
142
  const thread = this.thread(operation.threadId);
139
143
  const parentId = thread?.parentThreadId ?? (operation.type === "send" ? operation.parentThreadId : undefined);
140
144
  const parent = parentId ? this.thread(parentId) : undefined;
145
+ if ((this.revokedTeamIds ?? this.base.current.revokedTeamIds ?? []).includes((parent ?? thread)?.teamId ?? ""))
146
+ return Error("PROFILE_TEAM_ACCESS_REVOKED");
141
147
  if (thread?.status !== "ready" && !(operation.type === "send" && !thread && parent?.status === "ready") || parentId && parent?.status !== "ready")
142
148
  return Error("PROFILE_THREAD_NOT_READY");
143
149
  if (operation.type === "send") {
@@ -323,6 +329,20 @@ class ProfileProjectionDraft {
323
329
  this.seq = seq;
324
330
  }
325
331
  local(operation) {
332
+ if (operation.type === "teamAccess") {
333
+ if (!operation.teamId)
334
+ throw Error("INVALID_PROFILE_TEAM");
335
+ const revoked = new Set(this.revokedTeamIds ?? this.base.current.revokedTeamIds ?? []);
336
+ if (operation.allowed && !revoked.has(operation.teamId))
337
+ return;
338
+ if (operation.allowed)
339
+ revoked.delete(operation.teamId);
340
+ else
341
+ revoked.add(operation.teamId);
342
+ this.revokedTeamIds = [...revoked].sort();
343
+ this.teamAccessRevision = (this.teamAccessRevision ?? this.base.current.teamAccessRevision ?? 0) + 1;
344
+ return;
345
+ }
326
346
  if (operation.type === "restoreReadingCommand") {
327
347
  const command = operation.command, data = command.payload;
328
348
  if (!["reading", "played"].includes(command.kind) || !command.commandId || data.appId !== this.base.identity.appId || data.profileId !== this.base.identity.profileId)
@@ -730,7 +750,7 @@ class ProfileProjectionDraft {
730
750
  }
731
751
  finish() {
732
752
  return { current: { ...this.base.current, committedSeq: this.seq.toString(), commitRevision: this.revision, receiptsReady: this.receiptsReady,
733
- localMigrations: this.localMigrations ?? this.base.current.localMigrations },
753
+ localMigrations: this.localMigrations ?? this.base.current.localMigrations, revokedTeamIds: this.revokedTeamIds ?? this.base.current.revokedTeamIds, teamAccessRevision: this.teamAccessRevision ?? this.base.current.teamAccessRevision },
734
754
  threads: [...this.threads.values()], windows: [...this.windows.values()], messages: [...this.messages.values()], readingIndices: this.readingIndices,
735
755
  outbox: [...this.outbox.values()].filter(command => !this.deleteOutbox.has(command.commandId)), deleteOutbox: [...this.deleteOutbox] };
736
756
  }
@@ -119,6 +119,8 @@ export type ProfileCurrentState = {
119
119
  commitRevision: number;
120
120
  receiptsReady?: boolean;
121
121
  localMigrations?: string[];
122
+ revokedTeamIds?: string[];
123
+ teamAccessRevision?: number;
122
124
  };
123
125
  export type ProfileWriterLease = {
124
126
  key: "lease";
@@ -160,6 +162,8 @@ export type ProfileWriteBatch = {
160
162
  committedSeq: string;
161
163
  receiptsReady?: boolean;
162
164
  localMigrations?: string[];
165
+ revokedTeamIds?: string[];
166
+ teamAccessRevision?: number;
163
167
  threads?: ProfileThread[];
164
168
  windows?: ProfileWindow[];
165
169
  messages?: ProfileMessage[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/chat-sdk",
3
- "version": "0.13.0-profile.4",
3
+ "version": "0.13.0-profile.5",
4
4
  "description": "Local-first chat SDK, message subscriptions, reading state, and shared chat UI primitives",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",