@devanachat/sdk 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +49 -0
- package/README.md +31 -0
- package/build/index.cjs.js +762 -36
- package/build/index.cjs.js.map +1 -1
- package/build/index.mjs +762 -37
- 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/index.d.ts +3 -1
- 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 +18 -1
- package/build/types/state-tracker/RelationshipsManager.d.ts +11 -0
- package/build/types/state-tracker/RoomMessagesHistory.d.ts +26 -4
- 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 +45 -0
- package/build/types/state-tracker/UsersManager.d.ts +5 -0
- package/package.json +1 -1
package/build/index.mjs
CHANGED
|
@@ -439,7 +439,8 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
439
439
|
retainRatio: 1,
|
|
440
440
|
fetchLimit: 50,
|
|
441
441
|
lastFetchCount: 0,
|
|
442
|
-
oldestId: null
|
|
442
|
+
oldestId: null,
|
|
443
|
+
gaps: []
|
|
443
444
|
};
|
|
444
445
|
|
|
445
446
|
/**
|
|
@@ -494,6 +495,16 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
494
495
|
get hasLatest() {
|
|
495
496
|
return [WindowState.LATEST, WindowState.LIVE].includes(this.state);
|
|
496
497
|
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* IDs of the items with a gap in front of them: the item right above in the window is not the real predecessor,
|
|
501
|
+
* an unknown number of items in between was never fetched. Such a seam appears when {@link resyncToLatest} gets
|
|
502
|
+
* a full page that does not reach the loaded items. A marker lives as long as its item stays in the window and
|
|
503
|
+
* is closed once the items right before it are fetched.
|
|
504
|
+
*/
|
|
505
|
+
get gaps() {
|
|
506
|
+
return this.internalState.gaps;
|
|
507
|
+
}
|
|
497
508
|
get hasOldest() {
|
|
498
509
|
return this.state === WindowState.OLDEST || this.state === WindowState.LATEST && this.length < this.fetchLimit || this.internalState.oldestId !== null && this.has(this.internalState.oldestId);
|
|
499
510
|
}
|
|
@@ -503,15 +514,51 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
503
514
|
}
|
|
504
515
|
let result;
|
|
505
516
|
const originalState = this.state;
|
|
506
|
-
this.
|
|
517
|
+
this.beginFetch(WindowState.LATEST);
|
|
507
518
|
try {
|
|
508
519
|
result = await this.fetchLatestItems();
|
|
509
520
|
this.internalState.lastFetchCount = result.length;
|
|
510
521
|
} finally {
|
|
511
|
-
this.
|
|
522
|
+
this.endFetch();
|
|
523
|
+
}
|
|
524
|
+
this.replaceItems(result);
|
|
525
|
+
this.internalState.current = WindowState.LATEST;
|
|
526
|
+
this.emitChangeWithDiff(true, originalState);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Refresh the window with the latest page, keeping the already loaded items instead of replacing them - the
|
|
531
|
+
* reconnect-friendly variant of {@link resetToLatest}. The page is merged on top of the loaded items (an item in
|
|
532
|
+
* both is taken from the page) and only the window size limit pushes the oldest ones out. When the page is full
|
|
533
|
+
* and does not reach the newest loaded item, both parts are still kept and the seam is recorded in {@link gaps}.
|
|
534
|
+
*/
|
|
535
|
+
async resyncToLatest() {
|
|
536
|
+
if (this.internalState.ongoing) {
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
let result;
|
|
540
|
+
const originalState = this.state;
|
|
541
|
+
this.beginFetch(WindowState.LATEST);
|
|
542
|
+
try {
|
|
543
|
+
result = await this.fetchLatestItems();
|
|
544
|
+
this.internalState.lastFetchCount = result.length;
|
|
545
|
+
} finally {
|
|
546
|
+
this.endFetch();
|
|
547
|
+
}
|
|
548
|
+
const loaded = this.items;
|
|
549
|
+
const fetchedIds = new Set(result.map(item => this.getId(item)));
|
|
550
|
+
const retained = loaded.filter(item => !fetchedIds.has(this.getId(item)));
|
|
551
|
+
|
|
552
|
+
// A page shorter than the limit is everything the remote side has, so nothing can be missing in between.
|
|
553
|
+
const isContinuous = !retained.length || result.length < this.internalState.fetchLimit || fetchedIds.has(this.getId(loaded[loaded.length - 1]));
|
|
554
|
+
|
|
555
|
+
// The page is continuous, so only its first item can have a gap in front of it.
|
|
556
|
+
this.forgetGapsOf(result.slice(1).map(item => this.getId(item)));
|
|
557
|
+
if (!isContinuous && !this.internalState.gaps.includes(this.getId(result[0]))) {
|
|
558
|
+
this.internalState.gaps = [...this.internalState.gaps, this.getId(result[0])];
|
|
512
559
|
}
|
|
513
560
|
this._items.deleteAll(); // Directly call deleteAll to prevent event emit.
|
|
514
|
-
this.addItems(result, 'tail');
|
|
561
|
+
this.addItems([...retained, ...result], 'tail');
|
|
515
562
|
this.internalState.current = WindowState.LATEST;
|
|
516
563
|
this.emitChangeWithDiff(true, originalState);
|
|
517
564
|
}
|
|
@@ -521,18 +568,18 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
521
568
|
}
|
|
522
569
|
let result;
|
|
523
570
|
const originalState = this.state;
|
|
524
|
-
|
|
571
|
+
const firstItem = this.getAt(0);
|
|
572
|
+
this.beginFetch(WindowState.PAST);
|
|
525
573
|
try {
|
|
526
574
|
result = await this.fetchItemsBefore();
|
|
527
575
|
this.internalState.lastFetchCount = result ? result.length : 0;
|
|
528
576
|
} finally {
|
|
529
|
-
this.
|
|
577
|
+
this.endFetch();
|
|
530
578
|
}
|
|
531
579
|
if (!result) {
|
|
532
580
|
return this.resetToLatest();
|
|
533
581
|
}
|
|
534
582
|
if (!result.length) {
|
|
535
|
-
const firstItem = this.getAt(0);
|
|
536
583
|
this.internalState.oldestId = firstItem ? this.getId(firstItem) : null;
|
|
537
584
|
await this.refreshFetchedState();
|
|
538
585
|
|
|
@@ -543,6 +590,10 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
543
590
|
this.emitChangeWithDiff(false, originalState);
|
|
544
591
|
return;
|
|
545
592
|
}
|
|
593
|
+
if (firstItem) {
|
|
594
|
+
// The page was fetched as the direct predecessors of the first item, so a gap in front of it is closed.
|
|
595
|
+
this.forgetGapsOf([this.getId(firstItem)]);
|
|
596
|
+
}
|
|
546
597
|
this.addItems(result, 'head');
|
|
547
598
|
await this.refreshFetchedState();
|
|
548
599
|
this.emitChangeWithDiff(true, originalState);
|
|
@@ -563,12 +614,12 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
563
614
|
}
|
|
564
615
|
let result;
|
|
565
616
|
const originalState = this.state;
|
|
566
|
-
this.
|
|
617
|
+
this.beginFetch(WindowState.PAST);
|
|
567
618
|
try {
|
|
568
619
|
result = await this.fetchItemsAfter();
|
|
569
620
|
this.internalState.lastFetchCount = result ? result.length : 0;
|
|
570
621
|
} finally {
|
|
571
|
-
this.
|
|
622
|
+
this.endFetch();
|
|
572
623
|
}
|
|
573
624
|
if (!result) {
|
|
574
625
|
await this.resetToLatest(true);
|
|
@@ -581,26 +632,41 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
581
632
|
return;
|
|
582
633
|
}
|
|
583
634
|
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* Load the items around the given one. Asked for while another fetch is running, it waits for that fetch to end
|
|
638
|
+
* instead of being dropped - when the call returns, the window either holds the item or it could not be loaded.
|
|
639
|
+
*/
|
|
584
640
|
async jumpTo(id) {
|
|
585
|
-
|
|
641
|
+
while (this.internalState.ongoing) {
|
|
642
|
+
await this.internalState.fetchSettled;
|
|
643
|
+
}
|
|
644
|
+
if (this.state !== WindowState.LIVE && this._items.has(id)) {
|
|
586
645
|
return;
|
|
587
646
|
}
|
|
588
647
|
let result;
|
|
589
648
|
const originalState = this.state;
|
|
590
|
-
this.
|
|
649
|
+
this.beginFetch(WindowState.PAST);
|
|
591
650
|
try {
|
|
592
651
|
result = await this.fetchItemsAround(id);
|
|
593
652
|
this.internalState.lastFetchCount = result ? result.length : 0;
|
|
594
653
|
if (result) {
|
|
595
|
-
this.
|
|
596
|
-
this.addItems(result, 'tail');
|
|
654
|
+
this.replaceItems(result);
|
|
597
655
|
await this.refreshFetchedState();
|
|
598
656
|
}
|
|
599
657
|
} finally {
|
|
600
|
-
this.
|
|
658
|
+
this.endFetch();
|
|
601
659
|
}
|
|
602
660
|
this.emitChangeWithDiff(!!result, originalState);
|
|
603
661
|
}
|
|
662
|
+
delete(...ids) {
|
|
663
|
+
super.delete(...ids);
|
|
664
|
+
this.forgetGapsOf(ids);
|
|
665
|
+
}
|
|
666
|
+
deleteAll() {
|
|
667
|
+
super.deleteAll();
|
|
668
|
+
this.internalState.gaps = [];
|
|
669
|
+
}
|
|
604
670
|
async refreshFetchedState() {
|
|
605
671
|
this.internalState.current = (await this.isLatestItemLoaded()) ? WindowState.LATEST : WindowState.PAST;
|
|
606
672
|
}
|
|
@@ -623,6 +689,29 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
623
689
|
this._items.set(...entries, ...loaded);
|
|
624
690
|
return this.trimTail(this.getLowWatermark(highWatermark));
|
|
625
691
|
}
|
|
692
|
+
beginFetch(kind) {
|
|
693
|
+
this.internalState.ongoing = kind;
|
|
694
|
+
this.internalState.fetchSettled = new Promise(resolve => this.internalState.settleFetch = resolve);
|
|
695
|
+
}
|
|
696
|
+
endFetch() {
|
|
697
|
+
this.internalState.ongoing = undefined;
|
|
698
|
+
this.internalState.settleFetch?.();
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* Replace the whole content without emitting an event; the old gap markers mean nothing for the new content.
|
|
703
|
+
*/
|
|
704
|
+
replaceItems(items) {
|
|
705
|
+
this._items.deleteAll();
|
|
706
|
+
this.internalState.gaps = [];
|
|
707
|
+
this.addItems(items, 'tail');
|
|
708
|
+
}
|
|
709
|
+
forgetGapsOf(ids) {
|
|
710
|
+
if (this.internalState.gaps.length && ids.length) {
|
|
711
|
+
const removed = new Set(ids);
|
|
712
|
+
this.internalState.gaps = this.internalState.gaps.filter(id => !removed.has(id));
|
|
713
|
+
}
|
|
714
|
+
}
|
|
626
715
|
emitChangeWithDiff(itemChanged, originalState) {
|
|
627
716
|
if (itemChanged || originalState !== this.state) {
|
|
628
717
|
this.eventTarget.emit('change', {
|
|
@@ -649,6 +738,7 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
649
738
|
deletedItems.push(id);
|
|
650
739
|
}
|
|
651
740
|
this._items.delete(...deletedItems);
|
|
741
|
+
this.forgetGapsOf(deletedItems);
|
|
652
742
|
if (this.internalState.current === WindowState.OLDEST) {
|
|
653
743
|
this.internalState.current = WindowState.PAST;
|
|
654
744
|
}
|
|
@@ -660,6 +750,7 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
660
750
|
}
|
|
661
751
|
const deletedItems = Array.from(this._items.items.keys()).slice(keep);
|
|
662
752
|
this._items.delete(...deletedItems);
|
|
753
|
+
this.forgetGapsOf(deletedItems);
|
|
663
754
|
return deletedItems;
|
|
664
755
|
}
|
|
665
756
|
|
|
@@ -752,6 +843,12 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
752
843
|
}
|
|
753
844
|
return super.resetToLatest(force);
|
|
754
845
|
}
|
|
846
|
+
async resyncToLatest() {
|
|
847
|
+
if (this.internalState.traverseLock) {
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
return super.resyncToLatest();
|
|
851
|
+
}
|
|
755
852
|
async fetchNext() {
|
|
756
853
|
if (this.internalState.traverseLock) {
|
|
757
854
|
return;
|
|
@@ -774,6 +871,45 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
774
871
|
return super.jumpTo(id);
|
|
775
872
|
}
|
|
776
873
|
|
|
874
|
+
/**
|
|
875
|
+
* The latest messages (oldest first) with the reactions of the user, or null if the window does not hold them.
|
|
876
|
+
* @internal
|
|
877
|
+
*/
|
|
878
|
+
_exportLatest(limit) {
|
|
879
|
+
if (this.state !== WindowState.LATEST) {
|
|
880
|
+
return null;
|
|
881
|
+
}
|
|
882
|
+
const items = this.items;
|
|
883
|
+
// A restored window is taken for continuous, so nothing above the newest gap goes into it.
|
|
884
|
+
const continuousFrom = Math.max(0, ...this.internalState.gaps.map(id => items.findIndex(item => item.id === id)));
|
|
885
|
+
const messages = items.slice(Math.max(continuousFrom, items.length - Math.max(limit, this.fetchLimit)));
|
|
886
|
+
const myReactions = {};
|
|
887
|
+
for (const message of messages) {
|
|
888
|
+
if (this.internalState.myReactions[message.id]) {
|
|
889
|
+
myReactions[message.id] = this.internalState.myReactions[message.id];
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
return {
|
|
893
|
+
messages,
|
|
894
|
+
myReactions
|
|
895
|
+
};
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
/**
|
|
899
|
+
* Fill the window with saved latest messages, as if they were just fetched; the next reset to the latest replaces
|
|
900
|
+
* them.
|
|
901
|
+
* @internal
|
|
902
|
+
*/
|
|
903
|
+
_restoreLatest(messages, myReactions) {
|
|
904
|
+
const originalState = this.state;
|
|
905
|
+
this.replaceItems(messages);
|
|
906
|
+
this.internalState.myReactions = {
|
|
907
|
+
...myReactions
|
|
908
|
+
};
|
|
909
|
+
this.internalState.current = WindowState.LATEST;
|
|
910
|
+
this.emitChangeWithDiff(true, originalState);
|
|
911
|
+
}
|
|
912
|
+
|
|
777
913
|
/**
|
|
778
914
|
* For internal use.
|
|
779
915
|
* @internal
|
|
@@ -980,6 +1116,7 @@ class RoomMessagesHistory {
|
|
|
980
1116
|
roomId;
|
|
981
1117
|
historyWindows = new IndexedCollection();
|
|
982
1118
|
traverseLock = false;
|
|
1119
|
+
timeLimitedHistory = false;
|
|
983
1120
|
room;
|
|
984
1121
|
constructor(room, tracker) {
|
|
985
1122
|
this.tracker = tracker;
|
|
@@ -987,7 +1124,7 @@ class RoomMessagesHistory {
|
|
|
987
1124
|
this.room = typeof room === 'string' ? null : room;
|
|
988
1125
|
this.isPreview = this.room === null;
|
|
989
1126
|
if (this.room) {
|
|
990
|
-
this.
|
|
1127
|
+
this.updateHistoryMode(this.room);
|
|
991
1128
|
}
|
|
992
1129
|
if (this.room?.defaultTopic) {
|
|
993
1130
|
this.createHistoryWindowForTopic(this.room.defaultTopic);
|
|
@@ -1020,13 +1157,19 @@ class RoomMessagesHistory {
|
|
|
1020
1157
|
*
|
|
1021
1158
|
* The window bindings are preserved; only windows that the application had
|
|
1022
1159
|
* actually pulled to the latest page (state === LATEST) are refreshed, with
|
|
1023
|
-
* a single
|
|
1024
|
-
*
|
|
1160
|
+
* a single request instead of a chain of catch-up requests. Windows that
|
|
1161
|
+
* were never pulled (LIVE) or belong to an ephemeral room are left
|
|
1025
1162
|
* untouched so their in-memory context survives the reconnect.
|
|
1163
|
+
*
|
|
1164
|
+
* Rooms keeping the full history are reset to the latest page (the rest can
|
|
1165
|
+
* always be traversed back), while the windows of rooms with a time-limited
|
|
1166
|
+
* history (MaxAge) keep what they had loaded and get the missed messages on
|
|
1167
|
+
* top - the server may no longer serve the older ones. If more messages were
|
|
1168
|
+
* missed than a single page holds, the seam is recorded in the window gaps.
|
|
1026
1169
|
*/
|
|
1027
1170
|
async resync(room) {
|
|
1028
1171
|
this.room = room;
|
|
1029
|
-
this.
|
|
1172
|
+
this.updateHistoryMode(room);
|
|
1030
1173
|
if (this.room.defaultTopic) {
|
|
1031
1174
|
this.createHistoryWindowForTopic(this.room.defaultTopic);
|
|
1032
1175
|
}
|
|
@@ -1039,7 +1182,12 @@ class RoomMessagesHistory {
|
|
|
1039
1182
|
if (this.traverseLock) {
|
|
1040
1183
|
continue;
|
|
1041
1184
|
}
|
|
1042
|
-
if (window.state
|
|
1185
|
+
if (window.state !== WindowState.LATEST) {
|
|
1186
|
+
continue;
|
|
1187
|
+
}
|
|
1188
|
+
if (this.timeLimitedHistory) {
|
|
1189
|
+
await window.resyncToLatest();
|
|
1190
|
+
} else {
|
|
1043
1191
|
await window.resetToLatest(true);
|
|
1044
1192
|
}
|
|
1045
1193
|
} catch (_e) {
|
|
@@ -1050,13 +1198,38 @@ class RoomMessagesHistory {
|
|
|
1050
1198
|
}
|
|
1051
1199
|
}
|
|
1052
1200
|
|
|
1201
|
+
/**
|
|
1202
|
+
* For internal use.
|
|
1203
|
+
* @internal
|
|
1204
|
+
*/
|
|
1205
|
+
get _isEphemeral() {
|
|
1206
|
+
return this.traverseLock;
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
/**
|
|
1210
|
+
* The window of the topic, if created; unlike getMessagesWindow(), never creates nor fetches one.
|
|
1211
|
+
* @internal
|
|
1212
|
+
*/
|
|
1213
|
+
_peekWindow(topicId) {
|
|
1214
|
+
return this.historyWindows.get(topicId);
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
/**
|
|
1218
|
+
* For internal use.
|
|
1219
|
+
* @internal
|
|
1220
|
+
*/
|
|
1221
|
+
_restoreWindow(topic, messages, myReactions) {
|
|
1222
|
+
this.createHistoryWindowForTopic(topic);
|
|
1223
|
+
this.historyWindows.get(topic.id)._restoreLatest(messages, myReactions);
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1053
1226
|
/**
|
|
1054
1227
|
* For internal use.
|
|
1055
1228
|
* @internal
|
|
1056
1229
|
*/
|
|
1057
1230
|
async _handleRoomUpdated(ev) {
|
|
1058
1231
|
this.room = ev.room;
|
|
1059
|
-
this.
|
|
1232
|
+
this.updateHistoryMode(ev.room);
|
|
1060
1233
|
if (ev.room.defaultTopic) {
|
|
1061
1234
|
this.createHistoryWindowForTopic(ev.room.defaultTopic);
|
|
1062
1235
|
}
|
|
@@ -1142,8 +1315,9 @@ class RoomMessagesHistory {
|
|
|
1142
1315
|
this.tracker.rooms._deleteTopicsFromRoom(this.roomId, ...deletedTopicIds);
|
|
1143
1316
|
});
|
|
1144
1317
|
}
|
|
1145
|
-
|
|
1318
|
+
updateHistoryMode(room) {
|
|
1146
1319
|
this.traverseLock = room.history.mode === 'Ephemeral';
|
|
1320
|
+
this.timeLimitedHistory = room.history.mode === 'MaxAge';
|
|
1147
1321
|
}
|
|
1148
1322
|
}
|
|
1149
1323
|
;// ./src/state-tracker/MessagesManager.ts
|
|
@@ -1208,6 +1382,43 @@ class MessagesManager {
|
|
|
1208
1382
|
}
|
|
1209
1383
|
return message || null;
|
|
1210
1384
|
}
|
|
1385
|
+
|
|
1386
|
+
/**
|
|
1387
|
+
* For internal use.
|
|
1388
|
+
* @internal
|
|
1389
|
+
*/
|
|
1390
|
+
_importRooms(rooms) {
|
|
1391
|
+
for (const room of rooms) {
|
|
1392
|
+
this.createHistoryForNewRoom(room);
|
|
1393
|
+
}
|
|
1394
|
+
this.deferredSession.resolve();
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
/**
|
|
1398
|
+
* For internal use.
|
|
1399
|
+
* @internal
|
|
1400
|
+
*/
|
|
1401
|
+
_exportLatest(roomId, topicId, limit) {
|
|
1402
|
+
const history = this.roomHistories.get(roomId);
|
|
1403
|
+
|
|
1404
|
+
// Ephemeral history lives only in the memory of the session.
|
|
1405
|
+
if (!history || history._isEphemeral) {
|
|
1406
|
+
return null;
|
|
1407
|
+
}
|
|
1408
|
+
return history._peekWindow(topicId)?._exportLatest(limit) ?? null;
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
/**
|
|
1412
|
+
* For internal use.
|
|
1413
|
+
* @internal
|
|
1414
|
+
*/
|
|
1415
|
+
_importHistory(roomId, topic, snapshot) {
|
|
1416
|
+
const history = this.roomHistories.get(roomId);
|
|
1417
|
+
if (history && !history._isEphemeral) {
|
|
1418
|
+
const messages = snapshot.messages.filter(message => message.location.roomId === roomId && message.location.topicId === topic.id);
|
|
1419
|
+
history._restoreWindow(topic, messages, snapshot.myReactions);
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1211
1422
|
forEachRoomHistory(callback) {
|
|
1212
1423
|
for (const history of this.roomHistories.items.values()) {
|
|
1213
1424
|
callback(history);
|
|
@@ -1428,6 +1639,47 @@ class FollowedTopicsManager extends EventTarget {
|
|
|
1428
1639
|
this.followedTopics.get(roomId)?.delete(...topicIds);
|
|
1429
1640
|
topicIds.forEach(topicId => this.invalidateUnreadSummaries(roomId, topicId));
|
|
1430
1641
|
}
|
|
1642
|
+
|
|
1643
|
+
/**
|
|
1644
|
+
* For internal use.
|
|
1645
|
+
* @internal
|
|
1646
|
+
*/
|
|
1647
|
+
_exportFollowedTopics() {
|
|
1648
|
+
return Array.from(this.followedTopics.items).filter(([roomId]) => this.tracker.rooms._getRoom(roomId)).map(([roomId, followedTopics]) => ({
|
|
1649
|
+
roomId,
|
|
1650
|
+
followedTopics: followedTopics.items
|
|
1651
|
+
}));
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
/**
|
|
1655
|
+
* Expects the rooms to be imported already - followed topics of other rooms are skipped.
|
|
1656
|
+
* @internal
|
|
1657
|
+
*/
|
|
1658
|
+
_importFollowedTopics(snapshots) {
|
|
1659
|
+
for (const {
|
|
1660
|
+
roomId,
|
|
1661
|
+
followedTopics
|
|
1662
|
+
} of snapshots) {
|
|
1663
|
+
if (this.tracker.rooms._getRoom(roomId)) {
|
|
1664
|
+
this.applyRoomFollowedTopics(roomId, followedTopics);
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
this.invalidateUnreadSummaries();
|
|
1668
|
+
this.deferredSession.resolve();
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
/**
|
|
1672
|
+
* For internal use.
|
|
1673
|
+
* @internal
|
|
1674
|
+
*/
|
|
1675
|
+
_forgetRoomsExcept(roomIds) {
|
|
1676
|
+
const keptRoomIds = new Set(roomIds);
|
|
1677
|
+
for (const roomId of Array.from(this.followedTopics.items.keys())) {
|
|
1678
|
+
if (!keptRoomIds.has(roomId)) {
|
|
1679
|
+
this.clearRoomFollowedTopicsStructures(roomId);
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1431
1683
|
handleSession(ev) {
|
|
1432
1684
|
// Keep cached followed-topic collections (they drive unread indicators
|
|
1433
1685
|
// that would otherwise blank on reconnect), but mark them stale and drop
|
|
@@ -1628,11 +1880,148 @@ class FollowedTopicsManager extends EventTarget {
|
|
|
1628
1880
|
this.invalidateUnreadSummaries(roomId);
|
|
1629
1881
|
}
|
|
1630
1882
|
}
|
|
1883
|
+
;// ./src/protocol/schemes/User.ts
|
|
1884
|
+
/**
|
|
1885
|
+
* Availability of a user, aggregated over all of their sessions.
|
|
1886
|
+
*
|
|
1887
|
+
* - `Offline` - no session is connected and none can be reached asynchronously.
|
|
1888
|
+
* - `Online` - at least one session holds a live connection.
|
|
1889
|
+
* - `OnlineAsync` - no live connection, but a push-registered device was active
|
|
1890
|
+
* recently, so a message will still reach the user.
|
|
1891
|
+
*/
|
|
1892
|
+
let UserStatus = /*#__PURE__*/function (UserStatus) {
|
|
1893
|
+
UserStatus[UserStatus["Offline"] = 0] = "Offline";
|
|
1894
|
+
UserStatus[UserStatus["Online"] = 1] = "Online";
|
|
1895
|
+
UserStatus[UserStatus["OnlineAsync"] = 2] = "OnlineAsync";
|
|
1896
|
+
return UserStatus;
|
|
1897
|
+
}({});
|
|
1898
|
+
;// ./src/state-tracker/StateSnapshot.ts
|
|
1899
|
+
|
|
1900
|
+
|
|
1901
|
+
/**
|
|
1902
|
+
* Version of the {@link StateSnapshot} format. A snapshot of any other version is rejected on import.
|
|
1903
|
+
*/
|
|
1904
|
+
const STATE_SNAPSHOT_VERSION = 1;
|
|
1905
|
+
|
|
1906
|
+
/**
|
|
1907
|
+
* Plain, JSON-serializable copy of the state tracked by {@link ChatStateTracker}, made to restore the state at the
|
|
1908
|
+
* application start (before the connection is established) - see {@link ChatStateTracker.exportSnapshot}.
|
|
1909
|
+
*
|
|
1910
|
+
* Only the data that lasts is included: the current user, the joined spaces (with roles) and rooms (with the default
|
|
1911
|
+
* topics), and the lazily loaded lists already fetched - rooms of spaces, followed topics, emoticons and relationships.
|
|
1912
|
+
* The message histories of the topics named in the {@link StateSnapshotIntent} are included too, with what their
|
|
1913
|
+
* display needs: the topics referenced by the messages and, optionally, the members of the rooms.
|
|
1914
|
+
* Left out on purpose:
|
|
1915
|
+
* - presence - every user status is saved as `Offline`, a status from the past would show people online who are not;
|
|
1916
|
+
* - members of spaces, and of rooms not asked for - large, made mostly of presence, and refetched cheaply on demand;
|
|
1917
|
+
* - other message histories and topics, histories of ephemeral rooms (they live only in the memory of the session),
|
|
1918
|
+
* permission overwrites and room previews - loaded on demand once the connection is up, as on a reconnect.
|
|
1919
|
+
*/
|
|
1920
|
+
|
|
1921
|
+
/**
|
|
1922
|
+
* What {@link ChatStateTracker.exportSnapshot} should include beyond the state that is always exported.
|
|
1923
|
+
*/
|
|
1924
|
+
|
|
1925
|
+
function withoutPresence(user) {
|
|
1926
|
+
return {
|
|
1927
|
+
...user,
|
|
1928
|
+
status: UserStatus.Offline,
|
|
1929
|
+
online: false
|
|
1930
|
+
};
|
|
1931
|
+
}
|
|
1932
|
+
function messageWithoutPresence(message) {
|
|
1933
|
+
return {
|
|
1934
|
+
...message,
|
|
1935
|
+
author: {
|
|
1936
|
+
...message.author,
|
|
1937
|
+
user: withoutPresence(message.author.user)
|
|
1938
|
+
}
|
|
1939
|
+
};
|
|
1940
|
+
}
|
|
1941
|
+
function memberWithoutPresence(member) {
|
|
1942
|
+
return {
|
|
1943
|
+
...member,
|
|
1944
|
+
user: member.user ? withoutPresence(member.user) : member.user,
|
|
1945
|
+
spaceMember: member.spaceMember ? {
|
|
1946
|
+
...member.spaceMember,
|
|
1947
|
+
user: withoutPresence(member.spaceMember.user)
|
|
1948
|
+
} : member.spaceMember
|
|
1949
|
+
};
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
/**
|
|
1953
|
+
* Structural check of the data restored from a storage - deep enough that importing it cannot throw, as the state
|
|
1954
|
+
* handlers dereference these fields without checking them.
|
|
1955
|
+
*/
|
|
1956
|
+
function isValidStateSnapshot(value) {
|
|
1957
|
+
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);
|
|
1958
|
+
}
|
|
1959
|
+
function isObject(value) {
|
|
1960
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
1961
|
+
}
|
|
1962
|
+
function isArrayOf(value, isItem) {
|
|
1963
|
+
return Array.isArray(value) && value.every(isItem);
|
|
1964
|
+
}
|
|
1965
|
+
function isString(value) {
|
|
1966
|
+
return typeof value === 'string';
|
|
1967
|
+
}
|
|
1968
|
+
function hasId(value) {
|
|
1969
|
+
return isObject(value) && isString(value.id);
|
|
1970
|
+
}
|
|
1971
|
+
function isUser(value) {
|
|
1972
|
+
return hasId(value);
|
|
1973
|
+
}
|
|
1974
|
+
function isRole(value) {
|
|
1975
|
+
return hasId(value) && typeof value.priority === 'number';
|
|
1976
|
+
}
|
|
1977
|
+
function isSpace(value) {
|
|
1978
|
+
return hasId(value) && isArrayOf(value.roles, isRole) && isArrayOf(value.defaultRooms, isString) && (value.systemRoom === null || isString(value.systemRoom));
|
|
1979
|
+
}
|
|
1980
|
+
function isTopic(value) {
|
|
1981
|
+
return hasId(value) && (value.refMessage === undefined || value.refMessage === null || isMessage(value.refMessage));
|
|
1982
|
+
}
|
|
1983
|
+
function isMessage(value) {
|
|
1984
|
+
return hasId(value) && isObject(value.location) && isString(value.location.topicId);
|
|
1985
|
+
}
|
|
1986
|
+
function isFullMessage(value) {
|
|
1987
|
+
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);
|
|
1988
|
+
}
|
|
1989
|
+
function isRoom(value) {
|
|
1990
|
+
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);
|
|
1991
|
+
}
|
|
1992
|
+
function isSpaceRooms(value) {
|
|
1993
|
+
return isObject(value) && isString(value.spaceId) && isArrayOf(value.summaries, hasId);
|
|
1994
|
+
}
|
|
1995
|
+
function isFollowedTopic(value) {
|
|
1996
|
+
return isObject(value) && isObject(value.location) && isString(value.location.roomId) && isString(value.location.topicId);
|
|
1997
|
+
}
|
|
1998
|
+
function isRoomFollowedTopics(value) {
|
|
1999
|
+
return isObject(value) && isString(value.roomId) && isArrayOf(value.followedTopics, isFollowedTopic);
|
|
2000
|
+
}
|
|
2001
|
+
function isEmoticons(value) {
|
|
2002
|
+
return isObject(value) && (value.spaceId === null || isString(value.spaceId)) && isArrayOf(value.emoticons, hasId);
|
|
2003
|
+
}
|
|
2004
|
+
function isRelationship(value) {
|
|
2005
|
+
return isObject(value) && isUser(value.refUser) && isString(value.type);
|
|
2006
|
+
}
|
|
2007
|
+
function isUserReactions(value) {
|
|
2008
|
+
return isObject(value) && Object.values(value).every(reactions => isArrayOf(reactions, isObject));
|
|
2009
|
+
}
|
|
2010
|
+
function isTopicHistory(value) {
|
|
2011
|
+
return isObject(value) && isString(value.roomId) && isString(value.topicId) && isArrayOf(value.messages, isFullMessage) && isUserReactions(value.myReactions) && isArrayOf(value.topics, isTopic);
|
|
2012
|
+
}
|
|
2013
|
+
function isRoomMember(value) {
|
|
2014
|
+
return isObject(value) && (isUser(value.user) || isObject(value.spaceMember) && isUser(value.spaceMember.user));
|
|
2015
|
+
}
|
|
2016
|
+
function isRoomMembers(value) {
|
|
2017
|
+
return isObject(value) && isString(value.roomId) && isArrayOf(value.members, isRoomMember);
|
|
2018
|
+
}
|
|
1631
2019
|
;// ./src/state-tracker/RoomsManager.ts
|
|
1632
2020
|
|
|
1633
2021
|
|
|
1634
2022
|
|
|
1635
2023
|
|
|
2024
|
+
|
|
1636
2025
|
class RoomsManager {
|
|
1637
2026
|
messages;
|
|
1638
2027
|
followedTopics;
|
|
@@ -1642,6 +2031,7 @@ class RoomsManager {
|
|
|
1642
2031
|
deferredSession = new DeferredTask();
|
|
1643
2032
|
membersPromises = new PromiseRegistry();
|
|
1644
2033
|
topicsPromises = new PromiseRegistry();
|
|
2034
|
+
importedTopicIds = new Map();
|
|
1645
2035
|
constructor(tracker) {
|
|
1646
2036
|
this.tracker = tracker;
|
|
1647
2037
|
this.messages = new MessagesManager(tracker);
|
|
@@ -1746,6 +2136,148 @@ class RoomsManager {
|
|
|
1746
2136
|
_deleteTopicsFromRoom(roomId, ...topicIds) {
|
|
1747
2137
|
this.topics.get(roomId)?.delete(...topicIds);
|
|
1748
2138
|
}
|
|
2139
|
+
|
|
2140
|
+
/**
|
|
2141
|
+
* For internal use.
|
|
2142
|
+
* @internal
|
|
2143
|
+
*/
|
|
2144
|
+
_exportRooms() {
|
|
2145
|
+
return this.list.items.map(room => room.recipients ? {
|
|
2146
|
+
...room,
|
|
2147
|
+
recipients: room.recipients.map(withoutPresence)
|
|
2148
|
+
} : room);
|
|
2149
|
+
}
|
|
2150
|
+
|
|
2151
|
+
/**
|
|
2152
|
+
* For internal use.
|
|
2153
|
+
* @internal
|
|
2154
|
+
*/
|
|
2155
|
+
_importRooms(rooms) {
|
|
2156
|
+
this.messages._importRooms(rooms);
|
|
2157
|
+
this.addJoinedRooms(...rooms);
|
|
2158
|
+
this.deferredSession.resolve();
|
|
2159
|
+
}
|
|
2160
|
+
|
|
2161
|
+
/**
|
|
2162
|
+
* For internal use.
|
|
2163
|
+
* @internal
|
|
2164
|
+
*/
|
|
2165
|
+
_exportHistories(intent) {
|
|
2166
|
+
const histories = [];
|
|
2167
|
+
const exported = new Set();
|
|
2168
|
+
for (const {
|
|
2169
|
+
roomId,
|
|
2170
|
+
topicId: requestedTopicId
|
|
2171
|
+
} of intent.histories ?? []) {
|
|
2172
|
+
const room = this.list.get(roomId);
|
|
2173
|
+
const topicId = requestedTopicId ?? room?.defaultTopic?.id;
|
|
2174
|
+
if (!room || !topicId || exported.has(`${roomId}/${topicId}`)) {
|
|
2175
|
+
continue;
|
|
2176
|
+
}
|
|
2177
|
+
const latest = this.messages._exportLatest(roomId, topicId, intent.historyLimit ?? 0);
|
|
2178
|
+
const topics = this.topics.get(roomId);
|
|
2179
|
+
|
|
2180
|
+
// A topic other than the default one is restored from the snapshot, so it has to be known.
|
|
2181
|
+
if (!latest || topicId !== room.defaultTopic?.id && !topics?.has(topicId)) {
|
|
2182
|
+
continue;
|
|
2183
|
+
}
|
|
2184
|
+
const topicIds = new Set([topicId, ...latest.messages.map(message => message.topicRef)]);
|
|
2185
|
+
topicIds.delete(room.defaultTopic?.id);
|
|
2186
|
+
exported.add(`${roomId}/${topicId}`);
|
|
2187
|
+
histories.push({
|
|
2188
|
+
roomId,
|
|
2189
|
+
topicId,
|
|
2190
|
+
messages: latest.messages.map(messageWithoutPresence),
|
|
2191
|
+
myReactions: latest.myReactions,
|
|
2192
|
+
topics: Array.from(topicIds).filter(id => topics?.has(id)).map(id => topics.get(id))
|
|
2193
|
+
});
|
|
2194
|
+
}
|
|
2195
|
+
return histories;
|
|
2196
|
+
}
|
|
2197
|
+
|
|
2198
|
+
/**
|
|
2199
|
+
* Members of private conversations are not exported - they are made of the recipients of the room.
|
|
2200
|
+
* @internal
|
|
2201
|
+
*/
|
|
2202
|
+
_exportMembers(roomIds, limit) {
|
|
2203
|
+
const snapshots = [];
|
|
2204
|
+
if (!limit) {
|
|
2205
|
+
return snapshots;
|
|
2206
|
+
}
|
|
2207
|
+
for (const roomId of new Set(roomIds)) {
|
|
2208
|
+
const members = this.members.get(roomId);
|
|
2209
|
+
if (members && members.length <= limit && this.list.get(roomId)?.type !== 'Pm') {
|
|
2210
|
+
snapshots.push({
|
|
2211
|
+
roomId,
|
|
2212
|
+
members: members.items.map(memberWithoutPresence)
|
|
2213
|
+
});
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
return snapshots;
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
/**
|
|
2220
|
+
* Expects the rooms to be imported already - histories and members of other rooms are skipped.
|
|
2221
|
+
* @internal
|
|
2222
|
+
*/
|
|
2223
|
+
_importHistories(histories, roomMembers) {
|
|
2224
|
+
for (const {
|
|
2225
|
+
roomId,
|
|
2226
|
+
members
|
|
2227
|
+
} of roomMembers) {
|
|
2228
|
+
if (this.list.has(roomId) && this.list.get(roomId).type !== 'Pm') {
|
|
2229
|
+
this.handleRoomMembers({
|
|
2230
|
+
id: roomId,
|
|
2231
|
+
members
|
|
2232
|
+
});
|
|
2233
|
+
// Served from the snapshot until the session invalidates it.
|
|
2234
|
+
this.membersPromises.register(Promise.resolve(), roomId);
|
|
2235
|
+
}
|
|
2236
|
+
}
|
|
2237
|
+
for (const history of histories) {
|
|
2238
|
+
const room = this.list.get(history.roomId);
|
|
2239
|
+
if (!room) {
|
|
2240
|
+
continue;
|
|
2241
|
+
}
|
|
2242
|
+
const topics = history.topics.filter(topic => topic.id !== room.defaultTopic?.id);
|
|
2243
|
+
if (topics.length) {
|
|
2244
|
+
this.addJoinedRoomTopics(room.id, ...topics);
|
|
2245
|
+
const importedTopicIds = this.importedTopicIds.get(room.id) ?? new Set();
|
|
2246
|
+
topics.forEach(topic => importedTopicIds.add(topic.id));
|
|
2247
|
+
this.importedTopicIds.set(room.id, importedTopicIds);
|
|
2248
|
+
}
|
|
2249
|
+
const topic = history.topicId === room.defaultTopic?.id ? room.defaultTopic : this.topics.get(room.id)?.get(history.topicId);
|
|
2250
|
+
if (topic) {
|
|
2251
|
+
this.messages._importHistory(room.id, topic, history);
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
/**
|
|
2257
|
+
* The session carries only the default topics, so the other restored ones are fetched anew.
|
|
2258
|
+
* @internal
|
|
2259
|
+
*/
|
|
2260
|
+
_refreshImportedTopics() {
|
|
2261
|
+
const importedTopicIds = Array.from(this.importedTopicIds);
|
|
2262
|
+
this.importedTopicIds.clear();
|
|
2263
|
+
for (const [roomId, ids] of importedTopicIds) {
|
|
2264
|
+
if (!this.list.has(roomId)) {
|
|
2265
|
+
continue;
|
|
2266
|
+
}
|
|
2267
|
+
const topicIds = Array.from(ids);
|
|
2268
|
+
const promise = this.tracker.client.send('GetTopics', {
|
|
2269
|
+
roomId,
|
|
2270
|
+
topicIds
|
|
2271
|
+
}).then(result => {
|
|
2272
|
+
if (!result.error) {
|
|
2273
|
+
this.topics.get(roomId)?.set(...result.data.topics);
|
|
2274
|
+
}
|
|
2275
|
+
});
|
|
2276
|
+
topicIds.forEach(topicId => this.topicsPromises.register(promise, roomId + topicId));
|
|
2277
|
+
// Best effort: on a failure the restored topics stay, as after a reconnect.
|
|
2278
|
+
promise.catch(() => undefined);
|
|
2279
|
+
}
|
|
2280
|
+
}
|
|
1749
2281
|
deleteRoom(...roomIds) {
|
|
1750
2282
|
this.list.delete(...roomIds);
|
|
1751
2283
|
this.members.delete(...roomIds);
|
|
@@ -2142,6 +2674,47 @@ class SpacesManager {
|
|
|
2142
2674
|
const members = await this.getMembers(spaceId);
|
|
2143
2675
|
return members?.items.find(member => member.user.id === userId);
|
|
2144
2676
|
}
|
|
2677
|
+
|
|
2678
|
+
/**
|
|
2679
|
+
* For internal use.
|
|
2680
|
+
* @internal
|
|
2681
|
+
*/
|
|
2682
|
+
_exportSpaces() {
|
|
2683
|
+
return this.list.items;
|
|
2684
|
+
}
|
|
2685
|
+
|
|
2686
|
+
/**
|
|
2687
|
+
* Room listings of the joined spaces only - of other spaces they are fetched for a moment's look.
|
|
2688
|
+
* @internal
|
|
2689
|
+
*/
|
|
2690
|
+
_exportSpaceRooms() {
|
|
2691
|
+
return Array.from(this.rooms.items).filter(([spaceId]) => this.list.has(spaceId)).map(([spaceId, summaries]) => ({
|
|
2692
|
+
spaceId,
|
|
2693
|
+
summaries: summaries.items
|
|
2694
|
+
}));
|
|
2695
|
+
}
|
|
2696
|
+
|
|
2697
|
+
/**
|
|
2698
|
+
* For internal use.
|
|
2699
|
+
* @internal
|
|
2700
|
+
*/
|
|
2701
|
+
_importSpaces(spaces, spaceRooms) {
|
|
2702
|
+
this.addJoinedSpaces(...spaces);
|
|
2703
|
+
for (const {
|
|
2704
|
+
spaceId,
|
|
2705
|
+
summaries
|
|
2706
|
+
} of spaceRooms) {
|
|
2707
|
+
if (this.list.has(spaceId)) {
|
|
2708
|
+
this.handleSpaceRooms({
|
|
2709
|
+
id: spaceId,
|
|
2710
|
+
summaries
|
|
2711
|
+
});
|
|
2712
|
+
// Served from the snapshot until the session invalidates it.
|
|
2713
|
+
this.roomsPromises.register(Promise.resolve(), spaceId);
|
|
2714
|
+
}
|
|
2715
|
+
}
|
|
2716
|
+
this.deferredSession.resolve();
|
|
2717
|
+
}
|
|
2145
2718
|
handleNewRole(ev) {
|
|
2146
2719
|
this.roles.get(ev.spaceId).set(ev.role);
|
|
2147
2720
|
this.syncSpaceRoles(ev.spaceId);
|
|
@@ -2763,6 +3336,52 @@ class EmoticonsManager {
|
|
|
2763
3336
|
await this.emoticonsPromises.get(key);
|
|
2764
3337
|
return this.list.get(key);
|
|
2765
3338
|
}
|
|
3339
|
+
|
|
3340
|
+
/**
|
|
3341
|
+
* For internal use.
|
|
3342
|
+
* @internal
|
|
3343
|
+
*/
|
|
3344
|
+
_exportEmoticons() {
|
|
3345
|
+
return Array.from(this.list.items).map(([key, emoticons]) => ({
|
|
3346
|
+
spaceId: key === GLOBAL_KEY ? null : key,
|
|
3347
|
+
emoticons: emoticons.items
|
|
3348
|
+
}));
|
|
3349
|
+
}
|
|
3350
|
+
|
|
3351
|
+
/**
|
|
3352
|
+
* Emoticons of spaces other than the given ones are skipped.
|
|
3353
|
+
* @internal
|
|
3354
|
+
*/
|
|
3355
|
+
_importEmoticons(snapshots, spaceIds) {
|
|
3356
|
+
const joinedSpaceIds = new Set(spaceIds);
|
|
3357
|
+
for (const {
|
|
3358
|
+
spaceId,
|
|
3359
|
+
emoticons
|
|
3360
|
+
} of snapshots) {
|
|
3361
|
+
if (spaceId !== null && !joinedSpaceIds.has(spaceId)) {
|
|
3362
|
+
continue;
|
|
3363
|
+
}
|
|
3364
|
+
this.handleEmoticons({
|
|
3365
|
+
location: {
|
|
3366
|
+
spaceId: spaceId ?? undefined
|
|
3367
|
+
},
|
|
3368
|
+
emoticons
|
|
3369
|
+
});
|
|
3370
|
+
// Served from the snapshot until the session invalidates it.
|
|
3371
|
+
this.emoticonsPromises.register(Promise.resolve(), spaceId ?? GLOBAL_KEY);
|
|
3372
|
+
}
|
|
3373
|
+
}
|
|
3374
|
+
|
|
3375
|
+
/**
|
|
3376
|
+
* For internal use.
|
|
3377
|
+
* @internal
|
|
3378
|
+
*/
|
|
3379
|
+
_forgetSpacesExcept(spaceIds) {
|
|
3380
|
+
const keptKeys = new Set([GLOBAL_KEY, ...spaceIds]);
|
|
3381
|
+
const removedKeys = Array.from(this.list.items.keys()).filter(key => !keptKeys.has(key));
|
|
3382
|
+
this.list.delete(...removedKeys);
|
|
3383
|
+
this.emoticonsPromises.forget(...removedKeys);
|
|
3384
|
+
}
|
|
2766
3385
|
handleEmoticons(event) {
|
|
2767
3386
|
const spaceId = event.location.spaceId ?? GLOBAL_KEY;
|
|
2768
3387
|
if (!this.list.has(spaceId)) {
|
|
@@ -2818,6 +3437,14 @@ class UsersManager {
|
|
|
2818
3437
|
async getAvailable() {
|
|
2819
3438
|
return this.users;
|
|
2820
3439
|
}
|
|
3440
|
+
|
|
3441
|
+
/**
|
|
3442
|
+
* For internal use.
|
|
3443
|
+
* @internal
|
|
3444
|
+
*/
|
|
3445
|
+
_importUser(user) {
|
|
3446
|
+
this.handleUsers([user]);
|
|
3447
|
+
}
|
|
2821
3448
|
handleMembers(members) {
|
|
2822
3449
|
this.handleUsers(members.map(extractUserFromMember));
|
|
2823
3450
|
}
|
|
@@ -2864,11 +3491,13 @@ function isSameUser(a, b) {
|
|
|
2864
3491
|
;// ./src/state-tracker/RelationshipsManager.ts
|
|
2865
3492
|
|
|
2866
3493
|
|
|
3494
|
+
|
|
2867
3495
|
const getId = (refUserId, type) => `${refUserId}-${type}`;
|
|
2868
3496
|
const getIdFromRelationship = relationship => getId(relationship.refUser.id, relationship.type);
|
|
2869
3497
|
class RelationshipsManager {
|
|
2870
3498
|
relationships = new ObservableIndexedObjectCollection(getIdFromRelationship);
|
|
2871
3499
|
promises = new PromiseRegistry();
|
|
3500
|
+
loaded = false;
|
|
2872
3501
|
constructor(tracker) {
|
|
2873
3502
|
this.tracker = tracker;
|
|
2874
3503
|
this.tracker.client.on('Relationships', ev => this.handleRelationships(ev));
|
|
@@ -2892,7 +3521,33 @@ class RelationshipsManager {
|
|
|
2892
3521
|
await this.get();
|
|
2893
3522
|
return this.relationships.has(getId(refUserId, type));
|
|
2894
3523
|
}
|
|
3524
|
+
|
|
3525
|
+
/**
|
|
3526
|
+
* For internal use.
|
|
3527
|
+
* @internal
|
|
3528
|
+
*/
|
|
3529
|
+
_exportRelationships() {
|
|
3530
|
+
return this.loaded ? this.relationships.items.map(relationship => ({
|
|
3531
|
+
...relationship,
|
|
3532
|
+
refUser: withoutPresence(relationship.refUser)
|
|
3533
|
+
})) : null;
|
|
3534
|
+
}
|
|
3535
|
+
|
|
3536
|
+
/**
|
|
3537
|
+
* For internal use.
|
|
3538
|
+
* @internal
|
|
3539
|
+
*/
|
|
3540
|
+
_importRelationships(relationships) {
|
|
3541
|
+
if (relationships) {
|
|
3542
|
+
this.handleRelationships({
|
|
3543
|
+
relationships
|
|
3544
|
+
});
|
|
3545
|
+
// Served from the snapshot until the session invalidates it.
|
|
3546
|
+
this.promises.register(Promise.resolve(), 'all');
|
|
3547
|
+
}
|
|
3548
|
+
}
|
|
2895
3549
|
handleRelationships(ev) {
|
|
3550
|
+
this.loaded = true;
|
|
2896
3551
|
// Full list from the server: reconcile in place so a reconnect refetch
|
|
2897
3552
|
// updates a bound (visible) list without an intermediate empty state.
|
|
2898
3553
|
this.relationships.reconcile(...ev.relationships);
|
|
@@ -2921,6 +3576,7 @@ class RelationshipsManager {
|
|
|
2921
3576
|
|
|
2922
3577
|
|
|
2923
3578
|
|
|
3579
|
+
|
|
2924
3580
|
class ChatStateTracker {
|
|
2925
3581
|
client;
|
|
2926
3582
|
|
|
@@ -2955,6 +3611,8 @@ class ChatStateTracker {
|
|
|
2955
3611
|
relationships;
|
|
2956
3612
|
_me = null;
|
|
2957
3613
|
deferredSession = new DeferredTask();
|
|
3614
|
+
sessionReceived = false;
|
|
3615
|
+
snapshotImported = false;
|
|
2958
3616
|
constructor(client) {
|
|
2959
3617
|
this.client = client;
|
|
2960
3618
|
this.client.on('Session', ev => this.handleSession(ev));
|
|
@@ -2964,6 +3622,9 @@ class ChatStateTracker {
|
|
|
2964
3622
|
this.emoticons = new EmoticonsManager(this);
|
|
2965
3623
|
this.users = new UsersManager(this);
|
|
2966
3624
|
this.relationships = new RelationshipsManager(this);
|
|
3625
|
+
|
|
3626
|
+
// Registered after the managers, so it runs once they have applied the session.
|
|
3627
|
+
this.client.on('Session', ev => this.handleSessionAfterImport(ev));
|
|
2967
3628
|
}
|
|
2968
3629
|
get me() {
|
|
2969
3630
|
return this._me;
|
|
@@ -2972,10 +3633,88 @@ class ChatStateTracker {
|
|
|
2972
3633
|
await this.deferredSession.promise;
|
|
2973
3634
|
return this._me;
|
|
2974
3635
|
}
|
|
3636
|
+
|
|
3637
|
+
/**
|
|
3638
|
+
* Export the tracked state to a plain, JSON-serializable object, to be saved and restored with
|
|
3639
|
+
* {@link importSnapshot} at the next start of the application, before the connection is established.
|
|
3640
|
+
* See {@link StateSnapshot} for what is included.
|
|
3641
|
+
* @param intent What to include beyond the state that is always exported, e.g. the message histories.
|
|
3642
|
+
* @return Null if there is no state yet (no session has been received or imported).
|
|
3643
|
+
*/
|
|
3644
|
+
exportSnapshot(intent = {}) {
|
|
3645
|
+
if (!this._me) {
|
|
3646
|
+
return null;
|
|
3647
|
+
}
|
|
3648
|
+
const snapshot = {
|
|
3649
|
+
version: STATE_SNAPSHOT_VERSION,
|
|
3650
|
+
createdAt: Date.now(),
|
|
3651
|
+
user: withoutPresence(this._me),
|
|
3652
|
+
spaces: this.spaces._exportSpaces(),
|
|
3653
|
+
rooms: this.rooms._exportRooms(),
|
|
3654
|
+
spaceRooms: this.spaces._exportSpaceRooms(),
|
|
3655
|
+
followedTopics: this.rooms.followedTopics._exportFollowedTopics(),
|
|
3656
|
+
emoticons: this.emoticons._exportEmoticons(),
|
|
3657
|
+
relationships: this.relationships._exportRelationships(),
|
|
3658
|
+
histories: this.rooms._exportHistories(intent),
|
|
3659
|
+
roomMembers: this.rooms._exportMembers((intent.histories ?? []).map(history => history.roomId), intent.roomMembersLimit ?? 0)
|
|
3660
|
+
};
|
|
3661
|
+
|
|
3662
|
+
// Detaches the snapshot from the live state objects and leaves nothing a JSON round trip would change.
|
|
3663
|
+
return JSON.parse(JSON.stringify(snapshot));
|
|
3664
|
+
}
|
|
3665
|
+
|
|
3666
|
+
/**
|
|
3667
|
+
* Restore the state exported with {@link exportSnapshot}, so it is available before the connection is
|
|
3668
|
+
* established. The collections emit their usual change events and the getters waiting for the session resolve.
|
|
3669
|
+
* The first `Session` from the server then replaces the restored state the same way it does on a reconnect:
|
|
3670
|
+
* removed spaces and rooms are dropped, the rest is updated in place, the restored message histories and topics
|
|
3671
|
+
* other than the default ones are fetched anew (as a window holding the latest messages is on a reconnect), and
|
|
3672
|
+
* the lazily loaded lists are refetched on their next access.
|
|
3673
|
+
*
|
|
3674
|
+
* Allowed only once and only before the first `Session`.
|
|
3675
|
+
* @return False if the snapshot was rejected (another version, malformed data, or a session already received) -
|
|
3676
|
+
* the state is not changed then.
|
|
3677
|
+
*/
|
|
3678
|
+
importSnapshot(snapshot) {
|
|
3679
|
+
if (this.sessionReceived || this.snapshotImported || !isValidStateSnapshot(snapshot)) {
|
|
3680
|
+
return false;
|
|
3681
|
+
}
|
|
3682
|
+
this.snapshotImported = true;
|
|
3683
|
+
try {
|
|
3684
|
+
this._me = snapshot.user;
|
|
3685
|
+
this.users._importUser(snapshot.user);
|
|
3686
|
+
this.rooms._importRooms(snapshot.rooms);
|
|
3687
|
+
this.spaces._importSpaces(snapshot.spaces, snapshot.spaceRooms);
|
|
3688
|
+
this.rooms.followedTopics._importFollowedTopics(snapshot.followedTopics);
|
|
3689
|
+
this.emoticons._importEmoticons(snapshot.emoticons, snapshot.spaces.map(space => space.id));
|
|
3690
|
+
this.relationships._importRelationships(snapshot.relationships);
|
|
3691
|
+
this.rooms._importHistories(snapshot.histories, snapshot.roomMembers);
|
|
3692
|
+
} catch {
|
|
3693
|
+
// Not expected after the validation; whatever got restored is replaced by the first session anyway.
|
|
3694
|
+
return false;
|
|
3695
|
+
}
|
|
3696
|
+
this.deferredSession.resolve();
|
|
3697
|
+
return true;
|
|
3698
|
+
}
|
|
2975
3699
|
handleSession(ev) {
|
|
2976
3700
|
this._me = ev.user;
|
|
3701
|
+
this.sessionReceived = true;
|
|
2977
3702
|
this.deferredSession.resolve();
|
|
2978
3703
|
}
|
|
3704
|
+
|
|
3705
|
+
/**
|
|
3706
|
+
* A reconnect keeps the followed topics and emoticons of rooms and spaces that are gone; for the restored ones
|
|
3707
|
+
* the first session is the only chance to tell they are gone.
|
|
3708
|
+
*/
|
|
3709
|
+
handleSessionAfterImport(ev) {
|
|
3710
|
+
if (!this.snapshotImported) {
|
|
3711
|
+
return;
|
|
3712
|
+
}
|
|
3713
|
+
this.snapshotImported = false;
|
|
3714
|
+
this.rooms.followedTopics._forgetRoomsExcept(ev.state.rooms.map(room => room.id));
|
|
3715
|
+
this.emoticons._forgetSpacesExcept(ev.state.spaces.map(space => space.id));
|
|
3716
|
+
this.rooms._refreshImportedTopics();
|
|
3717
|
+
}
|
|
2979
3718
|
}
|
|
2980
3719
|
;// ./src/WebSocketChatClient.ts
|
|
2981
3720
|
|
|
@@ -3462,21 +4201,6 @@ let ErrorCode = /*#__PURE__*/function (ErrorCode) {
|
|
|
3462
4201
|
ErrorCode["UserNotFound"] = "UserNotFoundException";
|
|
3463
4202
|
return ErrorCode;
|
|
3464
4203
|
}({});
|
|
3465
|
-
;// ./src/protocol/schemes/User.ts
|
|
3466
|
-
/**
|
|
3467
|
-
* Availability of a user, aggregated over all of their sessions.
|
|
3468
|
-
*
|
|
3469
|
-
* - `Offline` - no session is connected and none can be reached asynchronously.
|
|
3470
|
-
* - `Online` - at least one session holds a live connection.
|
|
3471
|
-
* - `OnlineAsync` - no live connection, but a push-registered device was active
|
|
3472
|
-
* recently, so a message will still reach the user.
|
|
3473
|
-
*/
|
|
3474
|
-
let UserStatus = /*#__PURE__*/function (UserStatus) {
|
|
3475
|
-
UserStatus[UserStatus["Offline"] = 0] = "Offline";
|
|
3476
|
-
UserStatus[UserStatus["Online"] = 1] = "Online";
|
|
3477
|
-
UserStatus[UserStatus["OnlineAsync"] = 2] = "OnlineAsync";
|
|
3478
|
-
return UserStatus;
|
|
3479
|
-
}({});
|
|
3480
4204
|
;// ./src/protocol/schemes/Role.ts
|
|
3481
4205
|
/**
|
|
3482
4206
|
* Bit flags of a role, stored in {@link Role.flags}.
|
|
@@ -3608,6 +4332,7 @@ let SpaceIntent = /*#__PURE__*/function (SpaceIntent) {
|
|
|
3608
4332
|
|
|
3609
4333
|
|
|
3610
4334
|
|
|
3611
|
-
|
|
4335
|
+
|
|
4336
|
+
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 };
|
|
3612
4337
|
|
|
3613
4338
|
//# sourceMappingURL=index.mjs.map
|