@devanachat/sdk 0.10.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.
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.
@@ -2099,9 +2222,11 @@ function isSameUser(a, b) {
2099
2222
 
2100
2223
 
2101
2224
 
2225
+
2102
2226
  class RoomsManager {
2103
2227
  messages;
2104
2228
  followedTopics;
2229
+ typing;
2105
2230
  list = new ObservableIndexedObjectCollection('id');
2106
2231
  topics = new IndexedCollection();
2107
2232
  members = new IndexedCollection();
@@ -2113,6 +2238,7 @@ class RoomsManager {
2113
2238
  this.tracker = tracker;
2114
2239
  this.messages = new MessagesManager(tracker);
2115
2240
  this.followedTopics = new FollowedTopicsManager(tracker);
2241
+ this.typing = new TypingManager(tracker);
2116
2242
  this.tracker.client.on('NewMessage', ev => this.handleNewMessage(ev));
2117
2243
  this.tracker.client.on('MessagesRedacted', ev => this.handleMessagesRedacted(ev));
2118
2244
  this.tracker.client.on('NewTopic', ev => this.handleNewTopic(ev));
@@ -2505,6 +2631,9 @@ class RoomsManager {
2505
2631
  extras: ''
2506
2632
  };
2507
2633
  });
2634
+
2635
+ // GetRoomMembers is not supported for private conversations
2636
+ this.membersPromises.register(Promise.resolve(), room.id);
2508
2637
  if (current?.length === members.length && members.every(member => current.get(member.user.id) === member)) {
2509
2638
  return;
2510
2639
  }
@@ -2512,8 +2641,6 @@ class RoomsManager {
2512
2641
  id: room.id,
2513
2642
  members
2514
2643
  });
2515
- // GetRoomMembers is not supported for private conversations
2516
- this.membersPromises.register(Promise.resolve(), room.id);
2517
2644
  }
2518
2645
  handleRoomLeft(ev) {
2519
2646
  this.deleteRoom(ev.id);
@@ -2603,7 +2730,8 @@ class RoomsManager {
2603
2730
  const newTopic = {
2604
2731
  ...topic,
2605
2732
  messageCount: topic.messageCount + 1,
2606
- lastMessage: ev.message
2733
+ lastMessage: ev.message,
2734
+ stickyMessage: ev.sticky ? ev.message : topic.stickyMessage
2607
2735
  };
2608
2736
  topics.set(newTopic);
2609
2737
  this.updatePmDefaultTopic(ev.message.location.roomId, newTopic);
@@ -2615,10 +2743,14 @@ class RoomsManager {
2615
2743
  if (topic) {
2616
2744
  const messageCount = Math.max(topic.messageCount - ev.ids.length, 0);
2617
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;
2618
2749
  const newTopic = {
2619
2750
  ...topic,
2620
2751
  messageCount,
2621
- lastMessage
2752
+ lastMessage,
2753
+ stickyMessage
2622
2754
  };
2623
2755
  topics.set(newTopic);
2624
2756
  this.updatePmDefaultTopic(ev.location.roomId, newTopic);
@@ -2828,6 +2960,13 @@ class SpacesManager {
2828
2960
  }
2829
2961
  handleRoleDeleted(ev) {
2830
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
+ }
2831
2970
  this.syncSpaceRoles(ev.spaceId);
2832
2971
  }
2833
2972
 
@@ -3096,6 +3235,14 @@ class Permissions {
3096
3235
  UseExternalEmoticons: {
3097
3236
  value: 1 << 26,
3098
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
3099
3246
  }
3100
3247
  };
3101
3248
  static getNames() {
@@ -3229,7 +3376,7 @@ class PermissionsManager extends EventTarget {
3229
3376
  userId
3230
3377
  }).then(v => v.overwrites));
3231
3378
  }
3232
- return this.resolveOverwritesHierarchy(await Promise.all(promises));
3379
+ return this.resolveOverwritesHierarchy([].concat(...(await Promise.all(promises))));
3233
3380
  }
3234
3381
 
3235
3382
  /**
@@ -3306,34 +3453,22 @@ class PermissionsManager extends EventTarget {
3306
3453
  type: 'Role',
3307
3454
  roleId
3308
3455
  })));
3309
- return this.resolveOverwritesFromRolesByOrder(location.spaceId, roleOverwrites);
3310
- }
3311
- async resolveOverwritesFromRolesByOrder(spaceId, overwrites) {
3312
- let allows = 0,
3313
- denies = 0;
3314
- const roles = await this.tracker.spaces.getRoles(spaceId);
3315
- const sortedOverwrites = overwrites.sort((a, b) => roles.get(a.target.roleId).priority - roles.get(b.target.roleId).priority);
3316
-
3317
- // Max length of bit word
3318
- const permissionsLength = overwrites.reduce((previousValue, currentValue) => Math.max(previousValue, currentValue.overwrites.allow?.toString(2).length ?? 0, currentValue.overwrites.deny?.toString(2).length ?? 0), 0);
3319
- sortedOverwrites.forEach(overwriteEvent => {
3320
- const overwrites = overwriteEvent.overwrites;
3321
- const revDecDenies = overwrites.deny?.toString(2).split('').reverse().join('') ?? '';
3322
- const revDecAllows = overwrites.allow?.toString(2).split('').reverse().join('') ?? '';
3323
- for (let i = 0; i < permissionsLength; i++) {
3324
- const deny = parseInt(revDecDenies[i] ?? '0');
3325
- const allow = parseInt(revDecAllows[i] ?? '0');
3326
- if (deny) {
3327
- denies |= 1 << i;
3328
- }
3329
- if (allow) {
3330
- allows |= 1 << i;
3331
- }
3332
- }
3333
- });
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
+ }
3334
3469
  return {
3335
- allow: allows,
3336
- deny: denies
3470
+ allow,
3471
+ deny
3337
3472
  };
3338
3473
  }
3339
3474
  resolveOverwritesHierarchy(permissionOverwritesValues) {
@@ -4292,6 +4427,7 @@ let ErrorCode = /*#__PURE__*/function (ErrorCode) {
4292
4427
  ErrorCode["MessageNotFound"] = "MessageNotFoundException";
4293
4428
  ErrorCode["RoomIgnored"] = "RoomIgnoredException";
4294
4429
  ErrorCode["RoomNotFound"] = "RoomNotFoundException";
4430
+ ErrorCode["SpaceJoinRestricted"] = "SpaceJoinRestrictedException";
4295
4431
  ErrorCode["TopicNotFound"] = "TopicNotFoundException";
4296
4432
  ErrorCode["UserNotFound"] = "UserNotFoundException";
4297
4433
  return ErrorCode;
@@ -4429,6 +4565,7 @@ let SpaceIntent = /*#__PURE__*/function (SpaceIntent) {
4429
4565
 
4430
4566
 
4431
4567
 
4432
- 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, UiPurpose, UserFeature, UserStatus, WebApiChatClient, WebSocketChatClient, extractUserFromMember, getPmPeers, isGroupPm };
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 };
4433
4570
 
4434
4571
  //# sourceMappingURL=index.mjs.map