stream-chat 6.7.1 → 7.0.0-offline-support.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.
@@ -437,6 +437,10 @@ var ChannelState = /*#__PURE__*/function () {
437
437
  this.threads[parentID] = threadMessages;
438
438
  }
439
439
  }
440
+
441
+ return {
442
+ messageSet: this.messageSets[targetMessageSetIndex]
443
+ };
440
444
  }
441
445
  /**
442
446
  * addPinnedMessages - adds messages in pinnedMessages property
@@ -905,6 +909,42 @@ var ChannelState = /*#__PURE__*/function () {
905
909
 
906
910
  return loadMessageIntoState;
907
911
  }()
912
+ /**
913
+ * findMessage - Finds a message inside the state
914
+ *
915
+ * @param {string} messageId The id of the message
916
+ * @param {string} parentMessageId The id of the parent message, if we want load a thread reply
917
+ *
918
+ * @return {ReturnType<ChannelState<StreamChatGenerics>['formatMessage']>} Returns the message, or undefined if the message wasn't found
919
+ */
920
+
921
+ }, {
922
+ key: "findMessage",
923
+ value: function findMessage(messageId, parentMessageId) {
924
+ if (parentMessageId) {
925
+ var messages = this.threads[parentMessageId];
926
+
927
+ if (!messages) {
928
+ return undefined;
929
+ }
930
+
931
+ return messages.find(function (m) {
932
+ return m.id === messageId;
933
+ });
934
+ }
935
+
936
+ var messageSetIndex = this.findMessageSetIndex({
937
+ id: messageId
938
+ });
939
+
940
+ if (messageSetIndex === -1) {
941
+ return undefined;
942
+ }
943
+
944
+ return this.messageSets[messageSetIndex].messages.find(function (m) {
945
+ return m.id === messageId;
946
+ });
947
+ }
908
948
  }, {
909
949
  key: "switchToMessageSet",
910
950
  value: function switchToMessageSet(index) {
@@ -1077,6 +1117,7 @@ var EVENT_MAP = {
1077
1117
  'user.watching.start': true,
1078
1118
  'user.watching.stop': true,
1079
1119
  // local events
1120
+ 'channels.queried': true,
1080
1121
  'connection.changed': true,
1081
1122
  'connection.recovered': true,
1082
1123
  'transport.changed': true
@@ -1368,6 +1409,8 @@ var Channel = /*#__PURE__*/function () {
1368
1409
 
1369
1410
  _defineProperty(this, "initialized", void 0);
1370
1411
 
1412
+ _defineProperty(this, "staticState", void 0);
1413
+
1371
1414
  _defineProperty(this, "lastKeyStroke", void 0);
1372
1415
 
1373
1416
  _defineProperty(this, "lastTypingEvent", void 0);
@@ -1455,6 +1498,7 @@ var Channel = /*#__PURE__*/function () {
1455
1498
 
1456
1499
  this.state = new ChannelState(this);
1457
1500
  this.initialized = false;
1501
+ this.staticState = true;
1458
1502
  this.lastTypingEvent = null;
1459
1503
  this.isTyping = false;
1460
1504
  this.disconnected = false;
@@ -2774,7 +2818,7 @@ var Channel = /*#__PURE__*/function () {
2774
2818
  *
2775
2819
  * @param {ChannelQueryOptions<StreamChatGenerics>} options additional options for the query endpoint
2776
2820
  *
2777
- * @return {Promise<ChannelAPIResponse<StreamChatGenerics>>} The server response
2821
+ * @return {Promise<QueryChannelAPIResponse<StreamChatGenerics>>} The server response
2778
2822
  */
2779
2823
 
2780
2824
  }, {
@@ -3077,7 +3121,7 @@ var Channel = /*#__PURE__*/function () {
3077
3121
  /**
3078
3122
  * create - Creates a new channel
3079
3123
  *
3080
- * @return {Promise<ChannelAPIResponse<StreamChatGenerics>>} The Server Response
3124
+ * @return {Promise<QueryChannelAPIResponse<StreamChatGenerics>>} The Server Response
3081
3125
  */
3082
3126
 
3083
3127
  }, {
@@ -3089,7 +3133,7 @@ var Channel = /*#__PURE__*/function () {
3089
3133
  * @param {ChannelQueryOptions<StreamChatGenerics>} options The query options
3090
3134
  * @param {MessageSetType} messageSetToAddToIfDoesNotExist It's possible to load disjunct sets of a channel's messages into state, use `current` to load the initial channel state or if you want to extend the currently displayed messages, use `latest` if you want to load/extend the latest messages, `new` is used for loading a specific message and it's surroundings
3091
3135
  *
3092
- * @return {Promise<ChannelAPIResponse<StreamChatGenerics>>} Returns a query response
3136
+ * @return {Promise<QueryChannelAPIResponse<StreamChatGenerics>>} Returns a query response
3093
3137
  */
3094
3138
  function () {
3095
3139
  var _query = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee31(options) {
@@ -3098,7 +3142,10 @@ var Channel = /*#__PURE__*/function () {
3098
3142
  state,
3099
3143
  membersStr,
3100
3144
  tempChannelCid,
3145
+ _this$_initializeStat,
3146
+ messageSet,
3101
3147
  _args31 = arguments;
3148
+
3102
3149
  return _regeneratorRuntime.wrap(function _callee31$(_context31) {
3103
3150
  while (1) {
3104
3151
  switch (_context31.prev = _context31.next) {
@@ -3149,11 +3196,17 @@ var Channel = /*#__PURE__*/function () {
3149
3196
  this.getClient()._addChannelConfig(state); // add any messages to our channel state
3150
3197
 
3151
3198
 
3152
- this._initializeState(state, messageSetToAddToIfDoesNotExist);
3153
-
3199
+ _this$_initializeStat = this._initializeState(state, messageSetToAddToIfDoesNotExist), messageSet = _this$_initializeStat.messageSet;
3200
+ this.getClient().dispatchEvent({
3201
+ type: 'channels.queried',
3202
+ queriedChannels: {
3203
+ channels: [state],
3204
+ isLatestMessageSet: messageSet.isLatest
3205
+ }
3206
+ });
3154
3207
  return _context31.abrupt("return", state);
3155
3208
 
3156
- case 12:
3209
+ case 13:
3157
3210
  case "end":
3158
3211
  return _context31.stop();
3159
3212
  }
@@ -3598,6 +3651,8 @@ var Channel = /*#__PURE__*/function () {
3598
3651
 
3599
3652
  case 'message.deleted':
3600
3653
  if (event.message) {
3654
+ this._extendEventWithOwnReactions(event);
3655
+
3601
3656
  if (event.hard_delete) channelState.removeMessage(event.message);else channelState.addMessageSorted(event.message, false, false);
3602
3657
  channelState.removeQuotedMessageReferences(event.message);
3603
3658
 
@@ -3640,6 +3695,8 @@ var Channel = /*#__PURE__*/function () {
3640
3695
 
3641
3696
  case 'message.updated':
3642
3697
  if (event.message) {
3698
+ this._extendEventWithOwnReactions(event);
3699
+
3643
3700
  channelState.addMessageSorted(event.message, false, false);
3644
3701
 
3645
3702
  if (event.message.pinned) {
@@ -3664,6 +3721,13 @@ var Channel = /*#__PURE__*/function () {
3664
3721
  });
3665
3722
  });
3666
3723
  });
3724
+ channelState.pinnedMessages.forEach(function (_ref3) {
3725
+ var id = _ref3.id,
3726
+ createdAt = _ref3.created_at;
3727
+ if (truncatedAt > +createdAt) channelState.removePinnedMessage({
3728
+ id: id
3729
+ });
3730
+ });
3667
3731
  } else {
3668
3732
  channelState.clearMessages();
3669
3733
  }
@@ -3745,7 +3809,7 @@ var Channel = /*#__PURE__*/function () {
3745
3809
  }, {
3746
3810
  key: "_checkInitialized",
3747
3811
  value: function _checkInitialized() {
3748
- if (!this.initialized && !this.getClient()._isUsingServerAuth()) {
3812
+ if (!this.initialized && !this.staticState && !this.getClient()._isUsingServerAuth()) {
3749
3813
  throw Error("Channel ".concat(this.cid, " hasn't been initialized yet. Make sure to call .watch() and wait for it to resolve"));
3750
3814
  }
3751
3815
  } // eslint-disable-next-line sonarjs/cognitive-complexity
@@ -3787,7 +3851,8 @@ var Channel = /*#__PURE__*/function () {
3787
3851
  this.state.initMessages();
3788
3852
  }
3789
3853
 
3790
- this.state.addMessagesSorted(messages, false, true, true, messageSetToAddToIfDoesNotExist);
3854
+ var _this$state$addMessag = this.state.addMessagesSorted(messages, false, true, true, messageSetToAddToIfDoesNotExist),
3855
+ messageSet = _this$state$addMessag.messageSet;
3791
3856
 
3792
3857
  if (!this.state.pinnedMessages) {
3793
3858
  this.state.pinnedMessages = [];
@@ -3876,6 +3941,23 @@ var Channel = /*#__PURE__*/function () {
3876
3941
  _iterator4.f();
3877
3942
  }
3878
3943
  }
3944
+
3945
+ return {
3946
+ messageSet: messageSet
3947
+ };
3948
+ }
3949
+ }, {
3950
+ key: "_extendEventWithOwnReactions",
3951
+ value: function _extendEventWithOwnReactions(event) {
3952
+ if (!event.message) {
3953
+ return;
3954
+ }
3955
+
3956
+ var message = this.state.findMessage(event.message.id, event.message.parent_id);
3957
+
3958
+ if (message) {
3959
+ event.message.own_reactions = message.own_reactions;
3960
+ }
3879
3961
  }
3880
3962
  }, {
3881
3963
  key: "_disconnect",
@@ -4745,7 +4827,7 @@ var StableWSConnection = /*#__PURE__*/function () {
4745
4827
  while (1) {
4746
4828
  switch (_context5.prev = _context5.next) {
4747
4829
  case 0:
4748
- if (!(this.isConnecting || this.isDisconnected)) {
4830
+ if (!(this.isConnecting || this.isDisconnected && this.client.options.enableWSFallback)) {
4749
4831
  _context5.next = 2;
4750
4832
  break;
4751
4833
  }
@@ -4890,7 +4972,7 @@ var StableWSConnection = /*#__PURE__*/function () {
4890
4972
  return _context6.abrupt("return");
4891
4973
 
4892
4974
  case 12:
4893
- if (!this.isDisconnected) {
4975
+ if (!(this.isDisconnected && this.client.options.enableWSFallback)) {
4894
4976
  _context6.next = 15;
4895
4977
  break;
4896
4978
  }
@@ -5906,7 +5988,8 @@ function isString(x) {
5906
5988
 
5907
5989
  var StreamChat = /*#__PURE__*/function () {
5908
5990
  function StreamChat(_key, secretOrOptions, _options) {
5909
- var _this = this;
5991
+ var _this = this,
5992
+ _this$options;
5910
5993
 
5911
5994
  _classCallCheck(this, StreamChat);
5912
5995
 
@@ -5916,6 +5999,8 @@ var StreamChat = /*#__PURE__*/function () {
5916
5999
 
5917
6000
  _defineProperty(this, "anonymous", void 0);
5918
6001
 
6002
+ _defineProperty(this, "persistUserOnConnectionFailure", void 0);
6003
+
5919
6004
  _defineProperty(this, "axiosInstance", void 0);
5920
6005
 
5921
6006
  _defineProperty(this, "baseURL", void 0);
@@ -6043,8 +6128,12 @@ var StreamChat = /*#__PURE__*/function () {
6043
6128
  _context.prev = 20;
6044
6129
  _context.t0 = _context["catch"](14);
6045
6130
 
6046
- // cleanup client to allow the user to retry connectUser again
6047
- _this.disconnectUser();
6131
+ if (_this.persistUserOnConnectionFailure) {
6132
+ // cleanup client to allow the user to retry connectUser again
6133
+ _this.closeConnection();
6134
+ } else {
6135
+ _this.disconnectUser();
6136
+ }
6048
6137
 
6049
6138
  throw _context.t0;
6050
6139
 
@@ -6721,7 +6810,8 @@ var StreamChat = /*#__PURE__*/function () {
6721
6810
  this.activeChannels = {}; // mapping between channel groups and configs
6722
6811
 
6723
6812
  this.configs = {};
6724
- this.anonymous = false; // If its a server-side client, then lets initialize the tokenManager, since token will be
6813
+ this.anonymous = false;
6814
+ this.persistUserOnConnectionFailure = (_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.persistUserOnConnectionFailure; // If its a server-side client, then lets initialize the tokenManager, since token will be
6725
6815
  // generated from secret.
6726
6816
 
6727
6817
  this.tokenManager = new TokenManager(this.secret);
@@ -6830,7 +6920,8 @@ var StreamChat = /*#__PURE__*/function () {
6830
6920
  * This one is used by the frontend. This is a copy of the current user object stored on backend.
6831
6921
  * It contains reserved properties and own user properties which are not present in `this._user`.
6832
6922
  */
6833
- this.user = user; // this one is actually used for requests. This is a copy of current user provided to `connectUser` function.
6923
+ this.user = user;
6924
+ this.userID = user.id; // this one is actually used for requests. This is a copy of current user provided to `connectUser` function.
6834
6925
 
6835
6926
  this._user = _objectSpread({}, user);
6836
6927
  }
@@ -7862,20 +7953,10 @@ var StreamChat = /*#__PURE__*/function () {
7862
7953
  var sort,
7863
7954
  options,
7864
7955
  stateOptions,
7865
- skipInitialization,
7866
7956
  defaultOptions,
7867
7957
  payload,
7868
7958
  data,
7869
- channels,
7870
- _iterator2,
7871
- _step2,
7872
- channelState,
7873
- _iterator3,
7874
- _step3,
7875
- _channelState,
7876
- c,
7877
7959
  _args19 = arguments;
7878
-
7879
7960
  return _regeneratorRuntime.wrap(function _callee19$(_context19) {
7880
7961
  while (1) {
7881
7962
  switch (_context19.prev = _context19.next) {
@@ -7883,17 +7964,16 @@ var StreamChat = /*#__PURE__*/function () {
7883
7964
  sort = _args19.length > 1 && _args19[1] !== undefined ? _args19[1] : [];
7884
7965
  options = _args19.length > 2 && _args19[2] !== undefined ? _args19[2] : {};
7885
7966
  stateOptions = _args19.length > 3 && _args19[3] !== undefined ? _args19[3] : {};
7886
- skipInitialization = stateOptions.skipInitialization;
7887
7967
  defaultOptions = {
7888
7968
  state: true,
7889
7969
  watch: true,
7890
7970
  presence: false
7891
7971
  }; // Make sure we wait for the connect promise if there is a pending one
7892
7972
 
7893
- _context19.next = 7;
7973
+ _context19.next = 6;
7894
7974
  return this.setUserPromise;
7895
7975
 
7896
- case 7:
7976
+ case 6:
7897
7977
  if (!this._hasConnectionID()) {
7898
7978
  defaultOptions.watch = false;
7899
7979
  } // Return a list of channels
@@ -7903,55 +7983,21 @@ var StreamChat = /*#__PURE__*/function () {
7903
7983
  filter_conditions: filterConditions,
7904
7984
  sort: normalizeQuerySort(sort)
7905
7985
  }, defaultOptions), options);
7906
- _context19.next = 11;
7986
+ _context19.next = 10;
7907
7987
  return this.post(this.baseURL + '/channels', payload);
7908
7988
 
7909
- case 11:
7989
+ case 10:
7910
7990
  data = _context19.sent;
7911
- channels = []; // update our cache of the configs
7912
-
7913
- _iterator2 = _createForOfIteratorHelper(data.channels);
7914
-
7915
- try {
7916
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
7917
- channelState = _step2.value;
7918
-
7919
- this._addChannelConfig(channelState);
7920
- }
7921
- } catch (err) {
7922
- _iterator2.e(err);
7923
- } finally {
7924
- _iterator2.f();
7925
- }
7926
-
7927
- _iterator3 = _createForOfIteratorHelper(data.channels);
7928
-
7929
- try {
7930
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
7931
- _channelState = _step3.value;
7932
- c = this.channel(_channelState.channel.type, _channelState.channel.id);
7933
- c.data = _channelState.channel;
7934
- c.initialized = true;
7935
-
7936
- if (skipInitialization === undefined) {
7937
- c._initializeState(_channelState, 'latest');
7938
- } else if (!skipInitialization.includes(_channelState.channel.id)) {
7939
- c.state.clearMessages();
7940
-
7941
- c._initializeState(_channelState, 'latest');
7942
- }
7943
-
7944
- channels.push(c);
7991
+ this.dispatchEvent({
7992
+ type: 'channels.queried',
7993
+ queriedChannels: {
7994
+ channels: data.channels,
7995
+ isLatestMessageSet: true
7945
7996
  }
7946
- } catch (err) {
7947
- _iterator3.e(err);
7948
- } finally {
7949
- _iterator3.f();
7950
- }
7951
-
7952
- return _context19.abrupt("return", channels);
7997
+ });
7998
+ return _context19.abrupt("return", this.hydrateActiveChannels(data.channels, stateOptions));
7953
7999
 
7954
- case 18:
8000
+ case 13:
7955
8001
  case "end":
7956
8002
  return _context19.stop();
7957
8003
  }
@@ -7965,6 +8011,77 @@ var StreamChat = /*#__PURE__*/function () {
7965
8011
 
7966
8012
  return queryChannels;
7967
8013
  }()
8014
+ }, {
8015
+ key: "hydrateActiveChannels",
8016
+ value: function hydrateActiveChannels() {
8017
+ var channelsFromApi = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
8018
+ var stateOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
8019
+ var skipInitialization = stateOptions.skipInitialization,
8020
+ _stateOptions$staticS = stateOptions.staticState,
8021
+ staticState = _stateOptions$staticS === void 0 ? false : _stateOptions$staticS;
8022
+
8023
+ var _iterator2 = _createForOfIteratorHelper(channelsFromApi),
8024
+ _step2;
8025
+
8026
+ try {
8027
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
8028
+ var channelState = _step2.value;
8029
+
8030
+ this._addChannelConfig(channelState);
8031
+ }
8032
+ } catch (err) {
8033
+ _iterator2.e(err);
8034
+ } finally {
8035
+ _iterator2.f();
8036
+ }
8037
+
8038
+ var channels = [];
8039
+
8040
+ var _iterator3 = _createForOfIteratorHelper(channelsFromApi),
8041
+ _step3;
8042
+
8043
+ try {
8044
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
8045
+ var _channelState = _step3.value;
8046
+ var c = this.channel(_channelState.channel.type, _channelState.channel.id);
8047
+ c.data = _channelState.channel;
8048
+
8049
+ if (staticState) {
8050
+ c.staticState = true;
8051
+ } else {
8052
+ c.initialized = true;
8053
+ }
8054
+
8055
+ if (skipInitialization === undefined) {
8056
+ c._initializeState(_channelState, 'latest');
8057
+ } else if (!skipInitialization.includes(_channelState.channel.id)) {
8058
+ c.state.clearMessages();
8059
+
8060
+ c._initializeState(_channelState, 'latest');
8061
+ }
8062
+
8063
+ channels.push(c);
8064
+ }
8065
+ } catch (err) {
8066
+ _iterator3.e(err);
8067
+ } finally {
8068
+ _iterator3.f();
8069
+ }
8070
+
8071
+ if (!staticState) {
8072
+ // If the channels are coming from server, then clear out the
8073
+ // previously help offline channels.
8074
+ for (var _key5 in this.activeChannels) {
8075
+ var _channel7 = this.activeChannels[_key5];
8076
+
8077
+ if (_channel7.staticState) {
8078
+ delete this.activeChannels[_key5];
8079
+ }
8080
+ }
8081
+ }
8082
+
8083
+ return channels;
8084
+ }
7968
8085
  /**
7969
8086
  * search - Query messages
7970
8087
  *
@@ -9715,7 +9832,7 @@ var StreamChat = /*#__PURE__*/function () {
9715
9832
  }, {
9716
9833
  key: "getUserAgent",
9717
9834
  value: function getUserAgent() {
9718
- return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "6.7.1");
9835
+ return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "7.0.0-offline-support.0");
9719
9836
  }
9720
9837
  }, {
9721
9838
  key: "setUserAgent",
@@ -9779,9 +9896,9 @@ var StreamChat = /*#__PURE__*/function () {
9779
9896
  this.cleaningIntervalRef = setInterval(function () {
9780
9897
  // call clean on the channel, used for calling the stop.typing event etc.
9781
9898
  for (var _i3 = 0, _Object$values2 = Object.values(that.activeChannels); _i3 < _Object$values2.length; _i3++) {
9782
- var _channel7 = _Object$values2[_i3];
9899
+ var _channel8 = _Object$values2[_i3];
9783
9900
 
9784
- _channel7.clean();
9901
+ _channel8.clean();
9785
9902
  }
9786
9903
  }, 500);
9787
9904
  }