stream-chat 10.0.0-rc.31 → 10.0.0-rc.32

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.
@@ -2841,6 +2841,8 @@ var initState2 = ({
2841
2841
  };
2842
2842
  })
2843
2843
  });
2844
+ var isUploadInputAttachment = (input) => "localMetadata" in input;
2845
+ var toUploadError = (rejection) => rejection instanceof Error || isUploadCancellation(rejection) ? rejection : new Error("unknown error", { cause: rejection });
2844
2846
  var _AttachmentManager = class _AttachmentManager {
2845
2847
  constructor({ composer, message }) {
2846
2848
  this.setCustomUploadFn = (doUploadRequest) => {
@@ -3029,7 +3031,11 @@ var _AttachmentManager = class _AttachmentManager {
3029
3031
  localAttachment.localMetadata.uploadState = uploadPermissionCheck.uploadBlocked ? "blocked" : "pending";
3030
3032
  return localAttachment;
3031
3033
  };
3032
- this.ensureLocalUploadAttachment = async (attachment) => {
3034
+ /**
3035
+ * Whether `attachment` carries what an upload needs: a file and a local id. Reports the missing one
3036
+ * as an error notification.
3037
+ */
3038
+ this.isUploadableOrNotify = (attachment) => {
3033
3039
  if (!attachment.localMetadata?.file) {
3034
3040
  this.client.notifications.addError({
3035
3041
  message: "File is required for upload attachment",
@@ -3039,7 +3045,7 @@ var _AttachmentManager = class _AttachmentManager {
3039
3045
  },
3040
3046
  options: { type: CORE_NOTIFICATION_TYPE.attachmentFileMissing }
3041
3047
  });
3042
- return;
3048
+ return false;
3043
3049
  }
3044
3050
  if (!attachment.localMetadata.id) {
3045
3051
  this.client.notifications.addError({
@@ -3050,16 +3056,9 @@ var _AttachmentManager = class _AttachmentManager {
3050
3056
  },
3051
3057
  options: { type: CORE_NOTIFICATION_TYPE.attachmentIdMissing }
3052
3058
  });
3053
- return;
3054
- }
3055
- if (!this.fileUploadFilter(attachment)) return;
3056
- const newAttachment = await this.fileToLocalUploadAttachment(
3057
- attachment.localMetadata.file
3058
- );
3059
- if (attachment.localMetadata.id) {
3060
- newAttachment.localMetadata.id = attachment.localMetadata.id;
3059
+ return false;
3061
3060
  }
3062
- return newAttachment;
3061
+ return true;
3063
3062
  };
3064
3063
  /** The default upload, skipping the custom `doUploadRequest` so a custom one can delegate here. */
3065
3064
  this.doDefaultUploadRequest = async (fileLike, options) => {
@@ -3100,99 +3099,27 @@ var _AttachmentManager = class _AttachmentManager {
3100
3099
  }
3101
3100
  return this.doDefaultUploadRequest(fileLike, options);
3102
3101
  };
3103
- // @deprecated use attachmentManager.uploadFile(file)
3104
- this.uploadAttachment = async (attachment) => {
3105
- if (!this.isUploadEnabled) return;
3106
- const localAttachment = await this.ensureLocalUploadAttachment(attachment);
3107
- if (typeof localAttachment === "undefined") return;
3108
- if (localAttachment.localMetadata.uploadState === "blocked") {
3109
- this.upsertAttachments([localAttachment]);
3110
- this.client.notifications.addError({
3111
- message: `The attachment upload was blocked`,
3112
- origin: {
3113
- emitter: "AttachmentManager",
3114
- context: {
3115
- attachment,
3116
- blockedAttachment: localAttachment,
3117
- composer: this.composer
3118
- }
3119
- },
3120
- options: {
3121
- type: CORE_NOTIFICATION_TYPE.attachmentUploadBlocked,
3122
- metadata: {
3123
- reason: localAttachment.localMetadata.uploadPermissionCheck?.reason
3124
- }
3125
- }
3126
- });
3127
- return localAttachment;
3128
- }
3129
- let response;
3130
- try {
3131
- response = await this.upload(attachment);
3132
- } catch (error) {
3133
- const reason = error instanceof Error ? error.message : "unknown error";
3134
- const failedAttachment = {
3135
- ...attachment,
3136
- localMetadata: {
3137
- ...attachment.localMetadata,
3138
- uploadState: "failed",
3139
- uploadProgress: void 0,
3140
- // `false`, not `undefined`: `mergeWith` skips `undefined`, which would leave a stale `true`.
3141
- uploadConfirmationPending: false
3142
- }
3143
- };
3144
- if (!isUploadCancellation(error)) {
3145
- this.client.notifications.addError({
3146
- message: "Error uploading attachment",
3147
- origin: {
3148
- emitter: "AttachmentManager",
3149
- context: { attachment, failedAttachment, composer: this.composer }
3150
- },
3151
- options: {
3152
- type: CORE_NOTIFICATION_TYPE.attachmentUploadFailed,
3153
- metadata: { reason },
3154
- originalError: error instanceof Error ? error : void 0
3155
- }
3156
- });
3157
- }
3158
- this.updateAttachment(failedAttachment);
3159
- return failedAttachment;
3160
- }
3161
- if (!response) {
3162
- this.removeAttachments([attachment.localMetadata.id]);
3163
- return;
3164
- }
3165
- const uploadedAttachment = {
3166
- ...attachment,
3167
- localMetadata: {
3168
- ...attachment.localMetadata,
3169
- uploadState: "finished",
3170
- uploadProgress: void 0,
3171
- uploadConfirmationPending: false
3172
- }
3173
- };
3174
- const previewUri = uploadedAttachment.localMetadata.previewUri;
3175
- if (previewUri) {
3176
- if (previewUri.startsWith("blob:")) URL.revokeObjectURL(previewUri);
3177
- delete uploadedAttachment.localMetadata.previewUri;
3178
- }
3179
- if (isLocalImageAttachment(uploadedAttachment)) {
3180
- uploadedAttachment.image_url = response.file;
3102
+ /**
3103
+ * Uploads a file, or an existing local attachment, through the pre- and post-upload middleware.
3104
+ *
3105
+ * A file becomes a new attachment. An attachment keeps its `localMetadata.id`, its fields and its
3106
+ * position in `attachments`: a failed upload is retried in place, and an attachment the caller built
3107
+ * is uploaded with its own fields. An attachment without a file or a local id is returned unchanged,
3108
+ * with an error notification.
3109
+ *
3110
+ * Unlike {@link uploadFiles}, it does not check {@link isUploadEnabled}.
3111
+ */
3112
+ this.uploadFile = async (fileOrAttachment) => {
3113
+ let initialAttachment;
3114
+ if (isUploadInputAttachment(fileOrAttachment)) {
3115
+ if (!this.isUploadableOrNotify(fileOrAttachment)) return fileOrAttachment;
3116
+ initialAttachment = fileOrAttachment;
3181
3117
  } else {
3182
- uploadedAttachment.asset_url = response.file;
3118
+ initialAttachment = _AttachmentManager.toLocalUploadAttachment(fileOrAttachment);
3183
3119
  }
3184
- if (response.thumb_url) {
3185
- uploadedAttachment.thumb_url = response.thumb_url;
3186
- }
3187
- this.updateAttachment(uploadedAttachment);
3188
- return uploadedAttachment;
3189
- };
3190
- this.uploadFile = async (file) => {
3191
3120
  const preUpload = await this.preUploadMiddlewareExecutor.execute({
3192
3121
  eventName: "prepare",
3193
- initialValue: {
3194
- attachment: _AttachmentManager.toLocalUploadAttachment(file)
3195
- },
3122
+ initialValue: { attachment: initialAttachment },
3196
3123
  mode: "concurrent"
3197
3124
  });
3198
3125
  const attachment = preUpload.state.attachment;
@@ -3222,7 +3149,7 @@ var _AttachmentManager = class _AttachmentManager {
3222
3149
  try {
3223
3150
  response = await this.upload(attachment);
3224
3151
  } catch (err) {
3225
- error = err instanceof Error ? err : void 0;
3152
+ error = toUploadError(err);
3226
3153
  }
3227
3154
  const postUpload = await this.postUploadMiddlewareExecutor.execute({
3228
3155
  eventName: "postProcess",
@@ -7602,11 +7529,7 @@ var ChannelPaginator = class extends BasePaginator {
7602
7529
  }
7603
7530
  return this.client.hydrateChannels(
7604
7531
  cachedQuery.channels,
7605
- {
7606
- offlineMode: true,
7607
- skipInitialization: []
7608
- // an empty array clears existing channel messages (no duplicates)
7609
- },
7532
+ { offlineMode: true },
7610
7533
  request
7611
7534
  );
7612
7535
  }
@@ -16115,18 +16038,7 @@ var Channel = class extends ChannelApi {
16115
16038
  */
16116
16039
  async updatePartial(update, requestOptions) {
16117
16040
  const data = await this.updateChannelPartial(update, requestOptions);
16118
- const channel = data.channel;
16119
- const currentCapabilities = this.data?.own_capabilities ?? [];
16120
- const newCapabilities = channel?.own_capabilities;
16121
- const capabilitiesChanged = newCapabilities && [...currentCapabilities].sort().join() !== [...newCapabilities].sort().join();
16122
- this.data = channel;
16123
- if (capabilitiesChanged) {
16124
- this.getClient().dispatchEvent({
16125
- type: "capabilities.changed",
16126
- cid: this.cid,
16127
- own_capabilities: newCapabilities
16128
- });
16129
- }
16041
+ this.data = data.channel;
16130
16042
  return data;
16131
16043
  }
16132
16044
  /**
@@ -16623,13 +16535,18 @@ var Channel = class extends ChannelApi {
16623
16535
  * Reads `state.data`. Assigning publishes `data`, `memberCount` and `ownCapabilities` in one update.
16624
16536
  * If the object omits `member_count` or `own_capabilities`, the last known values are carried onto
16625
16537
  * a copy (the assigned object is not changed). Editing `channel.data.x` in place does not reach
16626
- * the state.
16538
+ * the state. `ownCapabilities` keeps its array while the capabilities stay the same (in any
16539
+ * order), so a subscriber to it is notified only when they change.
16627
16540
  */
16628
16541
  get data() {
16629
16542
  return this.state.getLatestValue().data;
16630
16543
  }
16631
16544
  set data(next) {
16632
- const previous = this.state.getLatestValue().data;
16545
+ const {
16546
+ data: previous,
16547
+ memberCount: previousMemberCount,
16548
+ ownCapabilities: previousOwnCapabilities
16549
+ } = this.state.getLatestValue();
16633
16550
  const carriedMemberCount = typeof next?.member_count !== "number" && typeof previous?.member_count === "number" ? previous.member_count : void 0;
16634
16551
  const carriedCapabilities = !Array.isArray(next?.own_capabilities) && Array.isArray(previous?.own_capabilities) ? [...previous.own_capabilities] : void 0;
16635
16552
  const data = next && (carriedMemberCount !== void 0 || carriedCapabilities) ? {
@@ -16638,11 +16555,13 @@ var Channel = class extends ChannelApi {
16638
16555
  ...carriedCapabilities && { own_capabilities: carriedCapabilities }
16639
16556
  } : next;
16640
16557
  const memberCount = data?.member_count ?? carriedMemberCount;
16641
- const ownCapabilities = data?.own_capabilities ?? carriedCapabilities;
16558
+ const ownCapabilities = data?.own_capabilities ?? carriedCapabilities ?? [];
16559
+ const toKey = (capabilities) => [...capabilities].sort().join();
16560
+ const haveSameOwnCapabilities = toKey(previousOwnCapabilities) === toKey(ownCapabilities);
16642
16561
  this.state.partialNext({
16643
16562
  data,
16644
- memberCount: memberCount ?? this.state.getLatestValue().memberCount,
16645
- ownCapabilities: ownCapabilities ? [...ownCapabilities] : []
16563
+ memberCount: memberCount ?? previousMemberCount,
16564
+ ownCapabilities: haveSameOwnCapabilities ? previousOwnCapabilities : [...ownCapabilities]
16646
16565
  });
16647
16566
  }
16648
16567
  /**
@@ -17126,18 +17045,8 @@ var Channel = class extends ChannelApi {
17126
17045
  this.getClient().polls.hydratePollCache(state.messages, true);
17127
17046
  this.getClient().reminders.hydrateState(state.messages);
17128
17047
  this.messageComposer.initStateFromChannelResponse(state);
17129
- const areCapabilitiesChanged = [...channel.own_capabilities || []].sort().join() !== [
17130
- ...this.data && Array.isArray(this.data?.own_capabilities) ? this.data.own_capabilities : []
17131
- ].sort().join();
17132
17048
  this.data = channel;
17133
17049
  this.state.partialNext({ initialized: true, offlineMode: false });
17134
- if (areCapabilitiesChanged) {
17135
- this.getClient().dispatchEvent({
17136
- type: "capabilities.changed",
17137
- cid: this.cid,
17138
- own_capabilities: channel.own_capabilities ?? []
17139
- });
17140
- }
17141
17050
  this.getClient().dispatchEvent({
17142
17051
  type: "channels.queried",
17143
17052
  queriedChannels: {
@@ -25256,7 +25165,7 @@ var StreamChat = class _StreamChat extends ChatApi {
25256
25165
  return this.userAgent;
25257
25166
  }
25258
25167
  if (!this.cachedUserAgent) {
25259
- const version = "10.0.0-rc.31";
25168
+ const version = "10.0.0-rc.32";
25260
25169
  const { name: sdkName, version: sdkVersion } = this.sdkIdentifier ?? {};
25261
25170
  const { name: appName, version: appVersion } = this.appIdentifier ?? {};
25262
25171
  const { os, model: deviceModel } = this.deviceIdentifier ?? {};
@@ -25833,7 +25742,7 @@ var StreamChat = class _StreamChat extends ChatApi {
25833
25742
  return await this.queryReactions(...args);
25834
25743
  }
25835
25744
  hydrateChannels(channelsFromApi = [], stateOptions = {}, queryChannelsOptions, headIntervalItemIdsAtRequestByCid) {
25836
- const { skipInitialization, offlineMode = false } = stateOptions;
25745
+ const { offlineMode = false } = stateOptions;
25837
25746
  const channels = [];
25838
25747
  for (const channelState of channelsFromApi) {
25839
25748
  if (!channelState.channel) continue;
@@ -25851,9 +25760,8 @@ var StreamChat = class _StreamChat extends ChatApi {
25851
25760
  c.watchStatus = ChannelWatchStatus.Watching;
25852
25761
  }
25853
25762
  c.push_preferences = channelState.push_preferences;
25854
- const willInitialize = skipInitialization === void 0 || !skipInitialization.includes(channelState.channel.id);
25855
25763
  const requestedPageSize = queryChannelsOptions?.message_limit ?? DEFAULT_QUERY_CHANNELS_MESSAGE_LIST_PAGE_SIZE;
25856
- const placesNewestPage = willInitialize && !c.active;
25764
+ const placesNewestPage = !c.active;
25857
25765
  let placedFirstPage = false;
25858
25766
  if (placesNewestPage) {
25859
25767
  c.messagePaginator.setQueriedLatestMessageAt(
@@ -25867,19 +25775,13 @@ var StreamChat = class _StreamChat extends ChatApi {
25867
25775
  }
25868
25776
  );
25869
25777
  }
25870
- if (skipInitialization === void 0) {
25871
- c._initializeState(channelState);
25872
- } else if (!skipInitialization.includes(channelState.channel.id)) {
25873
- c._initializeState(channelState);
25874
- }
25778
+ c._initializeState(channelState);
25875
25779
  if (placedFirstPage) c.messagePaginator.seedUnreadSnapshot();
25876
- if (willInitialize && !c.active && !wasWatching && !(placesNewestPage && channelState.messages.length)) {
25780
+ if (placesNewestPage && !wasWatching && !channelState.messages.length) {
25877
25781
  c.messagePaginator.unmarkStaleHead();
25878
25782
  }
25879
- if (willInitialize) {
25880
- this.polls.hydratePollCache(channelState.messages, true);
25881
- this.reminders.hydrateState(channelState.messages);
25882
- }
25783
+ this.polls.hydratePollCache(channelState.messages, true);
25784
+ this.reminders.hydrateState(channelState.messages);
25883
25785
  c.messageComposer.initStateFromChannelResponse(channelState);
25884
25786
  c.cooldownTimer.refresh();
25885
25787
  channels.push(c);