@devanachat/sdk 0.6.0 → 0.8.0
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/CHANGELOG.md +70 -0
- package/README.md +58 -0
- package/build/index.cjs.js +749 -38
- package/build/index.cjs.js.map +1 -1
- package/build/index.mjs +748 -39
- package/build/index.mjs.map +1 -1
- package/build/index.umd.js +1 -1
- package/build/index.umd.js.map +1 -1
- package/build/types/AbstractChatClient.d.ts +3 -1
- package/build/types/Permissions.d.ts +8 -0
- package/build/types/index.d.ts +4 -2
- package/build/types/protocol/index.d.ts +5 -3
- package/build/types/protocol/schemes/RoomSummary.d.ts +1 -0
- package/build/types/protocol/schemes/commands/GetComputedPermissions.d.ts +2 -2
- package/build/types/protocol/schemes/commands/SearchMessages.d.ts +18 -0
- package/build/types/protocol/schemes/events/Error.d.ts +10 -0
- package/build/types/protocol/schemes/events/FoundMessages.d.ts +16 -0
- package/build/types/state-tracker/ChatStateTracker.d.ts +29 -0
- package/build/types/state-tracker/EmoticonsManager.d.ts +16 -0
- package/build/types/state-tracker/FollowedTopicsManager.d.ts +16 -0
- package/build/types/state-tracker/MessagesManager.d.ts +23 -3
- package/build/types/state-tracker/PermissionsManager.d.ts +4 -0
- package/build/types/state-tracker/RelationshipsManager.d.ts +11 -0
- package/build/types/state-tracker/RoomMessagesHistory.d.ts +21 -3
- package/build/types/state-tracker/RoomsManager.d.ts +32 -0
- package/build/types/state-tracker/SpacesManager.d.ts +16 -0
- package/build/types/state-tracker/StateSnapshot.d.ts +102 -0
- package/build/types/state-tracker/TopicHistoryWindow.d.ts +24 -1
- package/build/types/state-tracker/UsersManager.d.ts +5 -0
- package/package.json +1 -1
package/build/index.cjs.js
CHANGED
|
@@ -40,6 +40,7 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
40
40
|
// EXPORTS
|
|
41
41
|
__webpack_require__.d(__webpack_exports__, {
|
|
42
42
|
AbstractRestClient: () => (/* reexport */ AbstractRestClient),
|
|
43
|
+
ErrorCode: () => (/* reexport */ ErrorCode),
|
|
43
44
|
FilesClient: () => (/* reexport */ FilesClient),
|
|
44
45
|
HistoryRotation: () => (/* reexport */ HistoryRotation),
|
|
45
46
|
IndexedCollection: () => (/* reexport */ IndexedCollection),
|
|
@@ -51,6 +52,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
51
52
|
PermissionDefinition: () => (/* reexport */ PermissionDefinition),
|
|
52
53
|
Permissions: () => (/* reexport */ Permissions),
|
|
53
54
|
RoleFlag: () => (/* reexport */ RoleFlag),
|
|
55
|
+
STATE_SNAPSHOT_VERSION: () => (/* reexport */ STATE_SNAPSHOT_VERSION),
|
|
54
56
|
STORAGE_UNIT_BYTES: () => (/* reexport */ STORAGE_UNIT_BYTES),
|
|
55
57
|
SearchRange: () => (/* reexport */ SearchRange),
|
|
56
58
|
SpaceFeature: () => (/* reexport */ SpaceFeature),
|
|
@@ -589,7 +591,17 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
589
591
|
this.emitChangeWithDiff(true, originalState);
|
|
590
592
|
}
|
|
591
593
|
async fetchNext() {
|
|
592
|
-
if (this.
|
|
594
|
+
if (this.hasLatest) {
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
return this.fetchAfterLast();
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* Append the items that follow the last one in the window; an empty window gets the latest items.
|
|
602
|
+
*/
|
|
603
|
+
async fetchAfterLast() {
|
|
604
|
+
if (this.internalState.ongoing) {
|
|
593
605
|
return;
|
|
594
606
|
}
|
|
595
607
|
let result;
|
|
@@ -602,7 +614,7 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
602
614
|
this.internalState.ongoing = undefined;
|
|
603
615
|
}
|
|
604
616
|
if (!result) {
|
|
605
|
-
await this.resetToLatest();
|
|
617
|
+
await this.resetToLatest(true);
|
|
606
618
|
return;
|
|
607
619
|
}
|
|
608
620
|
if (result.length) {
|
|
@@ -712,23 +724,31 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
712
724
|
/**
|
|
713
725
|
* The window does not subscribe to the client itself - the messages manager routes the events of its topic to it.
|
|
714
726
|
*/
|
|
715
|
-
constructor(roomId, topicId, tracker) {
|
|
727
|
+
constructor(roomId, topicId, tracker, preview = false) {
|
|
716
728
|
super('id');
|
|
717
729
|
this.roomId = roomId;
|
|
718
730
|
this.topicId = topicId;
|
|
719
731
|
this.tracker = tracker;
|
|
732
|
+
this.preview = preview;
|
|
720
733
|
this.internalState.traverseLock = false;
|
|
721
|
-
this.internalState.includeMyReactions =
|
|
734
|
+
this.internalState.includeMyReactions = !preview;
|
|
722
735
|
this.internalState.myReactions = {};
|
|
723
736
|
this.internalState.liveLimit = 50;
|
|
724
737
|
}
|
|
725
738
|
createMirror() {
|
|
726
|
-
const copy = new TopicHistoryWindow(this.roomId, this.topicId, this.tracker);
|
|
739
|
+
const copy = new TopicHistoryWindow(this.roomId, this.topicId, this.tracker, this.preview);
|
|
727
740
|
copy.eventTarget = this.eventTarget;
|
|
728
741
|
copy._items = this._items;
|
|
729
742
|
copy.internalState = this.internalState;
|
|
730
743
|
return copy;
|
|
731
744
|
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* Window of a room you have not joined. It gets no live messages - at the latest ones fetchNext() fetches the newer.
|
|
748
|
+
*/
|
|
749
|
+
get isPreview() {
|
|
750
|
+
return this.preview;
|
|
751
|
+
}
|
|
732
752
|
get isTraverseLocked() {
|
|
733
753
|
return this.internalState.traverseLock;
|
|
734
754
|
}
|
|
@@ -779,6 +799,9 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
779
799
|
if (this.internalState.traverseLock) {
|
|
780
800
|
return;
|
|
781
801
|
}
|
|
802
|
+
if (this.preview && this.hasLatest) {
|
|
803
|
+
return this.fetchAfterLast(); // No events reach a preview
|
|
804
|
+
}
|
|
782
805
|
return super.fetchNext();
|
|
783
806
|
}
|
|
784
807
|
async fetchPrevious() {
|
|
@@ -794,6 +817,43 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
794
817
|
return super.jumpTo(id);
|
|
795
818
|
}
|
|
796
819
|
|
|
820
|
+
/**
|
|
821
|
+
* The latest messages (oldest first) with the reactions of the user, or null if the window does not hold them.
|
|
822
|
+
* @internal
|
|
823
|
+
*/
|
|
824
|
+
_exportLatest(limit) {
|
|
825
|
+
if (this.state !== WindowState.LATEST) {
|
|
826
|
+
return null;
|
|
827
|
+
}
|
|
828
|
+
const messages = this.items.slice(-Math.max(limit, this.fetchLimit));
|
|
829
|
+
const myReactions = {};
|
|
830
|
+
for (const message of messages) {
|
|
831
|
+
if (this.internalState.myReactions[message.id]) {
|
|
832
|
+
myReactions[message.id] = this.internalState.myReactions[message.id];
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
return {
|
|
836
|
+
messages,
|
|
837
|
+
myReactions
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* Fill the window with saved latest messages, as if they were just fetched; the next reset to the latest replaces
|
|
843
|
+
* them.
|
|
844
|
+
* @internal
|
|
845
|
+
*/
|
|
846
|
+
_restoreLatest(messages, myReactions) {
|
|
847
|
+
const originalState = this.state;
|
|
848
|
+
this._items.deleteAll(); // Directly call deleteAll to prevent event emit.
|
|
849
|
+
this.addItems(messages, 'tail');
|
|
850
|
+
this.internalState.myReactions = {
|
|
851
|
+
...myReactions
|
|
852
|
+
};
|
|
853
|
+
this.internalState.current = WindowState.LATEST;
|
|
854
|
+
this.emitChangeWithDiff(true, originalState);
|
|
855
|
+
}
|
|
856
|
+
|
|
797
857
|
/**
|
|
798
858
|
* For internal use.
|
|
799
859
|
* @internal
|
|
@@ -871,6 +931,17 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
871
931
|
this.internalState.myReactions = next;
|
|
872
932
|
}
|
|
873
933
|
async getTopic() {
|
|
934
|
+
if (this.preview) {
|
|
935
|
+
// The tracker holds the topics of the joined rooms only
|
|
936
|
+
const result = await this.tracker.client.send('GetTopics', {
|
|
937
|
+
roomId: this.roomId,
|
|
938
|
+
topicIds: [this.topicId]
|
|
939
|
+
});
|
|
940
|
+
if (result.error) {
|
|
941
|
+
throw new Error(`Cannot fetch topic: ${result.error.message}`);
|
|
942
|
+
}
|
|
943
|
+
return result.data.topics.find(topic => topic.id === this.topicId);
|
|
944
|
+
}
|
|
874
945
|
return (await this.tracker.rooms.getTopics(this.roomId, [this.topicId])).get(this.topicId);
|
|
875
946
|
}
|
|
876
947
|
async getLatestMessageId() {
|
|
@@ -985,13 +1056,20 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
985
1056
|
* dropped together with its room stops receiving them.
|
|
986
1057
|
*/
|
|
987
1058
|
class RoomMessagesHistory {
|
|
1059
|
+
isPreview;
|
|
1060
|
+
roomId;
|
|
988
1061
|
historyWindows = new IndexedCollection();
|
|
989
1062
|
traverseLock = false;
|
|
1063
|
+
room;
|
|
990
1064
|
constructor(room, tracker) {
|
|
991
|
-
this.room = room;
|
|
992
1065
|
this.tracker = tracker;
|
|
993
|
-
this.
|
|
994
|
-
|
|
1066
|
+
this.roomId = typeof room === 'string' ? room : room.id;
|
|
1067
|
+
this.room = typeof room === 'string' ? null : room;
|
|
1068
|
+
this.isPreview = this.room === null;
|
|
1069
|
+
if (this.room) {
|
|
1070
|
+
this.updateTraverseLock(this.room);
|
|
1071
|
+
}
|
|
1072
|
+
if (this.room?.defaultTopic) {
|
|
995
1073
|
this.createHistoryWindowForTopic(this.room.defaultTopic);
|
|
996
1074
|
}
|
|
997
1075
|
}
|
|
@@ -1003,9 +1081,13 @@ class RoomMessagesHistory {
|
|
|
1003
1081
|
*/
|
|
1004
1082
|
async getMessagesWindow(topicId, peek = false) {
|
|
1005
1083
|
if (!this.historyWindows.has(topicId) && !peek) {
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1084
|
+
if (this.isPreview) {
|
|
1085
|
+
this.createHistoryWindow(topicId);
|
|
1086
|
+
} else {
|
|
1087
|
+
const topic = (await this.tracker.rooms.getTopics(this.roomId, [topicId])).get(topicId);
|
|
1088
|
+
if (topic) {
|
|
1089
|
+
this.createHistoryWindowForTopic(topic);
|
|
1090
|
+
}
|
|
1009
1091
|
}
|
|
1010
1092
|
}
|
|
1011
1093
|
return this.historyWindows.get(topicId);
|
|
@@ -1048,6 +1130,31 @@ class RoomMessagesHistory {
|
|
|
1048
1130
|
}
|
|
1049
1131
|
}
|
|
1050
1132
|
|
|
1133
|
+
/**
|
|
1134
|
+
* For internal use.
|
|
1135
|
+
* @internal
|
|
1136
|
+
*/
|
|
1137
|
+
get _isEphemeral() {
|
|
1138
|
+
return this.traverseLock;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
/**
|
|
1142
|
+
* The window of the topic, if created; unlike getMessagesWindow(), never creates nor fetches one.
|
|
1143
|
+
* @internal
|
|
1144
|
+
*/
|
|
1145
|
+
_peekWindow(topicId) {
|
|
1146
|
+
return this.historyWindows.get(topicId);
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/**
|
|
1150
|
+
* For internal use.
|
|
1151
|
+
* @internal
|
|
1152
|
+
*/
|
|
1153
|
+
_restoreWindow(topic, messages, myReactions) {
|
|
1154
|
+
this.createHistoryWindowForTopic(topic);
|
|
1155
|
+
this.historyWindows.get(topic.id)._restoreLatest(messages, myReactions);
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1051
1158
|
/**
|
|
1052
1159
|
* For internal use.
|
|
1053
1160
|
* @internal
|
|
@@ -1118,9 +1225,18 @@ class RoomMessagesHistory {
|
|
|
1118
1225
|
if (this.historyWindows.has(topic.id)) {
|
|
1119
1226
|
return;
|
|
1120
1227
|
}
|
|
1121
|
-
|
|
1228
|
+
this.createHistoryWindow(topic.id);
|
|
1229
|
+
|
|
1230
|
+
// If new topic refers to some message from this room, update other structures
|
|
1231
|
+
if (topic.refMessage) {
|
|
1232
|
+
const refHistoryWindow = this.historyWindows.get(topic.refMessage.location.topicId);
|
|
1233
|
+
refHistoryWindow?._updateMessageReference(topic);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
createHistoryWindow(topicId) {
|
|
1237
|
+
const historyWindow = new TopicHistoryWindow(this.roomId, topicId, this.tracker, this.isPreview);
|
|
1122
1238
|
void historyWindow.setTraverseLock(this.traverseLock);
|
|
1123
|
-
this.historyWindows.set([
|
|
1239
|
+
this.historyWindows.set([topicId, historyWindow]);
|
|
1124
1240
|
|
|
1125
1241
|
// Current behavior of deletion a message with referenced topic is to delete the whole side topic
|
|
1126
1242
|
// So we need to listen for topic deletions here
|
|
@@ -1128,14 +1244,8 @@ class RoomMessagesHistory {
|
|
|
1128
1244
|
for (const topicId of deletedTopicIds) {
|
|
1129
1245
|
this.historyWindows.delete(topicId);
|
|
1130
1246
|
}
|
|
1131
|
-
this.tracker.rooms._deleteTopicsFromRoom(this.
|
|
1247
|
+
this.tracker.rooms._deleteTopicsFromRoom(this.roomId, ...deletedTopicIds);
|
|
1132
1248
|
});
|
|
1133
|
-
|
|
1134
|
-
// If new topic refers to some message from this room, update other structures
|
|
1135
|
-
if (topic.refMessage) {
|
|
1136
|
-
const refHistoryWindow = this.historyWindows.get(topic.refMessage.location.topicId);
|
|
1137
|
-
refHistoryWindow?._updateMessageReference(topic);
|
|
1138
|
-
}
|
|
1139
1249
|
}
|
|
1140
1250
|
updateTraverseLock(room) {
|
|
1141
1251
|
this.traverseLock = room.history.mode === 'Ephemeral';
|
|
@@ -1147,6 +1257,7 @@ class RoomMessagesHistory {
|
|
|
1147
1257
|
|
|
1148
1258
|
class MessagesManager {
|
|
1149
1259
|
roomHistories = new IndexedCollection();
|
|
1260
|
+
previewHistories = new IndexedCollection();
|
|
1150
1261
|
deferredSession = new DeferredTask();
|
|
1151
1262
|
constructor(tracker) {
|
|
1152
1263
|
this.tracker = tracker;
|
|
@@ -1167,11 +1278,21 @@ class MessagesManager {
|
|
|
1167
1278
|
}
|
|
1168
1279
|
|
|
1169
1280
|
/**
|
|
1170
|
-
* Get history manager for given room ID.
|
|
1281
|
+
* Get history manager for given room ID. For a room you have not joined it is a preview (requires `PreviewRooms`),
|
|
1282
|
+
* kept until {@link forgetRoomPreview} or until you join the room.
|
|
1171
1283
|
*/
|
|
1172
1284
|
async getRoomHistory(roomId) {
|
|
1173
1285
|
await this.deferredSession.promise;
|
|
1174
|
-
|
|
1286
|
+
if (this.roomHistories.has(roomId)) {
|
|
1287
|
+
return this.roomHistories.get(roomId);
|
|
1288
|
+
}
|
|
1289
|
+
if (!this.previewHistories.has(roomId)) {
|
|
1290
|
+
this.previewHistories.set([roomId, new RoomMessagesHistory(roomId, this.tracker)]);
|
|
1291
|
+
}
|
|
1292
|
+
return this.previewHistories.get(roomId);
|
|
1293
|
+
}
|
|
1294
|
+
forgetRoomPreview(roomId) {
|
|
1295
|
+
this.previewHistories.delete(roomId);
|
|
1175
1296
|
}
|
|
1176
1297
|
|
|
1177
1298
|
/**
|
|
@@ -1179,8 +1300,10 @@ class MessagesManager {
|
|
|
1179
1300
|
* @internal
|
|
1180
1301
|
*/
|
|
1181
1302
|
async _resolveLastMessage(location) {
|
|
1182
|
-
// Try to get last message from history window (if cached)
|
|
1183
|
-
|
|
1303
|
+
// Try to get last message from history window (if cached); not getRoomHistory(), it would create a preview
|
|
1304
|
+
await this.deferredSession.promise;
|
|
1305
|
+
const historyWindow = await this.roomHistories.get(location.roomId)?.getMessagesWindow(location.topicId, true);
|
|
1306
|
+
let message = historyWindow?.hasLatest && historyWindow.getAt(historyWindow.length - 1);
|
|
1184
1307
|
if (!message) {
|
|
1185
1308
|
const result = await this.tracker.client.send('GetMessages', {
|
|
1186
1309
|
location,
|
|
@@ -1190,12 +1313,50 @@ class MessagesManager {
|
|
|
1190
1313
|
}
|
|
1191
1314
|
return message || null;
|
|
1192
1315
|
}
|
|
1316
|
+
|
|
1317
|
+
/**
|
|
1318
|
+
* For internal use.
|
|
1319
|
+
* @internal
|
|
1320
|
+
*/
|
|
1321
|
+
_importRooms(rooms) {
|
|
1322
|
+
for (const room of rooms) {
|
|
1323
|
+
this.createHistoryForNewRoom(room);
|
|
1324
|
+
}
|
|
1325
|
+
this.deferredSession.resolve();
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
/**
|
|
1329
|
+
* For internal use.
|
|
1330
|
+
* @internal
|
|
1331
|
+
*/
|
|
1332
|
+
_exportLatest(roomId, topicId, limit) {
|
|
1333
|
+
const history = this.roomHistories.get(roomId);
|
|
1334
|
+
|
|
1335
|
+
// Ephemeral history lives only in the memory of the session.
|
|
1336
|
+
if (!history || history._isEphemeral) {
|
|
1337
|
+
return null;
|
|
1338
|
+
}
|
|
1339
|
+
return history._peekWindow(topicId)?._exportLatest(limit) ?? null;
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
/**
|
|
1343
|
+
* For internal use.
|
|
1344
|
+
* @internal
|
|
1345
|
+
*/
|
|
1346
|
+
_importHistory(roomId, topic, snapshot) {
|
|
1347
|
+
const history = this.roomHistories.get(roomId);
|
|
1348
|
+
if (history && !history._isEphemeral) {
|
|
1349
|
+
const messages = snapshot.messages.filter(message => message.location.roomId === roomId && message.location.topicId === topic.id);
|
|
1350
|
+
history._restoreWindow(topic, messages, snapshot.myReactions);
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1193
1353
|
forEachRoomHistory(callback) {
|
|
1194
1354
|
for (const history of this.roomHistories.items.values()) {
|
|
1195
1355
|
callback(history);
|
|
1196
1356
|
}
|
|
1197
1357
|
}
|
|
1198
1358
|
createHistoryForNewRoom(room) {
|
|
1359
|
+
this.previewHistories.delete(room.id);
|
|
1199
1360
|
this.roomHistories.set([room.id, new RoomMessagesHistory(room, this.tracker)]);
|
|
1200
1361
|
}
|
|
1201
1362
|
handleRoomDeleted(ev) {
|
|
@@ -1409,6 +1570,47 @@ class FollowedTopicsManager extends EventTarget {
|
|
|
1409
1570
|
this.followedTopics.get(roomId)?.delete(...topicIds);
|
|
1410
1571
|
topicIds.forEach(topicId => this.invalidateUnreadSummaries(roomId, topicId));
|
|
1411
1572
|
}
|
|
1573
|
+
|
|
1574
|
+
/**
|
|
1575
|
+
* For internal use.
|
|
1576
|
+
* @internal
|
|
1577
|
+
*/
|
|
1578
|
+
_exportFollowedTopics() {
|
|
1579
|
+
return Array.from(this.followedTopics.items).filter(([roomId]) => this.tracker.rooms._getRoom(roomId)).map(([roomId, followedTopics]) => ({
|
|
1580
|
+
roomId,
|
|
1581
|
+
followedTopics: followedTopics.items
|
|
1582
|
+
}));
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
/**
|
|
1586
|
+
* Expects the rooms to be imported already - followed topics of other rooms are skipped.
|
|
1587
|
+
* @internal
|
|
1588
|
+
*/
|
|
1589
|
+
_importFollowedTopics(snapshots) {
|
|
1590
|
+
for (const {
|
|
1591
|
+
roomId,
|
|
1592
|
+
followedTopics
|
|
1593
|
+
} of snapshots) {
|
|
1594
|
+
if (this.tracker.rooms._getRoom(roomId)) {
|
|
1595
|
+
this.applyRoomFollowedTopics(roomId, followedTopics);
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
this.invalidateUnreadSummaries();
|
|
1599
|
+
this.deferredSession.resolve();
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
/**
|
|
1603
|
+
* For internal use.
|
|
1604
|
+
* @internal
|
|
1605
|
+
*/
|
|
1606
|
+
_forgetRoomsExcept(roomIds) {
|
|
1607
|
+
const keptRoomIds = new Set(roomIds);
|
|
1608
|
+
for (const roomId of Array.from(this.followedTopics.items.keys())) {
|
|
1609
|
+
if (!keptRoomIds.has(roomId)) {
|
|
1610
|
+
this.clearRoomFollowedTopicsStructures(roomId);
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1412
1614
|
handleSession(ev) {
|
|
1413
1615
|
// Keep cached followed-topic collections (they drive unread indicators
|
|
1414
1616
|
// that would otherwise blank on reconnect), but mark them stale and drop
|
|
@@ -1609,11 +1811,148 @@ class FollowedTopicsManager extends EventTarget {
|
|
|
1609
1811
|
this.invalidateUnreadSummaries(roomId);
|
|
1610
1812
|
}
|
|
1611
1813
|
}
|
|
1814
|
+
;// ./src/protocol/schemes/User.ts
|
|
1815
|
+
/**
|
|
1816
|
+
* Availability of a user, aggregated over all of their sessions.
|
|
1817
|
+
*
|
|
1818
|
+
* - `Offline` - no session is connected and none can be reached asynchronously.
|
|
1819
|
+
* - `Online` - at least one session holds a live connection.
|
|
1820
|
+
* - `OnlineAsync` - no live connection, but a push-registered device was active
|
|
1821
|
+
* recently, so a message will still reach the user.
|
|
1822
|
+
*/
|
|
1823
|
+
let UserStatus = /*#__PURE__*/function (UserStatus) {
|
|
1824
|
+
UserStatus[UserStatus["Offline"] = 0] = "Offline";
|
|
1825
|
+
UserStatus[UserStatus["Online"] = 1] = "Online";
|
|
1826
|
+
UserStatus[UserStatus["OnlineAsync"] = 2] = "OnlineAsync";
|
|
1827
|
+
return UserStatus;
|
|
1828
|
+
}({});
|
|
1829
|
+
;// ./src/state-tracker/StateSnapshot.ts
|
|
1830
|
+
|
|
1831
|
+
|
|
1832
|
+
/**
|
|
1833
|
+
* Version of the {@link StateSnapshot} format. A snapshot of any other version is rejected on import.
|
|
1834
|
+
*/
|
|
1835
|
+
const STATE_SNAPSHOT_VERSION = 1;
|
|
1836
|
+
|
|
1837
|
+
/**
|
|
1838
|
+
* Plain, JSON-serializable copy of the state tracked by {@link ChatStateTracker}, made to restore the state at the
|
|
1839
|
+
* application start (before the connection is established) - see {@link ChatStateTracker.exportSnapshot}.
|
|
1840
|
+
*
|
|
1841
|
+
* Only the data that lasts is included: the current user, the joined spaces (with roles) and rooms (with the default
|
|
1842
|
+
* topics), and the lazily loaded lists already fetched - rooms of spaces, followed topics, emoticons and relationships.
|
|
1843
|
+
* The message histories of the topics named in the {@link StateSnapshotIntent} are included too, with what their
|
|
1844
|
+
* display needs: the topics referenced by the messages and, optionally, the members of the rooms.
|
|
1845
|
+
* Left out on purpose:
|
|
1846
|
+
* - presence - every user status is saved as `Offline`, a status from the past would show people online who are not;
|
|
1847
|
+
* - members of spaces, and of rooms not asked for - large, made mostly of presence, and refetched cheaply on demand;
|
|
1848
|
+
* - other message histories and topics, histories of ephemeral rooms (they live only in the memory of the session),
|
|
1849
|
+
* permission overwrites and room previews - loaded on demand once the connection is up, as on a reconnect.
|
|
1850
|
+
*/
|
|
1851
|
+
|
|
1852
|
+
/**
|
|
1853
|
+
* What {@link ChatStateTracker.exportSnapshot} should include beyond the state that is always exported.
|
|
1854
|
+
*/
|
|
1855
|
+
|
|
1856
|
+
function withoutPresence(user) {
|
|
1857
|
+
return {
|
|
1858
|
+
...user,
|
|
1859
|
+
status: UserStatus.Offline,
|
|
1860
|
+
online: false
|
|
1861
|
+
};
|
|
1862
|
+
}
|
|
1863
|
+
function messageWithoutPresence(message) {
|
|
1864
|
+
return {
|
|
1865
|
+
...message,
|
|
1866
|
+
author: {
|
|
1867
|
+
...message.author,
|
|
1868
|
+
user: withoutPresence(message.author.user)
|
|
1869
|
+
}
|
|
1870
|
+
};
|
|
1871
|
+
}
|
|
1872
|
+
function memberWithoutPresence(member) {
|
|
1873
|
+
return {
|
|
1874
|
+
...member,
|
|
1875
|
+
user: member.user ? withoutPresence(member.user) : member.user,
|
|
1876
|
+
spaceMember: member.spaceMember ? {
|
|
1877
|
+
...member.spaceMember,
|
|
1878
|
+
user: withoutPresence(member.spaceMember.user)
|
|
1879
|
+
} : member.spaceMember
|
|
1880
|
+
};
|
|
1881
|
+
}
|
|
1882
|
+
|
|
1883
|
+
/**
|
|
1884
|
+
* Structural check of the data restored from a storage - deep enough that importing it cannot throw, as the state
|
|
1885
|
+
* handlers dereference these fields without checking them.
|
|
1886
|
+
*/
|
|
1887
|
+
function isValidStateSnapshot(value) {
|
|
1888
|
+
return isObject(value) && value.version === STATE_SNAPSHOT_VERSION && typeof value.createdAt === 'number' && isUser(value.user) && isArrayOf(value.spaces, isSpace) && isArrayOf(value.rooms, isRoom) && isArrayOf(value.spaceRooms, isSpaceRooms) && isArrayOf(value.followedTopics, isRoomFollowedTopics) && isArrayOf(value.emoticons, isEmoticons) && (value.relationships === null || isArrayOf(value.relationships, isRelationship)) && isArrayOf(value.histories, isTopicHistory) && isArrayOf(value.roomMembers, isRoomMembers);
|
|
1889
|
+
}
|
|
1890
|
+
function isObject(value) {
|
|
1891
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
1892
|
+
}
|
|
1893
|
+
function isArrayOf(value, isItem) {
|
|
1894
|
+
return Array.isArray(value) && value.every(isItem);
|
|
1895
|
+
}
|
|
1896
|
+
function isString(value) {
|
|
1897
|
+
return typeof value === 'string';
|
|
1898
|
+
}
|
|
1899
|
+
function hasId(value) {
|
|
1900
|
+
return isObject(value) && isString(value.id);
|
|
1901
|
+
}
|
|
1902
|
+
function isUser(value) {
|
|
1903
|
+
return hasId(value);
|
|
1904
|
+
}
|
|
1905
|
+
function isRole(value) {
|
|
1906
|
+
return hasId(value) && typeof value.priority === 'number';
|
|
1907
|
+
}
|
|
1908
|
+
function isSpace(value) {
|
|
1909
|
+
return hasId(value) && isArrayOf(value.roles, isRole) && isArrayOf(value.defaultRooms, isString) && (value.systemRoom === null || isString(value.systemRoom));
|
|
1910
|
+
}
|
|
1911
|
+
function isTopic(value) {
|
|
1912
|
+
return hasId(value) && (value.refMessage === undefined || value.refMessage === null || isMessage(value.refMessage));
|
|
1913
|
+
}
|
|
1914
|
+
function isMessage(value) {
|
|
1915
|
+
return hasId(value) && isObject(value.location) && isString(value.location.topicId);
|
|
1916
|
+
}
|
|
1917
|
+
function isFullMessage(value) {
|
|
1918
|
+
return hasId(value) && isObject(value.location) && isString(value.location.roomId) && isString(value.location.topicId) && isObject(value.author) && isUser(value.author.user) && Array.isArray(value.reactions);
|
|
1919
|
+
}
|
|
1920
|
+
function isRoom(value) {
|
|
1921
|
+
return hasId(value) && isString(value.type) && (value.spaceId === null || isString(value.spaceId)) && (value.defaultTopic === null || isTopic(value.defaultTopic)) && (value.recipients === null || isArrayOf(value.recipients, isUser)) && isObject(value.history) && isString(value.history.mode);
|
|
1922
|
+
}
|
|
1923
|
+
function isSpaceRooms(value) {
|
|
1924
|
+
return isObject(value) && isString(value.spaceId) && isArrayOf(value.summaries, hasId);
|
|
1925
|
+
}
|
|
1926
|
+
function isFollowedTopic(value) {
|
|
1927
|
+
return isObject(value) && isObject(value.location) && isString(value.location.roomId) && isString(value.location.topicId);
|
|
1928
|
+
}
|
|
1929
|
+
function isRoomFollowedTopics(value) {
|
|
1930
|
+
return isObject(value) && isString(value.roomId) && isArrayOf(value.followedTopics, isFollowedTopic);
|
|
1931
|
+
}
|
|
1932
|
+
function isEmoticons(value) {
|
|
1933
|
+
return isObject(value) && (value.spaceId === null || isString(value.spaceId)) && isArrayOf(value.emoticons, hasId);
|
|
1934
|
+
}
|
|
1935
|
+
function isRelationship(value) {
|
|
1936
|
+
return isObject(value) && isUser(value.refUser) && isString(value.type);
|
|
1937
|
+
}
|
|
1938
|
+
function isUserReactions(value) {
|
|
1939
|
+
return isObject(value) && Object.values(value).every(reactions => isArrayOf(reactions, isObject));
|
|
1940
|
+
}
|
|
1941
|
+
function isTopicHistory(value) {
|
|
1942
|
+
return isObject(value) && isString(value.roomId) && isString(value.topicId) && isArrayOf(value.messages, isFullMessage) && isUserReactions(value.myReactions) && isArrayOf(value.topics, isTopic);
|
|
1943
|
+
}
|
|
1944
|
+
function isRoomMember(value) {
|
|
1945
|
+
return isObject(value) && (isUser(value.user) || isObject(value.spaceMember) && isUser(value.spaceMember.user));
|
|
1946
|
+
}
|
|
1947
|
+
function isRoomMembers(value) {
|
|
1948
|
+
return isObject(value) && isString(value.roomId) && isArrayOf(value.members, isRoomMember);
|
|
1949
|
+
}
|
|
1612
1950
|
;// ./src/state-tracker/RoomsManager.ts
|
|
1613
1951
|
|
|
1614
1952
|
|
|
1615
1953
|
|
|
1616
1954
|
|
|
1955
|
+
|
|
1617
1956
|
class RoomsManager {
|
|
1618
1957
|
messages;
|
|
1619
1958
|
followedTopics;
|
|
@@ -1623,6 +1962,7 @@ class RoomsManager {
|
|
|
1623
1962
|
deferredSession = new DeferredTask();
|
|
1624
1963
|
membersPromises = new PromiseRegistry();
|
|
1625
1964
|
topicsPromises = new PromiseRegistry();
|
|
1965
|
+
importedTopicIds = new Map();
|
|
1626
1966
|
constructor(tracker) {
|
|
1627
1967
|
this.tracker = tracker;
|
|
1628
1968
|
this.messages = new MessagesManager(tracker);
|
|
@@ -1727,6 +2067,148 @@ class RoomsManager {
|
|
|
1727
2067
|
_deleteTopicsFromRoom(roomId, ...topicIds) {
|
|
1728
2068
|
this.topics.get(roomId)?.delete(...topicIds);
|
|
1729
2069
|
}
|
|
2070
|
+
|
|
2071
|
+
/**
|
|
2072
|
+
* For internal use.
|
|
2073
|
+
* @internal
|
|
2074
|
+
*/
|
|
2075
|
+
_exportRooms() {
|
|
2076
|
+
return this.list.items.map(room => room.recipients ? {
|
|
2077
|
+
...room,
|
|
2078
|
+
recipients: room.recipients.map(withoutPresence)
|
|
2079
|
+
} : room);
|
|
2080
|
+
}
|
|
2081
|
+
|
|
2082
|
+
/**
|
|
2083
|
+
* For internal use.
|
|
2084
|
+
* @internal
|
|
2085
|
+
*/
|
|
2086
|
+
_importRooms(rooms) {
|
|
2087
|
+
this.messages._importRooms(rooms);
|
|
2088
|
+
this.addJoinedRooms(...rooms);
|
|
2089
|
+
this.deferredSession.resolve();
|
|
2090
|
+
}
|
|
2091
|
+
|
|
2092
|
+
/**
|
|
2093
|
+
* For internal use.
|
|
2094
|
+
* @internal
|
|
2095
|
+
*/
|
|
2096
|
+
_exportHistories(intent) {
|
|
2097
|
+
const histories = [];
|
|
2098
|
+
const exported = new Set();
|
|
2099
|
+
for (const {
|
|
2100
|
+
roomId,
|
|
2101
|
+
topicId: requestedTopicId
|
|
2102
|
+
} of intent.histories ?? []) {
|
|
2103
|
+
const room = this.list.get(roomId);
|
|
2104
|
+
const topicId = requestedTopicId ?? room?.defaultTopic?.id;
|
|
2105
|
+
if (!room || !topicId || exported.has(`${roomId}/${topicId}`)) {
|
|
2106
|
+
continue;
|
|
2107
|
+
}
|
|
2108
|
+
const latest = this.messages._exportLatest(roomId, topicId, intent.historyLimit ?? 0);
|
|
2109
|
+
const topics = this.topics.get(roomId);
|
|
2110
|
+
|
|
2111
|
+
// A topic other than the default one is restored from the snapshot, so it has to be known.
|
|
2112
|
+
if (!latest || topicId !== room.defaultTopic?.id && !topics?.has(topicId)) {
|
|
2113
|
+
continue;
|
|
2114
|
+
}
|
|
2115
|
+
const topicIds = new Set([topicId, ...latest.messages.map(message => message.topicRef)]);
|
|
2116
|
+
topicIds.delete(room.defaultTopic?.id);
|
|
2117
|
+
exported.add(`${roomId}/${topicId}`);
|
|
2118
|
+
histories.push({
|
|
2119
|
+
roomId,
|
|
2120
|
+
topicId,
|
|
2121
|
+
messages: latest.messages.map(messageWithoutPresence),
|
|
2122
|
+
myReactions: latest.myReactions,
|
|
2123
|
+
topics: Array.from(topicIds).filter(id => topics?.has(id)).map(id => topics.get(id))
|
|
2124
|
+
});
|
|
2125
|
+
}
|
|
2126
|
+
return histories;
|
|
2127
|
+
}
|
|
2128
|
+
|
|
2129
|
+
/**
|
|
2130
|
+
* Members of private conversations are not exported - they are made of the recipients of the room.
|
|
2131
|
+
* @internal
|
|
2132
|
+
*/
|
|
2133
|
+
_exportMembers(roomIds, limit) {
|
|
2134
|
+
const snapshots = [];
|
|
2135
|
+
if (!limit) {
|
|
2136
|
+
return snapshots;
|
|
2137
|
+
}
|
|
2138
|
+
for (const roomId of new Set(roomIds)) {
|
|
2139
|
+
const members = this.members.get(roomId);
|
|
2140
|
+
if (members && members.length <= limit && this.list.get(roomId)?.type !== 'Pm') {
|
|
2141
|
+
snapshots.push({
|
|
2142
|
+
roomId,
|
|
2143
|
+
members: members.items.map(memberWithoutPresence)
|
|
2144
|
+
});
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
return snapshots;
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2150
|
+
/**
|
|
2151
|
+
* Expects the rooms to be imported already - histories and members of other rooms are skipped.
|
|
2152
|
+
* @internal
|
|
2153
|
+
*/
|
|
2154
|
+
_importHistories(histories, roomMembers) {
|
|
2155
|
+
for (const {
|
|
2156
|
+
roomId,
|
|
2157
|
+
members
|
|
2158
|
+
} of roomMembers) {
|
|
2159
|
+
if (this.list.has(roomId) && this.list.get(roomId).type !== 'Pm') {
|
|
2160
|
+
this.handleRoomMembers({
|
|
2161
|
+
id: roomId,
|
|
2162
|
+
members
|
|
2163
|
+
});
|
|
2164
|
+
// Served from the snapshot until the session invalidates it.
|
|
2165
|
+
this.membersPromises.register(Promise.resolve(), roomId);
|
|
2166
|
+
}
|
|
2167
|
+
}
|
|
2168
|
+
for (const history of histories) {
|
|
2169
|
+
const room = this.list.get(history.roomId);
|
|
2170
|
+
if (!room) {
|
|
2171
|
+
continue;
|
|
2172
|
+
}
|
|
2173
|
+
const topics = history.topics.filter(topic => topic.id !== room.defaultTopic?.id);
|
|
2174
|
+
if (topics.length) {
|
|
2175
|
+
this.addJoinedRoomTopics(room.id, ...topics);
|
|
2176
|
+
const importedTopicIds = this.importedTopicIds.get(room.id) ?? new Set();
|
|
2177
|
+
topics.forEach(topic => importedTopicIds.add(topic.id));
|
|
2178
|
+
this.importedTopicIds.set(room.id, importedTopicIds);
|
|
2179
|
+
}
|
|
2180
|
+
const topic = history.topicId === room.defaultTopic?.id ? room.defaultTopic : this.topics.get(room.id)?.get(history.topicId);
|
|
2181
|
+
if (topic) {
|
|
2182
|
+
this.messages._importHistory(room.id, topic, history);
|
|
2183
|
+
}
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
|
|
2187
|
+
/**
|
|
2188
|
+
* The session carries only the default topics, so the other restored ones are fetched anew.
|
|
2189
|
+
* @internal
|
|
2190
|
+
*/
|
|
2191
|
+
_refreshImportedTopics() {
|
|
2192
|
+
const importedTopicIds = Array.from(this.importedTopicIds);
|
|
2193
|
+
this.importedTopicIds.clear();
|
|
2194
|
+
for (const [roomId, ids] of importedTopicIds) {
|
|
2195
|
+
if (!this.list.has(roomId)) {
|
|
2196
|
+
continue;
|
|
2197
|
+
}
|
|
2198
|
+
const topicIds = Array.from(ids);
|
|
2199
|
+
const promise = this.tracker.client.send('GetTopics', {
|
|
2200
|
+
roomId,
|
|
2201
|
+
topicIds
|
|
2202
|
+
}).then(result => {
|
|
2203
|
+
if (!result.error) {
|
|
2204
|
+
this.topics.get(roomId)?.set(...result.data.topics);
|
|
2205
|
+
}
|
|
2206
|
+
});
|
|
2207
|
+
topicIds.forEach(topicId => this.topicsPromises.register(promise, roomId + topicId));
|
|
2208
|
+
// Best effort: on a failure the restored topics stay, as after a reconnect.
|
|
2209
|
+
promise.catch(() => undefined);
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
1730
2212
|
deleteRoom(...roomIds) {
|
|
1731
2213
|
this.list.delete(...roomIds);
|
|
1732
2214
|
this.members.delete(...roomIds);
|
|
@@ -2123,6 +2605,47 @@ class SpacesManager {
|
|
|
2123
2605
|
const members = await this.getMembers(spaceId);
|
|
2124
2606
|
return members?.items.find(member => member.user.id === userId);
|
|
2125
2607
|
}
|
|
2608
|
+
|
|
2609
|
+
/**
|
|
2610
|
+
* For internal use.
|
|
2611
|
+
* @internal
|
|
2612
|
+
*/
|
|
2613
|
+
_exportSpaces() {
|
|
2614
|
+
return this.list.items;
|
|
2615
|
+
}
|
|
2616
|
+
|
|
2617
|
+
/**
|
|
2618
|
+
* Room listings of the joined spaces only - of other spaces they are fetched for a moment's look.
|
|
2619
|
+
* @internal
|
|
2620
|
+
*/
|
|
2621
|
+
_exportSpaceRooms() {
|
|
2622
|
+
return Array.from(this.rooms.items).filter(([spaceId]) => this.list.has(spaceId)).map(([spaceId, summaries]) => ({
|
|
2623
|
+
spaceId,
|
|
2624
|
+
summaries: summaries.items
|
|
2625
|
+
}));
|
|
2626
|
+
}
|
|
2627
|
+
|
|
2628
|
+
/**
|
|
2629
|
+
* For internal use.
|
|
2630
|
+
* @internal
|
|
2631
|
+
*/
|
|
2632
|
+
_importSpaces(spaces, spaceRooms) {
|
|
2633
|
+
this.addJoinedSpaces(...spaces);
|
|
2634
|
+
for (const {
|
|
2635
|
+
spaceId,
|
|
2636
|
+
summaries
|
|
2637
|
+
} of spaceRooms) {
|
|
2638
|
+
if (this.list.has(spaceId)) {
|
|
2639
|
+
this.handleSpaceRooms({
|
|
2640
|
+
id: spaceId,
|
|
2641
|
+
summaries
|
|
2642
|
+
});
|
|
2643
|
+
// Served from the snapshot until the session invalidates it.
|
|
2644
|
+
this.roomsPromises.register(Promise.resolve(), spaceId);
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
this.deferredSession.resolve();
|
|
2648
|
+
}
|
|
2126
2649
|
handleNewRole(ev) {
|
|
2127
2650
|
this.roles.get(ev.spaceId).set(ev.role);
|
|
2128
2651
|
this.syncSpaceRoles(ev.spaceId);
|
|
@@ -2415,6 +2938,14 @@ class Permissions {
|
|
|
2415
2938
|
MentionAllRoles: {
|
|
2416
2939
|
value: 1 << 23,
|
|
2417
2940
|
maxLayer: Layer.Topic
|
|
2941
|
+
},
|
|
2942
|
+
PreviewRooms: {
|
|
2943
|
+
value: 1 << 24,
|
|
2944
|
+
maxLayer: Layer.Room
|
|
2945
|
+
},
|
|
2946
|
+
SearchMessages: {
|
|
2947
|
+
value: 1 << 25,
|
|
2948
|
+
maxLayer: Layer.Room
|
|
2418
2949
|
}
|
|
2419
2950
|
};
|
|
2420
2951
|
static getNames() {
|
|
@@ -2492,6 +3023,10 @@ class PermissionsManager extends EventTarget {
|
|
|
2492
3023
|
missing
|
|
2493
3024
|
};
|
|
2494
3025
|
}
|
|
3026
|
+
|
|
3027
|
+
/**
|
|
3028
|
+
* For a room or a space you have not joined the permissions are fetched from the server, on every call.
|
|
3029
|
+
*/
|
|
2495
3030
|
async calculatePermissions(location) {
|
|
2496
3031
|
const room = location.roomId ? (await this.tracker.rooms.get()).get(location.roomId) : null;
|
|
2497
3032
|
location = {
|
|
@@ -2500,6 +3035,9 @@ class PermissionsManager extends EventTarget {
|
|
|
2500
3035
|
topicId: location.topicId
|
|
2501
3036
|
};
|
|
2502
3037
|
this.validateLocation(location);
|
|
3038
|
+
if (location.roomId && !room || location.spaceId && !(await this.tracker.spaces.get())?.has(location.spaceId)) {
|
|
3039
|
+
return this.fetchComputedPermissions(location);
|
|
3040
|
+
}
|
|
2503
3041
|
const userId = (await this.tracker.getMe()).id;
|
|
2504
3042
|
const [spaceMember, roomMember] = await this.fetchMembersOrFail(location);
|
|
2505
3043
|
const userRoles = [...(spaceMember?.roles ?? []), ...(roomMember?.roles ?? [])];
|
|
@@ -2665,6 +3203,15 @@ class PermissionsManager extends EventTarget {
|
|
|
2665
3203
|
}
|
|
2666
3204
|
return result;
|
|
2667
3205
|
}
|
|
3206
|
+
async fetchComputedPermissions(location) {
|
|
3207
|
+
const result = await this.tracker.client.send('GetComputedPermissions', {
|
|
3208
|
+
location
|
|
3209
|
+
});
|
|
3210
|
+
if (result.error) {
|
|
3211
|
+
throw result.error;
|
|
3212
|
+
}
|
|
3213
|
+
return result.data.permissions;
|
|
3214
|
+
}
|
|
2668
3215
|
async fetchMembersOrFail(location) {
|
|
2669
3216
|
const results = await Promise.all([location.spaceId ? this.tracker.spaces.getMe(location.spaceId) : null, location.roomId ? this.tracker.rooms.getMe(location.roomId) : null]);
|
|
2670
3217
|
const spaceFail = location.spaceId && !results[0];
|
|
@@ -2720,6 +3267,52 @@ class EmoticonsManager {
|
|
|
2720
3267
|
await this.emoticonsPromises.get(key);
|
|
2721
3268
|
return this.list.get(key);
|
|
2722
3269
|
}
|
|
3270
|
+
|
|
3271
|
+
/**
|
|
3272
|
+
* For internal use.
|
|
3273
|
+
* @internal
|
|
3274
|
+
*/
|
|
3275
|
+
_exportEmoticons() {
|
|
3276
|
+
return Array.from(this.list.items).map(([key, emoticons]) => ({
|
|
3277
|
+
spaceId: key === GLOBAL_KEY ? null : key,
|
|
3278
|
+
emoticons: emoticons.items
|
|
3279
|
+
}));
|
|
3280
|
+
}
|
|
3281
|
+
|
|
3282
|
+
/**
|
|
3283
|
+
* Emoticons of spaces other than the given ones are skipped.
|
|
3284
|
+
* @internal
|
|
3285
|
+
*/
|
|
3286
|
+
_importEmoticons(snapshots, spaceIds) {
|
|
3287
|
+
const joinedSpaceIds = new Set(spaceIds);
|
|
3288
|
+
for (const {
|
|
3289
|
+
spaceId,
|
|
3290
|
+
emoticons
|
|
3291
|
+
} of snapshots) {
|
|
3292
|
+
if (spaceId !== null && !joinedSpaceIds.has(spaceId)) {
|
|
3293
|
+
continue;
|
|
3294
|
+
}
|
|
3295
|
+
this.handleEmoticons({
|
|
3296
|
+
location: {
|
|
3297
|
+
spaceId: spaceId ?? undefined
|
|
3298
|
+
},
|
|
3299
|
+
emoticons
|
|
3300
|
+
});
|
|
3301
|
+
// Served from the snapshot until the session invalidates it.
|
|
3302
|
+
this.emoticonsPromises.register(Promise.resolve(), spaceId ?? GLOBAL_KEY);
|
|
3303
|
+
}
|
|
3304
|
+
}
|
|
3305
|
+
|
|
3306
|
+
/**
|
|
3307
|
+
* For internal use.
|
|
3308
|
+
* @internal
|
|
3309
|
+
*/
|
|
3310
|
+
_forgetSpacesExcept(spaceIds) {
|
|
3311
|
+
const keptKeys = new Set([GLOBAL_KEY, ...spaceIds]);
|
|
3312
|
+
const removedKeys = Array.from(this.list.items.keys()).filter(key => !keptKeys.has(key));
|
|
3313
|
+
this.list.delete(...removedKeys);
|
|
3314
|
+
this.emoticonsPromises.forget(...removedKeys);
|
|
3315
|
+
}
|
|
2723
3316
|
handleEmoticons(event) {
|
|
2724
3317
|
const spaceId = event.location.spaceId ?? GLOBAL_KEY;
|
|
2725
3318
|
if (!this.list.has(spaceId)) {
|
|
@@ -2775,6 +3368,14 @@ class UsersManager {
|
|
|
2775
3368
|
async getAvailable() {
|
|
2776
3369
|
return this.users;
|
|
2777
3370
|
}
|
|
3371
|
+
|
|
3372
|
+
/**
|
|
3373
|
+
* For internal use.
|
|
3374
|
+
* @internal
|
|
3375
|
+
*/
|
|
3376
|
+
_importUser(user) {
|
|
3377
|
+
this.handleUsers([user]);
|
|
3378
|
+
}
|
|
2778
3379
|
handleMembers(members) {
|
|
2779
3380
|
this.handleUsers(members.map(extractUserFromMember));
|
|
2780
3381
|
}
|
|
@@ -2821,11 +3422,13 @@ function isSameUser(a, b) {
|
|
|
2821
3422
|
;// ./src/state-tracker/RelationshipsManager.ts
|
|
2822
3423
|
|
|
2823
3424
|
|
|
3425
|
+
|
|
2824
3426
|
const getId = (refUserId, type) => `${refUserId}-${type}`;
|
|
2825
3427
|
const getIdFromRelationship = relationship => getId(relationship.refUser.id, relationship.type);
|
|
2826
3428
|
class RelationshipsManager {
|
|
2827
3429
|
relationships = new ObservableIndexedObjectCollection(getIdFromRelationship);
|
|
2828
3430
|
promises = new PromiseRegistry();
|
|
3431
|
+
loaded = false;
|
|
2829
3432
|
constructor(tracker) {
|
|
2830
3433
|
this.tracker = tracker;
|
|
2831
3434
|
this.tracker.client.on('Relationships', ev => this.handleRelationships(ev));
|
|
@@ -2849,7 +3452,33 @@ class RelationshipsManager {
|
|
|
2849
3452
|
await this.get();
|
|
2850
3453
|
return this.relationships.has(getId(refUserId, type));
|
|
2851
3454
|
}
|
|
3455
|
+
|
|
3456
|
+
/**
|
|
3457
|
+
* For internal use.
|
|
3458
|
+
* @internal
|
|
3459
|
+
*/
|
|
3460
|
+
_exportRelationships() {
|
|
3461
|
+
return this.loaded ? this.relationships.items.map(relationship => ({
|
|
3462
|
+
...relationship,
|
|
3463
|
+
refUser: withoutPresence(relationship.refUser)
|
|
3464
|
+
})) : null;
|
|
3465
|
+
}
|
|
3466
|
+
|
|
3467
|
+
/**
|
|
3468
|
+
* For internal use.
|
|
3469
|
+
* @internal
|
|
3470
|
+
*/
|
|
3471
|
+
_importRelationships(relationships) {
|
|
3472
|
+
if (relationships) {
|
|
3473
|
+
this.handleRelationships({
|
|
3474
|
+
relationships
|
|
3475
|
+
});
|
|
3476
|
+
// Served from the snapshot until the session invalidates it.
|
|
3477
|
+
this.promises.register(Promise.resolve(), 'all');
|
|
3478
|
+
}
|
|
3479
|
+
}
|
|
2852
3480
|
handleRelationships(ev) {
|
|
3481
|
+
this.loaded = true;
|
|
2853
3482
|
// Full list from the server: reconcile in place so a reconnect refetch
|
|
2854
3483
|
// updates a bound (visible) list without an intermediate empty state.
|
|
2855
3484
|
this.relationships.reconcile(...ev.relationships);
|
|
@@ -2878,6 +3507,7 @@ class RelationshipsManager {
|
|
|
2878
3507
|
|
|
2879
3508
|
|
|
2880
3509
|
|
|
3510
|
+
|
|
2881
3511
|
class ChatStateTracker {
|
|
2882
3512
|
client;
|
|
2883
3513
|
|
|
@@ -2912,6 +3542,8 @@ class ChatStateTracker {
|
|
|
2912
3542
|
relationships;
|
|
2913
3543
|
_me = null;
|
|
2914
3544
|
deferredSession = new DeferredTask();
|
|
3545
|
+
sessionReceived = false;
|
|
3546
|
+
snapshotImported = false;
|
|
2915
3547
|
constructor(client) {
|
|
2916
3548
|
this.client = client;
|
|
2917
3549
|
this.client.on('Session', ev => this.handleSession(ev));
|
|
@@ -2921,6 +3553,9 @@ class ChatStateTracker {
|
|
|
2921
3553
|
this.emoticons = new EmoticonsManager(this);
|
|
2922
3554
|
this.users = new UsersManager(this);
|
|
2923
3555
|
this.relationships = new RelationshipsManager(this);
|
|
3556
|
+
|
|
3557
|
+
// Registered after the managers, so it runs once they have applied the session.
|
|
3558
|
+
this.client.on('Session', ev => this.handleSessionAfterImport(ev));
|
|
2924
3559
|
}
|
|
2925
3560
|
get me() {
|
|
2926
3561
|
return this._me;
|
|
@@ -2929,10 +3564,88 @@ class ChatStateTracker {
|
|
|
2929
3564
|
await this.deferredSession.promise;
|
|
2930
3565
|
return this._me;
|
|
2931
3566
|
}
|
|
3567
|
+
|
|
3568
|
+
/**
|
|
3569
|
+
* Export the tracked state to a plain, JSON-serializable object, to be saved and restored with
|
|
3570
|
+
* {@link importSnapshot} at the next start of the application, before the connection is established.
|
|
3571
|
+
* See {@link StateSnapshot} for what is included.
|
|
3572
|
+
* @param intent What to include beyond the state that is always exported, e.g. the message histories.
|
|
3573
|
+
* @return Null if there is no state yet (no session has been received or imported).
|
|
3574
|
+
*/
|
|
3575
|
+
exportSnapshot(intent = {}) {
|
|
3576
|
+
if (!this._me) {
|
|
3577
|
+
return null;
|
|
3578
|
+
}
|
|
3579
|
+
const snapshot = {
|
|
3580
|
+
version: STATE_SNAPSHOT_VERSION,
|
|
3581
|
+
createdAt: Date.now(),
|
|
3582
|
+
user: withoutPresence(this._me),
|
|
3583
|
+
spaces: this.spaces._exportSpaces(),
|
|
3584
|
+
rooms: this.rooms._exportRooms(),
|
|
3585
|
+
spaceRooms: this.spaces._exportSpaceRooms(),
|
|
3586
|
+
followedTopics: this.rooms.followedTopics._exportFollowedTopics(),
|
|
3587
|
+
emoticons: this.emoticons._exportEmoticons(),
|
|
3588
|
+
relationships: this.relationships._exportRelationships(),
|
|
3589
|
+
histories: this.rooms._exportHistories(intent),
|
|
3590
|
+
roomMembers: this.rooms._exportMembers((intent.histories ?? []).map(history => history.roomId), intent.roomMembersLimit ?? 0)
|
|
3591
|
+
};
|
|
3592
|
+
|
|
3593
|
+
// Detaches the snapshot from the live state objects and leaves nothing a JSON round trip would change.
|
|
3594
|
+
return JSON.parse(JSON.stringify(snapshot));
|
|
3595
|
+
}
|
|
3596
|
+
|
|
3597
|
+
/**
|
|
3598
|
+
* Restore the state exported with {@link exportSnapshot}, so it is available before the connection is
|
|
3599
|
+
* established. The collections emit their usual change events and the getters waiting for the session resolve.
|
|
3600
|
+
* The first `Session` from the server then replaces the restored state the same way it does on a reconnect:
|
|
3601
|
+
* removed spaces and rooms are dropped, the rest is updated in place, the restored message histories and topics
|
|
3602
|
+
* other than the default ones are fetched anew (as a window holding the latest messages is on a reconnect), and
|
|
3603
|
+
* the lazily loaded lists are refetched on their next access.
|
|
3604
|
+
*
|
|
3605
|
+
* Allowed only once and only before the first `Session`.
|
|
3606
|
+
* @return False if the snapshot was rejected (another version, malformed data, or a session already received) -
|
|
3607
|
+
* the state is not changed then.
|
|
3608
|
+
*/
|
|
3609
|
+
importSnapshot(snapshot) {
|
|
3610
|
+
if (this.sessionReceived || this.snapshotImported || !isValidStateSnapshot(snapshot)) {
|
|
3611
|
+
return false;
|
|
3612
|
+
}
|
|
3613
|
+
this.snapshotImported = true;
|
|
3614
|
+
try {
|
|
3615
|
+
this._me = snapshot.user;
|
|
3616
|
+
this.users._importUser(snapshot.user);
|
|
3617
|
+
this.rooms._importRooms(snapshot.rooms);
|
|
3618
|
+
this.spaces._importSpaces(snapshot.spaces, snapshot.spaceRooms);
|
|
3619
|
+
this.rooms.followedTopics._importFollowedTopics(snapshot.followedTopics);
|
|
3620
|
+
this.emoticons._importEmoticons(snapshot.emoticons, snapshot.spaces.map(space => space.id));
|
|
3621
|
+
this.relationships._importRelationships(snapshot.relationships);
|
|
3622
|
+
this.rooms._importHistories(snapshot.histories, snapshot.roomMembers);
|
|
3623
|
+
} catch {
|
|
3624
|
+
// Not expected after the validation; whatever got restored is replaced by the first session anyway.
|
|
3625
|
+
return false;
|
|
3626
|
+
}
|
|
3627
|
+
this.deferredSession.resolve();
|
|
3628
|
+
return true;
|
|
3629
|
+
}
|
|
2932
3630
|
handleSession(ev) {
|
|
2933
3631
|
this._me = ev.user;
|
|
3632
|
+
this.sessionReceived = true;
|
|
2934
3633
|
this.deferredSession.resolve();
|
|
2935
3634
|
}
|
|
3635
|
+
|
|
3636
|
+
/**
|
|
3637
|
+
* A reconnect keeps the followed topics and emoticons of rooms and spaces that are gone; for the restored ones
|
|
3638
|
+
* the first session is the only chance to tell they are gone.
|
|
3639
|
+
*/
|
|
3640
|
+
handleSessionAfterImport(ev) {
|
|
3641
|
+
if (!this.snapshotImported) {
|
|
3642
|
+
return;
|
|
3643
|
+
}
|
|
3644
|
+
this.snapshotImported = false;
|
|
3645
|
+
this.rooms.followedTopics._forgetRoomsExcept(ev.state.rooms.map(room => room.id));
|
|
3646
|
+
this.emoticons._forgetSpacesExcept(ev.state.spaces.map(space => space.id));
|
|
3647
|
+
this.rooms._refreshImportedTopics();
|
|
3648
|
+
}
|
|
2936
3649
|
}
|
|
2937
3650
|
;// ./src/WebSocketChatClient.ts
|
|
2938
3651
|
|
|
@@ -3407,20 +4120,17 @@ class FilesClient extends AbstractRestClient {
|
|
|
3407
4120
|
return this.send('GET', 'files?' + searchParams);
|
|
3408
4121
|
}
|
|
3409
4122
|
}
|
|
3410
|
-
;// ./src/protocol/schemes/
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
UserStatus[UserStatus["Online"] = 1] = "Online";
|
|
3422
|
-
UserStatus[UserStatus["OnlineAsync"] = 2] = "OnlineAsync";
|
|
3423
|
-
return UserStatus;
|
|
4123
|
+
;// ./src/protocol/schemes/events/Error.ts
|
|
4124
|
+
let ErrorCode = /*#__PURE__*/function (ErrorCode) {
|
|
4125
|
+
ErrorCode["Authentication"] = "AuthenticationException";
|
|
4126
|
+
ErrorCode["CommandRateLimitExceeded"] = "CommandRateLimitExceededException";
|
|
4127
|
+
ErrorCode["InvalidSearchQuery"] = "InvalidSearchQueryException";
|
|
4128
|
+
ErrorCode["MessageSearchUnavailable"] = "MessageSearchUnavailableException";
|
|
4129
|
+
ErrorCode["MessageNotFound"] = "MessageNotFoundException";
|
|
4130
|
+
ErrorCode["RoomNotFound"] = "RoomNotFoundException";
|
|
4131
|
+
ErrorCode["TopicNotFound"] = "TopicNotFoundException";
|
|
4132
|
+
ErrorCode["UserNotFound"] = "UserNotFoundException";
|
|
4133
|
+
return ErrorCode;
|
|
3424
4134
|
}({});
|
|
3425
4135
|
;// ./src/protocol/schemes/Role.ts
|
|
3426
4136
|
/**
|
|
@@ -3553,6 +4263,7 @@ let SpaceIntent = /*#__PURE__*/function (SpaceIntent) {
|
|
|
3553
4263
|
|
|
3554
4264
|
|
|
3555
4265
|
|
|
4266
|
+
|
|
3556
4267
|
module.exports = __webpack_exports__;
|
|
3557
4268
|
/******/ })()
|
|
3558
4269
|
;
|