@pmate/chat-sdk 0.12.0-next.91 → 0.12.0-next.93

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.
@@ -92,6 +92,8 @@ export class ChatSession {
92
92
  this.readers = new MessageReadersClient({ appId: scope.appId, profileId: scope.profileId, token: options.reading.sessionToken, scope, access: () => this.messages.scopeAccess(), onError: options.onError, hubHttpUrl: options.reading.hubHttpUrl, fetch: options.reading.fetch });
93
93
  if (options.loadGroup) {
94
94
  this.groups = new GroupClient({ local: !!options.inbox, scope, access: () => this.messages.scopeAccess(), load: options.loadGroup, onError: options.onError, onDenied: async (error, _threadId, signal) => {
95
+ if (isScopedGroupDenial(error))
96
+ return true;
95
97
  if (error?.status === 403 && options.messages.transport.authorizeScope) {
96
98
  try {
97
99
  await options.messages.transport.authorizeScope(signal ?? new AbortController().signal);
@@ -1,3 +1,4 @@
1
+ import { scopedGroupDenial } from "./groupDenial.js";
1
2
  import { IndexedDbDiscussionStore } from "./messageDiscussionStore.js";
2
3
  import { mergeStoredMessage } from "./messageStore.js";
3
4
  import { messageThreadRoot } from "./messageThread.js";
@@ -53,8 +54,11 @@ export function createChatSession(options) {
53
54
  loadGroup: async (groupId, signal) => {
54
55
  if (options.inbox) {
55
56
  const group = await options.inbox.store.group(groupId, teamId);
56
- if (group)
57
+ if (group) {
58
+ if (group.status !== "dissolved" && !group.members?.includes(profileId))
59
+ throw scopedGroupDenial(Object.assign(new Error("GROUP_ACCESS_DENIED"), { status: 403 }));
57
60
  return group;
61
+ }
58
62
  if (options.inbox.offlineMode() || isOffline())
59
63
  throw new Error("GROUP_METADATA_OFFLINE");
60
64
  }
@@ -52,7 +52,7 @@ type InboxLease = {
52
52
  expiresAt: number;
53
53
  };
54
54
  export type InboxMessage = ChatReaderSocialMessage & {
55
- seq: number;
55
+ seq?: number;
56
56
  recalled?: boolean;
57
57
  };
58
58
  type MessageRow = {
@@ -144,7 +144,7 @@ export declare class IndexedDbInbox {
144
144
  recipients?: string[];
145
145
  clientCreatedAt?: number;
146
146
  serverCreatedAt: number;
147
- seq: number;
147
+ seq?: number;
148
148
  }[]>;
149
149
  conversationMessages(thread: string, teamId: string): Promise<{
150
150
  recalled: boolean;
@@ -160,7 +160,7 @@ export declare class IndexedDbInbox {
160
160
  recipients?: string[];
161
161
  clientCreatedAt?: number;
162
162
  serverCreatedAt: number;
163
- seq: number;
163
+ seq?: number;
164
164
  }[]>;
165
165
  conversationMessage(thread: string, teamId: string, id: string): Promise<{
166
166
  sourceThreadId?: string;
@@ -176,7 +176,7 @@ export declare class IndexedDbInbox {
176
176
  recipients?: string[];
177
177
  clientCreatedAt?: number;
178
178
  serverCreatedAt: number;
179
- seq: number;
179
+ seq?: number;
180
180
  } | undefined>;
181
181
  conversationChanges(thread: string, teamId: string, afterSeq: string, afterId?: string): Promise<{
182
182
  changeSeq: string;
@@ -187,6 +187,7 @@ export declare class IndexedDbInbox {
187
187
  items: MessageThreadSummary[];
188
188
  revision: number;
189
189
  }>;
190
+ conversationGroup(thread: string, teamId: string): Promise<import("./socialReadClient.js").SocialGroup | undefined>;
190
191
  group(groupId: string, teamId: string): Promise<import("./socialReadClient.js").SocialGroup | undefined>;
191
192
  private annotatedBody;
192
193
  close(): void;
@@ -124,7 +124,7 @@ export class IndexedDbInbox {
124
124
  switch (event.kind) {
125
125
  case "messageAdded": {
126
126
  const message = data, thread = message.threadId;
127
- if (!thread || !message.messageId || !message.from || !Number.isSafeInteger(message.seq) || message.seq < 1 || !["dm", "group"].includes(message.threadKind))
127
+ if (!thread || !message.messageId || !message.from || typeof message.seq !== "number" || !Number.isSafeInteger(message.seq) || message.seq < 1 || !["dm", "group"].includes(message.threadKind))
128
128
  throw new Error("INVALID_INBOX_MESSAGE");
129
129
  if (message.from !== this.scope.profileId && !message.recipients?.includes(this.scope.profileId) && !(message.threadKind === "dm" && message.to === this.scope.profileId))
130
130
  throw new Error("INBOX_MESSAGE_INACCESSIBLE");
@@ -133,7 +133,6 @@ export class IndexedDbInbox {
133
133
  if (body?.kind === "group") {
134
134
  await this.facts.put({ owner, kind: "groupControl", id: message.messageId, thread, value: data });
135
135
  await this.facts.put({ owner, kind: "structure", id: thread, thread, value: { seq: event.inboxSeq } });
136
- return thread;
137
136
  }
138
137
  if (basis || event.acceptedAt < windowStart)
139
138
  return thread;
@@ -190,6 +189,18 @@ export class IndexedDbInbox {
190
189
  if (!data.pairId || ![data.actorId, data.targetId].includes(this.scope.profileId))
191
190
  throw new Error("INVALID_INBOX_RELATIONSHIP");
192
191
  await this.facts.put({ owner, kind: event.kind, id: data.pairId, thread: data.threadId, value: data });
192
+ // Older relationship logs contain the invitation's identity, without a
193
+ // sequence append. Reconstruct that display item without inventing a read
194
+ // ordinal or replacing a later authoritative message with the same ID.
195
+ if (data.action === "friendRequest" && typeof data.messageId === "string" && !basis && event.acceptedAt >= windowStart) {
196
+ if (!data.threadId || !Number.isSafeInteger(data.createdAt))
197
+ throw new Error("INVALID_INBOX_INVITATION");
198
+ await this.threads.put({ owner, id: data.threadId, kind: "dm", teamId: data.teamId ?? undefined, targetId: data.targetId });
199
+ if (!await this.messages.get([owner, data.threadId, data.messageId])) {
200
+ const value = { appId: this.scope.appId, teamId: data.teamId ?? undefined, threadId: data.threadId, threadKind: "dm", messageId: data.messageId, from: data.actorId, to: data.targetId, recipients: [data.targetId], serverCreatedAt: data.createdAt, body: { kind: "contact", threadKind: "dm", action: "friendRequest", payload: { appId: this.scope.appId, ...(data.teamId ? { teamId: data.teamId } : {}) } } };
201
+ await this.messages.put({ owner, thread: data.threadId, id: data.messageId, acceptedAt: event.acceptedAt, time: data.createdAt, seq: 0, changeSeq: event.inboxSeq.padStart(19, "0"), root: "", recalled: false, value });
202
+ }
203
+ }
193
204
  return data.threadId;
194
205
  }
195
206
  default: throw new Error(`UNSUPPORTED_INBOX_EVENT:${event.kind}`);
@@ -230,6 +241,8 @@ export class IndexedDbInbox {
230
241
  if (!thread)
231
242
  continue;
232
243
  const message = row.value, body = message.body;
244
+ if (body?.kind === "group" || row.seq < 1)
245
+ continue;
233
246
  const mentions = body?.content?.mentions ?? body?.content?.content?.mentions ?? [];
234
247
  const visible = body?.kind === undefined || ["message", "quote"].includes(body.kind) || body.kind === "contact" && ["friendRequest", "requestFriend"].includes(body.action);
235
248
  thread.messages.push({ id: row.id, seq: row.seq, from: message.from, recipients: message.recipients ?? [], threadRootId: row.root || undefined, mentions: [...new Set(Array.isArray(mentions) ? mentions.filter(item => item && typeof item.id === "string" && (this.scope.appId !== "pmate:merry" || !["all", "所有人"].includes(item.name))).map(item => item.id) : [])].sort(), readPolicy: messageRequiresPlayback(message.body) ? "explicit" : "visible", recalled: row.recalled || !visible || body?.kind === "recall" || Boolean(body?.content?.recall), payload: null, readingControl: isReadingControl(message.body, this.scope.appId) });
@@ -311,6 +324,12 @@ export class IndexedDbInbox {
311
324
  return { items, revision: Number(cursor?.seq ?? "0") };
312
325
  });
313
326
  }
327
+ async conversationGroup(thread, teamId) {
328
+ const assignments = (await this.facts.where("[owner+kind]").equals([this.owner, "threadAssigned"]).toArray()).filter(row => row.value.threadId === thread);
329
+ const records = await this.threads.bulkGet([thread, ...assignments.map(row => row.value.sourceThreadId)].map(source => [this.owner, source]));
330
+ const group = records.find(row => row?.kind === "group");
331
+ return group ? this.group(group.targetId, teamId) : undefined;
332
+ }
314
333
  async group(groupId, teamId) {
315
334
  const assignments = await this.facts.where("[owner+kind]").equals([this.owner, "threadAssigned"]).toArray();
316
335
  const controls = (await this.facts.where("[owner+kind]").equals([this.owner, "groupControl"]).toArray()).map(row => {
@@ -1,3 +1,4 @@
1
+ import { scopedGroupDenial } from "./groupDenial.js";
1
2
  import { inboxSequence } from "./inboxStore.js";
2
3
  import { messageThreadRoot } from "./messageThread.js";
3
4
  const encode = (cursor) => `ib1:${encodeURIComponent(JSON.stringify(cursor))}`;
@@ -19,7 +20,17 @@ const cached = (message) => ({ ...message, id: message.messageId, timestamp: mes
19
20
  export function inboxMessageTransport(inbox, team, root) {
20
21
  const db = inbox.store;
21
22
  const read = (run) => db.db.transaction("r", [db.cursors, db.messages, db.facts, db.threads], run);
23
+ const authorized = async (thread) => {
24
+ const group = await db.conversationGroup(thread, team);
25
+ if (group?.status === "dissolved")
26
+ return false;
27
+ if (group && !group.members?.includes(db.scope.profileId))
28
+ throw scopedGroupDenial(Object.assign(new Error("GROUP_ACCESS_DENIED"), { status: 403, threadId: thread }));
29
+ return true;
30
+ };
22
31
  const rows = async (thread) => {
32
+ if (!await authorized(thread))
33
+ return [];
23
34
  const messages = await db.conversationMessages(thread, team);
24
35
  const byId = new Map(messages.map(message => [message.messageId, message]));
25
36
  return messages.filter(message => {
@@ -47,7 +58,7 @@ export function inboxMessageTransport(inbox, team, root) {
47
58
  changes: (thread, after, signal) => read(async () => {
48
59
  signal.throwIfAborted();
49
60
  const current = await metadata(thread), cursor = decode(after);
50
- if (!valid(cursor, current) || await db.structureSequence(thread) > inboxSequence(cursor.seq))
61
+ if (!await authorized(thread) || !valid(cursor, current) || await db.structureSequence(thread) > inboxSequence(cursor.seq))
51
62
  return { messages: [], checkpoint: encode(current), resetRequired: true };
52
63
  const changed = await db.conversationChanges(thread, team, cursor.seq, cursor.id ?? "\uffff");
53
64
  const selected = changed.slice(0, 100), hasMore = changed.length > selected.length;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/chat-sdk",
3
- "version": "0.12.0-next.91",
3
+ "version": "0.12.0-next.93",
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",