@devanachat/sdk 0.9.0 → 0.11.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.
Files changed (35) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/build/index.cjs.js +322 -105
  3. package/build/index.cjs.js.map +1 -1
  4. package/build/index.mjs +317 -105
  5. package/build/index.mjs.map +1 -1
  6. package/build/index.umd.js +1 -1
  7. package/build/index.umd.js.map +1 -1
  8. package/build/types/AbstractChatClient.d.ts +5 -1
  9. package/build/types/Permissions.d.ts +8 -0
  10. package/build/types/index.d.ts +5 -4
  11. package/build/types/protocol/index.d.ts +9 -4
  12. package/build/types/protocol/schemes/Message.d.ts +1 -1
  13. package/build/types/protocol/schemes/PmParticipant.d.ts +8 -0
  14. package/build/types/protocol/schemes/Role.d.ts +6 -0
  15. package/build/types/protocol/schemes/Room.d.ts +3 -0
  16. package/build/types/protocol/schemes/Space.d.ts +1 -0
  17. package/build/types/protocol/schemes/Topic.d.ts +1 -0
  18. package/build/types/protocol/schemes/commands/CreateMessage.d.ts +1 -0
  19. package/build/types/protocol/schemes/commands/GetIgnoredRooms.d.ts +2 -0
  20. package/build/types/protocol/schemes/commands/LeaveRoom.d.ts +1 -0
  21. package/build/types/protocol/schemes/commands/Type.d.ts +4 -0
  22. package/build/types/protocol/schemes/commands/UpdateRole.d.ts +2 -0
  23. package/build/types/protocol/schemes/commands/UpdateSpace.d.ts +1 -0
  24. package/build/types/protocol/schemes/commands/UpdateTopic.d.ts +1 -0
  25. package/build/types/protocol/schemes/events/Error.d.ts +7 -0
  26. package/build/types/protocol/schemes/events/IgnoredRooms.d.ts +4 -0
  27. package/build/types/protocol/schemes/events/NewMessage.d.ts +1 -0
  28. package/build/types/protocol/schemes/events/Typing.d.ts +5 -0
  29. package/build/types/state-tracker/PermissionsManager.d.ts +1 -1
  30. package/build/types/state-tracker/RoomsManager.d.ts +4 -1
  31. package/build/types/state-tracker/StateSnapshot.d.ts +1 -1
  32. package/build/types/state-tracker/TypingManager.d.ts +35 -0
  33. package/build/types/state-tracker/UsersManager.d.ts +7 -1
  34. package/build/types/state-tracker/functions.d.ts +12 -1
  35. package/package.json +1 -1
package/build/index.mjs CHANGED
@@ -1882,6 +1882,129 @@ class FollowedTopicsManager extends EventTarget {
1882
1882
  this.invalidateUnreadSummaries(roomId);
1883
1883
  }
1884
1884
  }
1885
+ ;// ./src/state-tracker/TypingManager.ts
1886
+
1887
+ const TYPING_INTERVAL_MS = 5000;
1888
+ const TYPING_TIMEOUT_MS = 2 * TYPING_INTERVAL_MS;
1889
+
1890
+ /**
1891
+ * Ids of the users typing in a topic, each mapped to the time (`Date.now()`) its typing expires at.
1892
+ */
1893
+
1894
+ class TypingManager {
1895
+ topics = new IndexedCollection();
1896
+ lastSentAt = new Map();
1897
+ constructor(tracker) {
1898
+ this.tracker = tracker;
1899
+ this.tracker.client.on('Typing', ev => this.handleTyping(ev));
1900
+ this.tracker.client.on('NewMessage', ev => this.handleNewMessage(ev));
1901
+ this.tracker.client.on('TopicDeleted', ev => this.handleTopicDeleted(ev));
1902
+ this.tracker.client.on('RoomLeft', ev => this.handleRoomGone(ev));
1903
+ this.tracker.client.on('RoomDeleted', ev => this.handleRoomGone(ev));
1904
+ this.tracker.client.on('Session', () => this.handleSession());
1905
+ }
1906
+
1907
+ /**
1908
+ * Users typing in the topic, without you. Tracking of the topic starts with the first call, so the collection
1909
+ * fills up with the next notifications, within {@link TYPING_INTERVAL_MS}. A user leaves it
1910
+ * {@link TYPING_TIMEOUT_MS} after their last notification or at once with their message in the topic.
1911
+ * Only ids are kept - resolve the users yourself, e.g. from {@link RoomsManager.getMembers}.
1912
+ */
1913
+ get(roomId, topicId) {
1914
+ if (!this.topics.has(topicId)) {
1915
+ this.topics.set([topicId, {
1916
+ roomId,
1917
+ users: new ObservableIndexedCollection()
1918
+ }]);
1919
+ }
1920
+ return this.topics.get(topicId).users;
1921
+ }
1922
+
1923
+ /**
1924
+ * Tell the other members of the room that you are typing in the topic. Call it on every change of the text:
1925
+ * the command is sent at most once per {@link TYPING_INTERVAL_MS} for a topic, and again right after your
1926
+ * message in it. Classic rooms do not support typing.
1927
+ */
1928
+ notify(roomId, topicId) {
1929
+ const now = Date.now();
1930
+ if (now - (this.lastSentAt.get(topicId) ?? -Infinity) < TYPING_INTERVAL_MS) {
1931
+ return;
1932
+ }
1933
+ this.lastSentAt.set(topicId, now);
1934
+ this.tracker.client.send('Type', {
1935
+ location: {
1936
+ roomId,
1937
+ topicId
1938
+ }
1939
+ }).catch(() => undefined);
1940
+ }
1941
+ handleTyping(ev) {
1942
+ const topic = this.topics.get(ev.location.topicId);
1943
+ if (!topic || ev.userId === this.tracker.me?.id) {
1944
+ return;
1945
+ }
1946
+ topic.users.set([ev.userId, Date.now() + TYPING_TIMEOUT_MS]);
1947
+ this.scheduleExpiry(topic);
1948
+ }
1949
+ handleNewMessage(ev) {
1950
+ const topicId = ev.message.location.topicId;
1951
+ const authorId = ev.message.author.user.id;
1952
+ if (authorId === this.tracker.me?.id) {
1953
+ this.lastSentAt.delete(topicId);
1954
+ return;
1955
+ }
1956
+ const topic = this.topics.get(topicId);
1957
+ if (topic?.users.has(authorId)) {
1958
+ topic.users.delete(authorId);
1959
+ this.scheduleExpiry(topic);
1960
+ }
1961
+ }
1962
+ handleTopicDeleted(ev) {
1963
+ this.clear(ev.location.topicId);
1964
+ }
1965
+ handleRoomGone(ev) {
1966
+ for (const [topicId, topic] of this.topics.items.entries()) {
1967
+ if (topic.roomId === ev.id) {
1968
+ this.clear(topicId);
1969
+ }
1970
+ }
1971
+ }
1972
+ handleSession() {
1973
+ this.lastSentAt.clear();
1974
+ for (const topic of this.topics.items.values()) {
1975
+ clearTimeout(topic.timer);
1976
+ topic.users.deleteAll();
1977
+ }
1978
+ }
1979
+ clear(topicId) {
1980
+ const topic = this.topics.get(topicId);
1981
+ if (topic) {
1982
+ clearTimeout(topic.timer);
1983
+ topic.users.deleteAll();
1984
+ }
1985
+ this.lastSentAt.delete(topicId);
1986
+ }
1987
+ scheduleExpiry(topic) {
1988
+ clearTimeout(topic.timer);
1989
+ topic.timer = undefined;
1990
+ if (!topic.users.length) {
1991
+ return;
1992
+ }
1993
+ const nextExpiry = Math.min(...topic.users.items.values());
1994
+ topic.timer = setTimeout(() => this.expire(topic), Math.max(0, nextExpiry - Date.now()));
1995
+ }
1996
+ expire(topic) {
1997
+ const now = Date.now();
1998
+ const expiredIds = [];
1999
+ for (const [userId, expiresAt] of topic.users.items.entries()) {
2000
+ if (expiresAt <= now) {
2001
+ expiredIds.push(userId);
2002
+ }
2003
+ }
2004
+ topic.users.delete(...expiredIds);
2005
+ this.scheduleExpiry(topic);
2006
+ }
2007
+ }
1885
2008
  ;// ./src/protocol/schemes/User.ts
1886
2009
  /**
1887
2010
  * Availability of a user, aggregated over all of their sessions.
@@ -1903,7 +2026,7 @@ let UserStatus = /*#__PURE__*/function (UserStatus) {
1903
2026
  /**
1904
2027
  * Version of the {@link StateSnapshot} format. A snapshot of any other version is rejected on import.
1905
2028
  */
1906
- const STATE_SNAPSHOT_VERSION = 1;
2029
+ const STATE_SNAPSHOT_VERSION = 2;
1907
2030
 
1908
2031
  /**
1909
2032
  * Plain, JSON-serializable copy of the state tracked by {@link ChatStateTracker}, made to restore the state at the
@@ -1973,6 +2096,9 @@ function hasId(value) {
1973
2096
  function isUser(value) {
1974
2097
  return hasId(value);
1975
2098
  }
2099
+ function isPmParticipant(value) {
2100
+ return isObject(value) && isUser(value.user) && typeof value.flags === 'number';
2101
+ }
1976
2102
  function isRole(value) {
1977
2103
  return hasId(value) && typeof value.priority === 'number';
1978
2104
  }
@@ -1989,7 +2115,7 @@ function isFullMessage(value) {
1989
2115
  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);
1990
2116
  }
1991
2117
  function isRoom(value) {
1992
- 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);
2118
+ return hasId(value) && isString(value.type) && (value.spaceId === null || isString(value.spaceId)) && (value.defaultTopic === null || isTopic(value.defaultTopic)) && (value.participants === null || isArrayOf(value.participants, isPmParticipant)) && (value.recipients === null || isArrayOf(value.recipients, isUser)) && isObject(value.history) && isString(value.history.mode);
1993
2119
  }
1994
2120
  function isSpaceRooms(value) {
1995
2121
  return isObject(value) && isString(value.spaceId) && isArrayOf(value.summaries, hasId);
@@ -2018,15 +2144,89 @@ function isRoomMember(value) {
2018
2144
  function isRoomMembers(value) {
2019
2145
  return isObject(value) && isString(value.roomId) && isArrayOf(value.members, isRoomMember);
2020
2146
  }
2147
+ ;// ./src/protocol/schemes/PmParticipant.ts
2148
+ let PmParticipantFlag = /*#__PURE__*/function (PmParticipantFlag) {
2149
+ PmParticipantFlag[PmParticipantFlag["IgnoresRoom"] = 1] = "IgnoresRoom";
2150
+ return PmParticipantFlag;
2151
+ }({});
2152
+ ;// ./src/state-tracker/functions.ts
2153
+
2154
+
2155
+ /**
2156
+ * @return New objects of the other roles whose priority has to move; the given roles are left intact.
2157
+ */
2158
+ function reorderRolesOnPriorityUpdate(allRoles, oldRole, updatedRole) {
2159
+ // If the priority has changed, adjust the rest of roles
2160
+ const increased = updatedRole.priority - oldRole.priority > 0;
2161
+ const decreased = !increased;
2162
+ const changedRoles = [];
2163
+ allRoles.forEach(role => {
2164
+ if (role.id === updatedRole.id) {
2165
+ // Skip the updated role
2166
+ return;
2167
+ }
2168
+ if (increased && oldRole.priority <= role.priority) {
2169
+ changedRoles.push({
2170
+ ...role,
2171
+ priority: role.priority - 1
2172
+ });
2173
+ }
2174
+ if (decreased && updatedRole.priority <= role.priority) {
2175
+ changedRoles.push({
2176
+ ...role,
2177
+ priority: role.priority + 1
2178
+ });
2179
+ }
2180
+ });
2181
+ return changedRoles;
2182
+ }
2183
+ function extractUserFromMember(member) {
2184
+ return member.user ?? member.spaceMember?.user;
2185
+ }
2186
+
2187
+ /**
2188
+ * Whether the room is a private group conversation - 3 or more participants, including the ones ignoring it.
2189
+ */
2190
+ function isGroupPm(room) {
2191
+ return room.type === 'Pm' && (room.participants?.length ?? 0) >= 3;
2192
+ }
2193
+
2194
+ /**
2195
+ * Participants of a private conversation taking part in it: without the ones ignoring it and without the user
2196
+ * `meId`. Empty for notes and for rooms of spaces.
2197
+ */
2198
+ function getPmPeers(room, meId) {
2199
+ return getActivePmParticipants(room).filter(participant => participant.user.id !== meId);
2200
+ }
2201
+ function getActivePmParticipants(room) {
2202
+ return (room.participants ?? []).filter(participant => !(participant.flags & PmParticipantFlag.IgnoresRoom));
2203
+ }
2204
+ function isSameUser(a, b) {
2205
+ const keys = Object.keys(a);
2206
+ if (keys.length !== Object.keys(b).length) {
2207
+ return false;
2208
+ }
2209
+ return keys.every(key => {
2210
+ const valueA = a[key];
2211
+ const valueB = b[key];
2212
+ if (Array.isArray(valueA) && Array.isArray(valueB)) {
2213
+ return valueA.length === valueB.length && valueA.every((item, index) => item === valueB[index]);
2214
+ }
2215
+ return valueA === valueB;
2216
+ });
2217
+ }
2021
2218
  ;// ./src/state-tracker/RoomsManager.ts
2022
2219
 
2023
2220
 
2024
2221
 
2025
2222
 
2026
2223
 
2224
+
2225
+
2027
2226
  class RoomsManager {
2028
2227
  messages;
2029
2228
  followedTopics;
2229
+ typing;
2030
2230
  list = new ObservableIndexedObjectCollection('id');
2031
2231
  topics = new IndexedCollection();
2032
2232
  members = new IndexedCollection();
@@ -2038,6 +2238,7 @@ class RoomsManager {
2038
2238
  this.tracker = tracker;
2039
2239
  this.messages = new MessagesManager(tracker);
2040
2240
  this.followedTopics = new FollowedTopicsManager(tracker);
2241
+ this.typing = new TypingManager(tracker);
2041
2242
  this.tracker.client.on('NewMessage', ev => this.handleNewMessage(ev));
2042
2243
  this.tracker.client.on('MessagesRedacted', ev => this.handleMessagesRedacted(ev));
2043
2244
  this.tracker.client.on('NewTopic', ev => this.handleNewTopic(ev));
@@ -2152,9 +2353,13 @@ class RoomsManager {
2152
2353
  * @internal
2153
2354
  */
2154
2355
  _exportRooms() {
2155
- return this.list.items.map(room => room.recipients ? {
2356
+ return this.list.items.map(room => room.participants ? {
2156
2357
  ...room,
2157
- recipients: room.recipients.map(withoutPresence)
2358
+ participants: room.participants.map(participant => ({
2359
+ ...participant,
2360
+ user: withoutPresence(participant.user)
2361
+ })),
2362
+ recipients: room.recipients?.map(withoutPresence) ?? null
2158
2363
  } : room);
2159
2364
  }
2160
2365
 
@@ -2206,7 +2411,7 @@ class RoomsManager {
2206
2411
  }
2207
2412
 
2208
2413
  /**
2209
- * Members of private conversations are not exported - they are made of the recipients of the room.
2414
+ * Members of private conversations are not exported - they are made of the participants of the room.
2210
2415
  * @internal
2211
2416
  */
2212
2417
  _exportMembers(roomIds, limit) {
@@ -2332,6 +2537,9 @@ class RoomsManager {
2332
2537
  }
2333
2538
  const members = this.members.get(ev.roomId);
2334
2539
  const member = members.get(ev.userId);
2540
+ if (!member) {
2541
+ return;
2542
+ }
2335
2543
  const user = member.spaceMember?.user ?? member.user;
2336
2544
 
2337
2545
  // Preserving user object, because it's not included in event
@@ -2376,6 +2584,7 @@ class RoomsManager {
2376
2584
  handleRoomUpdated(ev) {
2377
2585
  if (this.list.has(ev.room.id)) {
2378
2586
  this.list.set(ev.room);
2587
+ this.setPmMembers(ev.room);
2379
2588
  }
2380
2589
  }
2381
2590
  handleRoomDeleted(ev) {
@@ -2399,27 +2608,40 @@ class RoomsManager {
2399
2608
  if (room.defaultTopic) {
2400
2609
  this.addJoinedRoomTopics(room.id, room.defaultTopic);
2401
2610
  }
2402
- if (room.type === 'Pm' && room.recipients) {
2403
- // Treat PM recipients as normal room members.
2404
- // We are registering fake promise in `memberPromises`
2405
- // because GetMembers are not supported for PM rooms.
2406
- this.handleRoomMembers({
2407
- id: room.id,
2408
- members: room.recipients.map(user => ({
2409
- user,
2410
- spaceMember: null,
2411
- roles: null,
2412
- customColor: null,
2413
- customNick: null,
2414
- customAvatar: null,
2415
- extras: ''
2416
- }))
2417
- });
2418
- this.membersPromises.register(Promise.resolve(), room.id);
2419
- }
2611
+ this.setPmMembers(room);
2420
2612
  }
2421
2613
  this.list.set(...rooms);
2422
2614
  }
2615
+ setPmMembers(room) {
2616
+ if (room.type !== 'Pm' || !room.participants) {
2617
+ return;
2618
+ }
2619
+ const current = this.members.get(room.id);
2620
+ const members = getActivePmParticipants(room).map(({
2621
+ user
2622
+ }) => {
2623
+ const member = current?.get(user.id);
2624
+ return member && isSameUser(member.user, user) ? member : {
2625
+ user,
2626
+ spaceMember: null,
2627
+ roles: null,
2628
+ customColor: null,
2629
+ customNick: null,
2630
+ customAvatar: null,
2631
+ extras: ''
2632
+ };
2633
+ });
2634
+
2635
+ // GetRoomMembers is not supported for private conversations
2636
+ this.membersPromises.register(Promise.resolve(), room.id);
2637
+ if (current?.length === members.length && members.every(member => current.get(member.user.id) === member)) {
2638
+ return;
2639
+ }
2640
+ this.handleRoomMembers({
2641
+ id: room.id,
2642
+ members
2643
+ });
2644
+ }
2423
2645
  handleRoomLeft(ev) {
2424
2646
  this.deleteRoom(ev.id);
2425
2647
  }
@@ -2483,13 +2705,17 @@ class RoomsManager {
2483
2705
  });
2484
2706
  });
2485
2707
 
2486
- // Update recipients users
2708
+ // Update users of private conversation participants
2487
2709
  const newRooms = [];
2488
2710
  this.list.items.forEach(room => {
2489
- if (room.recipients?.some(user => user.id === ev.user.id)) {
2711
+ if (room.participants?.some(participant => participant.user.id === ev.user.id)) {
2490
2712
  newRooms.push({
2491
2713
  ...room,
2492
- recipients: room.recipients.map(user => user.id === ev.user.id ? ev.user : user)
2714
+ participants: room.participants.map(participant => participant.user.id === ev.user.id ? {
2715
+ ...participant,
2716
+ user: ev.user
2717
+ } : participant),
2718
+ recipients: room.recipients?.map(user => user.id === ev.user.id ? ev.user : user) ?? null
2493
2719
  });
2494
2720
  }
2495
2721
  });
@@ -2504,7 +2730,8 @@ class RoomsManager {
2504
2730
  const newTopic = {
2505
2731
  ...topic,
2506
2732
  messageCount: topic.messageCount + 1,
2507
- lastMessage: ev.message
2733
+ lastMessage: ev.message,
2734
+ stickyMessage: ev.sticky ? ev.message : topic.stickyMessage
2508
2735
  };
2509
2736
  topics.set(newTopic);
2510
2737
  this.updatePmDefaultTopic(ev.message.location.roomId, newTopic);
@@ -2516,10 +2743,14 @@ class RoomsManager {
2516
2743
  if (topic) {
2517
2744
  const messageCount = Math.max(topic.messageCount - ev.ids.length, 0);
2518
2745
  const lastMessage = messageCount > 0 ? await this.messages._resolveLastMessage(ev.location) : null;
2746
+ // Read after the await - a message stuck meanwhile must not be replaced with the stale one
2747
+ const currentSticky = (topics.get(ev.location.topicId) ?? topic).stickyMessage;
2748
+ const stickyMessage = ev.ids.includes(currentSticky?.id) ? null : currentSticky;
2519
2749
  const newTopic = {
2520
2750
  ...topic,
2521
2751
  messageCount,
2522
- lastMessage
2752
+ lastMessage,
2753
+ stickyMessage
2523
2754
  };
2524
2755
  topics.set(newTopic);
2525
2756
  this.updatePmDefaultTopic(ev.location.roomId, newTopic);
@@ -2541,38 +2772,6 @@ class RoomsManager {
2541
2772
  }
2542
2773
  }
2543
2774
  }
2544
- ;// ./src/state-tracker/functions.ts
2545
- /**
2546
- * @return New objects of the other roles whose priority has to move; the given roles are left intact.
2547
- */
2548
- function reorderRolesOnPriorityUpdate(allRoles, oldRole, updatedRole) {
2549
- // If the priority has changed, adjust the rest of roles
2550
- const increased = updatedRole.priority - oldRole.priority > 0;
2551
- const decreased = !increased;
2552
- const changedRoles = [];
2553
- allRoles.forEach(role => {
2554
- if (role.id === updatedRole.id) {
2555
- // Skip the updated role
2556
- return;
2557
- }
2558
- if (increased && oldRole.priority <= role.priority) {
2559
- changedRoles.push({
2560
- ...role,
2561
- priority: role.priority - 1
2562
- });
2563
- }
2564
- if (decreased && updatedRole.priority <= role.priority) {
2565
- changedRoles.push({
2566
- ...role,
2567
- priority: role.priority + 1
2568
- });
2569
- }
2570
- });
2571
- return changedRoles;
2572
- }
2573
- function extractUserFromMember(member) {
2574
- return member.user ?? member.spaceMember?.user;
2575
- }
2576
2775
  ;// ./src/state-tracker/SpacesManager.ts
2577
2776
 
2578
2777
 
@@ -2761,6 +2960,13 @@ class SpacesManager {
2761
2960
  }
2762
2961
  handleRoleDeleted(ev) {
2763
2962
  this.roles.get(ev.spaceId).delete(ev.id);
2963
+ const space = this.list.get(ev.spaceId);
2964
+ if (space?.guestRole === ev.id) {
2965
+ this.list.set({
2966
+ ...space,
2967
+ guestRole: null
2968
+ });
2969
+ }
2764
2970
  this.syncSpaceRoles(ev.spaceId);
2765
2971
  }
2766
2972
 
@@ -3029,6 +3235,14 @@ class Permissions {
3029
3235
  UseExternalEmoticons: {
3030
3236
  value: 1 << 26,
3031
3237
  maxLayer: Layer.Room
3238
+ },
3239
+ ManagePins: {
3240
+ value: 1 << 27,
3241
+ maxLayer: Layer.Topic
3242
+ },
3243
+ SendAttachments: {
3244
+ value: 1 << 28,
3245
+ maxLayer: Layer.Topic
3032
3246
  }
3033
3247
  };
3034
3248
  static getNames() {
@@ -3162,7 +3376,7 @@ class PermissionsManager extends EventTarget {
3162
3376
  userId
3163
3377
  }).then(v => v.overwrites));
3164
3378
  }
3165
- return this.resolveOverwritesHierarchy(await Promise.all(promises));
3379
+ return this.resolveOverwritesHierarchy([].concat(...(await Promise.all(promises))));
3166
3380
  }
3167
3381
 
3168
3382
  /**
@@ -3239,34 +3453,22 @@ class PermissionsManager extends EventTarget {
3239
3453
  type: 'Role',
3240
3454
  roleId
3241
3455
  })));
3242
- return this.resolveOverwritesFromRolesByOrder(location.spaceId, roleOverwrites);
3243
- }
3244
- async resolveOverwritesFromRolesByOrder(spaceId, overwrites) {
3245
- let allows = 0,
3246
- denies = 0;
3247
- const roles = await this.tracker.spaces.getRoles(spaceId);
3248
- const sortedOverwrites = overwrites.sort((a, b) => roles.get(a.target.roleId).priority - roles.get(b.target.roleId).priority);
3249
-
3250
- // Max length of bit word
3251
- const permissionsLength = overwrites.reduce((previousValue, currentValue) => Math.max(previousValue, currentValue.overwrites.allow?.toString(2).length ?? 0, currentValue.overwrites.deny?.toString(2).length ?? 0), 0);
3252
- sortedOverwrites.forEach(overwriteEvent => {
3253
- const overwrites = overwriteEvent.overwrites;
3254
- const revDecDenies = overwrites.deny?.toString(2).split('').reverse().join('') ?? '';
3255
- const revDecAllows = overwrites.allow?.toString(2).split('').reverse().join('') ?? '';
3256
- for (let i = 0; i < permissionsLength; i++) {
3257
- const deny = parseInt(revDecDenies[i] ?? '0');
3258
- const allow = parseInt(revDecAllows[i] ?? '0');
3259
- if (deny) {
3260
- denies |= 1 << i;
3261
- }
3262
- if (allow) {
3263
- allows |= 1 << i;
3264
- }
3265
- }
3266
- });
3456
+ // The space default role (everyone) has the space id; it is applied before the other roles, as on the server.
3457
+ const isDefaultRole = overwrites => overwrites.target.roleId === location.spaceId;
3458
+ return [this.mergeRolesOverwrites(roleOverwrites.filter(isDefaultRole)), this.mergeRolesOverwrites(roleOverwrites.filter(overwrites => !isDefaultRole(overwrites)))];
3459
+ }
3460
+ mergeRolesOverwrites(rolesOverwrites) {
3461
+ let allow = 0,
3462
+ deny = 0;
3463
+ for (const {
3464
+ overwrites
3465
+ } of rolesOverwrites) {
3466
+ allow |= overwrites.allow ?? 0;
3467
+ deny |= overwrites.deny ?? 0;
3468
+ }
3267
3469
  return {
3268
- allow: allows,
3269
- deny: denies
3470
+ allow,
3471
+ deny
3270
3472
  };
3271
3473
  }
3272
3474
  resolveOverwritesHierarchy(permissionOverwritesValues) {
@@ -3448,6 +3650,9 @@ class UsersManager {
3448
3650
  tracker.client.on('RoomMembers', event => this.handleMembers(event.members));
3449
3651
  tracker.client.on('Messages', event => this.handleUsers(event.messages.map(message => message.author.user)));
3450
3652
  tracker.client.on('NewMessage', event => this.handleUsers([event.message.author.user]));
3653
+ tracker.client.on('RoomJoined', event => this.handleRooms([event.room]));
3654
+ tracker.client.on('RoomUpdated', event => this.handleRooms([event.room]));
3655
+ tracker.client.on('IgnoredRooms', event => this.handleRooms(event.rooms));
3451
3656
  tracker.client.on('Session', event => this.handleSession(event));
3452
3657
  }
3453
3658
 
@@ -3465,14 +3670,31 @@ class UsersManager {
3465
3670
  _importUser(user) {
3466
3671
  this.handleUsers([user]);
3467
3672
  }
3673
+
3674
+ /**
3675
+ * For internal use.
3676
+ * @internal
3677
+ */
3678
+ _importRooms(rooms) {
3679
+ this.handleRooms(rooms);
3680
+ }
3468
3681
  handleMembers(members) {
3469
3682
  this.handleUsers(members.map(extractUserFromMember));
3470
3683
  }
3684
+ handleRooms(rooms) {
3685
+ const users = new Map();
3686
+ for (const room of rooms) {
3687
+ for (const participant of room.participants ?? []) {
3688
+ users.set(participant.user.id, participant.user);
3689
+ }
3690
+ }
3691
+ this.handleUsers([...users.values()]);
3692
+ }
3471
3693
  handleSession(session) {
3472
3694
  // Keep the "seen users" cache across reconnects so bound user lists do
3473
- // not blank out; just ensure our own user is present/updated. Stale
3474
- // entries are refreshed as member/message collections refetch.
3695
+ // not blank out. Stale entries are refreshed as member/message collections refetch.
3475
3696
  this.handleUsers([session.user]);
3697
+ this.handleRooms(session.state.rooms);
3476
3698
  }
3477
3699
 
3478
3700
  /**
@@ -3494,20 +3716,6 @@ class UsersManager {
3494
3716
  this.users.set(...changedUsers);
3495
3717
  }
3496
3718
  }
3497
- function isSameUser(a, b) {
3498
- const keys = Object.keys(a);
3499
- if (keys.length !== Object.keys(b).length) {
3500
- return false;
3501
- }
3502
- return keys.every(key => {
3503
- const valueA = a[key];
3504
- const valueB = b[key];
3505
- if (Array.isArray(valueA) && Array.isArray(valueB)) {
3506
- return valueA.length === valueB.length && valueA.every((item, index) => item === valueB[index]);
3507
- }
3508
- return valueA === valueB;
3509
- });
3510
- }
3511
3719
  ;// ./src/state-tracker/RelationshipsManager.ts
3512
3720
 
3513
3721
 
@@ -3703,6 +3911,7 @@ class ChatStateTracker {
3703
3911
  try {
3704
3912
  this._me = snapshot.user;
3705
3913
  this.users._importUser(snapshot.user);
3914
+ this.users._importRooms(snapshot.rooms);
3706
3915
  this.rooms._importRooms(snapshot.rooms);
3707
3916
  this.spaces._importSpaces(snapshot.spaces, snapshot.spaceRooms);
3708
3917
  this.rooms.followedTopics._importFollowedTopics(snapshot.followedTopics);
@@ -4216,7 +4425,9 @@ let ErrorCode = /*#__PURE__*/function (ErrorCode) {
4216
4425
  ErrorCode["InvalidSearchQuery"] = "InvalidSearchQueryException";
4217
4426
  ErrorCode["MessageSearchUnavailable"] = "MessageSearchUnavailableException";
4218
4427
  ErrorCode["MessageNotFound"] = "MessageNotFoundException";
4428
+ ErrorCode["RoomIgnored"] = "RoomIgnoredException";
4219
4429
  ErrorCode["RoomNotFound"] = "RoomNotFoundException";
4430
+ ErrorCode["SpaceJoinRestricted"] = "SpaceJoinRestrictedException";
4220
4431
  ErrorCode["TopicNotFound"] = "TopicNotFoundException";
4221
4432
  ErrorCode["UserNotFound"] = "UserNotFoundException";
4222
4433
  return ErrorCode;
@@ -4354,6 +4565,7 @@ let SpaceIntent = /*#__PURE__*/function (SpaceIntent) {
4354
4565
 
4355
4566
 
4356
4567
 
4357
- 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 };
4568
+
4569
+ export { AbstractRestClient, ErrorCode, FilesClient, HistoryRotation, IndexedCollection, IndexedObjectCollection, Layer, NO_LIMIT, ObservableIndexedCollection, ObservableIndexedObjectCollection, PermissionDefinition, Permissions, PmParticipantFlag, RoleFlag, STATE_SNAPSHOT_VERSION, STORAGE_UNIT_BYTES, SearchRange, SpaceFeature, SpaceIntent, TYPING_INTERVAL_MS, TYPING_TIMEOUT_MS, UiPurpose, UserFeature, UserStatus, WebApiChatClient, WebSocketChatClient, extractUserFromMember, getPmPeers, isGroupPm };
4358
4570
 
4359
4571
  //# sourceMappingURL=index.mjs.map