@fabriktor/fx 0.0.53 → 0.0.55

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.
@@ -4,6 +4,7 @@ import { type SearchPage } from "../core/search.js";
4
4
  type ChatInsertOptions = Parameters<ChatOperator["insertOneChat"]>[0];
5
5
  type ChatReplaceOptions = Parameters<ChatOperator["replaceOneChat"]>[0];
6
6
  export type CreateChatOptions = ChatInsertOptions;
7
+ export type EnsureChatRoomOptions = Parameters<ChatOperator["ensureChatRoom"]>[0];
7
8
  export type MarkChatObjectsUploadedOptions = {
8
9
  id: ChatReplaceOptions["id"];
9
10
  };
@@ -104,6 +105,7 @@ export type GlobalChatRoomsSession = {
104
105
  };
105
106
  export interface ChatFXOperator {
106
107
  createChat(opts: CreateChatOptions): Promise<Chat>;
108
+ ensureChatRoom(opts: EnsureChatRoomOptions): Promise<string>;
107
109
  markChatObjectsUploaded(opts: MarkChatObjectsUploadedOptions): Promise<Chat>;
108
110
  searchChats(opts: SearchChatsOptions): Promise<SearchChatsPage>;
109
111
  newLatestChatsCursor(opts: LatestChatsOptions): LatestChatsCursor;
@@ -122,6 +124,7 @@ export declare class ChatFX implements ChatFXOperator {
122
124
  private users;
123
125
  constructor(opts: ChatFXOptions);
124
126
  createChat(opts: CreateChatOptions): Promise<Chat>;
127
+ ensureChatRoom(opts: EnsureChatRoomOptions): Promise<string>;
125
128
  markChatObjectsUploaded(opts: MarkChatObjectsUploadedOptions): Promise<Chat>;
126
129
  searchChats(opts: SearchChatsOptions): Promise<SearchChatsPage>;
127
130
  newLatestChatsCursor(opts: LatestChatsOptions): LatestChatsCursor;
@@ -12,6 +12,7 @@ const wsRefreshRetryMilliseconds = 30 * 1000;
12
12
  const resourceChat = "chat";
13
13
  const resourceUser = "user";
14
14
  const msgCreatedChatIDMissing = "Created chat response did not contain an ID.";
15
+ const msgEnsuredChatRoomIDMissing = "Ensured chat room response did not contain an ID.";
15
16
  const msgChatMissing = "Chat could not be resolved after the mutation.";
16
17
  const msgSearchQueryRequired = "Search query is required.";
17
18
  const msgChatObjectsUploading = "Cannot react to a chat while objects are still uploading.";
@@ -28,6 +29,10 @@ export class ChatFX {
28
29
  const id = requiredOperationID(out, msgCreatedChatIDMissing);
29
30
  return await this.findChat(id);
30
31
  }
32
+ async ensureChatRoom(opts) {
33
+ const out = await this.chat.ensureChatRoom(opts);
34
+ return requiredOperationID(out, msgEnsuredChatRoomIDMissing);
35
+ }
31
36
  async markChatObjectsUploaded(opts) {
32
37
  await this.chat.replaceOneChat({
33
38
  id: opts.id,
@@ -119,31 +124,35 @@ export class ChatFX {
119
124
  const roomIDs = [
120
125
  ...new Set(opts.chat_room_ids.map((id) => id.trim())),
121
126
  ].filter((id) => id !== "");
127
+ if (roomIDs.length === 0) {
128
+ return {};
129
+ }
122
130
  const result = await this.chat.queryChatRoomMembers({
123
131
  query: {
124
132
  chat_room_id: roomIDs,
125
133
  },
126
134
  });
127
- const allMembers = result.value ?? [];
128
- const entries = roomIDs.map((chatRoomID) => [
129
- chatRoomID,
130
- allMembers.filter((member) => member.chat_room_id === chatRoomID),
131
- ]);
132
- const ownerIDs = [
133
- ...new Set(entries.flatMap(([, members]) => members.map((member) => member.owner_id))),
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))];
135
141
  const userResults = await Promise.allSettled(ownerIDs.map(async (id) => [id, await this.findUser(id)]));
136
142
  const usersByID = new Map(userResults.flatMap((result) => result.status === "fulfilled" ? [result.value] : []));
137
- return Object.fromEntries(entries.map(([chatRoomID, members]) => [
138
- chatRoomID,
139
- {
140
- members,
141
- users: members.flatMap((member) => {
142
- const user = usersByID.get(member.owner_id);
143
- return user ? [user] : [];
144
- }),
145
- },
146
- ]));
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
+ }));
147
156
  }
148
157
  async openChatRoom(opts) {
149
158
  const latest = await this.latestChats({
@@ -447,7 +456,7 @@ function latestChatsPage(opts) {
447
456
  last_page: opts.raw.last_page,
448
457
  has_more: opts.raw.has_more,
449
458
  next_cursor: {
450
- chat_room_id: opts.chat_room_id,
459
+ chat_room_id: opts.raw.results[0]?.chat_room_id ?? opts.chat_room_id,
451
460
  next_page: opts.raw.next_page,
452
461
  per_page: opts.raw.per_page,
453
462
  done: opts.raw.done,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.53",
3
+ "version": "0.0.55",
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.52",
20
+ "@fabriktor/client": "0.0.53",
21
21
  "@fabriktor/schema": "0.0.34"
22
22
  }
23
23
  }