@fabriktor/fx 0.0.55 → 0.0.57

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.
@@ -1,5 +1,5 @@
1
1
  import type { ChatOperator, UsersOperator, WSCloseHandler, WSConnection, WSErrorHandler, WSMessageHandler } from "@fabriktor/client";
2
- import type { Chat, ChatRoom, ChatRoomMember, User } from "@fabriktor/schema";
2
+ import { type Chat, type ChatRoom, type ChatRoomMember, type User } from "@fabriktor/schema";
3
3
  import { type SearchPage } from "../core/search.js";
4
4
  type ChatInsertOptions = Parameters<ChatOperator["insertOneChat"]>[0];
5
5
  type ChatReplaceOptions = Parameters<ChatOperator["replaceOneChat"]>[0];
@@ -8,6 +8,9 @@ export type EnsureChatRoomOptions = Parameters<ChatOperator["ensureChatRoom"]>[0
8
8
  export type MarkChatObjectsUploadedOptions = {
9
9
  id: ChatReplaceOptions["id"];
10
10
  };
11
+ export type ClearChatObjectsOptions = {
12
+ id: ChatReplaceOptions["id"];
13
+ };
11
14
  export type ReactChatOptions = Parameters<ChatOperator["reactChat"]>[0];
12
15
  export type ChatFXOptions = {
13
16
  chat: ChatOperator;
@@ -59,6 +62,9 @@ export type SearchChatRoomMembersPage = SearchPage<ChatRoomMember>;
59
62
  export type ChatRoomMembersOptions = {
60
63
  chat_room_id: string;
61
64
  };
65
+ export type ChatRoomsOptions = {
66
+ ids: string[];
67
+ };
62
68
  export type MyChatRoomMembersOptions = {
63
69
  owner_id: string;
64
70
  };
@@ -100,6 +106,7 @@ export type ChatRoomSession = {
100
106
  export type GlobalChatRoomsSession = {
101
107
  chat_rooms_ws: WSConnection;
102
108
  members: ChatRoomMember[];
109
+ rooms: ChatRoom[];
103
110
  closeNormal(reason?: string): void;
104
111
  closeLeavingPage(reason?: string): void;
105
112
  };
@@ -107,12 +114,14 @@ export interface ChatFXOperator {
107
114
  createChat(opts: CreateChatOptions): Promise<Chat>;
108
115
  ensureChatRoom(opts: EnsureChatRoomOptions): Promise<string>;
109
116
  markChatObjectsUploaded(opts: MarkChatObjectsUploadedOptions): Promise<Chat>;
117
+ clearChatObjects(opts: ClearChatObjectsOptions): Promise<Chat>;
110
118
  searchChats(opts: SearchChatsOptions): Promise<SearchChatsPage>;
111
119
  newLatestChatsCursor(opts: LatestChatsOptions): LatestChatsCursor;
112
120
  latestChats(opts: LatestChatsOptions): Promise<LatestChatsPage>;
113
121
  nextLatestChats(opts: NextLatestChatsOptions): Promise<LatestChatsPage>;
114
122
  searchChatRoomMembers(opts: SearchChatRoomMembersOptions): Promise<SearchChatRoomMembersPage>;
115
123
  chatRoomMembers(opts: ChatRoomMembersOptions): Promise<ChatRoomMember[]>;
124
+ chatRooms(opts: ChatRoomsOptions): Promise<ChatRoom[]>;
116
125
  myChatRoomMembers(opts: MyChatRoomMembersOptions): Promise<ChatRoomMember[]>;
117
126
  chatRoomParticipants(opts: ChatRoomParticipantsOptions): Promise<ChatRoomParticipantsByRoom>;
118
127
  openChatRoom(opts: OpenChatRoomOptions): Promise<ChatRoomSession>;
@@ -126,12 +135,14 @@ export declare class ChatFX implements ChatFXOperator {
126
135
  createChat(opts: CreateChatOptions): Promise<Chat>;
127
136
  ensureChatRoom(opts: EnsureChatRoomOptions): Promise<string>;
128
137
  markChatObjectsUploaded(opts: MarkChatObjectsUploadedOptions): Promise<Chat>;
138
+ clearChatObjects(opts: ClearChatObjectsOptions): Promise<Chat>;
129
139
  searchChats(opts: SearchChatsOptions): Promise<SearchChatsPage>;
130
140
  newLatestChatsCursor(opts: LatestChatsOptions): LatestChatsCursor;
131
141
  latestChats(opts: LatestChatsOptions): Promise<LatestChatsPage>;
132
142
  nextLatestChats(opts: NextLatestChatsOptions): Promise<LatestChatsPage>;
133
143
  searchChatRoomMembers(opts: SearchChatRoomMembersOptions): Promise<SearchChatRoomMembersPage>;
134
144
  chatRoomMembers(opts: ChatRoomMembersOptions): Promise<ChatRoomMember[]>;
145
+ chatRooms(opts: ChatRoomsOptions): Promise<ChatRoom[]>;
135
146
  myChatRoomMembers(opts: MyChatRoomMembersOptions): Promise<ChatRoomMember[]>;
136
147
  chatRoomParticipants(opts: ChatRoomParticipantsOptions): Promise<ChatRoomParticipantsByRoom>;
137
148
  openChatRoom(opts: OpenChatRoomOptions): Promise<ChatRoomSession>;
@@ -139,6 +150,7 @@ export declare class ChatFX implements ChatFXOperator {
139
150
  reactUploadedChat(opts: ReactUploadedChatOptions): Promise<void>;
140
151
  private openChatRoomConnections;
141
152
  private openGlobalChatRoomsConnection;
153
+ private allChatRoomMembers;
142
154
  private findUser;
143
155
  private findChat;
144
156
  }
@@ -3,10 +3,12 @@
3
3
  // Licensed under the Apache License, Version 2.0 (the "License"); you may
4
4
  // not use this file except in compliance with the License. You may obtain
5
5
  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ import { IDKey, } from "@fabriktor/schema";
6
7
  import { errRequired, errResolveFailed, errValidation } from "../core/err.js";
7
8
  import { requiredOperationID } from "../core/id.js";
8
9
  import { normalizeSearchPage, normalizeSearchPerPage, normalizeSearchQuery, runSearch, } from "../core/search.js";
9
10
  const closeCodeNormal = 1000;
11
+ const closeCodeLeavingPage = 1001;
10
12
  const wsRefreshMilliseconds = 40 * 60 * 1000;
11
13
  const wsRefreshRetryMilliseconds = 30 * 1000;
12
14
  const resourceChat = "chat";
@@ -42,6 +44,16 @@ export class ChatFX {
42
44
  });
43
45
  return await this.findChat(opts.id);
44
46
  }
47
+ async clearChatObjects(opts) {
48
+ await this.chat.replaceOneChat({
49
+ id: opts.id,
50
+ in: {
51
+ has_objects: false,
52
+ objects_uploaded: false,
53
+ },
54
+ });
55
+ return await this.findChat(opts.id);
56
+ }
45
57
  async searchChats(opts) {
46
58
  const q = normalizeSearchQuery(opts.q);
47
59
  if (q === undefined) {
@@ -112,6 +124,18 @@ export class ChatFX {
112
124
  });
113
125
  return out.value ?? [];
114
126
  }
127
+ async chatRooms(opts) {
128
+ const ids = [...new Set(opts.ids.map((id) => id.trim()))].filter((id) => id !== "");
129
+ if (ids.length === 0) {
130
+ return [];
131
+ }
132
+ const out = await this.chat.queryChatRooms({
133
+ query: {
134
+ [IDKey]: ids,
135
+ },
136
+ });
137
+ return out.value ?? [];
138
+ }
115
139
  async myChatRoomMembers(opts) {
116
140
  const out = await this.chat.queryChatRoomMembers({
117
141
  query: {
@@ -124,35 +148,22 @@ export class ChatFX {
124
148
  const roomIDs = [
125
149
  ...new Set(opts.chat_room_ids.map((id) => id.trim())),
126
150
  ].filter((id) => id !== "");
127
- if (roomIDs.length === 0) {
128
- return {};
129
- }
130
- const result = await this.chat.queryChatRoomMembers({
131
- query: {
132
- chat_room_id: roomIDs,
133
- },
134
- });
135
- const members = result.value ?? [];
136
- const membersByRoom = new Map(roomIDs.map((id) => [id, []]));
137
- for (const member of members) {
138
- membersByRoom.get(member.chat_room_id)?.push(member);
139
- }
140
- const ownerIDs = [...new Set(members.map((member) => member.owner_id))];
151
+ const entries = await Promise.all(roomIDs.map(async (chatRoomID) => [chatRoomID, await this.allChatRoomMembers(chatRoomID)]));
152
+ const ownerIDs = [
153
+ ...new Set(entries.flatMap(([, members]) => members.map((member) => member.owner_id))),
154
+ ];
141
155
  const userResults = await Promise.allSettled(ownerIDs.map(async (id) => [id, await this.findUser(id)]));
142
156
  const usersByID = new Map(userResults.flatMap((result) => result.status === "fulfilled" ? [result.value] : []));
143
- return Object.fromEntries(roomIDs.map((chatRoomID) => {
144
- const roomMembers = membersByRoom.get(chatRoomID) ?? [];
145
- return [
146
- chatRoomID,
147
- {
148
- members: roomMembers,
149
- users: roomMembers.flatMap((member) => {
150
- const user = usersByID.get(member.owner_id);
151
- return user ? [user] : [];
152
- }),
153
- },
154
- ];
155
- }));
157
+ return Object.fromEntries(entries.map(([chatRoomID, members]) => [
158
+ chatRoomID,
159
+ {
160
+ members,
161
+ users: members.flatMap((member) => {
162
+ const user = usersByID.get(member.owner_id);
163
+ return user ? [user] : [];
164
+ }),
165
+ },
166
+ ]));
156
167
  }
157
168
  async openChatRoom(opts) {
158
169
  const latest = await this.latestChats({
@@ -218,7 +229,7 @@ export class ChatFX {
218
229
  closeSession(closeCodeNormal, reason);
219
230
  },
220
231
  closeLeavingPage(reason) {
221
- closeSession(closeCodeNormal, reason);
232
+ closeSession(closeCodeLeavingPage, reason);
222
233
  },
223
234
  };
224
235
  }
@@ -226,6 +237,9 @@ export class ChatFX {
226
237
  const members = await this.myChatRoomMembers({
227
238
  owner_id: opts.owner_id,
228
239
  });
240
+ const rooms = await this.chatRooms({
241
+ ids: members.map((member) => member.chat_room_id),
242
+ });
229
243
  const fx = this;
230
244
  let current = await fx.openGlobalChatRoomsConnection(opts);
231
245
  let refresh_timer = null;
@@ -275,6 +289,7 @@ export class ChatFX {
275
289
  }
276
290
  return {
277
291
  members,
292
+ rooms,
278
293
  get chat_rooms_ws() {
279
294
  return current.chat_rooms_ws;
280
295
  },
@@ -282,7 +297,7 @@ export class ChatFX {
282
297
  closeSession(closeCodeNormal, reason);
283
298
  },
284
299
  closeLeavingPage(reason) {
285
- closeSession(closeCodeNormal, reason);
300
+ closeSession(closeCodeLeavingPage, reason);
286
301
  },
287
302
  };
288
303
  }
@@ -415,6 +430,21 @@ export class ChatFX {
415
430
  },
416
431
  };
417
432
  }
433
+ async allChatRoomMembers(chatRoomID) {
434
+ const out = [];
435
+ let page = 1;
436
+ for (;;) {
437
+ const next = await this.searchChatRoomMembers({
438
+ chat_room_id: chatRoomID,
439
+ page,
440
+ });
441
+ out.push(...next.results);
442
+ if (next.done) {
443
+ return out;
444
+ }
445
+ page = next.next_page;
446
+ }
447
+ }
418
448
  async findUser(id) {
419
449
  const out = await this.users.findOneUser({ id });
420
450
  const user = out.value;
@@ -456,7 +486,7 @@ function latestChatsPage(opts) {
456
486
  last_page: opts.raw.last_page,
457
487
  has_more: opts.raw.has_more,
458
488
  next_cursor: {
459
- chat_room_id: opts.raw.results[0]?.chat_room_id ?? opts.chat_room_id,
489
+ chat_room_id: opts.chat_room_id,
460
490
  next_page: opts.raw.next_page,
461
491
  per_page: opts.raw.per_page,
462
492
  done: opts.raw.done,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.55",
3
+ "version": "0.0.57",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,7 +17,7 @@
17
17
  "typescript": "^6.0.3"
18
18
  },
19
19
  "dependencies": {
20
- "@fabriktor/client": "0.0.53",
20
+ "@fabriktor/client": "0.0.54",
21
21
  "@fabriktor/schema": "0.0.34"
22
22
  }
23
23
  }