@devanachat/sdk 0.7.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 +26 -0
- package/README.md +31 -0
- package/build/index.cjs.js +629 -15
- package/build/index.cjs.js.map +1 -1
- package/build/index.mjs +629 -16
- 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 +16 -1
- 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 +14 -0
- package/build/types/state-tracker/UsersManager.d.ts +5 -0
- package/package.json +1 -1
package/build/index.cjs.js
CHANGED
|
@@ -52,6 +52,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
52
52
|
PermissionDefinition: () => (/* reexport */ PermissionDefinition),
|
|
53
53
|
Permissions: () => (/* reexport */ Permissions),
|
|
54
54
|
RoleFlag: () => (/* reexport */ RoleFlag),
|
|
55
|
+
STATE_SNAPSHOT_VERSION: () => (/* reexport */ STATE_SNAPSHOT_VERSION),
|
|
55
56
|
STORAGE_UNIT_BYTES: () => (/* reexport */ STORAGE_UNIT_BYTES),
|
|
56
57
|
SearchRange: () => (/* reexport */ SearchRange),
|
|
57
58
|
SpaceFeature: () => (/* reexport */ SpaceFeature),
|
|
@@ -816,6 +817,43 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
816
817
|
return super.jumpTo(id);
|
|
817
818
|
}
|
|
818
819
|
|
|
820
|
+
/**
|
|
821
|
+
* The latest messages (oldest first) with the reactions of the user, or null if the window does not hold them.
|
|
822
|
+
* @internal
|
|
823
|
+
*/
|
|
824
|
+
_exportLatest(limit) {
|
|
825
|
+
if (this.state !== WindowState.LATEST) {
|
|
826
|
+
return null;
|
|
827
|
+
}
|
|
828
|
+
const messages = this.items.slice(-Math.max(limit, this.fetchLimit));
|
|
829
|
+
const myReactions = {};
|
|
830
|
+
for (const message of messages) {
|
|
831
|
+
if (this.internalState.myReactions[message.id]) {
|
|
832
|
+
myReactions[message.id] = this.internalState.myReactions[message.id];
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
return {
|
|
836
|
+
messages,
|
|
837
|
+
myReactions
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* Fill the window with saved latest messages, as if they were just fetched; the next reset to the latest replaces
|
|
843
|
+
* them.
|
|
844
|
+
* @internal
|
|
845
|
+
*/
|
|
846
|
+
_restoreLatest(messages, myReactions) {
|
|
847
|
+
const originalState = this.state;
|
|
848
|
+
this._items.deleteAll(); // Directly call deleteAll to prevent event emit.
|
|
849
|
+
this.addItems(messages, 'tail');
|
|
850
|
+
this.internalState.myReactions = {
|
|
851
|
+
...myReactions
|
|
852
|
+
};
|
|
853
|
+
this.internalState.current = WindowState.LATEST;
|
|
854
|
+
this.emitChangeWithDiff(true, originalState);
|
|
855
|
+
}
|
|
856
|
+
|
|
819
857
|
/**
|
|
820
858
|
* For internal use.
|
|
821
859
|
* @internal
|
|
@@ -1092,6 +1130,31 @@ class RoomMessagesHistory {
|
|
|
1092
1130
|
}
|
|
1093
1131
|
}
|
|
1094
1132
|
|
|
1133
|
+
/**
|
|
1134
|
+
* For internal use.
|
|
1135
|
+
* @internal
|
|
1136
|
+
*/
|
|
1137
|
+
get _isEphemeral() {
|
|
1138
|
+
return this.traverseLock;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
/**
|
|
1142
|
+
* The window of the topic, if created; unlike getMessagesWindow(), never creates nor fetches one.
|
|
1143
|
+
* @internal
|
|
1144
|
+
*/
|
|
1145
|
+
_peekWindow(topicId) {
|
|
1146
|
+
return this.historyWindows.get(topicId);
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/**
|
|
1150
|
+
* For internal use.
|
|
1151
|
+
* @internal
|
|
1152
|
+
*/
|
|
1153
|
+
_restoreWindow(topic, messages, myReactions) {
|
|
1154
|
+
this.createHistoryWindowForTopic(topic);
|
|
1155
|
+
this.historyWindows.get(topic.id)._restoreLatest(messages, myReactions);
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1095
1158
|
/**
|
|
1096
1159
|
* For internal use.
|
|
1097
1160
|
* @internal
|
|
@@ -1250,6 +1313,43 @@ class MessagesManager {
|
|
|
1250
1313
|
}
|
|
1251
1314
|
return message || null;
|
|
1252
1315
|
}
|
|
1316
|
+
|
|
1317
|
+
/**
|
|
1318
|
+
* For internal use.
|
|
1319
|
+
* @internal
|
|
1320
|
+
*/
|
|
1321
|
+
_importRooms(rooms) {
|
|
1322
|
+
for (const room of rooms) {
|
|
1323
|
+
this.createHistoryForNewRoom(room);
|
|
1324
|
+
}
|
|
1325
|
+
this.deferredSession.resolve();
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
/**
|
|
1329
|
+
* For internal use.
|
|
1330
|
+
* @internal
|
|
1331
|
+
*/
|
|
1332
|
+
_exportLatest(roomId, topicId, limit) {
|
|
1333
|
+
const history = this.roomHistories.get(roomId);
|
|
1334
|
+
|
|
1335
|
+
// Ephemeral history lives only in the memory of the session.
|
|
1336
|
+
if (!history || history._isEphemeral) {
|
|
1337
|
+
return null;
|
|
1338
|
+
}
|
|
1339
|
+
return history._peekWindow(topicId)?._exportLatest(limit) ?? null;
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
/**
|
|
1343
|
+
* For internal use.
|
|
1344
|
+
* @internal
|
|
1345
|
+
*/
|
|
1346
|
+
_importHistory(roomId, topic, snapshot) {
|
|
1347
|
+
const history = this.roomHistories.get(roomId);
|
|
1348
|
+
if (history && !history._isEphemeral) {
|
|
1349
|
+
const messages = snapshot.messages.filter(message => message.location.roomId === roomId && message.location.topicId === topic.id);
|
|
1350
|
+
history._restoreWindow(topic, messages, snapshot.myReactions);
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1253
1353
|
forEachRoomHistory(callback) {
|
|
1254
1354
|
for (const history of this.roomHistories.items.values()) {
|
|
1255
1355
|
callback(history);
|
|
@@ -1470,6 +1570,47 @@ class FollowedTopicsManager extends EventTarget {
|
|
|
1470
1570
|
this.followedTopics.get(roomId)?.delete(...topicIds);
|
|
1471
1571
|
topicIds.forEach(topicId => this.invalidateUnreadSummaries(roomId, topicId));
|
|
1472
1572
|
}
|
|
1573
|
+
|
|
1574
|
+
/**
|
|
1575
|
+
* For internal use.
|
|
1576
|
+
* @internal
|
|
1577
|
+
*/
|
|
1578
|
+
_exportFollowedTopics() {
|
|
1579
|
+
return Array.from(this.followedTopics.items).filter(([roomId]) => this.tracker.rooms._getRoom(roomId)).map(([roomId, followedTopics]) => ({
|
|
1580
|
+
roomId,
|
|
1581
|
+
followedTopics: followedTopics.items
|
|
1582
|
+
}));
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
/**
|
|
1586
|
+
* Expects the rooms to be imported already - followed topics of other rooms are skipped.
|
|
1587
|
+
* @internal
|
|
1588
|
+
*/
|
|
1589
|
+
_importFollowedTopics(snapshots) {
|
|
1590
|
+
for (const {
|
|
1591
|
+
roomId,
|
|
1592
|
+
followedTopics
|
|
1593
|
+
} of snapshots) {
|
|
1594
|
+
if (this.tracker.rooms._getRoom(roomId)) {
|
|
1595
|
+
this.applyRoomFollowedTopics(roomId, followedTopics);
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
this.invalidateUnreadSummaries();
|
|
1599
|
+
this.deferredSession.resolve();
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
/**
|
|
1603
|
+
* For internal use.
|
|
1604
|
+
* @internal
|
|
1605
|
+
*/
|
|
1606
|
+
_forgetRoomsExcept(roomIds) {
|
|
1607
|
+
const keptRoomIds = new Set(roomIds);
|
|
1608
|
+
for (const roomId of Array.from(this.followedTopics.items.keys())) {
|
|
1609
|
+
if (!keptRoomIds.has(roomId)) {
|
|
1610
|
+
this.clearRoomFollowedTopicsStructures(roomId);
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1473
1614
|
handleSession(ev) {
|
|
1474
1615
|
// Keep cached followed-topic collections (they drive unread indicators
|
|
1475
1616
|
// that would otherwise blank on reconnect), but mark them stale and drop
|
|
@@ -1670,11 +1811,148 @@ class FollowedTopicsManager extends EventTarget {
|
|
|
1670
1811
|
this.invalidateUnreadSummaries(roomId);
|
|
1671
1812
|
}
|
|
1672
1813
|
}
|
|
1814
|
+
;// ./src/protocol/schemes/User.ts
|
|
1815
|
+
/**
|
|
1816
|
+
* Availability of a user, aggregated over all of their sessions.
|
|
1817
|
+
*
|
|
1818
|
+
* - `Offline` - no session is connected and none can be reached asynchronously.
|
|
1819
|
+
* - `Online` - at least one session holds a live connection.
|
|
1820
|
+
* - `OnlineAsync` - no live connection, but a push-registered device was active
|
|
1821
|
+
* recently, so a message will still reach the user.
|
|
1822
|
+
*/
|
|
1823
|
+
let UserStatus = /*#__PURE__*/function (UserStatus) {
|
|
1824
|
+
UserStatus[UserStatus["Offline"] = 0] = "Offline";
|
|
1825
|
+
UserStatus[UserStatus["Online"] = 1] = "Online";
|
|
1826
|
+
UserStatus[UserStatus["OnlineAsync"] = 2] = "OnlineAsync";
|
|
1827
|
+
return UserStatus;
|
|
1828
|
+
}({});
|
|
1829
|
+
;// ./src/state-tracker/StateSnapshot.ts
|
|
1830
|
+
|
|
1831
|
+
|
|
1832
|
+
/**
|
|
1833
|
+
* Version of the {@link StateSnapshot} format. A snapshot of any other version is rejected on import.
|
|
1834
|
+
*/
|
|
1835
|
+
const STATE_SNAPSHOT_VERSION = 1;
|
|
1836
|
+
|
|
1837
|
+
/**
|
|
1838
|
+
* Plain, JSON-serializable copy of the state tracked by {@link ChatStateTracker}, made to restore the state at the
|
|
1839
|
+
* application start (before the connection is established) - see {@link ChatStateTracker.exportSnapshot}.
|
|
1840
|
+
*
|
|
1841
|
+
* Only the data that lasts is included: the current user, the joined spaces (with roles) and rooms (with the default
|
|
1842
|
+
* topics), and the lazily loaded lists already fetched - rooms of spaces, followed topics, emoticons and relationships.
|
|
1843
|
+
* The message histories of the topics named in the {@link StateSnapshotIntent} are included too, with what their
|
|
1844
|
+
* display needs: the topics referenced by the messages and, optionally, the members of the rooms.
|
|
1845
|
+
* Left out on purpose:
|
|
1846
|
+
* - presence - every user status is saved as `Offline`, a status from the past would show people online who are not;
|
|
1847
|
+
* - members of spaces, and of rooms not asked for - large, made mostly of presence, and refetched cheaply on demand;
|
|
1848
|
+
* - other message histories and topics, histories of ephemeral rooms (they live only in the memory of the session),
|
|
1849
|
+
* permission overwrites and room previews - loaded on demand once the connection is up, as on a reconnect.
|
|
1850
|
+
*/
|
|
1851
|
+
|
|
1852
|
+
/**
|
|
1853
|
+
* What {@link ChatStateTracker.exportSnapshot} should include beyond the state that is always exported.
|
|
1854
|
+
*/
|
|
1855
|
+
|
|
1856
|
+
function withoutPresence(user) {
|
|
1857
|
+
return {
|
|
1858
|
+
...user,
|
|
1859
|
+
status: UserStatus.Offline,
|
|
1860
|
+
online: false
|
|
1861
|
+
};
|
|
1862
|
+
}
|
|
1863
|
+
function messageWithoutPresence(message) {
|
|
1864
|
+
return {
|
|
1865
|
+
...message,
|
|
1866
|
+
author: {
|
|
1867
|
+
...message.author,
|
|
1868
|
+
user: withoutPresence(message.author.user)
|
|
1869
|
+
}
|
|
1870
|
+
};
|
|
1871
|
+
}
|
|
1872
|
+
function memberWithoutPresence(member) {
|
|
1873
|
+
return {
|
|
1874
|
+
...member,
|
|
1875
|
+
user: member.user ? withoutPresence(member.user) : member.user,
|
|
1876
|
+
spaceMember: member.spaceMember ? {
|
|
1877
|
+
...member.spaceMember,
|
|
1878
|
+
user: withoutPresence(member.spaceMember.user)
|
|
1879
|
+
} : member.spaceMember
|
|
1880
|
+
};
|
|
1881
|
+
}
|
|
1882
|
+
|
|
1883
|
+
/**
|
|
1884
|
+
* Structural check of the data restored from a storage - deep enough that importing it cannot throw, as the state
|
|
1885
|
+
* handlers dereference these fields without checking them.
|
|
1886
|
+
*/
|
|
1887
|
+
function isValidStateSnapshot(value) {
|
|
1888
|
+
return isObject(value) && value.version === STATE_SNAPSHOT_VERSION && typeof value.createdAt === 'number' && isUser(value.user) && isArrayOf(value.spaces, isSpace) && isArrayOf(value.rooms, isRoom) && isArrayOf(value.spaceRooms, isSpaceRooms) && isArrayOf(value.followedTopics, isRoomFollowedTopics) && isArrayOf(value.emoticons, isEmoticons) && (value.relationships === null || isArrayOf(value.relationships, isRelationship)) && isArrayOf(value.histories, isTopicHistory) && isArrayOf(value.roomMembers, isRoomMembers);
|
|
1889
|
+
}
|
|
1890
|
+
function isObject(value) {
|
|
1891
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
1892
|
+
}
|
|
1893
|
+
function isArrayOf(value, isItem) {
|
|
1894
|
+
return Array.isArray(value) && value.every(isItem);
|
|
1895
|
+
}
|
|
1896
|
+
function isString(value) {
|
|
1897
|
+
return typeof value === 'string';
|
|
1898
|
+
}
|
|
1899
|
+
function hasId(value) {
|
|
1900
|
+
return isObject(value) && isString(value.id);
|
|
1901
|
+
}
|
|
1902
|
+
function isUser(value) {
|
|
1903
|
+
return hasId(value);
|
|
1904
|
+
}
|
|
1905
|
+
function isRole(value) {
|
|
1906
|
+
return hasId(value) && typeof value.priority === 'number';
|
|
1907
|
+
}
|
|
1908
|
+
function isSpace(value) {
|
|
1909
|
+
return hasId(value) && isArrayOf(value.roles, isRole) && isArrayOf(value.defaultRooms, isString) && (value.systemRoom === null || isString(value.systemRoom));
|
|
1910
|
+
}
|
|
1911
|
+
function isTopic(value) {
|
|
1912
|
+
return hasId(value) && (value.refMessage === undefined || value.refMessage === null || isMessage(value.refMessage));
|
|
1913
|
+
}
|
|
1914
|
+
function isMessage(value) {
|
|
1915
|
+
return hasId(value) && isObject(value.location) && isString(value.location.topicId);
|
|
1916
|
+
}
|
|
1917
|
+
function isFullMessage(value) {
|
|
1918
|
+
return hasId(value) && isObject(value.location) && isString(value.location.roomId) && isString(value.location.topicId) && isObject(value.author) && isUser(value.author.user) && Array.isArray(value.reactions);
|
|
1919
|
+
}
|
|
1920
|
+
function isRoom(value) {
|
|
1921
|
+
return hasId(value) && isString(value.type) && (value.spaceId === null || isString(value.spaceId)) && (value.defaultTopic === null || isTopic(value.defaultTopic)) && (value.recipients === null || isArrayOf(value.recipients, isUser)) && isObject(value.history) && isString(value.history.mode);
|
|
1922
|
+
}
|
|
1923
|
+
function isSpaceRooms(value) {
|
|
1924
|
+
return isObject(value) && isString(value.spaceId) && isArrayOf(value.summaries, hasId);
|
|
1925
|
+
}
|
|
1926
|
+
function isFollowedTopic(value) {
|
|
1927
|
+
return isObject(value) && isObject(value.location) && isString(value.location.roomId) && isString(value.location.topicId);
|
|
1928
|
+
}
|
|
1929
|
+
function isRoomFollowedTopics(value) {
|
|
1930
|
+
return isObject(value) && isString(value.roomId) && isArrayOf(value.followedTopics, isFollowedTopic);
|
|
1931
|
+
}
|
|
1932
|
+
function isEmoticons(value) {
|
|
1933
|
+
return isObject(value) && (value.spaceId === null || isString(value.spaceId)) && isArrayOf(value.emoticons, hasId);
|
|
1934
|
+
}
|
|
1935
|
+
function isRelationship(value) {
|
|
1936
|
+
return isObject(value) && isUser(value.refUser) && isString(value.type);
|
|
1937
|
+
}
|
|
1938
|
+
function isUserReactions(value) {
|
|
1939
|
+
return isObject(value) && Object.values(value).every(reactions => isArrayOf(reactions, isObject));
|
|
1940
|
+
}
|
|
1941
|
+
function isTopicHistory(value) {
|
|
1942
|
+
return isObject(value) && isString(value.roomId) && isString(value.topicId) && isArrayOf(value.messages, isFullMessage) && isUserReactions(value.myReactions) && isArrayOf(value.topics, isTopic);
|
|
1943
|
+
}
|
|
1944
|
+
function isRoomMember(value) {
|
|
1945
|
+
return isObject(value) && (isUser(value.user) || isObject(value.spaceMember) && isUser(value.spaceMember.user));
|
|
1946
|
+
}
|
|
1947
|
+
function isRoomMembers(value) {
|
|
1948
|
+
return isObject(value) && isString(value.roomId) && isArrayOf(value.members, isRoomMember);
|
|
1949
|
+
}
|
|
1673
1950
|
;// ./src/state-tracker/RoomsManager.ts
|
|
1674
1951
|
|
|
1675
1952
|
|
|
1676
1953
|
|
|
1677
1954
|
|
|
1955
|
+
|
|
1678
1956
|
class RoomsManager {
|
|
1679
1957
|
messages;
|
|
1680
1958
|
followedTopics;
|
|
@@ -1684,6 +1962,7 @@ class RoomsManager {
|
|
|
1684
1962
|
deferredSession = new DeferredTask();
|
|
1685
1963
|
membersPromises = new PromiseRegistry();
|
|
1686
1964
|
topicsPromises = new PromiseRegistry();
|
|
1965
|
+
importedTopicIds = new Map();
|
|
1687
1966
|
constructor(tracker) {
|
|
1688
1967
|
this.tracker = tracker;
|
|
1689
1968
|
this.messages = new MessagesManager(tracker);
|
|
@@ -1788,6 +2067,148 @@ class RoomsManager {
|
|
|
1788
2067
|
_deleteTopicsFromRoom(roomId, ...topicIds) {
|
|
1789
2068
|
this.topics.get(roomId)?.delete(...topicIds);
|
|
1790
2069
|
}
|
|
2070
|
+
|
|
2071
|
+
/**
|
|
2072
|
+
* For internal use.
|
|
2073
|
+
* @internal
|
|
2074
|
+
*/
|
|
2075
|
+
_exportRooms() {
|
|
2076
|
+
return this.list.items.map(room => room.recipients ? {
|
|
2077
|
+
...room,
|
|
2078
|
+
recipients: room.recipients.map(withoutPresence)
|
|
2079
|
+
} : room);
|
|
2080
|
+
}
|
|
2081
|
+
|
|
2082
|
+
/**
|
|
2083
|
+
* For internal use.
|
|
2084
|
+
* @internal
|
|
2085
|
+
*/
|
|
2086
|
+
_importRooms(rooms) {
|
|
2087
|
+
this.messages._importRooms(rooms);
|
|
2088
|
+
this.addJoinedRooms(...rooms);
|
|
2089
|
+
this.deferredSession.resolve();
|
|
2090
|
+
}
|
|
2091
|
+
|
|
2092
|
+
/**
|
|
2093
|
+
* For internal use.
|
|
2094
|
+
* @internal
|
|
2095
|
+
*/
|
|
2096
|
+
_exportHistories(intent) {
|
|
2097
|
+
const histories = [];
|
|
2098
|
+
const exported = new Set();
|
|
2099
|
+
for (const {
|
|
2100
|
+
roomId,
|
|
2101
|
+
topicId: requestedTopicId
|
|
2102
|
+
} of intent.histories ?? []) {
|
|
2103
|
+
const room = this.list.get(roomId);
|
|
2104
|
+
const topicId = requestedTopicId ?? room?.defaultTopic?.id;
|
|
2105
|
+
if (!room || !topicId || exported.has(`${roomId}/${topicId}`)) {
|
|
2106
|
+
continue;
|
|
2107
|
+
}
|
|
2108
|
+
const latest = this.messages._exportLatest(roomId, topicId, intent.historyLimit ?? 0);
|
|
2109
|
+
const topics = this.topics.get(roomId);
|
|
2110
|
+
|
|
2111
|
+
// A topic other than the default one is restored from the snapshot, so it has to be known.
|
|
2112
|
+
if (!latest || topicId !== room.defaultTopic?.id && !topics?.has(topicId)) {
|
|
2113
|
+
continue;
|
|
2114
|
+
}
|
|
2115
|
+
const topicIds = new Set([topicId, ...latest.messages.map(message => message.topicRef)]);
|
|
2116
|
+
topicIds.delete(room.defaultTopic?.id);
|
|
2117
|
+
exported.add(`${roomId}/${topicId}`);
|
|
2118
|
+
histories.push({
|
|
2119
|
+
roomId,
|
|
2120
|
+
topicId,
|
|
2121
|
+
messages: latest.messages.map(messageWithoutPresence),
|
|
2122
|
+
myReactions: latest.myReactions,
|
|
2123
|
+
topics: Array.from(topicIds).filter(id => topics?.has(id)).map(id => topics.get(id))
|
|
2124
|
+
});
|
|
2125
|
+
}
|
|
2126
|
+
return histories;
|
|
2127
|
+
}
|
|
2128
|
+
|
|
2129
|
+
/**
|
|
2130
|
+
* Members of private conversations are not exported - they are made of the recipients of the room.
|
|
2131
|
+
* @internal
|
|
2132
|
+
*/
|
|
2133
|
+
_exportMembers(roomIds, limit) {
|
|
2134
|
+
const snapshots = [];
|
|
2135
|
+
if (!limit) {
|
|
2136
|
+
return snapshots;
|
|
2137
|
+
}
|
|
2138
|
+
for (const roomId of new Set(roomIds)) {
|
|
2139
|
+
const members = this.members.get(roomId);
|
|
2140
|
+
if (members && members.length <= limit && this.list.get(roomId)?.type !== 'Pm') {
|
|
2141
|
+
snapshots.push({
|
|
2142
|
+
roomId,
|
|
2143
|
+
members: members.items.map(memberWithoutPresence)
|
|
2144
|
+
});
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
return snapshots;
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2150
|
+
/**
|
|
2151
|
+
* Expects the rooms to be imported already - histories and members of other rooms are skipped.
|
|
2152
|
+
* @internal
|
|
2153
|
+
*/
|
|
2154
|
+
_importHistories(histories, roomMembers) {
|
|
2155
|
+
for (const {
|
|
2156
|
+
roomId,
|
|
2157
|
+
members
|
|
2158
|
+
} of roomMembers) {
|
|
2159
|
+
if (this.list.has(roomId) && this.list.get(roomId).type !== 'Pm') {
|
|
2160
|
+
this.handleRoomMembers({
|
|
2161
|
+
id: roomId,
|
|
2162
|
+
members
|
|
2163
|
+
});
|
|
2164
|
+
// Served from the snapshot until the session invalidates it.
|
|
2165
|
+
this.membersPromises.register(Promise.resolve(), roomId);
|
|
2166
|
+
}
|
|
2167
|
+
}
|
|
2168
|
+
for (const history of histories) {
|
|
2169
|
+
const room = this.list.get(history.roomId);
|
|
2170
|
+
if (!room) {
|
|
2171
|
+
continue;
|
|
2172
|
+
}
|
|
2173
|
+
const topics = history.topics.filter(topic => topic.id !== room.defaultTopic?.id);
|
|
2174
|
+
if (topics.length) {
|
|
2175
|
+
this.addJoinedRoomTopics(room.id, ...topics);
|
|
2176
|
+
const importedTopicIds = this.importedTopicIds.get(room.id) ?? new Set();
|
|
2177
|
+
topics.forEach(topic => importedTopicIds.add(topic.id));
|
|
2178
|
+
this.importedTopicIds.set(room.id, importedTopicIds);
|
|
2179
|
+
}
|
|
2180
|
+
const topic = history.topicId === room.defaultTopic?.id ? room.defaultTopic : this.topics.get(room.id)?.get(history.topicId);
|
|
2181
|
+
if (topic) {
|
|
2182
|
+
this.messages._importHistory(room.id, topic, history);
|
|
2183
|
+
}
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
|
|
2187
|
+
/**
|
|
2188
|
+
* The session carries only the default topics, so the other restored ones are fetched anew.
|
|
2189
|
+
* @internal
|
|
2190
|
+
*/
|
|
2191
|
+
_refreshImportedTopics() {
|
|
2192
|
+
const importedTopicIds = Array.from(this.importedTopicIds);
|
|
2193
|
+
this.importedTopicIds.clear();
|
|
2194
|
+
for (const [roomId, ids] of importedTopicIds) {
|
|
2195
|
+
if (!this.list.has(roomId)) {
|
|
2196
|
+
continue;
|
|
2197
|
+
}
|
|
2198
|
+
const topicIds = Array.from(ids);
|
|
2199
|
+
const promise = this.tracker.client.send('GetTopics', {
|
|
2200
|
+
roomId,
|
|
2201
|
+
topicIds
|
|
2202
|
+
}).then(result => {
|
|
2203
|
+
if (!result.error) {
|
|
2204
|
+
this.topics.get(roomId)?.set(...result.data.topics);
|
|
2205
|
+
}
|
|
2206
|
+
});
|
|
2207
|
+
topicIds.forEach(topicId => this.topicsPromises.register(promise, roomId + topicId));
|
|
2208
|
+
// Best effort: on a failure the restored topics stay, as after a reconnect.
|
|
2209
|
+
promise.catch(() => undefined);
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
1791
2212
|
deleteRoom(...roomIds) {
|
|
1792
2213
|
this.list.delete(...roomIds);
|
|
1793
2214
|
this.members.delete(...roomIds);
|
|
@@ -2184,6 +2605,47 @@ class SpacesManager {
|
|
|
2184
2605
|
const members = await this.getMembers(spaceId);
|
|
2185
2606
|
return members?.items.find(member => member.user.id === userId);
|
|
2186
2607
|
}
|
|
2608
|
+
|
|
2609
|
+
/**
|
|
2610
|
+
* For internal use.
|
|
2611
|
+
* @internal
|
|
2612
|
+
*/
|
|
2613
|
+
_exportSpaces() {
|
|
2614
|
+
return this.list.items;
|
|
2615
|
+
}
|
|
2616
|
+
|
|
2617
|
+
/**
|
|
2618
|
+
* Room listings of the joined spaces only - of other spaces they are fetched for a moment's look.
|
|
2619
|
+
* @internal
|
|
2620
|
+
*/
|
|
2621
|
+
_exportSpaceRooms() {
|
|
2622
|
+
return Array.from(this.rooms.items).filter(([spaceId]) => this.list.has(spaceId)).map(([spaceId, summaries]) => ({
|
|
2623
|
+
spaceId,
|
|
2624
|
+
summaries: summaries.items
|
|
2625
|
+
}));
|
|
2626
|
+
}
|
|
2627
|
+
|
|
2628
|
+
/**
|
|
2629
|
+
* For internal use.
|
|
2630
|
+
* @internal
|
|
2631
|
+
*/
|
|
2632
|
+
_importSpaces(spaces, spaceRooms) {
|
|
2633
|
+
this.addJoinedSpaces(...spaces);
|
|
2634
|
+
for (const {
|
|
2635
|
+
spaceId,
|
|
2636
|
+
summaries
|
|
2637
|
+
} of spaceRooms) {
|
|
2638
|
+
if (this.list.has(spaceId)) {
|
|
2639
|
+
this.handleSpaceRooms({
|
|
2640
|
+
id: spaceId,
|
|
2641
|
+
summaries
|
|
2642
|
+
});
|
|
2643
|
+
// Served from the snapshot until the session invalidates it.
|
|
2644
|
+
this.roomsPromises.register(Promise.resolve(), spaceId);
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
this.deferredSession.resolve();
|
|
2648
|
+
}
|
|
2187
2649
|
handleNewRole(ev) {
|
|
2188
2650
|
this.roles.get(ev.spaceId).set(ev.role);
|
|
2189
2651
|
this.syncSpaceRoles(ev.spaceId);
|
|
@@ -2805,6 +3267,52 @@ class EmoticonsManager {
|
|
|
2805
3267
|
await this.emoticonsPromises.get(key);
|
|
2806
3268
|
return this.list.get(key);
|
|
2807
3269
|
}
|
|
3270
|
+
|
|
3271
|
+
/**
|
|
3272
|
+
* For internal use.
|
|
3273
|
+
* @internal
|
|
3274
|
+
*/
|
|
3275
|
+
_exportEmoticons() {
|
|
3276
|
+
return Array.from(this.list.items).map(([key, emoticons]) => ({
|
|
3277
|
+
spaceId: key === GLOBAL_KEY ? null : key,
|
|
3278
|
+
emoticons: emoticons.items
|
|
3279
|
+
}));
|
|
3280
|
+
}
|
|
3281
|
+
|
|
3282
|
+
/**
|
|
3283
|
+
* Emoticons of spaces other than the given ones are skipped.
|
|
3284
|
+
* @internal
|
|
3285
|
+
*/
|
|
3286
|
+
_importEmoticons(snapshots, spaceIds) {
|
|
3287
|
+
const joinedSpaceIds = new Set(spaceIds);
|
|
3288
|
+
for (const {
|
|
3289
|
+
spaceId,
|
|
3290
|
+
emoticons
|
|
3291
|
+
} of snapshots) {
|
|
3292
|
+
if (spaceId !== null && !joinedSpaceIds.has(spaceId)) {
|
|
3293
|
+
continue;
|
|
3294
|
+
}
|
|
3295
|
+
this.handleEmoticons({
|
|
3296
|
+
location: {
|
|
3297
|
+
spaceId: spaceId ?? undefined
|
|
3298
|
+
},
|
|
3299
|
+
emoticons
|
|
3300
|
+
});
|
|
3301
|
+
// Served from the snapshot until the session invalidates it.
|
|
3302
|
+
this.emoticonsPromises.register(Promise.resolve(), spaceId ?? GLOBAL_KEY);
|
|
3303
|
+
}
|
|
3304
|
+
}
|
|
3305
|
+
|
|
3306
|
+
/**
|
|
3307
|
+
* For internal use.
|
|
3308
|
+
* @internal
|
|
3309
|
+
*/
|
|
3310
|
+
_forgetSpacesExcept(spaceIds) {
|
|
3311
|
+
const keptKeys = new Set([GLOBAL_KEY, ...spaceIds]);
|
|
3312
|
+
const removedKeys = Array.from(this.list.items.keys()).filter(key => !keptKeys.has(key));
|
|
3313
|
+
this.list.delete(...removedKeys);
|
|
3314
|
+
this.emoticonsPromises.forget(...removedKeys);
|
|
3315
|
+
}
|
|
2808
3316
|
handleEmoticons(event) {
|
|
2809
3317
|
const spaceId = event.location.spaceId ?? GLOBAL_KEY;
|
|
2810
3318
|
if (!this.list.has(spaceId)) {
|
|
@@ -2860,6 +3368,14 @@ class UsersManager {
|
|
|
2860
3368
|
async getAvailable() {
|
|
2861
3369
|
return this.users;
|
|
2862
3370
|
}
|
|
3371
|
+
|
|
3372
|
+
/**
|
|
3373
|
+
* For internal use.
|
|
3374
|
+
* @internal
|
|
3375
|
+
*/
|
|
3376
|
+
_importUser(user) {
|
|
3377
|
+
this.handleUsers([user]);
|
|
3378
|
+
}
|
|
2863
3379
|
handleMembers(members) {
|
|
2864
3380
|
this.handleUsers(members.map(extractUserFromMember));
|
|
2865
3381
|
}
|
|
@@ -2906,11 +3422,13 @@ function isSameUser(a, b) {
|
|
|
2906
3422
|
;// ./src/state-tracker/RelationshipsManager.ts
|
|
2907
3423
|
|
|
2908
3424
|
|
|
3425
|
+
|
|
2909
3426
|
const getId = (refUserId, type) => `${refUserId}-${type}`;
|
|
2910
3427
|
const getIdFromRelationship = relationship => getId(relationship.refUser.id, relationship.type);
|
|
2911
3428
|
class RelationshipsManager {
|
|
2912
3429
|
relationships = new ObservableIndexedObjectCollection(getIdFromRelationship);
|
|
2913
3430
|
promises = new PromiseRegistry();
|
|
3431
|
+
loaded = false;
|
|
2914
3432
|
constructor(tracker) {
|
|
2915
3433
|
this.tracker = tracker;
|
|
2916
3434
|
this.tracker.client.on('Relationships', ev => this.handleRelationships(ev));
|
|
@@ -2934,7 +3452,33 @@ class RelationshipsManager {
|
|
|
2934
3452
|
await this.get();
|
|
2935
3453
|
return this.relationships.has(getId(refUserId, type));
|
|
2936
3454
|
}
|
|
3455
|
+
|
|
3456
|
+
/**
|
|
3457
|
+
* For internal use.
|
|
3458
|
+
* @internal
|
|
3459
|
+
*/
|
|
3460
|
+
_exportRelationships() {
|
|
3461
|
+
return this.loaded ? this.relationships.items.map(relationship => ({
|
|
3462
|
+
...relationship,
|
|
3463
|
+
refUser: withoutPresence(relationship.refUser)
|
|
3464
|
+
})) : null;
|
|
3465
|
+
}
|
|
3466
|
+
|
|
3467
|
+
/**
|
|
3468
|
+
* For internal use.
|
|
3469
|
+
* @internal
|
|
3470
|
+
*/
|
|
3471
|
+
_importRelationships(relationships) {
|
|
3472
|
+
if (relationships) {
|
|
3473
|
+
this.handleRelationships({
|
|
3474
|
+
relationships
|
|
3475
|
+
});
|
|
3476
|
+
// Served from the snapshot until the session invalidates it.
|
|
3477
|
+
this.promises.register(Promise.resolve(), 'all');
|
|
3478
|
+
}
|
|
3479
|
+
}
|
|
2937
3480
|
handleRelationships(ev) {
|
|
3481
|
+
this.loaded = true;
|
|
2938
3482
|
// Full list from the server: reconcile in place so a reconnect refetch
|
|
2939
3483
|
// updates a bound (visible) list without an intermediate empty state.
|
|
2940
3484
|
this.relationships.reconcile(...ev.relationships);
|
|
@@ -2963,6 +3507,7 @@ class RelationshipsManager {
|
|
|
2963
3507
|
|
|
2964
3508
|
|
|
2965
3509
|
|
|
3510
|
+
|
|
2966
3511
|
class ChatStateTracker {
|
|
2967
3512
|
client;
|
|
2968
3513
|
|
|
@@ -2997,6 +3542,8 @@ class ChatStateTracker {
|
|
|
2997
3542
|
relationships;
|
|
2998
3543
|
_me = null;
|
|
2999
3544
|
deferredSession = new DeferredTask();
|
|
3545
|
+
sessionReceived = false;
|
|
3546
|
+
snapshotImported = false;
|
|
3000
3547
|
constructor(client) {
|
|
3001
3548
|
this.client = client;
|
|
3002
3549
|
this.client.on('Session', ev => this.handleSession(ev));
|
|
@@ -3006,6 +3553,9 @@ class ChatStateTracker {
|
|
|
3006
3553
|
this.emoticons = new EmoticonsManager(this);
|
|
3007
3554
|
this.users = new UsersManager(this);
|
|
3008
3555
|
this.relationships = new RelationshipsManager(this);
|
|
3556
|
+
|
|
3557
|
+
// Registered after the managers, so it runs once they have applied the session.
|
|
3558
|
+
this.client.on('Session', ev => this.handleSessionAfterImport(ev));
|
|
3009
3559
|
}
|
|
3010
3560
|
get me() {
|
|
3011
3561
|
return this._me;
|
|
@@ -3014,10 +3564,88 @@ class ChatStateTracker {
|
|
|
3014
3564
|
await this.deferredSession.promise;
|
|
3015
3565
|
return this._me;
|
|
3016
3566
|
}
|
|
3567
|
+
|
|
3568
|
+
/**
|
|
3569
|
+
* Export the tracked state to a plain, JSON-serializable object, to be saved and restored with
|
|
3570
|
+
* {@link importSnapshot} at the next start of the application, before the connection is established.
|
|
3571
|
+
* See {@link StateSnapshot} for what is included.
|
|
3572
|
+
* @param intent What to include beyond the state that is always exported, e.g. the message histories.
|
|
3573
|
+
* @return Null if there is no state yet (no session has been received or imported).
|
|
3574
|
+
*/
|
|
3575
|
+
exportSnapshot(intent = {}) {
|
|
3576
|
+
if (!this._me) {
|
|
3577
|
+
return null;
|
|
3578
|
+
}
|
|
3579
|
+
const snapshot = {
|
|
3580
|
+
version: STATE_SNAPSHOT_VERSION,
|
|
3581
|
+
createdAt: Date.now(),
|
|
3582
|
+
user: withoutPresence(this._me),
|
|
3583
|
+
spaces: this.spaces._exportSpaces(),
|
|
3584
|
+
rooms: this.rooms._exportRooms(),
|
|
3585
|
+
spaceRooms: this.spaces._exportSpaceRooms(),
|
|
3586
|
+
followedTopics: this.rooms.followedTopics._exportFollowedTopics(),
|
|
3587
|
+
emoticons: this.emoticons._exportEmoticons(),
|
|
3588
|
+
relationships: this.relationships._exportRelationships(),
|
|
3589
|
+
histories: this.rooms._exportHistories(intent),
|
|
3590
|
+
roomMembers: this.rooms._exportMembers((intent.histories ?? []).map(history => history.roomId), intent.roomMembersLimit ?? 0)
|
|
3591
|
+
};
|
|
3592
|
+
|
|
3593
|
+
// Detaches the snapshot from the live state objects and leaves nothing a JSON round trip would change.
|
|
3594
|
+
return JSON.parse(JSON.stringify(snapshot));
|
|
3595
|
+
}
|
|
3596
|
+
|
|
3597
|
+
/**
|
|
3598
|
+
* Restore the state exported with {@link exportSnapshot}, so it is available before the connection is
|
|
3599
|
+
* established. The collections emit their usual change events and the getters waiting for the session resolve.
|
|
3600
|
+
* The first `Session` from the server then replaces the restored state the same way it does on a reconnect:
|
|
3601
|
+
* removed spaces and rooms are dropped, the rest is updated in place, the restored message histories and topics
|
|
3602
|
+
* other than the default ones are fetched anew (as a window holding the latest messages is on a reconnect), and
|
|
3603
|
+
* the lazily loaded lists are refetched on their next access.
|
|
3604
|
+
*
|
|
3605
|
+
* Allowed only once and only before the first `Session`.
|
|
3606
|
+
* @return False if the snapshot was rejected (another version, malformed data, or a session already received) -
|
|
3607
|
+
* the state is not changed then.
|
|
3608
|
+
*/
|
|
3609
|
+
importSnapshot(snapshot) {
|
|
3610
|
+
if (this.sessionReceived || this.snapshotImported || !isValidStateSnapshot(snapshot)) {
|
|
3611
|
+
return false;
|
|
3612
|
+
}
|
|
3613
|
+
this.snapshotImported = true;
|
|
3614
|
+
try {
|
|
3615
|
+
this._me = snapshot.user;
|
|
3616
|
+
this.users._importUser(snapshot.user);
|
|
3617
|
+
this.rooms._importRooms(snapshot.rooms);
|
|
3618
|
+
this.spaces._importSpaces(snapshot.spaces, snapshot.spaceRooms);
|
|
3619
|
+
this.rooms.followedTopics._importFollowedTopics(snapshot.followedTopics);
|
|
3620
|
+
this.emoticons._importEmoticons(snapshot.emoticons, snapshot.spaces.map(space => space.id));
|
|
3621
|
+
this.relationships._importRelationships(snapshot.relationships);
|
|
3622
|
+
this.rooms._importHistories(snapshot.histories, snapshot.roomMembers);
|
|
3623
|
+
} catch {
|
|
3624
|
+
// Not expected after the validation; whatever got restored is replaced by the first session anyway.
|
|
3625
|
+
return false;
|
|
3626
|
+
}
|
|
3627
|
+
this.deferredSession.resolve();
|
|
3628
|
+
return true;
|
|
3629
|
+
}
|
|
3017
3630
|
handleSession(ev) {
|
|
3018
3631
|
this._me = ev.user;
|
|
3632
|
+
this.sessionReceived = true;
|
|
3019
3633
|
this.deferredSession.resolve();
|
|
3020
3634
|
}
|
|
3635
|
+
|
|
3636
|
+
/**
|
|
3637
|
+
* A reconnect keeps the followed topics and emoticons of rooms and spaces that are gone; for the restored ones
|
|
3638
|
+
* the first session is the only chance to tell they are gone.
|
|
3639
|
+
*/
|
|
3640
|
+
handleSessionAfterImport(ev) {
|
|
3641
|
+
if (!this.snapshotImported) {
|
|
3642
|
+
return;
|
|
3643
|
+
}
|
|
3644
|
+
this.snapshotImported = false;
|
|
3645
|
+
this.rooms.followedTopics._forgetRoomsExcept(ev.state.rooms.map(room => room.id));
|
|
3646
|
+
this.emoticons._forgetSpacesExcept(ev.state.spaces.map(space => space.id));
|
|
3647
|
+
this.rooms._refreshImportedTopics();
|
|
3648
|
+
}
|
|
3021
3649
|
}
|
|
3022
3650
|
;// ./src/WebSocketChatClient.ts
|
|
3023
3651
|
|
|
@@ -3504,21 +4132,6 @@ let ErrorCode = /*#__PURE__*/function (ErrorCode) {
|
|
|
3504
4132
|
ErrorCode["UserNotFound"] = "UserNotFoundException";
|
|
3505
4133
|
return ErrorCode;
|
|
3506
4134
|
}({});
|
|
3507
|
-
;// ./src/protocol/schemes/User.ts
|
|
3508
|
-
/**
|
|
3509
|
-
* Availability of a user, aggregated over all of their sessions.
|
|
3510
|
-
*
|
|
3511
|
-
* - `Offline` - no session is connected and none can be reached asynchronously.
|
|
3512
|
-
* - `Online` - at least one session holds a live connection.
|
|
3513
|
-
* - `OnlineAsync` - no live connection, but a push-registered device was active
|
|
3514
|
-
* recently, so a message will still reach the user.
|
|
3515
|
-
*/
|
|
3516
|
-
let UserStatus = /*#__PURE__*/function (UserStatus) {
|
|
3517
|
-
UserStatus[UserStatus["Offline"] = 0] = "Offline";
|
|
3518
|
-
UserStatus[UserStatus["Online"] = 1] = "Online";
|
|
3519
|
-
UserStatus[UserStatus["OnlineAsync"] = 2] = "OnlineAsync";
|
|
3520
|
-
return UserStatus;
|
|
3521
|
-
}({});
|
|
3522
4135
|
;// ./src/protocol/schemes/Role.ts
|
|
3523
4136
|
/**
|
|
3524
4137
|
* Bit flags of a role, stored in {@link Role.flags}.
|
|
@@ -3650,6 +4263,7 @@ let SpaceIntent = /*#__PURE__*/function (SpaceIntent) {
|
|
|
3650
4263
|
|
|
3651
4264
|
|
|
3652
4265
|
|
|
4266
|
+
|
|
3653
4267
|
module.exports = __webpack_exports__;
|
|
3654
4268
|
/******/ })()
|
|
3655
4269
|
;
|