@devanachat/sdk 0.6.0 → 0.7.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
@@ -548,7 +548,17 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
548
548
  this.emitChangeWithDiff(true, originalState);
549
549
  }
550
550
  async fetchNext() {
551
- if (this.internalState.ongoing || this.hasLatest) {
551
+ if (this.hasLatest) {
552
+ return;
553
+ }
554
+ return this.fetchAfterLast();
555
+ }
556
+
557
+ /**
558
+ * Append the items that follow the last one in the window; an empty window gets the latest items.
559
+ */
560
+ async fetchAfterLast() {
561
+ if (this.internalState.ongoing) {
552
562
  return;
553
563
  }
554
564
  let result;
@@ -561,7 +571,7 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
561
571
  this.internalState.ongoing = undefined;
562
572
  }
563
573
  if (!result) {
564
- await this.resetToLatest();
574
+ await this.resetToLatest(true);
565
575
  return;
566
576
  }
567
577
  if (result.length) {
@@ -671,23 +681,31 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
671
681
  /**
672
682
  * The window does not subscribe to the client itself - the messages manager routes the events of its topic to it.
673
683
  */
674
- constructor(roomId, topicId, tracker) {
684
+ constructor(roomId, topicId, tracker, preview = false) {
675
685
  super('id');
676
686
  this.roomId = roomId;
677
687
  this.topicId = topicId;
678
688
  this.tracker = tracker;
689
+ this.preview = preview;
679
690
  this.internalState.traverseLock = false;
680
- this.internalState.includeMyReactions = true;
691
+ this.internalState.includeMyReactions = !preview;
681
692
  this.internalState.myReactions = {};
682
693
  this.internalState.liveLimit = 50;
683
694
  }
684
695
  createMirror() {
685
- const copy = new TopicHistoryWindow(this.roomId, this.topicId, this.tracker);
696
+ const copy = new TopicHistoryWindow(this.roomId, this.topicId, this.tracker, this.preview);
686
697
  copy.eventTarget = this.eventTarget;
687
698
  copy._items = this._items;
688
699
  copy.internalState = this.internalState;
689
700
  return copy;
690
701
  }
702
+
703
+ /**
704
+ * Window of a room you have not joined. It gets no live messages - at the latest ones fetchNext() fetches the newer.
705
+ */
706
+ get isPreview() {
707
+ return this.preview;
708
+ }
691
709
  get isTraverseLocked() {
692
710
  return this.internalState.traverseLock;
693
711
  }
@@ -738,6 +756,9 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
738
756
  if (this.internalState.traverseLock) {
739
757
  return;
740
758
  }
759
+ if (this.preview && this.hasLatest) {
760
+ return this.fetchAfterLast(); // No events reach a preview
761
+ }
741
762
  return super.fetchNext();
742
763
  }
743
764
  async fetchPrevious() {
@@ -830,6 +851,17 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
830
851
  this.internalState.myReactions = next;
831
852
  }
832
853
  async getTopic() {
854
+ if (this.preview) {
855
+ // The tracker holds the topics of the joined rooms only
856
+ const result = await this.tracker.client.send('GetTopics', {
857
+ roomId: this.roomId,
858
+ topicIds: [this.topicId]
859
+ });
860
+ if (result.error) {
861
+ throw new Error(`Cannot fetch topic: ${result.error.message}`);
862
+ }
863
+ return result.data.topics.find(topic => topic.id === this.topicId);
864
+ }
833
865
  return (await this.tracker.rooms.getTopics(this.roomId, [this.topicId])).get(this.topicId);
834
866
  }
835
867
  async getLatestMessageId() {
@@ -944,13 +976,20 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
944
976
  * dropped together with its room stops receiving them.
945
977
  */
946
978
  class RoomMessagesHistory {
979
+ isPreview;
980
+ roomId;
947
981
  historyWindows = new IndexedCollection();
948
982
  traverseLock = false;
983
+ room;
949
984
  constructor(room, tracker) {
950
- this.room = room;
951
985
  this.tracker = tracker;
952
- this.updateTraverseLock(this.room);
953
- if (this.room.defaultTopic) {
986
+ this.roomId = typeof room === 'string' ? room : room.id;
987
+ this.room = typeof room === 'string' ? null : room;
988
+ this.isPreview = this.room === null;
989
+ if (this.room) {
990
+ this.updateTraverseLock(this.room);
991
+ }
992
+ if (this.room?.defaultTopic) {
954
993
  this.createHistoryWindowForTopic(this.room.defaultTopic);
955
994
  }
956
995
  }
@@ -962,9 +1001,13 @@ class RoomMessagesHistory {
962
1001
  */
963
1002
  async getMessagesWindow(topicId, peek = false) {
964
1003
  if (!this.historyWindows.has(topicId) && !peek) {
965
- const topic = (await this.tracker.rooms.getTopics(this.room.id, [topicId])).get(topicId);
966
- if (topic) {
967
- this.createHistoryWindowForTopic(topic);
1004
+ if (this.isPreview) {
1005
+ this.createHistoryWindow(topicId);
1006
+ } else {
1007
+ const topic = (await this.tracker.rooms.getTopics(this.roomId, [topicId])).get(topicId);
1008
+ if (topic) {
1009
+ this.createHistoryWindowForTopic(topic);
1010
+ }
968
1011
  }
969
1012
  }
970
1013
  return this.historyWindows.get(topicId);
@@ -1077,9 +1120,18 @@ class RoomMessagesHistory {
1077
1120
  if (this.historyWindows.has(topic.id)) {
1078
1121
  return;
1079
1122
  }
1080
- const historyWindow = new TopicHistoryWindow(this.room.id, topic.id, this.tracker);
1123
+ this.createHistoryWindow(topic.id);
1124
+
1125
+ // If new topic refers to some message from this room, update other structures
1126
+ if (topic.refMessage) {
1127
+ const refHistoryWindow = this.historyWindows.get(topic.refMessage.location.topicId);
1128
+ refHistoryWindow?._updateMessageReference(topic);
1129
+ }
1130
+ }
1131
+ createHistoryWindow(topicId) {
1132
+ const historyWindow = new TopicHistoryWindow(this.roomId, topicId, this.tracker, this.isPreview);
1081
1133
  void historyWindow.setTraverseLock(this.traverseLock);
1082
- this.historyWindows.set([topic.id, historyWindow]);
1134
+ this.historyWindows.set([topicId, historyWindow]);
1083
1135
 
1084
1136
  // Current behavior of deletion a message with referenced topic is to delete the whole side topic
1085
1137
  // So we need to listen for topic deletions here
@@ -1087,14 +1139,8 @@ class RoomMessagesHistory {
1087
1139
  for (const topicId of deletedTopicIds) {
1088
1140
  this.historyWindows.delete(topicId);
1089
1141
  }
1090
- this.tracker.rooms._deleteTopicsFromRoom(this.room.id, ...deletedTopicIds);
1142
+ this.tracker.rooms._deleteTopicsFromRoom(this.roomId, ...deletedTopicIds);
1091
1143
  });
1092
-
1093
- // If new topic refers to some message from this room, update other structures
1094
- if (topic.refMessage) {
1095
- const refHistoryWindow = this.historyWindows.get(topic.refMessage.location.topicId);
1096
- refHistoryWindow?._updateMessageReference(topic);
1097
- }
1098
1144
  }
1099
1145
  updateTraverseLock(room) {
1100
1146
  this.traverseLock = room.history.mode === 'Ephemeral';
@@ -1106,6 +1152,7 @@ class RoomMessagesHistory {
1106
1152
 
1107
1153
  class MessagesManager {
1108
1154
  roomHistories = new IndexedCollection();
1155
+ previewHistories = new IndexedCollection();
1109
1156
  deferredSession = new DeferredTask();
1110
1157
  constructor(tracker) {
1111
1158
  this.tracker = tracker;
@@ -1126,11 +1173,21 @@ class MessagesManager {
1126
1173
  }
1127
1174
 
1128
1175
  /**
1129
- * Get history manager for given room ID.
1176
+ * Get history manager for given room ID. For a room you have not joined it is a preview (requires `PreviewRooms`),
1177
+ * kept until {@link forgetRoomPreview} or until you join the room.
1130
1178
  */
1131
1179
  async getRoomHistory(roomId) {
1132
1180
  await this.deferredSession.promise;
1133
- return this.roomHistories.get(roomId);
1181
+ if (this.roomHistories.has(roomId)) {
1182
+ return this.roomHistories.get(roomId);
1183
+ }
1184
+ if (!this.previewHistories.has(roomId)) {
1185
+ this.previewHistories.set([roomId, new RoomMessagesHistory(roomId, this.tracker)]);
1186
+ }
1187
+ return this.previewHistories.get(roomId);
1188
+ }
1189
+ forgetRoomPreview(roomId) {
1190
+ this.previewHistories.delete(roomId);
1134
1191
  }
1135
1192
 
1136
1193
  /**
@@ -1138,8 +1195,10 @@ class MessagesManager {
1138
1195
  * @internal
1139
1196
  */
1140
1197
  async _resolveLastMessage(location) {
1141
- // Try to get last message from history window (if cached)
1142
- let message = await this.getRoomHistory(location.roomId).then(roomHistory => roomHistory?.getMessagesWindow(location.topicId, true)).then(historyWindow => historyWindow?.hasLatest && historyWindow.getAt(historyWindow.length - 1));
1198
+ // Try to get last message from history window (if cached); not getRoomHistory(), it would create a preview
1199
+ await this.deferredSession.promise;
1200
+ const historyWindow = await this.roomHistories.get(location.roomId)?.getMessagesWindow(location.topicId, true);
1201
+ let message = historyWindow?.hasLatest && historyWindow.getAt(historyWindow.length - 1);
1143
1202
  if (!message) {
1144
1203
  const result = await this.tracker.client.send('GetMessages', {
1145
1204
  location,
@@ -1155,6 +1214,7 @@ class MessagesManager {
1155
1214
  }
1156
1215
  }
1157
1216
  createHistoryForNewRoom(room) {
1217
+ this.previewHistories.delete(room.id);
1158
1218
  this.roomHistories.set([room.id, new RoomMessagesHistory(room, this.tracker)]);
1159
1219
  }
1160
1220
  handleRoomDeleted(ev) {
@@ -2374,6 +2434,14 @@ class Permissions {
2374
2434
  MentionAllRoles: {
2375
2435
  value: 1 << 23,
2376
2436
  maxLayer: Layer.Topic
2437
+ },
2438
+ PreviewRooms: {
2439
+ value: 1 << 24,
2440
+ maxLayer: Layer.Room
2441
+ },
2442
+ SearchMessages: {
2443
+ value: 1 << 25,
2444
+ maxLayer: Layer.Room
2377
2445
  }
2378
2446
  };
2379
2447
  static getNames() {
@@ -2451,6 +2519,10 @@ class PermissionsManager extends EventTarget {
2451
2519
  missing
2452
2520
  };
2453
2521
  }
2522
+
2523
+ /**
2524
+ * For a room or a space you have not joined the permissions are fetched from the server, on every call.
2525
+ */
2454
2526
  async calculatePermissions(location) {
2455
2527
  const room = location.roomId ? (await this.tracker.rooms.get()).get(location.roomId) : null;
2456
2528
  location = {
@@ -2459,6 +2531,9 @@ class PermissionsManager extends EventTarget {
2459
2531
  topicId: location.topicId
2460
2532
  };
2461
2533
  this.validateLocation(location);
2534
+ if (location.roomId && !room || location.spaceId && !(await this.tracker.spaces.get())?.has(location.spaceId)) {
2535
+ return this.fetchComputedPermissions(location);
2536
+ }
2462
2537
  const userId = (await this.tracker.getMe()).id;
2463
2538
  const [spaceMember, roomMember] = await this.fetchMembersOrFail(location);
2464
2539
  const userRoles = [...(spaceMember?.roles ?? []), ...(roomMember?.roles ?? [])];
@@ -2624,6 +2699,15 @@ class PermissionsManager extends EventTarget {
2624
2699
  }
2625
2700
  return result;
2626
2701
  }
2702
+ async fetchComputedPermissions(location) {
2703
+ const result = await this.tracker.client.send('GetComputedPermissions', {
2704
+ location
2705
+ });
2706
+ if (result.error) {
2707
+ throw result.error;
2708
+ }
2709
+ return result.data.permissions;
2710
+ }
2627
2711
  async fetchMembersOrFail(location) {
2628
2712
  const results = await Promise.all([location.spaceId ? this.tracker.spaces.getMe(location.spaceId) : null, location.roomId ? this.tracker.rooms.getMe(location.roomId) : null]);
2629
2713
  const spaceFail = location.spaceId && !results[0];
@@ -3366,6 +3450,18 @@ class FilesClient extends AbstractRestClient {
3366
3450
  return this.send('GET', 'files?' + searchParams);
3367
3451
  }
3368
3452
  }
3453
+ ;// ./src/protocol/schemes/events/Error.ts
3454
+ let ErrorCode = /*#__PURE__*/function (ErrorCode) {
3455
+ ErrorCode["Authentication"] = "AuthenticationException";
3456
+ ErrorCode["CommandRateLimitExceeded"] = "CommandRateLimitExceededException";
3457
+ ErrorCode["InvalidSearchQuery"] = "InvalidSearchQueryException";
3458
+ ErrorCode["MessageSearchUnavailable"] = "MessageSearchUnavailableException";
3459
+ ErrorCode["MessageNotFound"] = "MessageNotFoundException";
3460
+ ErrorCode["RoomNotFound"] = "RoomNotFoundException";
3461
+ ErrorCode["TopicNotFound"] = "TopicNotFoundException";
3462
+ ErrorCode["UserNotFound"] = "UserNotFoundException";
3463
+ return ErrorCode;
3464
+ }({});
3369
3465
  ;// ./src/protocol/schemes/User.ts
3370
3466
  /**
3371
3467
  * Availability of a user, aggregated over all of their sessions.
@@ -3512,6 +3608,6 @@ let SpaceIntent = /*#__PURE__*/function (SpaceIntent) {
3512
3608
 
3513
3609
 
3514
3610
 
3515
- export { AbstractRestClient, FilesClient, HistoryRotation, IndexedCollection, IndexedObjectCollection, Layer, NO_LIMIT, ObservableIndexedCollection, ObservableIndexedObjectCollection, PermissionDefinition, Permissions, RoleFlag, STORAGE_UNIT_BYTES, SearchRange, SpaceFeature, SpaceIntent, UiPurpose, UserFeature, UserStatus, WebApiChatClient, WebSocketChatClient, extractUserFromMember };
3611
+ export { AbstractRestClient, ErrorCode, FilesClient, HistoryRotation, IndexedCollection, IndexedObjectCollection, Layer, NO_LIMIT, ObservableIndexedCollection, ObservableIndexedObjectCollection, PermissionDefinition, Permissions, RoleFlag, STORAGE_UNIT_BYTES, SearchRange, SpaceFeature, SpaceIntent, UiPurpose, UserFeature, UserStatus, WebApiChatClient, WebSocketChatClient, extractUserFromMember };
3516
3612
 
3517
3613
  //# sourceMappingURL=index.mjs.map