@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.mjs
CHANGED
|
@@ -548,7 +548,17 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
548
548
|
this.emitChangeWithDiff(true, originalState);
|
|
549
549
|
}
|
|
550
550
|
async fetchNext() {
|
|
551
|
-
if (this.
|
|
551
|
+
if (this.hasLatest) {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
return this.fetchAfterLast();
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Append the items that follow the last one in the window; an empty window gets the latest items.
|
|
559
|
+
*/
|
|
560
|
+
async fetchAfterLast() {
|
|
561
|
+
if (this.internalState.ongoing) {
|
|
552
562
|
return;
|
|
553
563
|
}
|
|
554
564
|
let result;
|
|
@@ -561,7 +571,7 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
561
571
|
this.internalState.ongoing = undefined;
|
|
562
572
|
}
|
|
563
573
|
if (!result) {
|
|
564
|
-
await this.resetToLatest();
|
|
574
|
+
await this.resetToLatest(true);
|
|
565
575
|
return;
|
|
566
576
|
}
|
|
567
577
|
if (result.length) {
|
|
@@ -671,23 +681,31 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
671
681
|
/**
|
|
672
682
|
* The window does not subscribe to the client itself - the messages manager routes the events of its topic to it.
|
|
673
683
|
*/
|
|
674
|
-
constructor(roomId, topicId, tracker) {
|
|
684
|
+
constructor(roomId, topicId, tracker, preview = false) {
|
|
675
685
|
super('id');
|
|
676
686
|
this.roomId = roomId;
|
|
677
687
|
this.topicId = topicId;
|
|
678
688
|
this.tracker = tracker;
|
|
689
|
+
this.preview = preview;
|
|
679
690
|
this.internalState.traverseLock = false;
|
|
680
|
-
this.internalState.includeMyReactions =
|
|
691
|
+
this.internalState.includeMyReactions = !preview;
|
|
681
692
|
this.internalState.myReactions = {};
|
|
682
693
|
this.internalState.liveLimit = 50;
|
|
683
694
|
}
|
|
684
695
|
createMirror() {
|
|
685
|
-
const copy = new TopicHistoryWindow(this.roomId, this.topicId, this.tracker);
|
|
696
|
+
const copy = new TopicHistoryWindow(this.roomId, this.topicId, this.tracker, this.preview);
|
|
686
697
|
copy.eventTarget = this.eventTarget;
|
|
687
698
|
copy._items = this._items;
|
|
688
699
|
copy.internalState = this.internalState;
|
|
689
700
|
return copy;
|
|
690
701
|
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* Window of a room you have not joined. It gets no live messages - at the latest ones fetchNext() fetches the newer.
|
|
705
|
+
*/
|
|
706
|
+
get isPreview() {
|
|
707
|
+
return this.preview;
|
|
708
|
+
}
|
|
691
709
|
get isTraverseLocked() {
|
|
692
710
|
return this.internalState.traverseLock;
|
|
693
711
|
}
|
|
@@ -738,6 +756,9 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
738
756
|
if (this.internalState.traverseLock) {
|
|
739
757
|
return;
|
|
740
758
|
}
|
|
759
|
+
if (this.preview && this.hasLatest) {
|
|
760
|
+
return this.fetchAfterLast(); // No events reach a preview
|
|
761
|
+
}
|
|
741
762
|
return super.fetchNext();
|
|
742
763
|
}
|
|
743
764
|
async fetchPrevious() {
|
|
@@ -753,6 +774,43 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
753
774
|
return super.jumpTo(id);
|
|
754
775
|
}
|
|
755
776
|
|
|
777
|
+
/**
|
|
778
|
+
* The latest messages (oldest first) with the reactions of the user, or null if the window does not hold them.
|
|
779
|
+
* @internal
|
|
780
|
+
*/
|
|
781
|
+
_exportLatest(limit) {
|
|
782
|
+
if (this.state !== WindowState.LATEST) {
|
|
783
|
+
return null;
|
|
784
|
+
}
|
|
785
|
+
const messages = this.items.slice(-Math.max(limit, this.fetchLimit));
|
|
786
|
+
const myReactions = {};
|
|
787
|
+
for (const message of messages) {
|
|
788
|
+
if (this.internalState.myReactions[message.id]) {
|
|
789
|
+
myReactions[message.id] = this.internalState.myReactions[message.id];
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
return {
|
|
793
|
+
messages,
|
|
794
|
+
myReactions
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* Fill the window with saved latest messages, as if they were just fetched; the next reset to the latest replaces
|
|
800
|
+
* them.
|
|
801
|
+
* @internal
|
|
802
|
+
*/
|
|
803
|
+
_restoreLatest(messages, myReactions) {
|
|
804
|
+
const originalState = this.state;
|
|
805
|
+
this._items.deleteAll(); // Directly call deleteAll to prevent event emit.
|
|
806
|
+
this.addItems(messages, 'tail');
|
|
807
|
+
this.internalState.myReactions = {
|
|
808
|
+
...myReactions
|
|
809
|
+
};
|
|
810
|
+
this.internalState.current = WindowState.LATEST;
|
|
811
|
+
this.emitChangeWithDiff(true, originalState);
|
|
812
|
+
}
|
|
813
|
+
|
|
756
814
|
/**
|
|
757
815
|
* For internal use.
|
|
758
816
|
* @internal
|
|
@@ -830,6 +888,17 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
830
888
|
this.internalState.myReactions = next;
|
|
831
889
|
}
|
|
832
890
|
async getTopic() {
|
|
891
|
+
if (this.preview) {
|
|
892
|
+
// The tracker holds the topics of the joined rooms only
|
|
893
|
+
const result = await this.tracker.client.send('GetTopics', {
|
|
894
|
+
roomId: this.roomId,
|
|
895
|
+
topicIds: [this.topicId]
|
|
896
|
+
});
|
|
897
|
+
if (result.error) {
|
|
898
|
+
throw new Error(`Cannot fetch topic: ${result.error.message}`);
|
|
899
|
+
}
|
|
900
|
+
return result.data.topics.find(topic => topic.id === this.topicId);
|
|
901
|
+
}
|
|
833
902
|
return (await this.tracker.rooms.getTopics(this.roomId, [this.topicId])).get(this.topicId);
|
|
834
903
|
}
|
|
835
904
|
async getLatestMessageId() {
|
|
@@ -944,13 +1013,20 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
944
1013
|
* dropped together with its room stops receiving them.
|
|
945
1014
|
*/
|
|
946
1015
|
class RoomMessagesHistory {
|
|
1016
|
+
isPreview;
|
|
1017
|
+
roomId;
|
|
947
1018
|
historyWindows = new IndexedCollection();
|
|
948
1019
|
traverseLock = false;
|
|
1020
|
+
room;
|
|
949
1021
|
constructor(room, tracker) {
|
|
950
|
-
this.room = room;
|
|
951
1022
|
this.tracker = tracker;
|
|
952
|
-
this.
|
|
953
|
-
|
|
1023
|
+
this.roomId = typeof room === 'string' ? room : room.id;
|
|
1024
|
+
this.room = typeof room === 'string' ? null : room;
|
|
1025
|
+
this.isPreview = this.room === null;
|
|
1026
|
+
if (this.room) {
|
|
1027
|
+
this.updateTraverseLock(this.room);
|
|
1028
|
+
}
|
|
1029
|
+
if (this.room?.defaultTopic) {
|
|
954
1030
|
this.createHistoryWindowForTopic(this.room.defaultTopic);
|
|
955
1031
|
}
|
|
956
1032
|
}
|
|
@@ -962,9 +1038,13 @@ class RoomMessagesHistory {
|
|
|
962
1038
|
*/
|
|
963
1039
|
async getMessagesWindow(topicId, peek = false) {
|
|
964
1040
|
if (!this.historyWindows.has(topicId) && !peek) {
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
1041
|
+
if (this.isPreview) {
|
|
1042
|
+
this.createHistoryWindow(topicId);
|
|
1043
|
+
} else {
|
|
1044
|
+
const topic = (await this.tracker.rooms.getTopics(this.roomId, [topicId])).get(topicId);
|
|
1045
|
+
if (topic) {
|
|
1046
|
+
this.createHistoryWindowForTopic(topic);
|
|
1047
|
+
}
|
|
968
1048
|
}
|
|
969
1049
|
}
|
|
970
1050
|
return this.historyWindows.get(topicId);
|
|
@@ -1007,6 +1087,31 @@ class RoomMessagesHistory {
|
|
|
1007
1087
|
}
|
|
1008
1088
|
}
|
|
1009
1089
|
|
|
1090
|
+
/**
|
|
1091
|
+
* For internal use.
|
|
1092
|
+
* @internal
|
|
1093
|
+
*/
|
|
1094
|
+
get _isEphemeral() {
|
|
1095
|
+
return this.traverseLock;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
/**
|
|
1099
|
+
* The window of the topic, if created; unlike getMessagesWindow(), never creates nor fetches one.
|
|
1100
|
+
* @internal
|
|
1101
|
+
*/
|
|
1102
|
+
_peekWindow(topicId) {
|
|
1103
|
+
return this.historyWindows.get(topicId);
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
/**
|
|
1107
|
+
* For internal use.
|
|
1108
|
+
* @internal
|
|
1109
|
+
*/
|
|
1110
|
+
_restoreWindow(topic, messages, myReactions) {
|
|
1111
|
+
this.createHistoryWindowForTopic(topic);
|
|
1112
|
+
this.historyWindows.get(topic.id)._restoreLatest(messages, myReactions);
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1010
1115
|
/**
|
|
1011
1116
|
* For internal use.
|
|
1012
1117
|
* @internal
|
|
@@ -1077,9 +1182,18 @@ class RoomMessagesHistory {
|
|
|
1077
1182
|
if (this.historyWindows.has(topic.id)) {
|
|
1078
1183
|
return;
|
|
1079
1184
|
}
|
|
1080
|
-
|
|
1185
|
+
this.createHistoryWindow(topic.id);
|
|
1186
|
+
|
|
1187
|
+
// If new topic refers to some message from this room, update other structures
|
|
1188
|
+
if (topic.refMessage) {
|
|
1189
|
+
const refHistoryWindow = this.historyWindows.get(topic.refMessage.location.topicId);
|
|
1190
|
+
refHistoryWindow?._updateMessageReference(topic);
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
createHistoryWindow(topicId) {
|
|
1194
|
+
const historyWindow = new TopicHistoryWindow(this.roomId, topicId, this.tracker, this.isPreview);
|
|
1081
1195
|
void historyWindow.setTraverseLock(this.traverseLock);
|
|
1082
|
-
this.historyWindows.set([
|
|
1196
|
+
this.historyWindows.set([topicId, historyWindow]);
|
|
1083
1197
|
|
|
1084
1198
|
// Current behavior of deletion a message with referenced topic is to delete the whole side topic
|
|
1085
1199
|
// So we need to listen for topic deletions here
|
|
@@ -1087,14 +1201,8 @@ class RoomMessagesHistory {
|
|
|
1087
1201
|
for (const topicId of deletedTopicIds) {
|
|
1088
1202
|
this.historyWindows.delete(topicId);
|
|
1089
1203
|
}
|
|
1090
|
-
this.tracker.rooms._deleteTopicsFromRoom(this.
|
|
1204
|
+
this.tracker.rooms._deleteTopicsFromRoom(this.roomId, ...deletedTopicIds);
|
|
1091
1205
|
});
|
|
1092
|
-
|
|
1093
|
-
// If new topic refers to some message from this room, update other structures
|
|
1094
|
-
if (topic.refMessage) {
|
|
1095
|
-
const refHistoryWindow = this.historyWindows.get(topic.refMessage.location.topicId);
|
|
1096
|
-
refHistoryWindow?._updateMessageReference(topic);
|
|
1097
|
-
}
|
|
1098
1206
|
}
|
|
1099
1207
|
updateTraverseLock(room) {
|
|
1100
1208
|
this.traverseLock = room.history.mode === 'Ephemeral';
|
|
@@ -1106,6 +1214,7 @@ class RoomMessagesHistory {
|
|
|
1106
1214
|
|
|
1107
1215
|
class MessagesManager {
|
|
1108
1216
|
roomHistories = new IndexedCollection();
|
|
1217
|
+
previewHistories = new IndexedCollection();
|
|
1109
1218
|
deferredSession = new DeferredTask();
|
|
1110
1219
|
constructor(tracker) {
|
|
1111
1220
|
this.tracker = tracker;
|
|
@@ -1126,11 +1235,21 @@ class MessagesManager {
|
|
|
1126
1235
|
}
|
|
1127
1236
|
|
|
1128
1237
|
/**
|
|
1129
|
-
* Get history manager for given room ID.
|
|
1238
|
+
* Get history manager for given room ID. For a room you have not joined it is a preview (requires `PreviewRooms`),
|
|
1239
|
+
* kept until {@link forgetRoomPreview} or until you join the room.
|
|
1130
1240
|
*/
|
|
1131
1241
|
async getRoomHistory(roomId) {
|
|
1132
1242
|
await this.deferredSession.promise;
|
|
1133
|
-
|
|
1243
|
+
if (this.roomHistories.has(roomId)) {
|
|
1244
|
+
return this.roomHistories.get(roomId);
|
|
1245
|
+
}
|
|
1246
|
+
if (!this.previewHistories.has(roomId)) {
|
|
1247
|
+
this.previewHistories.set([roomId, new RoomMessagesHistory(roomId, this.tracker)]);
|
|
1248
|
+
}
|
|
1249
|
+
return this.previewHistories.get(roomId);
|
|
1250
|
+
}
|
|
1251
|
+
forgetRoomPreview(roomId) {
|
|
1252
|
+
this.previewHistories.delete(roomId);
|
|
1134
1253
|
}
|
|
1135
1254
|
|
|
1136
1255
|
/**
|
|
@@ -1138,8 +1257,10 @@ class MessagesManager {
|
|
|
1138
1257
|
* @internal
|
|
1139
1258
|
*/
|
|
1140
1259
|
async _resolveLastMessage(location) {
|
|
1141
|
-
// Try to get last message from history window (if cached)
|
|
1142
|
-
|
|
1260
|
+
// Try to get last message from history window (if cached); not getRoomHistory(), it would create a preview
|
|
1261
|
+
await this.deferredSession.promise;
|
|
1262
|
+
const historyWindow = await this.roomHistories.get(location.roomId)?.getMessagesWindow(location.topicId, true);
|
|
1263
|
+
let message = historyWindow?.hasLatest && historyWindow.getAt(historyWindow.length - 1);
|
|
1143
1264
|
if (!message) {
|
|
1144
1265
|
const result = await this.tracker.client.send('GetMessages', {
|
|
1145
1266
|
location,
|
|
@@ -1149,12 +1270,50 @@ class MessagesManager {
|
|
|
1149
1270
|
}
|
|
1150
1271
|
return message || null;
|
|
1151
1272
|
}
|
|
1273
|
+
|
|
1274
|
+
/**
|
|
1275
|
+
* For internal use.
|
|
1276
|
+
* @internal
|
|
1277
|
+
*/
|
|
1278
|
+
_importRooms(rooms) {
|
|
1279
|
+
for (const room of rooms) {
|
|
1280
|
+
this.createHistoryForNewRoom(room);
|
|
1281
|
+
}
|
|
1282
|
+
this.deferredSession.resolve();
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
/**
|
|
1286
|
+
* For internal use.
|
|
1287
|
+
* @internal
|
|
1288
|
+
*/
|
|
1289
|
+
_exportLatest(roomId, topicId, limit) {
|
|
1290
|
+
const history = this.roomHistories.get(roomId);
|
|
1291
|
+
|
|
1292
|
+
// Ephemeral history lives only in the memory of the session.
|
|
1293
|
+
if (!history || history._isEphemeral) {
|
|
1294
|
+
return null;
|
|
1295
|
+
}
|
|
1296
|
+
return history._peekWindow(topicId)?._exportLatest(limit) ?? null;
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
/**
|
|
1300
|
+
* For internal use.
|
|
1301
|
+
* @internal
|
|
1302
|
+
*/
|
|
1303
|
+
_importHistory(roomId, topic, snapshot) {
|
|
1304
|
+
const history = this.roomHistories.get(roomId);
|
|
1305
|
+
if (history && !history._isEphemeral) {
|
|
1306
|
+
const messages = snapshot.messages.filter(message => message.location.roomId === roomId && message.location.topicId === topic.id);
|
|
1307
|
+
history._restoreWindow(topic, messages, snapshot.myReactions);
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1152
1310
|
forEachRoomHistory(callback) {
|
|
1153
1311
|
for (const history of this.roomHistories.items.values()) {
|
|
1154
1312
|
callback(history);
|
|
1155
1313
|
}
|
|
1156
1314
|
}
|
|
1157
1315
|
createHistoryForNewRoom(room) {
|
|
1316
|
+
this.previewHistories.delete(room.id);
|
|
1158
1317
|
this.roomHistories.set([room.id, new RoomMessagesHistory(room, this.tracker)]);
|
|
1159
1318
|
}
|
|
1160
1319
|
handleRoomDeleted(ev) {
|
|
@@ -1368,6 +1527,47 @@ class FollowedTopicsManager extends EventTarget {
|
|
|
1368
1527
|
this.followedTopics.get(roomId)?.delete(...topicIds);
|
|
1369
1528
|
topicIds.forEach(topicId => this.invalidateUnreadSummaries(roomId, topicId));
|
|
1370
1529
|
}
|
|
1530
|
+
|
|
1531
|
+
/**
|
|
1532
|
+
* For internal use.
|
|
1533
|
+
* @internal
|
|
1534
|
+
*/
|
|
1535
|
+
_exportFollowedTopics() {
|
|
1536
|
+
return Array.from(this.followedTopics.items).filter(([roomId]) => this.tracker.rooms._getRoom(roomId)).map(([roomId, followedTopics]) => ({
|
|
1537
|
+
roomId,
|
|
1538
|
+
followedTopics: followedTopics.items
|
|
1539
|
+
}));
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
/**
|
|
1543
|
+
* Expects the rooms to be imported already - followed topics of other rooms are skipped.
|
|
1544
|
+
* @internal
|
|
1545
|
+
*/
|
|
1546
|
+
_importFollowedTopics(snapshots) {
|
|
1547
|
+
for (const {
|
|
1548
|
+
roomId,
|
|
1549
|
+
followedTopics
|
|
1550
|
+
} of snapshots) {
|
|
1551
|
+
if (this.tracker.rooms._getRoom(roomId)) {
|
|
1552
|
+
this.applyRoomFollowedTopics(roomId, followedTopics);
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
this.invalidateUnreadSummaries();
|
|
1556
|
+
this.deferredSession.resolve();
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1559
|
+
/**
|
|
1560
|
+
* For internal use.
|
|
1561
|
+
* @internal
|
|
1562
|
+
*/
|
|
1563
|
+
_forgetRoomsExcept(roomIds) {
|
|
1564
|
+
const keptRoomIds = new Set(roomIds);
|
|
1565
|
+
for (const roomId of Array.from(this.followedTopics.items.keys())) {
|
|
1566
|
+
if (!keptRoomIds.has(roomId)) {
|
|
1567
|
+
this.clearRoomFollowedTopicsStructures(roomId);
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1371
1571
|
handleSession(ev) {
|
|
1372
1572
|
// Keep cached followed-topic collections (they drive unread indicators
|
|
1373
1573
|
// that would otherwise blank on reconnect), but mark them stale and drop
|
|
@@ -1568,11 +1768,148 @@ class FollowedTopicsManager extends EventTarget {
|
|
|
1568
1768
|
this.invalidateUnreadSummaries(roomId);
|
|
1569
1769
|
}
|
|
1570
1770
|
}
|
|
1771
|
+
;// ./src/protocol/schemes/User.ts
|
|
1772
|
+
/**
|
|
1773
|
+
* Availability of a user, aggregated over all of their sessions.
|
|
1774
|
+
*
|
|
1775
|
+
* - `Offline` - no session is connected and none can be reached asynchronously.
|
|
1776
|
+
* - `Online` - at least one session holds a live connection.
|
|
1777
|
+
* - `OnlineAsync` - no live connection, but a push-registered device was active
|
|
1778
|
+
* recently, so a message will still reach the user.
|
|
1779
|
+
*/
|
|
1780
|
+
let UserStatus = /*#__PURE__*/function (UserStatus) {
|
|
1781
|
+
UserStatus[UserStatus["Offline"] = 0] = "Offline";
|
|
1782
|
+
UserStatus[UserStatus["Online"] = 1] = "Online";
|
|
1783
|
+
UserStatus[UserStatus["OnlineAsync"] = 2] = "OnlineAsync";
|
|
1784
|
+
return UserStatus;
|
|
1785
|
+
}({});
|
|
1786
|
+
;// ./src/state-tracker/StateSnapshot.ts
|
|
1787
|
+
|
|
1788
|
+
|
|
1789
|
+
/**
|
|
1790
|
+
* Version of the {@link StateSnapshot} format. A snapshot of any other version is rejected on import.
|
|
1791
|
+
*/
|
|
1792
|
+
const STATE_SNAPSHOT_VERSION = 1;
|
|
1793
|
+
|
|
1794
|
+
/**
|
|
1795
|
+
* Plain, JSON-serializable copy of the state tracked by {@link ChatStateTracker}, made to restore the state at the
|
|
1796
|
+
* application start (before the connection is established) - see {@link ChatStateTracker.exportSnapshot}.
|
|
1797
|
+
*
|
|
1798
|
+
* Only the data that lasts is included: the current user, the joined spaces (with roles) and rooms (with the default
|
|
1799
|
+
* topics), and the lazily loaded lists already fetched - rooms of spaces, followed topics, emoticons and relationships.
|
|
1800
|
+
* The message histories of the topics named in the {@link StateSnapshotIntent} are included too, with what their
|
|
1801
|
+
* display needs: the topics referenced by the messages and, optionally, the members of the rooms.
|
|
1802
|
+
* Left out on purpose:
|
|
1803
|
+
* - presence - every user status is saved as `Offline`, a status from the past would show people online who are not;
|
|
1804
|
+
* - members of spaces, and of rooms not asked for - large, made mostly of presence, and refetched cheaply on demand;
|
|
1805
|
+
* - other message histories and topics, histories of ephemeral rooms (they live only in the memory of the session),
|
|
1806
|
+
* permission overwrites and room previews - loaded on demand once the connection is up, as on a reconnect.
|
|
1807
|
+
*/
|
|
1808
|
+
|
|
1809
|
+
/**
|
|
1810
|
+
* What {@link ChatStateTracker.exportSnapshot} should include beyond the state that is always exported.
|
|
1811
|
+
*/
|
|
1812
|
+
|
|
1813
|
+
function withoutPresence(user) {
|
|
1814
|
+
return {
|
|
1815
|
+
...user,
|
|
1816
|
+
status: UserStatus.Offline,
|
|
1817
|
+
online: false
|
|
1818
|
+
};
|
|
1819
|
+
}
|
|
1820
|
+
function messageWithoutPresence(message) {
|
|
1821
|
+
return {
|
|
1822
|
+
...message,
|
|
1823
|
+
author: {
|
|
1824
|
+
...message.author,
|
|
1825
|
+
user: withoutPresence(message.author.user)
|
|
1826
|
+
}
|
|
1827
|
+
};
|
|
1828
|
+
}
|
|
1829
|
+
function memberWithoutPresence(member) {
|
|
1830
|
+
return {
|
|
1831
|
+
...member,
|
|
1832
|
+
user: member.user ? withoutPresence(member.user) : member.user,
|
|
1833
|
+
spaceMember: member.spaceMember ? {
|
|
1834
|
+
...member.spaceMember,
|
|
1835
|
+
user: withoutPresence(member.spaceMember.user)
|
|
1836
|
+
} : member.spaceMember
|
|
1837
|
+
};
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
/**
|
|
1841
|
+
* Structural check of the data restored from a storage - deep enough that importing it cannot throw, as the state
|
|
1842
|
+
* handlers dereference these fields without checking them.
|
|
1843
|
+
*/
|
|
1844
|
+
function isValidStateSnapshot(value) {
|
|
1845
|
+
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);
|
|
1846
|
+
}
|
|
1847
|
+
function isObject(value) {
|
|
1848
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
1849
|
+
}
|
|
1850
|
+
function isArrayOf(value, isItem) {
|
|
1851
|
+
return Array.isArray(value) && value.every(isItem);
|
|
1852
|
+
}
|
|
1853
|
+
function isString(value) {
|
|
1854
|
+
return typeof value === 'string';
|
|
1855
|
+
}
|
|
1856
|
+
function hasId(value) {
|
|
1857
|
+
return isObject(value) && isString(value.id);
|
|
1858
|
+
}
|
|
1859
|
+
function isUser(value) {
|
|
1860
|
+
return hasId(value);
|
|
1861
|
+
}
|
|
1862
|
+
function isRole(value) {
|
|
1863
|
+
return hasId(value) && typeof value.priority === 'number';
|
|
1864
|
+
}
|
|
1865
|
+
function isSpace(value) {
|
|
1866
|
+
return hasId(value) && isArrayOf(value.roles, isRole) && isArrayOf(value.defaultRooms, isString) && (value.systemRoom === null || isString(value.systemRoom));
|
|
1867
|
+
}
|
|
1868
|
+
function isTopic(value) {
|
|
1869
|
+
return hasId(value) && (value.refMessage === undefined || value.refMessage === null || isMessage(value.refMessage));
|
|
1870
|
+
}
|
|
1871
|
+
function isMessage(value) {
|
|
1872
|
+
return hasId(value) && isObject(value.location) && isString(value.location.topicId);
|
|
1873
|
+
}
|
|
1874
|
+
function isFullMessage(value) {
|
|
1875
|
+
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);
|
|
1876
|
+
}
|
|
1877
|
+
function isRoom(value) {
|
|
1878
|
+
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);
|
|
1879
|
+
}
|
|
1880
|
+
function isSpaceRooms(value) {
|
|
1881
|
+
return isObject(value) && isString(value.spaceId) && isArrayOf(value.summaries, hasId);
|
|
1882
|
+
}
|
|
1883
|
+
function isFollowedTopic(value) {
|
|
1884
|
+
return isObject(value) && isObject(value.location) && isString(value.location.roomId) && isString(value.location.topicId);
|
|
1885
|
+
}
|
|
1886
|
+
function isRoomFollowedTopics(value) {
|
|
1887
|
+
return isObject(value) && isString(value.roomId) && isArrayOf(value.followedTopics, isFollowedTopic);
|
|
1888
|
+
}
|
|
1889
|
+
function isEmoticons(value) {
|
|
1890
|
+
return isObject(value) && (value.spaceId === null || isString(value.spaceId)) && isArrayOf(value.emoticons, hasId);
|
|
1891
|
+
}
|
|
1892
|
+
function isRelationship(value) {
|
|
1893
|
+
return isObject(value) && isUser(value.refUser) && isString(value.type);
|
|
1894
|
+
}
|
|
1895
|
+
function isUserReactions(value) {
|
|
1896
|
+
return isObject(value) && Object.values(value).every(reactions => isArrayOf(reactions, isObject));
|
|
1897
|
+
}
|
|
1898
|
+
function isTopicHistory(value) {
|
|
1899
|
+
return isObject(value) && isString(value.roomId) && isString(value.topicId) && isArrayOf(value.messages, isFullMessage) && isUserReactions(value.myReactions) && isArrayOf(value.topics, isTopic);
|
|
1900
|
+
}
|
|
1901
|
+
function isRoomMember(value) {
|
|
1902
|
+
return isObject(value) && (isUser(value.user) || isObject(value.spaceMember) && isUser(value.spaceMember.user));
|
|
1903
|
+
}
|
|
1904
|
+
function isRoomMembers(value) {
|
|
1905
|
+
return isObject(value) && isString(value.roomId) && isArrayOf(value.members, isRoomMember);
|
|
1906
|
+
}
|
|
1571
1907
|
;// ./src/state-tracker/RoomsManager.ts
|
|
1572
1908
|
|
|
1573
1909
|
|
|
1574
1910
|
|
|
1575
1911
|
|
|
1912
|
+
|
|
1576
1913
|
class RoomsManager {
|
|
1577
1914
|
messages;
|
|
1578
1915
|
followedTopics;
|
|
@@ -1582,6 +1919,7 @@ class RoomsManager {
|
|
|
1582
1919
|
deferredSession = new DeferredTask();
|
|
1583
1920
|
membersPromises = new PromiseRegistry();
|
|
1584
1921
|
topicsPromises = new PromiseRegistry();
|
|
1922
|
+
importedTopicIds = new Map();
|
|
1585
1923
|
constructor(tracker) {
|
|
1586
1924
|
this.tracker = tracker;
|
|
1587
1925
|
this.messages = new MessagesManager(tracker);
|
|
@@ -1686,6 +2024,148 @@ class RoomsManager {
|
|
|
1686
2024
|
_deleteTopicsFromRoom(roomId, ...topicIds) {
|
|
1687
2025
|
this.topics.get(roomId)?.delete(...topicIds);
|
|
1688
2026
|
}
|
|
2027
|
+
|
|
2028
|
+
/**
|
|
2029
|
+
* For internal use.
|
|
2030
|
+
* @internal
|
|
2031
|
+
*/
|
|
2032
|
+
_exportRooms() {
|
|
2033
|
+
return this.list.items.map(room => room.recipients ? {
|
|
2034
|
+
...room,
|
|
2035
|
+
recipients: room.recipients.map(withoutPresence)
|
|
2036
|
+
} : room);
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
/**
|
|
2040
|
+
* For internal use.
|
|
2041
|
+
* @internal
|
|
2042
|
+
*/
|
|
2043
|
+
_importRooms(rooms) {
|
|
2044
|
+
this.messages._importRooms(rooms);
|
|
2045
|
+
this.addJoinedRooms(...rooms);
|
|
2046
|
+
this.deferredSession.resolve();
|
|
2047
|
+
}
|
|
2048
|
+
|
|
2049
|
+
/**
|
|
2050
|
+
* For internal use.
|
|
2051
|
+
* @internal
|
|
2052
|
+
*/
|
|
2053
|
+
_exportHistories(intent) {
|
|
2054
|
+
const histories = [];
|
|
2055
|
+
const exported = new Set();
|
|
2056
|
+
for (const {
|
|
2057
|
+
roomId,
|
|
2058
|
+
topicId: requestedTopicId
|
|
2059
|
+
} of intent.histories ?? []) {
|
|
2060
|
+
const room = this.list.get(roomId);
|
|
2061
|
+
const topicId = requestedTopicId ?? room?.defaultTopic?.id;
|
|
2062
|
+
if (!room || !topicId || exported.has(`${roomId}/${topicId}`)) {
|
|
2063
|
+
continue;
|
|
2064
|
+
}
|
|
2065
|
+
const latest = this.messages._exportLatest(roomId, topicId, intent.historyLimit ?? 0);
|
|
2066
|
+
const topics = this.topics.get(roomId);
|
|
2067
|
+
|
|
2068
|
+
// A topic other than the default one is restored from the snapshot, so it has to be known.
|
|
2069
|
+
if (!latest || topicId !== room.defaultTopic?.id && !topics?.has(topicId)) {
|
|
2070
|
+
continue;
|
|
2071
|
+
}
|
|
2072
|
+
const topicIds = new Set([topicId, ...latest.messages.map(message => message.topicRef)]);
|
|
2073
|
+
topicIds.delete(room.defaultTopic?.id);
|
|
2074
|
+
exported.add(`${roomId}/${topicId}`);
|
|
2075
|
+
histories.push({
|
|
2076
|
+
roomId,
|
|
2077
|
+
topicId,
|
|
2078
|
+
messages: latest.messages.map(messageWithoutPresence),
|
|
2079
|
+
myReactions: latest.myReactions,
|
|
2080
|
+
topics: Array.from(topicIds).filter(id => topics?.has(id)).map(id => topics.get(id))
|
|
2081
|
+
});
|
|
2082
|
+
}
|
|
2083
|
+
return histories;
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
/**
|
|
2087
|
+
* Members of private conversations are not exported - they are made of the recipients of the room.
|
|
2088
|
+
* @internal
|
|
2089
|
+
*/
|
|
2090
|
+
_exportMembers(roomIds, limit) {
|
|
2091
|
+
const snapshots = [];
|
|
2092
|
+
if (!limit) {
|
|
2093
|
+
return snapshots;
|
|
2094
|
+
}
|
|
2095
|
+
for (const roomId of new Set(roomIds)) {
|
|
2096
|
+
const members = this.members.get(roomId);
|
|
2097
|
+
if (members && members.length <= limit && this.list.get(roomId)?.type !== 'Pm') {
|
|
2098
|
+
snapshots.push({
|
|
2099
|
+
roomId,
|
|
2100
|
+
members: members.items.map(memberWithoutPresence)
|
|
2101
|
+
});
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
return snapshots;
|
|
2105
|
+
}
|
|
2106
|
+
|
|
2107
|
+
/**
|
|
2108
|
+
* Expects the rooms to be imported already - histories and members of other rooms are skipped.
|
|
2109
|
+
* @internal
|
|
2110
|
+
*/
|
|
2111
|
+
_importHistories(histories, roomMembers) {
|
|
2112
|
+
for (const {
|
|
2113
|
+
roomId,
|
|
2114
|
+
members
|
|
2115
|
+
} of roomMembers) {
|
|
2116
|
+
if (this.list.has(roomId) && this.list.get(roomId).type !== 'Pm') {
|
|
2117
|
+
this.handleRoomMembers({
|
|
2118
|
+
id: roomId,
|
|
2119
|
+
members
|
|
2120
|
+
});
|
|
2121
|
+
// Served from the snapshot until the session invalidates it.
|
|
2122
|
+
this.membersPromises.register(Promise.resolve(), roomId);
|
|
2123
|
+
}
|
|
2124
|
+
}
|
|
2125
|
+
for (const history of histories) {
|
|
2126
|
+
const room = this.list.get(history.roomId);
|
|
2127
|
+
if (!room) {
|
|
2128
|
+
continue;
|
|
2129
|
+
}
|
|
2130
|
+
const topics = history.topics.filter(topic => topic.id !== room.defaultTopic?.id);
|
|
2131
|
+
if (topics.length) {
|
|
2132
|
+
this.addJoinedRoomTopics(room.id, ...topics);
|
|
2133
|
+
const importedTopicIds = this.importedTopicIds.get(room.id) ?? new Set();
|
|
2134
|
+
topics.forEach(topic => importedTopicIds.add(topic.id));
|
|
2135
|
+
this.importedTopicIds.set(room.id, importedTopicIds);
|
|
2136
|
+
}
|
|
2137
|
+
const topic = history.topicId === room.defaultTopic?.id ? room.defaultTopic : this.topics.get(room.id)?.get(history.topicId);
|
|
2138
|
+
if (topic) {
|
|
2139
|
+
this.messages._importHistory(room.id, topic, history);
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
|
|
2144
|
+
/**
|
|
2145
|
+
* The session carries only the default topics, so the other restored ones are fetched anew.
|
|
2146
|
+
* @internal
|
|
2147
|
+
*/
|
|
2148
|
+
_refreshImportedTopics() {
|
|
2149
|
+
const importedTopicIds = Array.from(this.importedTopicIds);
|
|
2150
|
+
this.importedTopicIds.clear();
|
|
2151
|
+
for (const [roomId, ids] of importedTopicIds) {
|
|
2152
|
+
if (!this.list.has(roomId)) {
|
|
2153
|
+
continue;
|
|
2154
|
+
}
|
|
2155
|
+
const topicIds = Array.from(ids);
|
|
2156
|
+
const promise = this.tracker.client.send('GetTopics', {
|
|
2157
|
+
roomId,
|
|
2158
|
+
topicIds
|
|
2159
|
+
}).then(result => {
|
|
2160
|
+
if (!result.error) {
|
|
2161
|
+
this.topics.get(roomId)?.set(...result.data.topics);
|
|
2162
|
+
}
|
|
2163
|
+
});
|
|
2164
|
+
topicIds.forEach(topicId => this.topicsPromises.register(promise, roomId + topicId));
|
|
2165
|
+
// Best effort: on a failure the restored topics stay, as after a reconnect.
|
|
2166
|
+
promise.catch(() => undefined);
|
|
2167
|
+
}
|
|
2168
|
+
}
|
|
1689
2169
|
deleteRoom(...roomIds) {
|
|
1690
2170
|
this.list.delete(...roomIds);
|
|
1691
2171
|
this.members.delete(...roomIds);
|
|
@@ -2082,6 +2562,47 @@ class SpacesManager {
|
|
|
2082
2562
|
const members = await this.getMembers(spaceId);
|
|
2083
2563
|
return members?.items.find(member => member.user.id === userId);
|
|
2084
2564
|
}
|
|
2565
|
+
|
|
2566
|
+
/**
|
|
2567
|
+
* For internal use.
|
|
2568
|
+
* @internal
|
|
2569
|
+
*/
|
|
2570
|
+
_exportSpaces() {
|
|
2571
|
+
return this.list.items;
|
|
2572
|
+
}
|
|
2573
|
+
|
|
2574
|
+
/**
|
|
2575
|
+
* Room listings of the joined spaces only - of other spaces they are fetched for a moment's look.
|
|
2576
|
+
* @internal
|
|
2577
|
+
*/
|
|
2578
|
+
_exportSpaceRooms() {
|
|
2579
|
+
return Array.from(this.rooms.items).filter(([spaceId]) => this.list.has(spaceId)).map(([spaceId, summaries]) => ({
|
|
2580
|
+
spaceId,
|
|
2581
|
+
summaries: summaries.items
|
|
2582
|
+
}));
|
|
2583
|
+
}
|
|
2584
|
+
|
|
2585
|
+
/**
|
|
2586
|
+
* For internal use.
|
|
2587
|
+
* @internal
|
|
2588
|
+
*/
|
|
2589
|
+
_importSpaces(spaces, spaceRooms) {
|
|
2590
|
+
this.addJoinedSpaces(...spaces);
|
|
2591
|
+
for (const {
|
|
2592
|
+
spaceId,
|
|
2593
|
+
summaries
|
|
2594
|
+
} of spaceRooms) {
|
|
2595
|
+
if (this.list.has(spaceId)) {
|
|
2596
|
+
this.handleSpaceRooms({
|
|
2597
|
+
id: spaceId,
|
|
2598
|
+
summaries
|
|
2599
|
+
});
|
|
2600
|
+
// Served from the snapshot until the session invalidates it.
|
|
2601
|
+
this.roomsPromises.register(Promise.resolve(), spaceId);
|
|
2602
|
+
}
|
|
2603
|
+
}
|
|
2604
|
+
this.deferredSession.resolve();
|
|
2605
|
+
}
|
|
2085
2606
|
handleNewRole(ev) {
|
|
2086
2607
|
this.roles.get(ev.spaceId).set(ev.role);
|
|
2087
2608
|
this.syncSpaceRoles(ev.spaceId);
|
|
@@ -2374,6 +2895,14 @@ class Permissions {
|
|
|
2374
2895
|
MentionAllRoles: {
|
|
2375
2896
|
value: 1 << 23,
|
|
2376
2897
|
maxLayer: Layer.Topic
|
|
2898
|
+
},
|
|
2899
|
+
PreviewRooms: {
|
|
2900
|
+
value: 1 << 24,
|
|
2901
|
+
maxLayer: Layer.Room
|
|
2902
|
+
},
|
|
2903
|
+
SearchMessages: {
|
|
2904
|
+
value: 1 << 25,
|
|
2905
|
+
maxLayer: Layer.Room
|
|
2377
2906
|
}
|
|
2378
2907
|
};
|
|
2379
2908
|
static getNames() {
|
|
@@ -2451,6 +2980,10 @@ class PermissionsManager extends EventTarget {
|
|
|
2451
2980
|
missing
|
|
2452
2981
|
};
|
|
2453
2982
|
}
|
|
2983
|
+
|
|
2984
|
+
/**
|
|
2985
|
+
* For a room or a space you have not joined the permissions are fetched from the server, on every call.
|
|
2986
|
+
*/
|
|
2454
2987
|
async calculatePermissions(location) {
|
|
2455
2988
|
const room = location.roomId ? (await this.tracker.rooms.get()).get(location.roomId) : null;
|
|
2456
2989
|
location = {
|
|
@@ -2459,6 +2992,9 @@ class PermissionsManager extends EventTarget {
|
|
|
2459
2992
|
topicId: location.topicId
|
|
2460
2993
|
};
|
|
2461
2994
|
this.validateLocation(location);
|
|
2995
|
+
if (location.roomId && !room || location.spaceId && !(await this.tracker.spaces.get())?.has(location.spaceId)) {
|
|
2996
|
+
return this.fetchComputedPermissions(location);
|
|
2997
|
+
}
|
|
2462
2998
|
const userId = (await this.tracker.getMe()).id;
|
|
2463
2999
|
const [spaceMember, roomMember] = await this.fetchMembersOrFail(location);
|
|
2464
3000
|
const userRoles = [...(spaceMember?.roles ?? []), ...(roomMember?.roles ?? [])];
|
|
@@ -2624,6 +3160,15 @@ class PermissionsManager extends EventTarget {
|
|
|
2624
3160
|
}
|
|
2625
3161
|
return result;
|
|
2626
3162
|
}
|
|
3163
|
+
async fetchComputedPermissions(location) {
|
|
3164
|
+
const result = await this.tracker.client.send('GetComputedPermissions', {
|
|
3165
|
+
location
|
|
3166
|
+
});
|
|
3167
|
+
if (result.error) {
|
|
3168
|
+
throw result.error;
|
|
3169
|
+
}
|
|
3170
|
+
return result.data.permissions;
|
|
3171
|
+
}
|
|
2627
3172
|
async fetchMembersOrFail(location) {
|
|
2628
3173
|
const results = await Promise.all([location.spaceId ? this.tracker.spaces.getMe(location.spaceId) : null, location.roomId ? this.tracker.rooms.getMe(location.roomId) : null]);
|
|
2629
3174
|
const spaceFail = location.spaceId && !results[0];
|
|
@@ -2679,6 +3224,52 @@ class EmoticonsManager {
|
|
|
2679
3224
|
await this.emoticonsPromises.get(key);
|
|
2680
3225
|
return this.list.get(key);
|
|
2681
3226
|
}
|
|
3227
|
+
|
|
3228
|
+
/**
|
|
3229
|
+
* For internal use.
|
|
3230
|
+
* @internal
|
|
3231
|
+
*/
|
|
3232
|
+
_exportEmoticons() {
|
|
3233
|
+
return Array.from(this.list.items).map(([key, emoticons]) => ({
|
|
3234
|
+
spaceId: key === GLOBAL_KEY ? null : key,
|
|
3235
|
+
emoticons: emoticons.items
|
|
3236
|
+
}));
|
|
3237
|
+
}
|
|
3238
|
+
|
|
3239
|
+
/**
|
|
3240
|
+
* Emoticons of spaces other than the given ones are skipped.
|
|
3241
|
+
* @internal
|
|
3242
|
+
*/
|
|
3243
|
+
_importEmoticons(snapshots, spaceIds) {
|
|
3244
|
+
const joinedSpaceIds = new Set(spaceIds);
|
|
3245
|
+
for (const {
|
|
3246
|
+
spaceId,
|
|
3247
|
+
emoticons
|
|
3248
|
+
} of snapshots) {
|
|
3249
|
+
if (spaceId !== null && !joinedSpaceIds.has(spaceId)) {
|
|
3250
|
+
continue;
|
|
3251
|
+
}
|
|
3252
|
+
this.handleEmoticons({
|
|
3253
|
+
location: {
|
|
3254
|
+
spaceId: spaceId ?? undefined
|
|
3255
|
+
},
|
|
3256
|
+
emoticons
|
|
3257
|
+
});
|
|
3258
|
+
// Served from the snapshot until the session invalidates it.
|
|
3259
|
+
this.emoticonsPromises.register(Promise.resolve(), spaceId ?? GLOBAL_KEY);
|
|
3260
|
+
}
|
|
3261
|
+
}
|
|
3262
|
+
|
|
3263
|
+
/**
|
|
3264
|
+
* For internal use.
|
|
3265
|
+
* @internal
|
|
3266
|
+
*/
|
|
3267
|
+
_forgetSpacesExcept(spaceIds) {
|
|
3268
|
+
const keptKeys = new Set([GLOBAL_KEY, ...spaceIds]);
|
|
3269
|
+
const removedKeys = Array.from(this.list.items.keys()).filter(key => !keptKeys.has(key));
|
|
3270
|
+
this.list.delete(...removedKeys);
|
|
3271
|
+
this.emoticonsPromises.forget(...removedKeys);
|
|
3272
|
+
}
|
|
2682
3273
|
handleEmoticons(event) {
|
|
2683
3274
|
const spaceId = event.location.spaceId ?? GLOBAL_KEY;
|
|
2684
3275
|
if (!this.list.has(spaceId)) {
|
|
@@ -2734,6 +3325,14 @@ class UsersManager {
|
|
|
2734
3325
|
async getAvailable() {
|
|
2735
3326
|
return this.users;
|
|
2736
3327
|
}
|
|
3328
|
+
|
|
3329
|
+
/**
|
|
3330
|
+
* For internal use.
|
|
3331
|
+
* @internal
|
|
3332
|
+
*/
|
|
3333
|
+
_importUser(user) {
|
|
3334
|
+
this.handleUsers([user]);
|
|
3335
|
+
}
|
|
2737
3336
|
handleMembers(members) {
|
|
2738
3337
|
this.handleUsers(members.map(extractUserFromMember));
|
|
2739
3338
|
}
|
|
@@ -2780,11 +3379,13 @@ function isSameUser(a, b) {
|
|
|
2780
3379
|
;// ./src/state-tracker/RelationshipsManager.ts
|
|
2781
3380
|
|
|
2782
3381
|
|
|
3382
|
+
|
|
2783
3383
|
const getId = (refUserId, type) => `${refUserId}-${type}`;
|
|
2784
3384
|
const getIdFromRelationship = relationship => getId(relationship.refUser.id, relationship.type);
|
|
2785
3385
|
class RelationshipsManager {
|
|
2786
3386
|
relationships = new ObservableIndexedObjectCollection(getIdFromRelationship);
|
|
2787
3387
|
promises = new PromiseRegistry();
|
|
3388
|
+
loaded = false;
|
|
2788
3389
|
constructor(tracker) {
|
|
2789
3390
|
this.tracker = tracker;
|
|
2790
3391
|
this.tracker.client.on('Relationships', ev => this.handleRelationships(ev));
|
|
@@ -2808,7 +3409,33 @@ class RelationshipsManager {
|
|
|
2808
3409
|
await this.get();
|
|
2809
3410
|
return this.relationships.has(getId(refUserId, type));
|
|
2810
3411
|
}
|
|
3412
|
+
|
|
3413
|
+
/**
|
|
3414
|
+
* For internal use.
|
|
3415
|
+
* @internal
|
|
3416
|
+
*/
|
|
3417
|
+
_exportRelationships() {
|
|
3418
|
+
return this.loaded ? this.relationships.items.map(relationship => ({
|
|
3419
|
+
...relationship,
|
|
3420
|
+
refUser: withoutPresence(relationship.refUser)
|
|
3421
|
+
})) : null;
|
|
3422
|
+
}
|
|
3423
|
+
|
|
3424
|
+
/**
|
|
3425
|
+
* For internal use.
|
|
3426
|
+
* @internal
|
|
3427
|
+
*/
|
|
3428
|
+
_importRelationships(relationships) {
|
|
3429
|
+
if (relationships) {
|
|
3430
|
+
this.handleRelationships({
|
|
3431
|
+
relationships
|
|
3432
|
+
});
|
|
3433
|
+
// Served from the snapshot until the session invalidates it.
|
|
3434
|
+
this.promises.register(Promise.resolve(), 'all');
|
|
3435
|
+
}
|
|
3436
|
+
}
|
|
2811
3437
|
handleRelationships(ev) {
|
|
3438
|
+
this.loaded = true;
|
|
2812
3439
|
// Full list from the server: reconcile in place so a reconnect refetch
|
|
2813
3440
|
// updates a bound (visible) list without an intermediate empty state.
|
|
2814
3441
|
this.relationships.reconcile(...ev.relationships);
|
|
@@ -2837,6 +3464,7 @@ class RelationshipsManager {
|
|
|
2837
3464
|
|
|
2838
3465
|
|
|
2839
3466
|
|
|
3467
|
+
|
|
2840
3468
|
class ChatStateTracker {
|
|
2841
3469
|
client;
|
|
2842
3470
|
|
|
@@ -2871,6 +3499,8 @@ class ChatStateTracker {
|
|
|
2871
3499
|
relationships;
|
|
2872
3500
|
_me = null;
|
|
2873
3501
|
deferredSession = new DeferredTask();
|
|
3502
|
+
sessionReceived = false;
|
|
3503
|
+
snapshotImported = false;
|
|
2874
3504
|
constructor(client) {
|
|
2875
3505
|
this.client = client;
|
|
2876
3506
|
this.client.on('Session', ev => this.handleSession(ev));
|
|
@@ -2880,6 +3510,9 @@ class ChatStateTracker {
|
|
|
2880
3510
|
this.emoticons = new EmoticonsManager(this);
|
|
2881
3511
|
this.users = new UsersManager(this);
|
|
2882
3512
|
this.relationships = new RelationshipsManager(this);
|
|
3513
|
+
|
|
3514
|
+
// Registered after the managers, so it runs once they have applied the session.
|
|
3515
|
+
this.client.on('Session', ev => this.handleSessionAfterImport(ev));
|
|
2883
3516
|
}
|
|
2884
3517
|
get me() {
|
|
2885
3518
|
return this._me;
|
|
@@ -2888,10 +3521,88 @@ class ChatStateTracker {
|
|
|
2888
3521
|
await this.deferredSession.promise;
|
|
2889
3522
|
return this._me;
|
|
2890
3523
|
}
|
|
3524
|
+
|
|
3525
|
+
/**
|
|
3526
|
+
* Export the tracked state to a plain, JSON-serializable object, to be saved and restored with
|
|
3527
|
+
* {@link importSnapshot} at the next start of the application, before the connection is established.
|
|
3528
|
+
* See {@link StateSnapshot} for what is included.
|
|
3529
|
+
* @param intent What to include beyond the state that is always exported, e.g. the message histories.
|
|
3530
|
+
* @return Null if there is no state yet (no session has been received or imported).
|
|
3531
|
+
*/
|
|
3532
|
+
exportSnapshot(intent = {}) {
|
|
3533
|
+
if (!this._me) {
|
|
3534
|
+
return null;
|
|
3535
|
+
}
|
|
3536
|
+
const snapshot = {
|
|
3537
|
+
version: STATE_SNAPSHOT_VERSION,
|
|
3538
|
+
createdAt: Date.now(),
|
|
3539
|
+
user: withoutPresence(this._me),
|
|
3540
|
+
spaces: this.spaces._exportSpaces(),
|
|
3541
|
+
rooms: this.rooms._exportRooms(),
|
|
3542
|
+
spaceRooms: this.spaces._exportSpaceRooms(),
|
|
3543
|
+
followedTopics: this.rooms.followedTopics._exportFollowedTopics(),
|
|
3544
|
+
emoticons: this.emoticons._exportEmoticons(),
|
|
3545
|
+
relationships: this.relationships._exportRelationships(),
|
|
3546
|
+
histories: this.rooms._exportHistories(intent),
|
|
3547
|
+
roomMembers: this.rooms._exportMembers((intent.histories ?? []).map(history => history.roomId), intent.roomMembersLimit ?? 0)
|
|
3548
|
+
};
|
|
3549
|
+
|
|
3550
|
+
// Detaches the snapshot from the live state objects and leaves nothing a JSON round trip would change.
|
|
3551
|
+
return JSON.parse(JSON.stringify(snapshot));
|
|
3552
|
+
}
|
|
3553
|
+
|
|
3554
|
+
/**
|
|
3555
|
+
* Restore the state exported with {@link exportSnapshot}, so it is available before the connection is
|
|
3556
|
+
* established. The collections emit their usual change events and the getters waiting for the session resolve.
|
|
3557
|
+
* The first `Session` from the server then replaces the restored state the same way it does on a reconnect:
|
|
3558
|
+
* removed spaces and rooms are dropped, the rest is updated in place, the restored message histories and topics
|
|
3559
|
+
* other than the default ones are fetched anew (as a window holding the latest messages is on a reconnect), and
|
|
3560
|
+
* the lazily loaded lists are refetched on their next access.
|
|
3561
|
+
*
|
|
3562
|
+
* Allowed only once and only before the first `Session`.
|
|
3563
|
+
* @return False if the snapshot was rejected (another version, malformed data, or a session already received) -
|
|
3564
|
+
* the state is not changed then.
|
|
3565
|
+
*/
|
|
3566
|
+
importSnapshot(snapshot) {
|
|
3567
|
+
if (this.sessionReceived || this.snapshotImported || !isValidStateSnapshot(snapshot)) {
|
|
3568
|
+
return false;
|
|
3569
|
+
}
|
|
3570
|
+
this.snapshotImported = true;
|
|
3571
|
+
try {
|
|
3572
|
+
this._me = snapshot.user;
|
|
3573
|
+
this.users._importUser(snapshot.user);
|
|
3574
|
+
this.rooms._importRooms(snapshot.rooms);
|
|
3575
|
+
this.spaces._importSpaces(snapshot.spaces, snapshot.spaceRooms);
|
|
3576
|
+
this.rooms.followedTopics._importFollowedTopics(snapshot.followedTopics);
|
|
3577
|
+
this.emoticons._importEmoticons(snapshot.emoticons, snapshot.spaces.map(space => space.id));
|
|
3578
|
+
this.relationships._importRelationships(snapshot.relationships);
|
|
3579
|
+
this.rooms._importHistories(snapshot.histories, snapshot.roomMembers);
|
|
3580
|
+
} catch {
|
|
3581
|
+
// Not expected after the validation; whatever got restored is replaced by the first session anyway.
|
|
3582
|
+
return false;
|
|
3583
|
+
}
|
|
3584
|
+
this.deferredSession.resolve();
|
|
3585
|
+
return true;
|
|
3586
|
+
}
|
|
2891
3587
|
handleSession(ev) {
|
|
2892
3588
|
this._me = ev.user;
|
|
3589
|
+
this.sessionReceived = true;
|
|
2893
3590
|
this.deferredSession.resolve();
|
|
2894
3591
|
}
|
|
3592
|
+
|
|
3593
|
+
/**
|
|
3594
|
+
* A reconnect keeps the followed topics and emoticons of rooms and spaces that are gone; for the restored ones
|
|
3595
|
+
* the first session is the only chance to tell they are gone.
|
|
3596
|
+
*/
|
|
3597
|
+
handleSessionAfterImport(ev) {
|
|
3598
|
+
if (!this.snapshotImported) {
|
|
3599
|
+
return;
|
|
3600
|
+
}
|
|
3601
|
+
this.snapshotImported = false;
|
|
3602
|
+
this.rooms.followedTopics._forgetRoomsExcept(ev.state.rooms.map(room => room.id));
|
|
3603
|
+
this.emoticons._forgetSpacesExcept(ev.state.spaces.map(space => space.id));
|
|
3604
|
+
this.rooms._refreshImportedTopics();
|
|
3605
|
+
}
|
|
2895
3606
|
}
|
|
2896
3607
|
;// ./src/WebSocketChatClient.ts
|
|
2897
3608
|
|
|
@@ -3366,20 +4077,17 @@ class FilesClient extends AbstractRestClient {
|
|
|
3366
4077
|
return this.send('GET', 'files?' + searchParams);
|
|
3367
4078
|
}
|
|
3368
4079
|
}
|
|
3369
|
-
;// ./src/protocol/schemes/
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
UserStatus[UserStatus["Online"] = 1] = "Online";
|
|
3381
|
-
UserStatus[UserStatus["OnlineAsync"] = 2] = "OnlineAsync";
|
|
3382
|
-
return UserStatus;
|
|
4080
|
+
;// ./src/protocol/schemes/events/Error.ts
|
|
4081
|
+
let ErrorCode = /*#__PURE__*/function (ErrorCode) {
|
|
4082
|
+
ErrorCode["Authentication"] = "AuthenticationException";
|
|
4083
|
+
ErrorCode["CommandRateLimitExceeded"] = "CommandRateLimitExceededException";
|
|
4084
|
+
ErrorCode["InvalidSearchQuery"] = "InvalidSearchQueryException";
|
|
4085
|
+
ErrorCode["MessageSearchUnavailable"] = "MessageSearchUnavailableException";
|
|
4086
|
+
ErrorCode["MessageNotFound"] = "MessageNotFoundException";
|
|
4087
|
+
ErrorCode["RoomNotFound"] = "RoomNotFoundException";
|
|
4088
|
+
ErrorCode["TopicNotFound"] = "TopicNotFoundException";
|
|
4089
|
+
ErrorCode["UserNotFound"] = "UserNotFoundException";
|
|
4090
|
+
return ErrorCode;
|
|
3383
4091
|
}({});
|
|
3384
4092
|
;// ./src/protocol/schemes/Role.ts
|
|
3385
4093
|
/**
|
|
@@ -3512,6 +4220,7 @@ let SpaceIntent = /*#__PURE__*/function (SpaceIntent) {
|
|
|
3512
4220
|
|
|
3513
4221
|
|
|
3514
4222
|
|
|
3515
|
-
|
|
4223
|
+
|
|
4224
|
+
export { AbstractRestClient, ErrorCode, FilesClient, HistoryRotation, IndexedCollection, IndexedObjectCollection, Layer, NO_LIMIT, ObservableIndexedCollection, ObservableIndexedObjectCollection, PermissionDefinition, Permissions, RoleFlag, STATE_SNAPSHOT_VERSION, STORAGE_UNIT_BYTES, SearchRange, SpaceFeature, SpaceIntent, UiPurpose, UserFeature, UserStatus, WebApiChatClient, WebSocketChatClient, extractUserFromMember };
|
|
3516
4225
|
|
|
3517
4226
|
//# sourceMappingURL=index.mjs.map
|