stream-chat 6.7.2 → 6.9.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/dist/index.js CHANGED
@@ -159,6 +159,8 @@ var ChannelState = /*#__PURE__*/function () {
159
159
 
160
160
  _defineProperty__default['default'](this, "pinnedMessages", void 0);
161
161
 
162
+ _defineProperty__default['default'](this, "pending_messages", void 0);
163
+
162
164
  _defineProperty__default['default'](this, "threads", void 0);
163
165
 
164
166
  _defineProperty__default['default'](this, "mutedUsers", void 0);
@@ -279,6 +281,7 @@ var ChannelState = /*#__PURE__*/function () {
279
281
  this.read = {};
280
282
  this.initMessages();
281
283
  this.pinnedMessages = [];
284
+ this.pending_messages = [];
282
285
  this.threads = {}; // a list of users to hide messages from
283
286
 
284
287
  this.mutedUsers = [];
@@ -835,40 +838,46 @@ var ChannelState = /*#__PURE__*/function () {
835
838
  var _loadMessageIntoState = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee(messageId, parentMessageId) {
836
839
  var _this$threads$parentM;
837
840
 
838
- var messageSetIndex, switchedToMessageSet, loadedMessageThread, messageIdToFind;
841
+ var limit,
842
+ messageSetIndex,
843
+ switchedToMessageSet,
844
+ loadedMessageThread,
845
+ messageIdToFind,
846
+ _args = arguments;
839
847
  return _regeneratorRuntime__default['default'].wrap(function _callee$(_context) {
840
848
  while (1) {
841
849
  switch (_context.prev = _context.next) {
842
850
  case 0:
851
+ limit = _args.length > 2 && _args[2] !== undefined ? _args[2] : 25;
843
852
  switchedToMessageSet = false;
844
853
  loadedMessageThread = false;
845
854
  messageIdToFind = parentMessageId || messageId;
846
855
 
847
856
  if (!(messageId === 'latest')) {
848
- _context.next = 9;
857
+ _context.next = 10;
849
858
  break;
850
859
  }
851
860
 
852
861
  if (!(this.messages === this.latestMessages)) {
853
- _context.next = 6;
862
+ _context.next = 7;
854
863
  break;
855
864
  }
856
865
 
857
866
  return _context.abrupt("return");
858
867
 
859
- case 6:
868
+ case 7:
860
869
  messageSetIndex = this.messageSets.findIndex(function (s) {
861
870
  return s.isLatest;
862
871
  });
863
- _context.next = 10;
872
+ _context.next = 11;
864
873
  break;
865
874
 
866
- case 9:
875
+ case 10:
867
876
  messageSetIndex = this.findMessageSetIndex({
868
877
  id: messageIdToFind
869
878
  });
870
879
 
871
- case 10:
880
+ case 11:
872
881
  if (messageSetIndex !== -1) {
873
882
  this.switchToMessageSet(messageSetIndex);
874
883
  switchedToMessageSet = true;
@@ -879,39 +888,39 @@ var ChannelState = /*#__PURE__*/function () {
879
888
  }));
880
889
 
881
890
  if (!(switchedToMessageSet && loadedMessageThread)) {
882
- _context.next = 14;
891
+ _context.next = 15;
883
892
  break;
884
893
  }
885
894
 
886
895
  return _context.abrupt("return");
887
896
 
888
- case 14:
897
+ case 15:
889
898
  if (switchedToMessageSet) {
890
- _context.next = 17;
899
+ _context.next = 18;
891
900
  break;
892
901
  }
893
902
 
894
- _context.next = 17;
903
+ _context.next = 18;
895
904
  return this._channel.query({
896
905
  messages: {
897
906
  id_around: messageIdToFind,
898
- limit: 25
907
+ limit: limit
899
908
  }
900
909
  }, 'new');
901
910
 
902
- case 17:
911
+ case 18:
903
912
  if (!(!loadedMessageThread && parentMessageId)) {
904
- _context.next = 20;
913
+ _context.next = 21;
905
914
  break;
906
915
  }
907
916
 
908
- _context.next = 20;
917
+ _context.next = 21;
909
918
  return this._channel.getReplies(parentMessageId, {
910
919
  id_around: messageId,
911
- limit: 25
920
+ limit: limit
912
921
  });
913
922
 
914
- case 20:
923
+ case 21:
915
924
  messageSetIndex = this.findMessageSetIndex({
916
925
  id: messageIdToFind
917
926
  });
@@ -920,7 +929,7 @@ var ChannelState = /*#__PURE__*/function () {
920
929
  this.switchToMessageSet(messageSetIndex);
921
930
  }
922
931
 
923
- case 22:
932
+ case 23:
924
933
  case "end":
925
934
  return _context.stop();
926
935
  }
@@ -934,6 +943,42 @@ var ChannelState = /*#__PURE__*/function () {
934
943
 
935
944
  return loadMessageIntoState;
936
945
  }()
946
+ /**
947
+ * findMessage - Finds a message inside the state
948
+ *
949
+ * @param {string} messageId The id of the message
950
+ * @param {string} parentMessageId The id of the parent message, if we want load a thread reply
951
+ *
952
+ * @return {ReturnType<ChannelState<StreamChatGenerics>['formatMessage']>} Returns the message, or undefined if the message wasn't found
953
+ */
954
+
955
+ }, {
956
+ key: "findMessage",
957
+ value: function findMessage(messageId, parentMessageId) {
958
+ if (parentMessageId) {
959
+ var messages = this.threads[parentMessageId];
960
+
961
+ if (!messages) {
962
+ return undefined;
963
+ }
964
+
965
+ return messages.find(function (m) {
966
+ return m.id === messageId;
967
+ });
968
+ }
969
+
970
+ var messageSetIndex = this.findMessageSetIndex({
971
+ id: messageId
972
+ });
973
+
974
+ if (messageSetIndex === -1) {
975
+ return undefined;
976
+ }
977
+
978
+ return this.messageSets[messageSetIndex].messages.find(function (m) {
979
+ return m.id === messageId;
980
+ });
981
+ }
937
982
  }, {
938
983
  key: "switchToMessageSet",
939
984
  value: function switchToMessageSet(index) {
@@ -1522,6 +1567,8 @@ var Channel = /*#__PURE__*/function () {
1522
1567
  * @param {Message<StreamChatGenerics>} message The Message object
1523
1568
  * @param {boolean} [options.skip_enrich_url] Do not try to enrich the URLs within message
1524
1569
  * @param {boolean} [options.skip_push] Skip sending push notifications
1570
+ * @param {boolean} [options.is_pending_message] Make this message pending
1571
+ * @param {Record<string,string>} [options.pending_message_metadata] Metadata for the pending message
1525
1572
  *
1526
1573
  * @return {Promise<SendMessageAPIResponse<StreamChatGenerics>>} The Server Response
1527
1574
  */
@@ -1928,6 +1975,7 @@ var Channel = /*#__PURE__*/function () {
1928
1975
  key: "updatePartial",
1929
1976
  value: function () {
1930
1977
  var _updatePartial = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee8(update) {
1978
+ var data;
1931
1979
  return _regeneratorRuntime__default['default'].wrap(function _callee8$(_context8) {
1932
1980
  while (1) {
1933
1981
  switch (_context8.prev = _context8.next) {
@@ -1936,9 +1984,11 @@ var Channel = /*#__PURE__*/function () {
1936
1984
  return this.getClient().patch(this._channelURL(), update);
1937
1985
 
1938
1986
  case 2:
1939
- return _context8.abrupt("return", _context8.sent);
1987
+ data = _context8.sent;
1988
+ this.data = data.channel;
1989
+ return _context8.abrupt("return", data);
1940
1990
 
1941
- case 3:
1991
+ case 5:
1942
1992
  case "end":
1943
1993
  return _context8.stop();
1944
1994
  }
@@ -3180,9 +3230,10 @@ var Channel = /*#__PURE__*/function () {
3180
3230
 
3181
3231
  this._initializeState(state, messageSetToAddToIfDoesNotExist);
3182
3232
 
3233
+ this.data = state.channel;
3183
3234
  return _context31.abrupt("return", state);
3184
3235
 
3185
- case 12:
3236
+ case 13:
3186
3237
  case "end":
3187
3238
  return _context31.stop();
3188
3239
  }
@@ -3627,6 +3678,8 @@ var Channel = /*#__PURE__*/function () {
3627
3678
 
3628
3679
  case 'message.deleted':
3629
3680
  if (event.message) {
3681
+ this._extendEventWithOwnReactions(event);
3682
+
3630
3683
  if (event.hard_delete) channelState.removeMessage(event.message);else channelState.addMessageSorted(event.message, false, false);
3631
3684
  channelState.removeQuotedMessageReferences(event.message);
3632
3685
 
@@ -3669,6 +3722,8 @@ var Channel = /*#__PURE__*/function () {
3669
3722
 
3670
3723
  case 'message.updated':
3671
3724
  if (event.message) {
3725
+ this._extendEventWithOwnReactions(event);
3726
+
3672
3727
  channelState.addMessageSorted(event.message, false, false);
3673
3728
 
3674
3729
  if (event.message.pinned) {
@@ -3830,6 +3885,11 @@ var Channel = /*#__PURE__*/function () {
3830
3885
  }
3831
3886
 
3832
3887
  this.state.addPinnedMessages(state.pinned_messages || []);
3888
+
3889
+ if (state.pending_messages) {
3890
+ this.state.pending_messages = state.pending_messages;
3891
+ }
3892
+
3833
3893
  this.state.watcher_count = state.watcher_count || 0; // convert the arrays into objects for easier syncing...
3834
3894
 
3835
3895
  if (state.watchers) {
@@ -3913,6 +3973,19 @@ var Channel = /*#__PURE__*/function () {
3913
3973
  }
3914
3974
  }
3915
3975
  }
3976
+ }, {
3977
+ key: "_extendEventWithOwnReactions",
3978
+ value: function _extendEventWithOwnReactions(event) {
3979
+ if (!event.message) {
3980
+ return;
3981
+ }
3982
+
3983
+ var message = this.state.findMessage(event.message.id, event.message.parent_id);
3984
+
3985
+ if (message) {
3986
+ event.message.own_reactions = message.own_reactions;
3987
+ }
3988
+ }
3916
3989
  }, {
3917
3990
  key: "_disconnect",
3918
3991
  value: function _disconnect() {
@@ -4488,6 +4561,11 @@ var StableWSConnection = /*#__PURE__*/function () {
4488
4561
  tags: ['connection']
4489
4562
  }, extra));
4490
4563
  }
4564
+ }, {
4565
+ key: "setClient",
4566
+ value: function setClient(client) {
4567
+ this.client = client;
4568
+ }
4491
4569
  /**
4492
4570
  * connect - Connect to the WS URL
4493
4571
  * the default 15s timeout allows between 2~3 tries
@@ -6152,7 +6230,7 @@ var StreamChat = /*#__PURE__*/function () {
6152
6230
  throw Error('User is not set on client, use client.connectUser or client.connectAnonymousUser instead');
6153
6231
 
6154
6232
  case 2:
6155
- if (!((_this$wsConnection3 = _this.wsConnection) !== null && _this$wsConnection3 !== void 0 && _this$wsConnection3.isConnecting)) {
6233
+ if (!((_this$wsConnection3 = _this.wsConnection) !== null && _this$wsConnection3 !== void 0 && _this$wsConnection3.isConnecting && _this.wsPromise)) {
6156
6234
  _context3.next = 5;
6157
6235
  break;
6158
6236
  }
@@ -6161,7 +6239,7 @@ var StreamChat = /*#__PURE__*/function () {
6161
6239
  tags: ['connection', 'client']
6162
6240
  });
6163
6241
 
6164
- return _context3.abrupt("return", Promise.resolve());
6242
+ return _context3.abrupt("return", _this.wsPromise);
6165
6243
 
6166
6244
  case 5:
6167
6245
  if (!(((_this$wsConnection4 = _this.wsConnection) !== null && _this$wsConnection4 !== void 0 && _this$wsConnection4.isHealthy || (_this$wsFallback3 = _this.wsFallback) !== null && _this$wsFallback3 !== void 0 && _this$wsFallback3.isHealthy()) && _this._hasConnectionID())) {
@@ -6917,23 +6995,28 @@ var StreamChat = /*#__PURE__*/function () {
6917
6995
  */
6918
6996
  function () {
6919
6997
  var _updateAppSettings = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee7(options) {
6920
- var _options$apn_config;
6921
-
6998
+ var apn_config;
6922
6999
  return _regeneratorRuntime__default['default'].wrap(function _callee7$(_context7) {
6923
7000
  while (1) {
6924
7001
  switch (_context7.prev = _context7.next) {
6925
7002
  case 0:
6926
- if ((_options$apn_config = options.apn_config) !== null && _options$apn_config !== void 0 && _options$apn_config.p12_cert) {
6927
- options.apn_config.p12_cert = Buffer.from(options.apn_config.p12_cert).toString('base64');
7003
+ apn_config = options.apn_config;
7004
+
7005
+ if (apn_config !== null && apn_config !== void 0 && apn_config.p12_cert) {
7006
+ options = _objectSpread(_objectSpread({}, options), {}, {
7007
+ apn_config: _objectSpread(_objectSpread({}, apn_config), {}, {
7008
+ p12_cert: Buffer.from(apn_config.p12_cert).toString('base64')
7009
+ })
7010
+ });
6928
7011
  }
6929
7012
 
6930
- _context7.next = 3;
7013
+ _context7.next = 4;
6931
7014
  return this.patch(this.baseURL + '/app', options);
6932
7015
 
6933
- case 3:
7016
+ case 4:
6934
7017
  return _context7.abrupt("return", _context7.sent);
6935
7018
 
6936
- case 4:
7019
+ case 5:
6937
7020
  case "end":
6938
7021
  return _context7.stop();
6939
7022
  }
@@ -7621,9 +7704,16 @@ var StreamChat = /*#__PURE__*/function () {
7621
7704
  } // The StableWSConnection handles all the reconnection logic.
7622
7705
 
7623
7706
 
7624
- this.wsConnection = new StableWSConnection({
7625
- client: this
7626
- });
7707
+ if (this.options.wsConnection && this.node) {
7708
+ // Intentionally avoiding adding ts generics on wsConnection in options since its only useful for unit test purpose.
7709
+ this.options.wsConnection.setClient(this);
7710
+ this.wsConnection = this.options.wsConnection;
7711
+ } else {
7712
+ this.wsConnection = new StableWSConnection({
7713
+ client: this
7714
+ });
7715
+ }
7716
+
7627
7717
  _context15.prev = 8;
7628
7718
 
7629
7719
  if (!this.wsFallback) {
@@ -7749,7 +7839,7 @@ var StreamChat = /*#__PURE__*/function () {
7749
7839
  }; // Make sure we wait for the connect promise if there is a pending one
7750
7840
 
7751
7841
  _context16.next = 5;
7752
- return this.setUserPromise;
7842
+ return this.wsPromise;
7753
7843
 
7754
7844
  case 5:
7755
7845
  if (!this._hasConnectionID()) {
@@ -7929,7 +8019,7 @@ var StreamChat = /*#__PURE__*/function () {
7929
8019
  }; // Make sure we wait for the connect promise if there is a pending one
7930
8020
 
7931
8021
  _context19.next = 7;
7932
- return this.setUserPromise;
8022
+ return this.wsPromise;
7933
8023
 
7934
8024
  case 7:
7935
8025
  if (!this._hasConnectionID()) {
@@ -8064,7 +8154,7 @@ var StreamChat = /*#__PURE__*/function () {
8064
8154
 
8065
8155
  case 13:
8066
8156
  _context20.next = 15;
8067
- return this.setUserPromise;
8157
+ return this.wsPromise;
8068
8158
 
8069
8159
  case 15:
8070
8160
  _context20.next = 17;
@@ -9753,7 +9843,7 @@ var StreamChat = /*#__PURE__*/function () {
9753
9843
  }, {
9754
9844
  key: "getUserAgent",
9755
9845
  value: function getUserAgent() {
9756
- return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "6.7.2");
9846
+ return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "6.9.0");
9757
9847
  }
9758
9848
  }, {
9759
9849
  key: "setUserAgent",
@@ -9926,15 +10016,19 @@ var StreamChat = /*#__PURE__*/function () {
9926
10016
  /** sync - returns all events that happened for a list of channels since last sync
9927
10017
  * @param {string[]} channel_cids list of channel CIDs
9928
10018
  * @param {string} last_sync_at last time the user was online and in sync. RFC3339 ie. "2020-05-06T15:05:01.207Z"
10019
+ * @param {SyncOptions} options See JSDoc in the type fields for more info
10020
+ *
10021
+ * @returns {Promise<SyncResponse>}
9929
10022
  */
9930
10023
 
9931
10024
  }, {
9932
10025
  key: "sync",
9933
10026
  value: function sync(channel_cids, last_sync_at) {
9934
- return this.post("".concat(this.baseURL, "/sync"), {
10027
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
10028
+ return this.post("".concat(this.baseURL, "/sync"), _objectSpread({
9935
10029
  channel_cids: channel_cids,
9936
10030
  last_sync_at: last_sync_at
9937
- });
10031
+ }, options));
9938
10032
  }
9939
10033
  /**
9940
10034
  * sendUserCustomEvent - Send a custom event to a user