stream-chat 9.0.1 → 9.1.1

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/esm/index.js CHANGED
@@ -6073,6 +6073,10 @@ var pollStateChangeValidators = {
6073
6073
  max_votes_allowed: ({ data, value }) => {
6074
6074
  if (data.enforce_unique_vote && value)
6075
6075
  return { max_votes_allowed: "Enforce unique vote is enabled" };
6076
+ const numericMatch = value.match(/^[0-9]+$/);
6077
+ if (!numericMatch && value) {
6078
+ return { max_votes_allowed: "Only numbers are allowed" };
6079
+ }
6076
6080
  if (value?.length > 1 && !value.match(VALID_MAX_VOTES_VALUE_REGEX))
6077
6081
  return { max_votes_allowed: "Type a number from 2 to 10" };
6078
6082
  return { max_votes_allowed: void 0 };
@@ -6187,7 +6191,7 @@ var createPollComposerStateMiddleware = ({
6187
6191
  forward
6188
6192
  }) => {
6189
6193
  if (!state.targetFields) return forward();
6190
- const { previousState } = state;
6194
+ const { previousState, injectedFieldErrors } = state;
6191
6195
  const finalValidators = {
6192
6196
  ...pollStateChangeValidators,
6193
6197
  ...defaultPollFieldChangeEventValidators,
@@ -6207,7 +6211,7 @@ var createPollComposerStateMiddleware = ({
6207
6211
  nextState: {
6208
6212
  ...previousState,
6209
6213
  data: { ...previousState.data, ...newData },
6210
- errors: { ...previousState.errors, ...newErrors }
6214
+ errors: { ...previousState.errors, ...newErrors, ...injectedFieldErrors }
6211
6215
  }
6212
6216
  });
6213
6217
  },
@@ -6233,7 +6237,11 @@ var createPollComposerStateMiddleware = ({
6233
6237
  nextState: {
6234
6238
  ...previousState,
6235
6239
  data: { ...previousState.data, ...newData },
6236
- errors: { ...previousState.errors, ...newErrors }
6240
+ errors: {
6241
+ ...previousState.errors,
6242
+ ...newErrors,
6243
+ ...state.injectedFieldErrors
6244
+ }
6237
6245
  }
6238
6246
  });
6239
6247
  }
@@ -6294,13 +6302,20 @@ var PollComposer = class {
6294
6302
  this.initState = () => {
6295
6303
  this.state.next(this.initialState);
6296
6304
  };
6297
- this.updateFields = async (data) => {
6305
+ /**
6306
+ * Updates specified fields and generates relevant errors
6307
+ * @param data
6308
+ * @param injectedFieldErrors - errors produced externally that will take precedence over the errors generated in the middleware chaing
6309
+ */
6310
+ // FIXME: change method params to a single object with the next major release
6311
+ this.updateFields = async (data, injectedFieldErrors) => {
6298
6312
  const { state, status } = await this.stateMiddlewareExecutor.execute({
6299
6313
  eventName: "handleFieldChange",
6300
6314
  initialValue: {
6301
6315
  nextState: { ...this.state.getLatestValue() },
6302
6316
  previousState: { ...this.state.getLatestValue() },
6303
- targetFields: data
6317
+ targetFields: data,
6318
+ injectedFieldErrors
6304
6319
  }
6305
6320
  });
6306
6321
  if (status === "discard") return;
@@ -6772,11 +6787,48 @@ var createDraftMessageComposerStateCompositionMiddleware = (composer) => ({
6772
6787
  }
6773
6788
  });
6774
6789
 
6790
+ // src/messageComposer/middleware/messageComposer/pollOnly.ts
6791
+ var pollLocalMessageNullifiedFields = {
6792
+ attachments: [],
6793
+ mentioned_users: [],
6794
+ parent_id: void 0,
6795
+ quoted_message: void 0,
6796
+ text: ""
6797
+ };
6798
+ var createPollOnlyCompositionMiddleware = (composer) => ({
6799
+ id: "stream-io/message-composer-middleware/poll-only",
6800
+ handlers: {
6801
+ compose: ({
6802
+ state,
6803
+ complete,
6804
+ forward
6805
+ }) => {
6806
+ const pollId = composer.pollId;
6807
+ const isEditingMessage = !!composer.editedMessage;
6808
+ const isComposingThreadReply = !!composer.threadId;
6809
+ if (!pollId || isComposingThreadReply || isEditingMessage) return forward();
6810
+ return complete({
6811
+ ...state,
6812
+ localMessage: {
6813
+ ...state.localMessage,
6814
+ ...pollLocalMessageNullifiedFields,
6815
+ poll_id: pollId
6816
+ },
6817
+ message: {
6818
+ id: state.localMessage.id,
6819
+ poll_id: pollId
6820
+ }
6821
+ });
6822
+ }
6823
+ }
6824
+ });
6825
+
6775
6826
  // src/messageComposer/middleware/messageComposer/MessageComposerMiddlewareExecutor.ts
6776
6827
  var MessageComposerMiddlewareExecutor = class extends MiddlewareExecutor {
6777
6828
  constructor({ composer }) {
6778
6829
  super();
6779
6830
  this.use([
6831
+ createPollOnlyCompositionMiddleware(composer),
6780
6832
  createTextComposerCompositionMiddleware(composer),
6781
6833
  createAttachmentsCompositionMiddleware(composer),
6782
6834
  createLinkPreviewsCompositionMiddleware(composer),
@@ -7628,27 +7680,8 @@ var TextComposerMiddlewareExecutor = class extends MiddlewareExecutor {
7628
7680
  eventName,
7629
7681
  initialValue: initialState
7630
7682
  });
7631
- if (result && result.state.suggestions) {
7632
- try {
7633
- const searchResult = await withCancellation(
7634
- "textComposer-suggestions-search",
7635
- async () => {
7636
- await result.state.suggestions?.searchSource.search(
7637
- result.state.suggestions?.query
7638
- );
7639
- }
7640
- );
7641
- if (searchResult === "canceled") return { ...result, status: "discard" };
7642
- } catch (error) {
7643
- return {
7644
- ...result,
7645
- state: {
7646
- ...result.state,
7647
- suggestions: void 0
7648
- }
7649
- };
7650
- }
7651
- }
7683
+ const { query, searchSource } = result.state.suggestions ?? {};
7684
+ searchSource?.search(query)?.catch(console.error);
7652
7685
  return result;
7653
7686
  }
7654
7687
  };
@@ -8326,15 +8359,17 @@ var initState5 = (composition) => {
8326
8359
  const quotedMessage = composition.quoted_message;
8327
8360
  let message;
8328
8361
  let draftId = null;
8362
+ let id = MessageComposer.generateId();
8329
8363
  if (compositionIsDraftResponse(composition)) {
8330
8364
  message = composition.message;
8331
8365
  draftId = composition.message.id;
8332
8366
  } else {
8333
8367
  message = composition;
8368
+ id = composition.id;
8334
8369
  }
8335
8370
  return {
8336
8371
  draftId,
8337
- id: message.id,
8372
+ id,
8338
8373
  quotedMessage: quotedMessage ? formatMessage(quotedMessage) : null,
8339
8374
  pollId: message.poll_id ?? null
8340
8375
  };
@@ -8357,6 +8392,7 @@ var _MessageComposer = class _MessageComposer {
8357
8392
  this.attachmentManager.initState({ message });
8358
8393
  this.linkPreviewsManager.initState({ message });
8359
8394
  this.textComposer.initState({ message });
8395
+ this.pollComposer.initState();
8360
8396
  this.customDataManager.initState({ message });
8361
8397
  this.state.next(initState5(composition));
8362
8398
  if (composition && !compositionIsDraftResponse(composition) && message && isLocalMessage(message)) {
@@ -8517,11 +8553,6 @@ var _MessageComposer = class _MessageComposer {
8517
8553
  this.state.partialNext({ quotedMessage });
8518
8554
  };
8519
8555
  this.clear = () => {
8520
- this.attachmentManager.initState();
8521
- this.linkPreviewsManager.initState();
8522
- this.textComposer.initState();
8523
- this.pollComposer.initState();
8524
- this.customDataManager.initState();
8525
8556
  this.initState();
8526
8557
  };
8527
8558
  this.restore = () => {
@@ -8596,6 +8627,7 @@ var _MessageComposer = class _MessageComposer {
8596
8627
  try {
8597
8628
  const { poll } = await this.client.createPoll(composition.data);
8598
8629
  this.state.partialNext({ pollId: poll.id });
8630
+ this.pollComposer.initState();
8599
8631
  } catch (error) {
8600
8632
  this.client.notifications.add({
8601
8633
  message: "Failed to create the poll",
@@ -13829,18 +13861,16 @@ var StreamChat = class _StreamChat {
13829
13861
  );
13830
13862
  }
13831
13863
  /**
13832
- * queryChannels - Query channels
13864
+ * queryChannelsRequest - Queries channels and returns the raw response
13833
13865
  *
13834
13866
  * @param {ChannelFilters} filterConditions object MongoDB style filters
13835
13867
  * @param {ChannelSort} [sort] Sort options, for instance {created_at: -1}.
13836
13868
  * When using multiple fields, make sure you use array of objects to guarantee field order, for instance [{last_updated: -1}, {created_at: 1}]
13837
13869
  * @param {ChannelOptions} [options] Options object
13838
- * @param {ChannelStateOptions} [stateOptions] State options object. These options will only be used for state management and won't be sent in the request.
13839
- * - stateOptions.skipInitialization - Skips the initialization of the state for the channels matching the ids in the list.
13840
13870
  *
13841
- * @return {Promise<{ channels: Array<ChannelAPIResponse>}> } search channels response
13871
+ * @return {Promise<Array<ChannelAPIResponse>>} search channels response
13842
13872
  */
13843
- async queryChannels(filterConditions, sort = [], options = {}, stateOptions = {}) {
13873
+ async queryChannelsRequest(filterConditions, sort = [], options = {}) {
13844
13874
  const defaultOptions = {
13845
13875
  state: true,
13846
13876
  watch: true,
@@ -13860,14 +13890,31 @@ var StreamChat = class _StreamChat {
13860
13890
  this.baseURL + "/channels",
13861
13891
  payload
13862
13892
  );
13893
+ return data.channels;
13894
+ }
13895
+ /**
13896
+ * queryChannels - Query channels
13897
+ *
13898
+ * @param {ChannelFilters} filterConditions object MongoDB style filters
13899
+ * @param {ChannelSort} [sort] Sort options, for instance {created_at: -1}.
13900
+ * When using multiple fields, make sure you use array of objects to guarantee field order, for instance [{last_updated: -1}, {created_at: 1}]
13901
+ * @param {ChannelOptions} [options] Options object
13902
+ * @param {ChannelStateOptions} [stateOptions] State options object. These options will only be used for state management and won't be sent in the request.
13903
+ * - stateOptions.skipInitialization - Skips the initialization of the state for the channels matching the ids in the list.
13904
+ * - stateOptions.skipHydration - Skips returning the channels as instances of the Channel class and rather returns the raw query response.
13905
+ *
13906
+ * @return {Promise<Array<Channel>>} search channels response
13907
+ */
13908
+ async queryChannels(filterConditions, sort = [], options = {}, stateOptions = {}) {
13909
+ const channels = await this.queryChannelsRequest(filterConditions, sort, options);
13863
13910
  this.dispatchEvent({
13864
13911
  type: "channels.queried",
13865
13912
  queriedChannels: {
13866
- channels: data.channels,
13913
+ channels,
13867
13914
  isLatestMessageSet: true
13868
13915
  }
13869
13916
  });
13870
- return this.hydrateActiveChannels(data.channels, stateOptions, options);
13917
+ return this.hydrateActiveChannels(channels, stateOptions, options);
13871
13918
  }
13872
13919
  /**
13873
13920
  * queryReactions - Query reactions
@@ -14814,7 +14861,7 @@ var StreamChat = class _StreamChat {
14814
14861
  if (this.userAgent) {
14815
14862
  return this.userAgent;
14816
14863
  }
14817
- const version = "9.0.1";
14864
+ const version = "9.1.1";
14818
14865
  const clientBundle = "browser-esm";
14819
14866
  let userAgentString = "";
14820
14867
  if (this.sdkIdentifier) {