@chat-leo/chatleolib 0.0.0-stage → 2.0.1
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/LICENSE +21 -0
- package/README.md +69 -2
- package/dist/ChatLeoWorker.d.ts +83 -0
- package/dist/ChatLeoWorker.d.ts.map +1 -0
- package/dist/ChatLeoWorker.js +255 -0
- package/dist/ChatLeoWorker.js.map +1 -0
- package/dist/ChatRoom.d.ts +121 -0
- package/dist/ChatRoom.d.ts.map +1 -0
- package/dist/ChatRoom.js +416 -0
- package/dist/ChatRoom.js.map +1 -0
- package/dist/ChatStore.d.ts +252 -0
- package/dist/ChatStore.d.ts.map +1 -0
- package/dist/ChatStore.js +774 -0
- package/dist/ChatStore.js.map +1 -0
- package/dist/JwtUtils.d.ts +39 -0
- package/dist/JwtUtils.d.ts.map +1 -0
- package/dist/JwtUtils.js +89 -0
- package/dist/JwtUtils.js.map +1 -0
- package/dist/Outbox.d.ts +68 -0
- package/dist/Outbox.d.ts.map +1 -0
- package/dist/Outbox.js +253 -0
- package/dist/Outbox.js.map +1 -0
- package/dist/UploadManager.d.ts +80 -0
- package/dist/UploadManager.d.ts.map +1 -0
- package/dist/UploadManager.js +204 -0
- package/dist/UploadManager.js.map +1 -0
- package/dist/chatleo-push-sw.js +149 -0
- package/dist/chatleo-push-sw.js.map +1 -0
- package/dist/index.d.ts +236 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.esm.js +8526 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +8535 -0
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +8541 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/push/PushService.d.ts +32 -0
- package/dist/push/PushService.d.ts.map +1 -0
- package/dist/push/PushService.js +215 -0
- package/dist/push/PushService.js.map +1 -0
- package/dist/push-sw/index.d.ts +9 -0
- package/dist/push-sw/index.d.ts.map +1 -0
- package/dist/push-sw/index.js +142 -0
- package/dist/push-sw/index.js.map +1 -0
- package/dist/push-sw/standalone.d.ts +2 -0
- package/dist/push-sw/standalone.d.ts.map +1 -0
- package/dist/push-sw/standalone.js +3 -0
- package/dist/push-sw/standalone.js.map +1 -0
- package/dist/transport/SocketIoTransport.d.ts +37 -0
- package/dist/transport/SocketIoTransport.d.ts.map +1 -0
- package/dist/transport/SocketIoTransport.js +209 -0
- package/dist/transport/SocketIoTransport.js.map +1 -0
- package/dist/transport/WsV2Transport.d.ts +109 -0
- package/dist/transport/WsV2Transport.d.ts.map +1 -0
- package/dist/transport/WsV2Transport.js +604 -0
- package/dist/transport/WsV2Transport.js.map +1 -0
- package/dist/transport/discovery.d.ts +43 -0
- package/dist/transport/discovery.d.ts.map +1 -0
- package/dist/transport/discovery.js +87 -0
- package/dist/transport/discovery.js.map +1 -0
- package/dist/transport/factory.d.ts +38 -0
- package/dist/transport/factory.d.ts.map +1 -0
- package/dist/transport/factory.js +79 -0
- package/dist/transport/factory.js.map +1 -0
- package/dist/transport/index.d.ts +7 -0
- package/dist/transport/index.d.ts.map +1 -0
- package/dist/transport/index.js +5 -0
- package/dist/transport/index.js.map +1 -0
- package/dist/transport/types.d.ts +174 -0
- package/dist/transport/types.d.ts.map +1 -0
- package/dist/transport/types.js +35 -0
- package/dist/transport/types.js.map +1 -0
- package/dist/transport/util.d.ts +8 -0
- package/dist/transport/util.d.ts.map +1 -0
- package/dist/transport/util.js +33 -0
- package/dist/transport/util.js.map +1 -0
- package/dist/types/shared.d.ts +281 -0
- package/dist/types/shared.d.ts.map +1 -0
- package/dist/types/shared.js +2 -0
- package/dist/types/shared.js.map +1 -0
- package/dist/types/uploads.d.ts +14 -0
- package/dist/types/uploads.d.ts.map +1 -0
- package/dist/types/uploads.js +3 -0
- package/dist/types/uploads.js.map +1 -0
- package/package.json +70 -3
|
@@ -0,0 +1,774 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _ChatStore_instances, _ChatStore_rooms, _ChatStore_users, _ChatStore_presence, _ChatStore_profile, _ChatStore_status, _ChatStore_isSessionExpired, _ChatStore_activeRoomId, _ChatStore_lastUnreadTotal, _ChatStore_presenceKind, _ChatStore_activities, _ChatStore_streams, _ChatStore_activeCalls, _ChatStore_incomingCall, _ChatStore_incomingTimer, _ChatStore_composerUploads, _ChatStore_olderMessagesLoader, _ChatStore_olderMessagesLoading, _ChatStore_messageActions, _ChatStore_boundOnRoomMessage, _ChatStore_boundOnRoomRead, _ChatStore_boundOnRoomMessageUpdated, _ChatStore_boundOnRoomMessageRemoved, _ChatStore_boundOnUnreadCountChanged, _ChatStore_boundOnNotificationLevelChanged, _ChatStore_withProfile, _ChatStore_roomOfCall, _ChatStore_emitUnreadTotal, _ChatStore_setActiveCall, _ChatStore_clearActiveCall, _ChatStore_setIncomingCall, _ChatStore_activityKey, _ChatStore_applyActivity, _ChatStore_clearActivitiesOf, _ChatStore_dispatchStream;
|
|
13
|
+
import { ChatRoom } from './ChatRoom';
|
|
14
|
+
import { UploadManager } from './UploadManager';
|
|
15
|
+
/**
|
|
16
|
+
* @class ChatStore
|
|
17
|
+
* @extends EventTarget
|
|
18
|
+
* @description This class is the central state management for the chat application. It holds the collections of rooms, users, and presence information. It also owns the `UploadManager` and dispatches events when the store's data changes.
|
|
19
|
+
*/
|
|
20
|
+
export class ChatStore extends EventTarget {
|
|
21
|
+
/**
|
|
22
|
+
* @constructor
|
|
23
|
+
* @description Initializes the `ChatStore` by creating new maps for rooms and users, setting the initial connection status to `false`, and creating an `UploadManager` instance.
|
|
24
|
+
*/
|
|
25
|
+
constructor() {
|
|
26
|
+
super();
|
|
27
|
+
_ChatStore_instances.add(this);
|
|
28
|
+
_ChatStore_rooms.set(this, void 0);
|
|
29
|
+
_ChatStore_users.set(this, void 0);
|
|
30
|
+
_ChatStore_presence.set(this, new Set());
|
|
31
|
+
_ChatStore_profile.set(this, void 0);
|
|
32
|
+
_ChatStore_status.set(this, 'disconnected');
|
|
33
|
+
_ChatStore_isSessionExpired.set(this, false);
|
|
34
|
+
_ChatStore_activeRoomId.set(this, null);
|
|
35
|
+
_ChatStore_lastUnreadTotal.set(this, 0);
|
|
36
|
+
_ChatStore_presenceKind.set(this, new Map());
|
|
37
|
+
_ChatStore_activities.set(this, new Map());
|
|
38
|
+
_ChatStore_streams.set(this, new Map());
|
|
39
|
+
_ChatStore_activeCalls.set(this, new Map());
|
|
40
|
+
_ChatStore_incomingCall.set(this, null);
|
|
41
|
+
_ChatStore_incomingTimer.set(this, void 0);
|
|
42
|
+
// Map of messageId -> localId -> AttachmentUploadRef (temporary composer uploads)
|
|
43
|
+
_ChatStore_composerUploads.set(this, new Map());
|
|
44
|
+
_ChatStore_olderMessagesLoader.set(this, void 0);
|
|
45
|
+
_ChatStore_olderMessagesLoading.set(this, new Set());
|
|
46
|
+
_ChatStore_messageActions.set(this, void 0);
|
|
47
|
+
// Bound room handlers (so unbindRoom removes the exact functions it added)
|
|
48
|
+
_ChatStore_boundOnRoomMessage.set(this, (e) => this.onRoomMessage(e));
|
|
49
|
+
_ChatStore_boundOnRoomRead.set(this, (e) => this.onRoomRead(e));
|
|
50
|
+
_ChatStore_boundOnRoomMessageUpdated.set(this, (e) => this.onRoomMessageUpdated(e));
|
|
51
|
+
_ChatStore_boundOnRoomMessageRemoved.set(this, (e) => this.onRoomMessageRemoved(e));
|
|
52
|
+
_ChatStore_boundOnUnreadCountChanged.set(this, (e) => this.onUnreadCountChanged(e));
|
|
53
|
+
_ChatStore_boundOnNotificationLevelChanged.set(this, (e) => this.dispatchEvent(new CustomEvent('roomNotificationLevelChanged', { detail: e.detail })));
|
|
54
|
+
__classPrivateFieldSet(this, _ChatStore_rooms, new Map(), "f");
|
|
55
|
+
__classPrivateFieldSet(this, _ChatStore_users, new Map(), "f");
|
|
56
|
+
this.uploadManager = new UploadManager();
|
|
57
|
+
this.bindUploadManager();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @method setAccessToken
|
|
61
|
+
* @description Sets the access token for the `UploadManager` to use for authorization.
|
|
62
|
+
* @param {string} token - The access token.
|
|
63
|
+
*/
|
|
64
|
+
setAccessToken(token) {
|
|
65
|
+
this.uploadManager.acess_token = token;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* @method addRoom
|
|
69
|
+
* @description Adds a new room to the store and dispatches a `roomAdded` event.
|
|
70
|
+
* @param {ChatRoom} room - The `ChatRoom` object to add.
|
|
71
|
+
*/
|
|
72
|
+
addRoom(room) {
|
|
73
|
+
const existing = __classPrivateFieldGet(this, _ChatStore_rooms, "f").get(room.id);
|
|
74
|
+
if (existing) {
|
|
75
|
+
// Keep the existing instance (and its loaded messages); refresh server metadata only.
|
|
76
|
+
existing.setServerMeta({ lastSeq: room.serverLastSeq, lastReadSeq: room.lastReadSeq });
|
|
77
|
+
return existing;
|
|
78
|
+
}
|
|
79
|
+
console.debug('Adding room', room);
|
|
80
|
+
this.bindRoom(room);
|
|
81
|
+
room.setUsernameResolver(this.usernameResolver.bind(this));
|
|
82
|
+
__classPrivateFieldGet(this, _ChatStore_rooms, "f").set(room.id, room);
|
|
83
|
+
this.dispatchEvent(new CustomEvent('roomAdded', { detail: { room } }));
|
|
84
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_emitUnreadTotal).call(this);
|
|
85
|
+
return room;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @method usernameResolver
|
|
89
|
+
* @description Resolves a user's profile from their ID. It first checks if the ID matches the current user's profile, and if not, it looks up the user in the `users` map.
|
|
90
|
+
* @param {string} userId - The ID of the user to resolve.
|
|
91
|
+
* @returns {UserProfile | undefined} The user's profile, or `undefined` if not found.
|
|
92
|
+
*/
|
|
93
|
+
usernameResolver(userId) {
|
|
94
|
+
var _a;
|
|
95
|
+
return ((_a = __classPrivateFieldGet(this, _ChatStore_profile, "f")) === null || _a === void 0 ? void 0 : _a.id) === userId ? __classPrivateFieldGet(this, _ChatStore_profile, "f") : __classPrivateFieldGet(this, _ChatStore_users, "f").get(userId);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* @method addUser
|
|
99
|
+
* @description Adds a single user to the store and dispatches a `userChanged` event.
|
|
100
|
+
* @param {UserProfile} u - The user's profile.
|
|
101
|
+
*/
|
|
102
|
+
addUser(u) {
|
|
103
|
+
__classPrivateFieldGet(this, _ChatStore_users, "f").set(u.id, u);
|
|
104
|
+
this.dispatchEvent(new CustomEvent('userChanged', { detail: { user: u } }));
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* @method addUsers
|
|
108
|
+
* @description Adds multiple users to the store and dispatches a `userAdded` event.
|
|
109
|
+
* @param {UserProfile[]} users - An array of user profiles to add.
|
|
110
|
+
*/
|
|
111
|
+
addUsers(users) {
|
|
112
|
+
for (const u of users) {
|
|
113
|
+
__classPrivateFieldGet(this, _ChatStore_users, "f").set(u.id, u);
|
|
114
|
+
}
|
|
115
|
+
this.dispatchEvent(new CustomEvent('userAdded', { detail: { users } }));
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @method addProfile
|
|
119
|
+
* @description Sets the current user's profile and dispatches a `profileChanged` event.
|
|
120
|
+
* @param {UserProfile} u - The user's profile.
|
|
121
|
+
*/
|
|
122
|
+
addProfile(u) {
|
|
123
|
+
__classPrivateFieldSet(this, _ChatStore_profile, u, "f");
|
|
124
|
+
this.dispatchEvent(new CustomEvent('profileChanged', { detail: { profile: u } }));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* @method addRoomMessages
|
|
128
|
+
* @description Adds an array of messages to a specific room.
|
|
129
|
+
* @param {string} roomId - The ID of the room to add the messages to.
|
|
130
|
+
* @param {ChatLeoMessage[]} msgs - The messages to add.
|
|
131
|
+
*/
|
|
132
|
+
addRoomMessages(roomId, msgs, isLive = false) {
|
|
133
|
+
var _a;
|
|
134
|
+
(_a = __classPrivateFieldGet(this, _ChatStore_rooms, "f").get(roomId)) === null || _a === void 0 ? void 0 : _a.addMessages(msgs, isLive);
|
|
135
|
+
}
|
|
136
|
+
markRoomAsRead(roomId, seq) {
|
|
137
|
+
var _a;
|
|
138
|
+
(_a = __classPrivateFieldGet(this, _ChatStore_rooms, "f").get(roomId)) === null || _a === void 0 ? void 0 : _a.markAsRead(seq);
|
|
139
|
+
}
|
|
140
|
+
/** Applies a read marker reported by the server (another device), without echoing it back. */
|
|
141
|
+
applyRemoteRead(roomId, seq) {
|
|
142
|
+
var _a;
|
|
143
|
+
(_a = __classPrivateFieldGet(this, _ChatStore_rooms, "f").get(roomId)) === null || _a === void 0 ? void 0 : _a.applyRemoteRead(seq);
|
|
144
|
+
}
|
|
145
|
+
/** Updates the unread badge from a server-side count (sync or room list). */
|
|
146
|
+
setRoomUnreadCount(roomId, count) {
|
|
147
|
+
var _a;
|
|
148
|
+
(_a = __classPrivateFieldGet(this, _ChatStore_rooms, "f").get(roomId)) === null || _a === void 0 ? void 0 : _a.setUnreadCount(count);
|
|
149
|
+
}
|
|
150
|
+
setRoomNotificationLevel(roomId, level) {
|
|
151
|
+
var _a;
|
|
152
|
+
(_a = __classPrivateFieldGet(this, _ChatStore_rooms, "f").get(roomId)) === null || _a === void 0 ? void 0 : _a.setNotificationLevel(level);
|
|
153
|
+
}
|
|
154
|
+
roomById(roomId) {
|
|
155
|
+
return __classPrivateFieldGet(this, _ChatStore_rooms, "f").get(roomId);
|
|
156
|
+
}
|
|
157
|
+
/** The call currently running in a room, or `undefined`. */
|
|
158
|
+
activeCall(roomId) {
|
|
159
|
+
return __classPrivateFieldGet(this, _ChatStore_activeCalls, "f").get(roomId);
|
|
160
|
+
}
|
|
161
|
+
/** The ringing direct call, if any. */
|
|
162
|
+
get incomingCall() {
|
|
163
|
+
return __classPrivateFieldGet(this, _ChatStore_incomingCall, "f");
|
|
164
|
+
}
|
|
165
|
+
/** Apply the active calls from a room list or sync response; rooms without one are cleared. */
|
|
166
|
+
applyActiveCalls(rooms) {
|
|
167
|
+
const seen = new Set();
|
|
168
|
+
for (const summary of rooms) {
|
|
169
|
+
seen.add(summary.id);
|
|
170
|
+
if (summary.active_call)
|
|
171
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_setActiveCall).call(this, summary.active_call);
|
|
172
|
+
else
|
|
173
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_clearActiveCall).call(this, summary.id);
|
|
174
|
+
}
|
|
175
|
+
for (const roomId of Array.from(__classPrivateFieldGet(this, _ChatStore_activeCalls, "f").keys())) {
|
|
176
|
+
if (!seen.has(roomId))
|
|
177
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_clearActiveCall).call(this, roomId);
|
|
178
|
+
}
|
|
179
|
+
// A ringing call not confirmed by the server after reconnect is stale.
|
|
180
|
+
if (__classPrivateFieldGet(this, _ChatStore_incomingCall, "f") && !seen.has(__classPrivateFieldGet(this, _ChatStore_incomingCall, "f").room_id))
|
|
181
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_setIncomingCall).call(this, null);
|
|
182
|
+
}
|
|
183
|
+
/** Apply one room's call summary (sync entry) or clear it. */
|
|
184
|
+
applyActiveCall(roomId, call) {
|
|
185
|
+
if (call)
|
|
186
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_setActiveCall).call(this, call);
|
|
187
|
+
else
|
|
188
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_clearActiveCall).call(this, roomId);
|
|
189
|
+
if (__classPrivateFieldGet(this, _ChatStore_incomingCall, "f") && __classPrivateFieldGet(this, _ChatStore_incomingCall, "f").room_id === roomId && !call)
|
|
190
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_setIncomingCall).call(this, null);
|
|
191
|
+
}
|
|
192
|
+
/** Apply one normalised call event. */
|
|
193
|
+
applyCallEvent(event) {
|
|
194
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
195
|
+
// A stage role change names the call and the user; it changes no call or room state here.
|
|
196
|
+
// The component of that user answers by fetching a fresh SFU token (promotion, demotion).
|
|
197
|
+
if (event.event === 'role') {
|
|
198
|
+
this.dispatchEvent(new CustomEvent('callRole', { detail: { callId: event.call_id, userId: event.user_id, promoted: !!event.promoted } }));
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
// call.accepted / call.declined / call.ended name only the call: find its room.
|
|
202
|
+
const roomId = event.room_id || __classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_roomOfCall).call(this, event.call_id);
|
|
203
|
+
const existing = __classPrivateFieldGet(this, _ChatStore_activeCalls, "f").get(roomId);
|
|
204
|
+
const call = {
|
|
205
|
+
id: event.call_id,
|
|
206
|
+
room_id: roomId,
|
|
207
|
+
// id-only events (accepted, …) carry no mode: keep the one the call started with
|
|
208
|
+
mode: event.room_id ? event.mode : ((_a = existing === null || existing === void 0 ? void 0 : existing.mode) !== null && _a !== void 0 ? _a : event.mode),
|
|
209
|
+
state: event.event === 'started' && event.mode === 'direct' ? 'ringing' : event.event === 'started' ? 'active' : ((_b = existing === null || existing === void 0 ? void 0 : existing.state) !== null && _b !== void 0 ? _b : 'active'),
|
|
210
|
+
starter_id: (_d = (_c = event.starter_id) !== null && _c !== void 0 ? _c : existing === null || existing === void 0 ? void 0 : existing.starter_id) !== null && _d !== void 0 ? _d : '',
|
|
211
|
+
started_at: existing === null || existing === void 0 ? void 0 : existing.started_at,
|
|
212
|
+
ring_expires_at: (_e = event.expires_at) !== null && _e !== void 0 ? _e : existing === null || existing === void 0 ? void 0 : existing.ring_expires_at,
|
|
213
|
+
video: (_f = event.video) !== null && _f !== void 0 ? _f : existing === null || existing === void 0 ? void 0 : existing.video,
|
|
214
|
+
};
|
|
215
|
+
switch (event.event) {
|
|
216
|
+
case 'started':
|
|
217
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_setActiveCall).call(this, call);
|
|
218
|
+
break;
|
|
219
|
+
case 'ringing': {
|
|
220
|
+
const incoming = {
|
|
221
|
+
id: event.call_id,
|
|
222
|
+
room_id: roomId,
|
|
223
|
+
mode: 'direct',
|
|
224
|
+
caller: __classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_withProfile).call(this, (_g = event.caller) !== null && _g !== void 0 ? _g : { id: (_h = event.starter_id) !== null && _h !== void 0 ? _h : '' }),
|
|
225
|
+
expires_at: event.expires_at,
|
|
226
|
+
video: event.video,
|
|
227
|
+
};
|
|
228
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_setActiveCall).call(this, { ...call, state: 'ringing' });
|
|
229
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_setIncomingCall).call(this, incoming);
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
case 'accepted':
|
|
233
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_setActiveCall).call(this, { ...call, state: 'active' });
|
|
234
|
+
if (((_j = __classPrivateFieldGet(this, _ChatStore_incomingCall, "f")) === null || _j === void 0 ? void 0 : _j.id) === event.call_id)
|
|
235
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_setIncomingCall).call(this, null);
|
|
236
|
+
break;
|
|
237
|
+
case 'declined':
|
|
238
|
+
case 'ended':
|
|
239
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_clearActiveCall).call(this, roomId);
|
|
240
|
+
if (((_k = __classPrivateFieldGet(this, _ChatStore_incomingCall, "f")) === null || _k === void 0 ? void 0 : _k.id) === event.call_id)
|
|
241
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_setIncomingCall).call(this, null);
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/** Installs the library's paging implementation used by scroll-back in the room view. */
|
|
246
|
+
setOlderMessagesLoader(loader) {
|
|
247
|
+
__classPrivateFieldSet(this, _ChatStore_olderMessagesLoader, loader, "f");
|
|
248
|
+
}
|
|
249
|
+
/** Installs the library's outbox actions used by retry/discard in message components. */
|
|
250
|
+
setMessageActions(actions) {
|
|
251
|
+
__classPrivateFieldSet(this, _ChatStore_messageActions, actions, "f");
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Loads one page of older messages for a room (scroll-back). Returns `true` when more pages
|
|
255
|
+
* exist. No-op while a page for that room is already loading.
|
|
256
|
+
*/
|
|
257
|
+
async loadOlderMessages(roomId) {
|
|
258
|
+
const room = __classPrivateFieldGet(this, _ChatStore_rooms, "f").get(roomId);
|
|
259
|
+
if (!room || !__classPrivateFieldGet(this, _ChatStore_olderMessagesLoader, "f") || __classPrivateFieldGet(this, _ChatStore_olderMessagesLoading, "f").has(roomId))
|
|
260
|
+
return false;
|
|
261
|
+
if (!room.messages.length)
|
|
262
|
+
return false;
|
|
263
|
+
const oldestSeq = room.oldestSeq;
|
|
264
|
+
if (typeof oldestSeq === 'number' && oldestSeq <= 1)
|
|
265
|
+
return false;
|
|
266
|
+
const before = typeof oldestSeq === 'number' ? { seq: oldestSeq } : room.oldestCreatedAt ? { createdAt: room.oldestCreatedAt } : undefined;
|
|
267
|
+
if (!before)
|
|
268
|
+
return false;
|
|
269
|
+
__classPrivateFieldGet(this, _ChatStore_olderMessagesLoading, "f").add(roomId);
|
|
270
|
+
try {
|
|
271
|
+
const page = await __classPrivateFieldGet(this, _ChatStore_olderMessagesLoader, "f").call(this, roomId, before);
|
|
272
|
+
if (page.messages.length)
|
|
273
|
+
room.addMessages(page.messages);
|
|
274
|
+
return page.hasMore;
|
|
275
|
+
}
|
|
276
|
+
catch (e) {
|
|
277
|
+
console.debug('loadOlderMessages failed', roomId, e);
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
finally {
|
|
281
|
+
__classPrivateFieldGet(this, _ChatStore_olderMessagesLoading, "f").delete(roomId);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
retryMessage(roomId, clientId) {
|
|
285
|
+
var _a;
|
|
286
|
+
const room = __classPrivateFieldGet(this, _ChatStore_rooms, "f").get(roomId);
|
|
287
|
+
if (!room)
|
|
288
|
+
return;
|
|
289
|
+
room.markMessagePending(clientId);
|
|
290
|
+
(_a = __classPrivateFieldGet(this, _ChatStore_messageActions, "f")) === null || _a === void 0 ? void 0 : _a.retry(roomId, clientId);
|
|
291
|
+
}
|
|
292
|
+
discardMessage(roomId, clientId) {
|
|
293
|
+
var _a, _b;
|
|
294
|
+
(_a = __classPrivateFieldGet(this, _ChatStore_rooms, "f").get(roomId)) === null || _a === void 0 ? void 0 : _a.removeMessageByClientId(clientId);
|
|
295
|
+
(_b = __classPrivateFieldGet(this, _ChatStore_messageActions, "f")) === null || _b === void 0 ? void 0 : _b.discard(roomId, clientId);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* @method setUserPresence
|
|
299
|
+
* @description Sets the presence status for a user and dispatches a `presenceChanged` event.
|
|
300
|
+
* @param {string} userId - The ID of the user.
|
|
301
|
+
* @param {boolean} online - `true` if the user is online, `false` otherwise.
|
|
302
|
+
*/
|
|
303
|
+
setUserPresence(userId, online, kind) {
|
|
304
|
+
if (online) {
|
|
305
|
+
__classPrivateFieldGet(this, _ChatStore_presence, "f").add(userId);
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
__classPrivateFieldGet(this, _ChatStore_presence, "f").delete(userId);
|
|
309
|
+
}
|
|
310
|
+
if (kind)
|
|
311
|
+
__classPrivateFieldGet(this, _ChatStore_presenceKind, "f").set(userId, kind);
|
|
312
|
+
console.debug(`userPresence`, __classPrivateFieldGet(this, _ChatStore_presence, "f"));
|
|
313
|
+
this.dispatchEvent(new CustomEvent('presenceChanged', { detail: { userId, online, kind: kind !== null && kind !== void 0 ? kind : __classPrivateFieldGet(this, _ChatStore_presenceKind, "f").get(userId) } }));
|
|
314
|
+
}
|
|
315
|
+
/** `agent` / `system` for non-human participants whose presence was announced with a kind. */
|
|
316
|
+
presenceKind(userId) {
|
|
317
|
+
return __classPrivateFieldGet(this, _ChatStore_presenceKind, "f").get(userId);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* @method isOnline
|
|
321
|
+
* @description Checks if a user is currently online.
|
|
322
|
+
* @param {string} userId - The ID of the user to check.
|
|
323
|
+
* @returns {boolean} `true` if the user is online, `false` otherwise.
|
|
324
|
+
*/
|
|
325
|
+
isOnline(userId) {
|
|
326
|
+
const isOnline = __classPrivateFieldGet(this, _ChatStore_presence, "f").has(userId);
|
|
327
|
+
console.debug(`isOnline`, { userId, isOnline });
|
|
328
|
+
return isOnline;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* @getter profile
|
|
332
|
+
* @description Returns the profile of the current user.
|
|
333
|
+
* @returns {UserProfile | undefined} The user's profile, or `undefined` if not set.
|
|
334
|
+
*/
|
|
335
|
+
get profile() {
|
|
336
|
+
return __classPrivateFieldGet(this, _ChatStore_profile, "f");
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* @getter connectionStatus
|
|
340
|
+
* @description Returns the explicit connection status.
|
|
341
|
+
*/
|
|
342
|
+
get connectionStatus() {
|
|
343
|
+
return __classPrivateFieldGet(this, _ChatStore_status, "f");
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* @getter isConnected
|
|
347
|
+
* @description Returns `true` when status is `connected`. Kept for backward compatibility.
|
|
348
|
+
*/
|
|
349
|
+
get isConnected() {
|
|
350
|
+
return __classPrivateFieldGet(this, _ChatStore_status, "f") === 'connected';
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* @method setConnectionStatus
|
|
354
|
+
* @description Sets the explicit connection status and dispatches `connectionStatusChanged`
|
|
355
|
+
* (and the legacy `connectionChanged` when the boolean changes).
|
|
356
|
+
*/
|
|
357
|
+
setConnectionStatus(status) {
|
|
358
|
+
const prevConnected = __classPrivateFieldGet(this, _ChatStore_status, "f") === 'connected';
|
|
359
|
+
if (__classPrivateFieldGet(this, _ChatStore_status, "f") === status)
|
|
360
|
+
return;
|
|
361
|
+
__classPrivateFieldSet(this, _ChatStore_status, status, "f");
|
|
362
|
+
this.dispatchEvent(new CustomEvent('connectionStatusChanged', { detail: { status } }));
|
|
363
|
+
const nowConnected = status === 'connected';
|
|
364
|
+
if (prevConnected !== nowConnected) {
|
|
365
|
+
this.dispatchEvent(new CustomEvent('connectionChanged', { detail: { isConnected: nowConnected } }));
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* @method setConnected
|
|
370
|
+
* @description Backward-compatible boolean bridge: maps true→connected, false→disconnected.
|
|
371
|
+
*/
|
|
372
|
+
setConnected(connected) {
|
|
373
|
+
this.setConnectionStatus(connected ? 'connected' : 'disconnected');
|
|
374
|
+
}
|
|
375
|
+
get activeRoomId() {
|
|
376
|
+
return __classPrivateFieldGet(this, _ChatStore_activeRoomId, "f");
|
|
377
|
+
}
|
|
378
|
+
setActiveRoomId(id) {
|
|
379
|
+
if (__classPrivateFieldGet(this, _ChatStore_activeRoomId, "f") === id)
|
|
380
|
+
return;
|
|
381
|
+
__classPrivateFieldSet(this, _ChatStore_activeRoomId, id, "f");
|
|
382
|
+
this.dispatchEvent(new CustomEvent('activeRoomChanged', { detail: { roomId: id } }));
|
|
383
|
+
}
|
|
384
|
+
/** Sum of unread messages across all rooms. */
|
|
385
|
+
get unreadTotal() {
|
|
386
|
+
return this.rooms.reduce((total, room) => total + (room.unreadCount || 0), 0);
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* @getter isSessionExpired
|
|
390
|
+
* @description Returns whether the session has expired (token refresh failed).
|
|
391
|
+
*/
|
|
392
|
+
get isSessionExpired() {
|
|
393
|
+
return __classPrivateFieldGet(this, _ChatStore_isSessionExpired, "f");
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* @method setSessionExpired
|
|
397
|
+
* @description Sets the session-expired flag and dispatches a `sessionExpiredChanged` event.
|
|
398
|
+
*/
|
|
399
|
+
setSessionExpired(expired) {
|
|
400
|
+
if (__classPrivateFieldGet(this, _ChatStore_isSessionExpired, "f") !== expired) {
|
|
401
|
+
__classPrivateFieldSet(this, _ChatStore_isSessionExpired, expired, "f");
|
|
402
|
+
this.dispatchEvent(new CustomEvent('sessionExpiredChanged', { detail: { isSessionExpired: expired } }));
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* @getter rooms
|
|
407
|
+
* @description Returns an array of all the rooms in the store.
|
|
408
|
+
* @returns {ChatRoom[]} An array of `ChatRoom` objects.
|
|
409
|
+
*/
|
|
410
|
+
get rooms() {
|
|
411
|
+
return Array.from(__classPrivateFieldGet(this, _ChatStore_rooms, "f").values());
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* @method roomMessages
|
|
415
|
+
* @description Retrieves all the messages for a specific room.
|
|
416
|
+
* @param {string} roomId - The ID of the room.
|
|
417
|
+
* @returns {ChatLeoMessage[]} An array of messages for the given room.
|
|
418
|
+
*/
|
|
419
|
+
roomMessages(roomId) {
|
|
420
|
+
var _a, _b;
|
|
421
|
+
// console.debug("Getting room messages", roomId);
|
|
422
|
+
return (_b = (_a = __classPrivateFieldGet(this, _ChatStore_rooms, "f").get(roomId)) === null || _a === void 0 ? void 0 : _a.messages) !== null && _b !== void 0 ? _b : [];
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* @getter users
|
|
426
|
+
* @description Returns an array of all the users in the store.
|
|
427
|
+
* @returns {UserProfile[]} An array of `UserProfile` objects.
|
|
428
|
+
*/
|
|
429
|
+
get users() {
|
|
430
|
+
return Array.from(__classPrivateFieldGet(this, _ChatStore_users, "f").values());
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* @method bindRoom
|
|
434
|
+
* @description Binds a `messageAdded` event listener to a room, which will re-dispatch the event from the store.
|
|
435
|
+
* @param {ChatRoom} room - The room to bind to.
|
|
436
|
+
*/
|
|
437
|
+
bindRoom(room) {
|
|
438
|
+
room.addEventListener('messageAdded', __classPrivateFieldGet(this, _ChatStore_boundOnRoomMessage, "f"));
|
|
439
|
+
room.addEventListener('messageUpdated', __classPrivateFieldGet(this, _ChatStore_boundOnRoomMessageUpdated, "f"));
|
|
440
|
+
room.addEventListener('messageRemoved', __classPrivateFieldGet(this, _ChatStore_boundOnRoomMessageRemoved, "f"));
|
|
441
|
+
room.addEventListener('roomRead', __classPrivateFieldGet(this, _ChatStore_boundOnRoomRead, "f"));
|
|
442
|
+
room.addEventListener('unreadCountChanged', __classPrivateFieldGet(this, _ChatStore_boundOnUnreadCountChanged, "f"));
|
|
443
|
+
room.addEventListener('notificationLevelChanged', __classPrivateFieldGet(this, _ChatStore_boundOnNotificationLevelChanged, "f"));
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* @method unbindRoom
|
|
447
|
+
* @description Removes the `messageAdded` event listener from a room.
|
|
448
|
+
* @param {ChatRoom} room - The room to unbind from.
|
|
449
|
+
*/
|
|
450
|
+
unbindRoom(room) {
|
|
451
|
+
room.removeEventListener('messageAdded', __classPrivateFieldGet(this, _ChatStore_boundOnRoomMessage, "f"));
|
|
452
|
+
room.removeEventListener('messageUpdated', __classPrivateFieldGet(this, _ChatStore_boundOnRoomMessageUpdated, "f"));
|
|
453
|
+
room.removeEventListener('messageRemoved', __classPrivateFieldGet(this, _ChatStore_boundOnRoomMessageRemoved, "f"));
|
|
454
|
+
room.removeEventListener('roomRead', __classPrivateFieldGet(this, _ChatStore_boundOnRoomRead, "f"));
|
|
455
|
+
room.removeEventListener('unreadCountChanged', __classPrivateFieldGet(this, _ChatStore_boundOnUnreadCountChanged, "f"));
|
|
456
|
+
room.removeEventListener('notificationLevelChanged', __classPrivateFieldGet(this, _ChatStore_boundOnNotificationLevelChanged, "f"));
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* @method onRoomMessage
|
|
460
|
+
* @description Event handler for the `messageAdded` event from a `ChatRoom`. It re-dispatches the event as `roomMessageAdded` from the `ChatStore`.
|
|
461
|
+
* @param {Event} event - The `messageAdded` event.
|
|
462
|
+
*/
|
|
463
|
+
onRoomMessage(event) {
|
|
464
|
+
event.preventDefault();
|
|
465
|
+
this.dispatchEvent(new CustomEvent('roomMessageAdded', { detail: event === null || event === void 0 ? void 0 : event.detail }));
|
|
466
|
+
}
|
|
467
|
+
onRoomMessageUpdated(event) {
|
|
468
|
+
event.preventDefault();
|
|
469
|
+
this.dispatchEvent(new CustomEvent('roomMessageUpdated', { detail: event === null || event === void 0 ? void 0 : event.detail }));
|
|
470
|
+
}
|
|
471
|
+
onRoomMessageRemoved(event) {
|
|
472
|
+
event.preventDefault();
|
|
473
|
+
this.dispatchEvent(new CustomEvent('roomMessageRemoved', { detail: event === null || event === void 0 ? void 0 : event.detail }));
|
|
474
|
+
}
|
|
475
|
+
onRoomRead(event) {
|
|
476
|
+
const detail = event === null || event === void 0 ? void 0 : event.detail;
|
|
477
|
+
this.dispatchEvent(new CustomEvent('roomReadCountChanged', { detail }));
|
|
478
|
+
if (detail && !detail.remote) {
|
|
479
|
+
this.dispatchEvent(new CustomEvent('roomReadRequested', { detail }));
|
|
480
|
+
}
|
|
481
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_emitUnreadTotal).call(this);
|
|
482
|
+
}
|
|
483
|
+
onUnreadCountChanged(event) {
|
|
484
|
+
this.dispatchEvent(new CustomEvent('roomReadCountChanged', { detail: event === null || event === void 0 ? void 0 : event.detail }));
|
|
485
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_emitUnreadTotal).call(this);
|
|
486
|
+
}
|
|
487
|
+
// --- Normalised transport events ---
|
|
488
|
+
/**
|
|
489
|
+
* Highest message `seq` held per room. Used as resume cursors by protocol v2; rooms without
|
|
490
|
+
* sequenced messages are omitted.
|
|
491
|
+
*/
|
|
492
|
+
roomCursors() {
|
|
493
|
+
const cursors = {};
|
|
494
|
+
for (const room of __classPrivateFieldGet(this, _ChatStore_rooms, "f").values()) {
|
|
495
|
+
const cursor = room.lastSeq;
|
|
496
|
+
if (cursor > 0)
|
|
497
|
+
cursors[room.id] = cursor;
|
|
498
|
+
}
|
|
499
|
+
return cursors;
|
|
500
|
+
}
|
|
501
|
+
/** Cursors for `POST /api/v1/sync`: the highest contiguous seq, safe even after a live gap. */
|
|
502
|
+
roomSyncCursors() {
|
|
503
|
+
const cursors = {};
|
|
504
|
+
for (const room of __classPrivateFieldGet(this, _ChatStore_rooms, "f").values()) {
|
|
505
|
+
const cursor = room.contiguousSeq;
|
|
506
|
+
if (cursor > 0)
|
|
507
|
+
cursors[room.id] = cursor;
|
|
508
|
+
}
|
|
509
|
+
return cursors;
|
|
510
|
+
}
|
|
511
|
+
/** Applies a protocol-independent event from a transport. Connection-level events are handled by `ChatLeoLib`. */
|
|
512
|
+
applyTransportEvent(e) {
|
|
513
|
+
var _a, _b, _c;
|
|
514
|
+
switch (e.type) {
|
|
515
|
+
case 'message':
|
|
516
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_clearActivitiesOf).call(this, e.message.room_id, e.message.sender_id);
|
|
517
|
+
this.addRoomMessages(e.message.room_id, [e.message], true);
|
|
518
|
+
break;
|
|
519
|
+
case 'roomAdded':
|
|
520
|
+
this.addRoom(ChatRoom.fromRoomSummary(e.room, (_b = (_a = __classPrivateFieldGet(this, _ChatStore_profile, "f")) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : ''));
|
|
521
|
+
break;
|
|
522
|
+
case 'roomRemoved':
|
|
523
|
+
this.removeRoom(e.roomId);
|
|
524
|
+
break;
|
|
525
|
+
case 'roomRead':
|
|
526
|
+
this.applyRemoteRead(e.roomId, e.seq);
|
|
527
|
+
break;
|
|
528
|
+
case 'call':
|
|
529
|
+
this.applyCallEvent(e.call);
|
|
530
|
+
break;
|
|
531
|
+
case 'presence':
|
|
532
|
+
for (const u of e.users)
|
|
533
|
+
this.setUserPresence(u.userId, u.online, u.kind);
|
|
534
|
+
break;
|
|
535
|
+
case 'activity':
|
|
536
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_applyActivity).call(this, e);
|
|
537
|
+
break;
|
|
538
|
+
case 'streamStart':
|
|
539
|
+
__classPrivateFieldGet(this, _ChatStore_streams, "f").set(e.streamId, {
|
|
540
|
+
streamId: e.streamId,
|
|
541
|
+
roomId: e.roomId,
|
|
542
|
+
senderId: e.senderId,
|
|
543
|
+
contentType: e.contentType,
|
|
544
|
+
text: e.contentSoFar,
|
|
545
|
+
status: 'streaming',
|
|
546
|
+
nextIndex: 0,
|
|
547
|
+
});
|
|
548
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_dispatchStream).call(this, e.streamId);
|
|
549
|
+
break;
|
|
550
|
+
case 'streamDelta': {
|
|
551
|
+
const s = (_c = __classPrivateFieldGet(this, _ChatStore_streams, "f").get(e.streamId)) !== null && _c !== void 0 ? _c : {
|
|
552
|
+
streamId: e.streamId,
|
|
553
|
+
roomId: e.roomId,
|
|
554
|
+
senderId: e.senderId,
|
|
555
|
+
contentType: 'text/plain',
|
|
556
|
+
text: '',
|
|
557
|
+
status: 'streaming',
|
|
558
|
+
nextIndex: 0,
|
|
559
|
+
};
|
|
560
|
+
if (e.index < s.nextIndex)
|
|
561
|
+
break; // duplicate
|
|
562
|
+
s.text += e.text;
|
|
563
|
+
s.nextIndex = e.index + 1;
|
|
564
|
+
__classPrivateFieldGet(this, _ChatStore_streams, "f").set(e.streamId, s);
|
|
565
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_dispatchStream).call(this, e.streamId);
|
|
566
|
+
break;
|
|
567
|
+
}
|
|
568
|
+
case 'streamEnd':
|
|
569
|
+
case 'streamAbort': {
|
|
570
|
+
const s = __classPrivateFieldGet(this, _ChatStore_streams, "f").get(e.streamId);
|
|
571
|
+
if (!s)
|
|
572
|
+
break;
|
|
573
|
+
s.status = e.type === 'streamEnd' ? 'ended' : 'aborted';
|
|
574
|
+
if (e.type === 'streamAbort')
|
|
575
|
+
s.reason = e.reason;
|
|
576
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_dispatchStream).call(this, e.streamId);
|
|
577
|
+
__classPrivateFieldGet(this, _ChatStore_streams, "f").delete(e.streamId);
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
default:
|
|
581
|
+
break; // status / resync / authRequired / fatal are connection concerns
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
removeRoom(roomId) {
|
|
585
|
+
const room = __classPrivateFieldGet(this, _ChatStore_rooms, "f").get(roomId);
|
|
586
|
+
if (!room)
|
|
587
|
+
return;
|
|
588
|
+
this.unbindRoom(room);
|
|
589
|
+
__classPrivateFieldGet(this, _ChatStore_rooms, "f").delete(roomId);
|
|
590
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_clearActiveCall).call(this, roomId);
|
|
591
|
+
if (__classPrivateFieldGet(this, _ChatStore_activeRoomId, "f") === roomId)
|
|
592
|
+
this.setActiveRoomId(null);
|
|
593
|
+
this.dispatchEvent(new CustomEvent('roomRemoved', { detail: { roomId } }));
|
|
594
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_emitUnreadTotal).call(this);
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Drops every room, message, user and call held in memory.
|
|
598
|
+
*
|
|
599
|
+
* Used on logout and when a session ends so a different person on the same device
|
|
600
|
+
* never sees the previous user's data.
|
|
601
|
+
*/
|
|
602
|
+
reset() {
|
|
603
|
+
for (const room of __classPrivateFieldGet(this, _ChatStore_rooms, "f").values())
|
|
604
|
+
this.unbindRoom(room);
|
|
605
|
+
__classPrivateFieldGet(this, _ChatStore_rooms, "f").clear();
|
|
606
|
+
__classPrivateFieldGet(this, _ChatStore_users, "f").clear();
|
|
607
|
+
__classPrivateFieldGet(this, _ChatStore_presence, "f").clear();
|
|
608
|
+
__classPrivateFieldGet(this, _ChatStore_presenceKind, "f").clear();
|
|
609
|
+
__classPrivateFieldGet(this, _ChatStore_activities, "f").clear();
|
|
610
|
+
__classPrivateFieldGet(this, _ChatStore_streams, "f").clear();
|
|
611
|
+
__classPrivateFieldGet(this, _ChatStore_activeCalls, "f").clear();
|
|
612
|
+
__classPrivateFieldGet(this, _ChatStore_composerUploads, "f").clear();
|
|
613
|
+
__classPrivateFieldSet(this, _ChatStore_incomingCall, null, "f");
|
|
614
|
+
if (__classPrivateFieldGet(this, _ChatStore_incomingTimer, "f")) {
|
|
615
|
+
clearTimeout(__classPrivateFieldGet(this, _ChatStore_incomingTimer, "f"));
|
|
616
|
+
__classPrivateFieldSet(this, _ChatStore_incomingTimer, undefined, "f");
|
|
617
|
+
}
|
|
618
|
+
__classPrivateFieldSet(this, _ChatStore_profile, undefined, "f");
|
|
619
|
+
__classPrivateFieldSet(this, _ChatStore_activeRoomId, null, "f");
|
|
620
|
+
__classPrivateFieldSet(this, _ChatStore_lastUnreadTotal, 0, "f");
|
|
621
|
+
this.dispatchEvent(new CustomEvent('reset'));
|
|
622
|
+
this.dispatchEvent(new CustomEvent('activeRoomChanged', { detail: { roomId: null } }));
|
|
623
|
+
this.dispatchEvent(new CustomEvent('unreadTotalChanged', { detail: { count: 0 } }));
|
|
624
|
+
}
|
|
625
|
+
/** Activities currently displayed in a room (expired and ended ones are filtered out). */
|
|
626
|
+
getActivities(roomId) {
|
|
627
|
+
const now = Date.now();
|
|
628
|
+
const out = [];
|
|
629
|
+
for (const [key, a] of __classPrivateFieldGet(this, _ChatStore_activities, "f")) {
|
|
630
|
+
if (a.expiresAt <= now) {
|
|
631
|
+
__classPrivateFieldGet(this, _ChatStore_activities, "f").delete(key);
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
if (a.roomId === roomId)
|
|
635
|
+
out.push(a);
|
|
636
|
+
}
|
|
637
|
+
return out;
|
|
638
|
+
}
|
|
639
|
+
/** Streamed messages that are still in progress in a room. */
|
|
640
|
+
getStreams(roomId) {
|
|
641
|
+
return Array.from(__classPrivateFieldGet(this, _ChatStore_streams, "f").values())
|
|
642
|
+
.filter(s => s.roomId === roomId)
|
|
643
|
+
.map(({ nextIndex: _n, ...s }) => ({ ...s }));
|
|
644
|
+
}
|
|
645
|
+
// --- Uploads state and events ---
|
|
646
|
+
ensureMessageUploads(messageId) {
|
|
647
|
+
if (!__classPrivateFieldGet(this, _ChatStore_composerUploads, "f").has(messageId)) {
|
|
648
|
+
__classPrivateFieldGet(this, _ChatStore_composerUploads, "f").set(messageId, new Map());
|
|
649
|
+
}
|
|
650
|
+
return __classPrivateFieldGet(this, _ChatStore_composerUploads, "f").get(messageId);
|
|
651
|
+
}
|
|
652
|
+
getComposerUploadsByMessage(messageId) {
|
|
653
|
+
return Array.from(this.ensureMessageUploads(messageId).values());
|
|
654
|
+
}
|
|
655
|
+
bindUploadManager() {
|
|
656
|
+
this.uploadManager.addEventListener('uploadQueued', (e) => {
|
|
657
|
+
// No messageId yet; do not store in composerUploads
|
|
658
|
+
const { ref } = e.detail || {};
|
|
659
|
+
this.dispatchEvent(new CustomEvent('attachmentUploadQueued', { detail: { ref } }));
|
|
660
|
+
});
|
|
661
|
+
this.uploadManager.addEventListener('uploadProgress', (e) => {
|
|
662
|
+
const { messageId, localId, progress, ref } = e.detail || {};
|
|
663
|
+
if (!messageId || !localId)
|
|
664
|
+
return;
|
|
665
|
+
const map = this.ensureMessageUploads(messageId);
|
|
666
|
+
map.set(localId, { ...ref, progress, status: 'uploading' });
|
|
667
|
+
this.dispatchEvent(new CustomEvent('attachmentUploadProgress', { detail: { messageId, ref: map.get(localId) } }));
|
|
668
|
+
});
|
|
669
|
+
this.uploadManager.addEventListener('uploadCompleted', (e) => {
|
|
670
|
+
const { messageId, localId, ref } = e.detail || {};
|
|
671
|
+
if (!messageId || !localId)
|
|
672
|
+
return;
|
|
673
|
+
const map = this.ensureMessageUploads(messageId);
|
|
674
|
+
map.set(localId, { ...ref, progress: 100, status: 'done' });
|
|
675
|
+
this.dispatchEvent(new CustomEvent('attachmentUploadCompleted', { detail: { messageId, ref: map.get(localId) } }));
|
|
676
|
+
});
|
|
677
|
+
this.uploadManager.addEventListener('uploadFailed', (e) => {
|
|
678
|
+
const { messageId, localId, error, ref } = e.detail || {};
|
|
679
|
+
if (!messageId || !localId)
|
|
680
|
+
return;
|
|
681
|
+
const map = this.ensureMessageUploads(messageId);
|
|
682
|
+
map.set(localId, { ...ref, status: 'error', error });
|
|
683
|
+
this.dispatchEvent(new CustomEvent('attachmentUploadFailed', { detail: { messageId, ref: map.get(localId) } }));
|
|
684
|
+
});
|
|
685
|
+
this.uploadManager.addEventListener('uploadRemoved', (e) => {
|
|
686
|
+
const { messageId, localId, ref } = e.detail || {};
|
|
687
|
+
if (!messageId || !localId)
|
|
688
|
+
return;
|
|
689
|
+
const map = this.ensureMessageUploads(messageId);
|
|
690
|
+
map.delete(localId);
|
|
691
|
+
this.dispatchEvent(new CustomEvent('attachmentUploadRemoved', { detail: { messageId, ref } }));
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
_ChatStore_rooms = new WeakMap(), _ChatStore_users = new WeakMap(), _ChatStore_presence = new WeakMap(), _ChatStore_profile = new WeakMap(), _ChatStore_status = new WeakMap(), _ChatStore_isSessionExpired = new WeakMap(), _ChatStore_activeRoomId = new WeakMap(), _ChatStore_lastUnreadTotal = new WeakMap(), _ChatStore_presenceKind = new WeakMap(), _ChatStore_activities = new WeakMap(), _ChatStore_streams = new WeakMap(), _ChatStore_activeCalls = new WeakMap(), _ChatStore_incomingCall = new WeakMap(), _ChatStore_incomingTimer = new WeakMap(), _ChatStore_composerUploads = new WeakMap(), _ChatStore_olderMessagesLoader = new WeakMap(), _ChatStore_olderMessagesLoading = new WeakMap(), _ChatStore_messageActions = new WeakMap(), _ChatStore_boundOnRoomMessage = new WeakMap(), _ChatStore_boundOnRoomRead = new WeakMap(), _ChatStore_boundOnRoomMessageUpdated = new WeakMap(), _ChatStore_boundOnRoomMessageRemoved = new WeakMap(), _ChatStore_boundOnUnreadCountChanged = new WeakMap(), _ChatStore_boundOnNotificationLevelChanged = new WeakMap(), _ChatStore_instances = new WeakSet(), _ChatStore_withProfile = function _ChatStore_withProfile(participant) {
|
|
696
|
+
var _a, _b;
|
|
697
|
+
const profile = this.usernameResolver(participant.id);
|
|
698
|
+
return {
|
|
699
|
+
...participant,
|
|
700
|
+
display_name: participant.display_name || (profile === null || profile === void 0 ? void 0 : profile.display_name) || undefined,
|
|
701
|
+
profile_picture_url: (_b = (_a = participant.profile_picture_url) !== null && _a !== void 0 ? _a : profile === null || profile === void 0 ? void 0 : profile.profile_picture_url) !== null && _b !== void 0 ? _b : null,
|
|
702
|
+
};
|
|
703
|
+
}, _ChatStore_roomOfCall = function _ChatStore_roomOfCall(callId) {
|
|
704
|
+
var _a;
|
|
705
|
+
for (const [roomId, call] of __classPrivateFieldGet(this, _ChatStore_activeCalls, "f"))
|
|
706
|
+
if (call.id === callId)
|
|
707
|
+
return roomId;
|
|
708
|
+
return ((_a = __classPrivateFieldGet(this, _ChatStore_incomingCall, "f")) === null || _a === void 0 ? void 0 : _a.id) === callId ? __classPrivateFieldGet(this, _ChatStore_incomingCall, "f").room_id : '';
|
|
709
|
+
}, _ChatStore_emitUnreadTotal = function _ChatStore_emitUnreadTotal() {
|
|
710
|
+
const count = this.unreadTotal;
|
|
711
|
+
if (count === __classPrivateFieldGet(this, _ChatStore_lastUnreadTotal, "f"))
|
|
712
|
+
return;
|
|
713
|
+
__classPrivateFieldSet(this, _ChatStore_lastUnreadTotal, count, "f");
|
|
714
|
+
this.dispatchEvent(new CustomEvent('unreadTotalChanged', { detail: { count } }));
|
|
715
|
+
}, _ChatStore_setActiveCall = function _ChatStore_setActiveCall(call) {
|
|
716
|
+
__classPrivateFieldGet(this, _ChatStore_activeCalls, "f").set(call.room_id, call);
|
|
717
|
+
this.dispatchEvent(new CustomEvent('callChanged', { detail: { roomId: call.room_id, call } }));
|
|
718
|
+
}, _ChatStore_clearActiveCall = function _ChatStore_clearActiveCall(roomId) {
|
|
719
|
+
if (!__classPrivateFieldGet(this, _ChatStore_activeCalls, "f").delete(roomId))
|
|
720
|
+
return;
|
|
721
|
+
this.dispatchEvent(new CustomEvent('callChanged', { detail: { roomId, call: null } }));
|
|
722
|
+
}, _ChatStore_setIncomingCall = function _ChatStore_setIncomingCall(call) {
|
|
723
|
+
if (__classPrivateFieldGet(this, _ChatStore_incomingTimer, "f")) {
|
|
724
|
+
clearTimeout(__classPrivateFieldGet(this, _ChatStore_incomingTimer, "f"));
|
|
725
|
+
__classPrivateFieldSet(this, _ChatStore_incomingTimer, undefined, "f");
|
|
726
|
+
}
|
|
727
|
+
__classPrivateFieldSet(this, _ChatStore_incomingCall, call, "f");
|
|
728
|
+
this.dispatchEvent(new CustomEvent('callRinging', { detail: { call } }));
|
|
729
|
+
if (!(call === null || call === void 0 ? void 0 : call.expires_at))
|
|
730
|
+
return;
|
|
731
|
+
const delay = Date.parse(call.expires_at) - Date.now();
|
|
732
|
+
if (!Number.isFinite(delay))
|
|
733
|
+
return;
|
|
734
|
+
__classPrivateFieldSet(this, _ChatStore_incomingTimer, setTimeout(() => {
|
|
735
|
+
var _a;
|
|
736
|
+
__classPrivateFieldSet(this, _ChatStore_incomingTimer, undefined, "f");
|
|
737
|
+
if (((_a = __classPrivateFieldGet(this, _ChatStore_incomingCall, "f")) === null || _a === void 0 ? void 0 : _a.id) === call.id)
|
|
738
|
+
__classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_setIncomingCall).call(this, null);
|
|
739
|
+
}, Math.max(0, delay)), "f");
|
|
740
|
+
}, _ChatStore_activityKey = function _ChatStore_activityKey(a) {
|
|
741
|
+
var _a;
|
|
742
|
+
return `${a.roomId}|${a.senderId}|${a.kind}|${(_a = a.activityId) !== null && _a !== void 0 ? _a : ''}`;
|
|
743
|
+
}, _ChatStore_applyActivity = function _ChatStore_applyActivity(e) {
|
|
744
|
+
const activity = {
|
|
745
|
+
roomId: e.roomId,
|
|
746
|
+
senderId: e.senderId,
|
|
747
|
+
kind: e.kind,
|
|
748
|
+
activityId: e.activityId,
|
|
749
|
+
state: e.state,
|
|
750
|
+
ttlMs: e.ttlMs,
|
|
751
|
+
expiresAt: Date.now() + e.ttlMs,
|
|
752
|
+
data: e.data,
|
|
753
|
+
};
|
|
754
|
+
const key = __classPrivateFieldGet(this, _ChatStore_instances, "m", _ChatStore_activityKey).call(this, activity);
|
|
755
|
+
if (e.state === 'end')
|
|
756
|
+
__classPrivateFieldGet(this, _ChatStore_activities, "f").delete(key);
|
|
757
|
+
else
|
|
758
|
+
__classPrivateFieldGet(this, _ChatStore_activities, "f").set(key, activity);
|
|
759
|
+
this.dispatchEvent(new CustomEvent('activity', { detail: { ...activity } }));
|
|
760
|
+
}, _ChatStore_clearActivitiesOf = function _ChatStore_clearActivitiesOf(roomId, senderId) {
|
|
761
|
+
for (const [key, a] of Array.from(__classPrivateFieldGet(this, _ChatStore_activities, "f"))) {
|
|
762
|
+
if (a.roomId === roomId && a.senderId === senderId) {
|
|
763
|
+
__classPrivateFieldGet(this, _ChatStore_activities, "f").delete(key);
|
|
764
|
+
this.dispatchEvent(new CustomEvent('activity', { detail: { ...a, state: 'end' } }));
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}, _ChatStore_dispatchStream = function _ChatStore_dispatchStream(streamId) {
|
|
768
|
+
const s = __classPrivateFieldGet(this, _ChatStore_streams, "f").get(streamId);
|
|
769
|
+
if (!s)
|
|
770
|
+
return;
|
|
771
|
+
const { nextIndex: _n, ...detail } = s;
|
|
772
|
+
this.dispatchEvent(new CustomEvent('streamUpdated', { detail: { ...detail } }));
|
|
773
|
+
};
|
|
774
|
+
//# sourceMappingURL=ChatStore.js.map
|