@fabriktor/fx 0.0.55 → 0.0.56
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/dist/src/chat/chat.d.ts +8 -1
- package/dist/src/chat/chat.js +50 -30
- package/package.json +2 -2
package/dist/src/chat/chat.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ChatOperator, UsersOperator, WSCloseHandler, WSConnection, WSErrorHandler, WSMessageHandler } from "@fabriktor/client";
|
|
2
|
-
import type
|
|
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];
|
|
@@ -59,6 +59,9 @@ export type SearchChatRoomMembersPage = SearchPage<ChatRoomMember>;
|
|
|
59
59
|
export type ChatRoomMembersOptions = {
|
|
60
60
|
chat_room_id: string;
|
|
61
61
|
};
|
|
62
|
+
export type ChatRoomsOptions = {
|
|
63
|
+
ids: string[];
|
|
64
|
+
};
|
|
62
65
|
export type MyChatRoomMembersOptions = {
|
|
63
66
|
owner_id: string;
|
|
64
67
|
};
|
|
@@ -100,6 +103,7 @@ export type ChatRoomSession = {
|
|
|
100
103
|
export type GlobalChatRoomsSession = {
|
|
101
104
|
chat_rooms_ws: WSConnection;
|
|
102
105
|
members: ChatRoomMember[];
|
|
106
|
+
rooms: ChatRoom[];
|
|
103
107
|
closeNormal(reason?: string): void;
|
|
104
108
|
closeLeavingPage(reason?: string): void;
|
|
105
109
|
};
|
|
@@ -113,6 +117,7 @@ export interface ChatFXOperator {
|
|
|
113
117
|
nextLatestChats(opts: NextLatestChatsOptions): Promise<LatestChatsPage>;
|
|
114
118
|
searchChatRoomMembers(opts: SearchChatRoomMembersOptions): Promise<SearchChatRoomMembersPage>;
|
|
115
119
|
chatRoomMembers(opts: ChatRoomMembersOptions): Promise<ChatRoomMember[]>;
|
|
120
|
+
chatRooms(opts: ChatRoomsOptions): Promise<ChatRoom[]>;
|
|
116
121
|
myChatRoomMembers(opts: MyChatRoomMembersOptions): Promise<ChatRoomMember[]>;
|
|
117
122
|
chatRoomParticipants(opts: ChatRoomParticipantsOptions): Promise<ChatRoomParticipantsByRoom>;
|
|
118
123
|
openChatRoom(opts: OpenChatRoomOptions): Promise<ChatRoomSession>;
|
|
@@ -132,6 +137,7 @@ export declare class ChatFX implements ChatFXOperator {
|
|
|
132
137
|
nextLatestChats(opts: NextLatestChatsOptions): Promise<LatestChatsPage>;
|
|
133
138
|
searchChatRoomMembers(opts: SearchChatRoomMembersOptions): Promise<SearchChatRoomMembersPage>;
|
|
134
139
|
chatRoomMembers(opts: ChatRoomMembersOptions): Promise<ChatRoomMember[]>;
|
|
140
|
+
chatRooms(opts: ChatRoomsOptions): Promise<ChatRoom[]>;
|
|
135
141
|
myChatRoomMembers(opts: MyChatRoomMembersOptions): Promise<ChatRoomMember[]>;
|
|
136
142
|
chatRoomParticipants(opts: ChatRoomParticipantsOptions): Promise<ChatRoomParticipantsByRoom>;
|
|
137
143
|
openChatRoom(opts: OpenChatRoomOptions): Promise<ChatRoomSession>;
|
|
@@ -139,6 +145,7 @@ export declare class ChatFX implements ChatFXOperator {
|
|
|
139
145
|
reactUploadedChat(opts: ReactUploadedChatOptions): Promise<void>;
|
|
140
146
|
private openChatRoomConnections;
|
|
141
147
|
private openGlobalChatRoomsConnection;
|
|
148
|
+
private allChatRoomMembers;
|
|
142
149
|
private findUser;
|
|
143
150
|
private findChat;
|
|
144
151
|
}
|
package/dist/src/chat/chat.js
CHANGED
|
@@ -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";
|
|
@@ -112,6 +114,18 @@ export class ChatFX {
|
|
|
112
114
|
});
|
|
113
115
|
return out.value ?? [];
|
|
114
116
|
}
|
|
117
|
+
async chatRooms(opts) {
|
|
118
|
+
const ids = [...new Set(opts.ids.map((id) => id.trim()))].filter((id) => id !== "");
|
|
119
|
+
if (ids.length === 0) {
|
|
120
|
+
return [];
|
|
121
|
+
}
|
|
122
|
+
const out = await this.chat.queryChatRooms({
|
|
123
|
+
query: {
|
|
124
|
+
[IDKey]: ids,
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
return out.value ?? [];
|
|
128
|
+
}
|
|
115
129
|
async myChatRoomMembers(opts) {
|
|
116
130
|
const out = await this.chat.queryChatRoomMembers({
|
|
117
131
|
query: {
|
|
@@ -124,35 +138,22 @@ export class ChatFX {
|
|
|
124
138
|
const roomIDs = [
|
|
125
139
|
...new Set(opts.chat_room_ids.map((id) => id.trim())),
|
|
126
140
|
].filter((id) => id !== "");
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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))];
|
|
141
|
+
const entries = await Promise.all(roomIDs.map(async (chatRoomID) => [chatRoomID, await this.allChatRoomMembers(chatRoomID)]));
|
|
142
|
+
const ownerIDs = [
|
|
143
|
+
...new Set(entries.flatMap(([, members]) => members.map((member) => member.owner_id))),
|
|
144
|
+
];
|
|
141
145
|
const userResults = await Promise.allSettled(ownerIDs.map(async (id) => [id, await this.findUser(id)]));
|
|
142
146
|
const usersByID = new Map(userResults.flatMap((result) => result.status === "fulfilled" ? [result.value] : []));
|
|
143
|
-
return Object.fromEntries(
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
{
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
},
|
|
154
|
-
];
|
|
155
|
-
}));
|
|
147
|
+
return Object.fromEntries(entries.map(([chatRoomID, members]) => [
|
|
148
|
+
chatRoomID,
|
|
149
|
+
{
|
|
150
|
+
members,
|
|
151
|
+
users: members.flatMap((member) => {
|
|
152
|
+
const user = usersByID.get(member.owner_id);
|
|
153
|
+
return user ? [user] : [];
|
|
154
|
+
}),
|
|
155
|
+
},
|
|
156
|
+
]));
|
|
156
157
|
}
|
|
157
158
|
async openChatRoom(opts) {
|
|
158
159
|
const latest = await this.latestChats({
|
|
@@ -218,7 +219,7 @@ export class ChatFX {
|
|
|
218
219
|
closeSession(closeCodeNormal, reason);
|
|
219
220
|
},
|
|
220
221
|
closeLeavingPage(reason) {
|
|
221
|
-
closeSession(
|
|
222
|
+
closeSession(closeCodeLeavingPage, reason);
|
|
222
223
|
},
|
|
223
224
|
};
|
|
224
225
|
}
|
|
@@ -226,6 +227,9 @@ export class ChatFX {
|
|
|
226
227
|
const members = await this.myChatRoomMembers({
|
|
227
228
|
owner_id: opts.owner_id,
|
|
228
229
|
});
|
|
230
|
+
const rooms = await this.chatRooms({
|
|
231
|
+
ids: members.map((member) => member.chat_room_id),
|
|
232
|
+
});
|
|
229
233
|
const fx = this;
|
|
230
234
|
let current = await fx.openGlobalChatRoomsConnection(opts);
|
|
231
235
|
let refresh_timer = null;
|
|
@@ -275,6 +279,7 @@ export class ChatFX {
|
|
|
275
279
|
}
|
|
276
280
|
return {
|
|
277
281
|
members,
|
|
282
|
+
rooms,
|
|
278
283
|
get chat_rooms_ws() {
|
|
279
284
|
return current.chat_rooms_ws;
|
|
280
285
|
},
|
|
@@ -282,7 +287,7 @@ export class ChatFX {
|
|
|
282
287
|
closeSession(closeCodeNormal, reason);
|
|
283
288
|
},
|
|
284
289
|
closeLeavingPage(reason) {
|
|
285
|
-
closeSession(
|
|
290
|
+
closeSession(closeCodeLeavingPage, reason);
|
|
286
291
|
},
|
|
287
292
|
};
|
|
288
293
|
}
|
|
@@ -415,6 +420,21 @@ export class ChatFX {
|
|
|
415
420
|
},
|
|
416
421
|
};
|
|
417
422
|
}
|
|
423
|
+
async allChatRoomMembers(chatRoomID) {
|
|
424
|
+
const out = [];
|
|
425
|
+
let page = 1;
|
|
426
|
+
for (;;) {
|
|
427
|
+
const next = await this.searchChatRoomMembers({
|
|
428
|
+
chat_room_id: chatRoomID,
|
|
429
|
+
page,
|
|
430
|
+
});
|
|
431
|
+
out.push(...next.results);
|
|
432
|
+
if (next.done) {
|
|
433
|
+
return out;
|
|
434
|
+
}
|
|
435
|
+
page = next.next_page;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
418
438
|
async findUser(id) {
|
|
419
439
|
const out = await this.users.findOneUser({ id });
|
|
420
440
|
const user = out.value;
|
|
@@ -456,7 +476,7 @@ function latestChatsPage(opts) {
|
|
|
456
476
|
last_page: opts.raw.last_page,
|
|
457
477
|
has_more: opts.raw.has_more,
|
|
458
478
|
next_cursor: {
|
|
459
|
-
chat_room_id: opts.
|
|
479
|
+
chat_room_id: opts.chat_room_id,
|
|
460
480
|
next_page: opts.raw.next_page,
|
|
461
481
|
per_page: opts.raw.per_page,
|
|
462
482
|
done: opts.raw.done,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabriktor/fx",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.56",
|
|
4
4
|
"description": "",
|
|
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.
|
|
20
|
+
"@fabriktor/client": "0.0.54",
|
|
21
21
|
"@fabriktor/schema": "0.0.34"
|
|
22
22
|
}
|
|
23
23
|
}
|