stream-chat 9.54.1 → 9.54.2

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.
@@ -761,7 +761,7 @@ export declare class Channel {
761
761
  */
762
762
  _channelURL: () => string;
763
763
  _checkInitialized(): void;
764
- _initializeState(state: ChannelAPIResponse, messageSetToAddToIfDoesNotExist?: MessageSetType): {
764
+ _initializeState(state: ChannelAPIResponse, messageSetToAddToIfDoesNotExist?: MessageSetType, isNewestPage?: boolean): {
765
765
  messageSet: import("./types").MessageSet;
766
766
  filteredMessageIds: string[];
767
767
  };
@@ -214,6 +214,12 @@ export declare class ChannelState {
214
214
  findMessage(messageId: string, parentMessageId?: string): LocalMessage | undefined;
215
215
  findMessageByTimestamp(timestampMs: number, parentMessageId?: string, exactTsMatch?: boolean): LocalMessage | null;
216
216
  private switchToMessageSet;
217
+ /**
218
+ * Empties the latest message set if the newest page is newer than it and shares no messages
219
+ * with it. Merging them would hide the messages in between from pagination.
220
+ * Skipped while the set holds local-only messages, so they are not lost.
221
+ */
222
+ _dropLatestMessagesDisjointFrom(newestPage: (MessageResponse | LocalMessage)[]): void;
217
223
  private areMessageSetsOverlap;
218
224
  private findMessageSetIndex;
219
225
  /**
@@ -268,6 +268,7 @@ type MessagePaginationUpdatedParams = {
268
268
  export declare function binarySearchByDateEqualOrNearestGreater(array: {
269
269
  created_at?: string;
270
270
  }[], targetDate: Date): number;
271
+ export declare const hasMessagePaginationCursor: (options?: MessagePaginationOptions) => boolean;
271
272
  export declare const messageSetPagination: (params: MessagePaginationUpdatedParams) => {
272
273
  hasNext: boolean;
273
274
  hasPrev: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stream-chat",
3
- "version": "9.54.1",
3
+ "version": "9.54.2",
4
4
  "description": "JS SDK for the Stream Chat API",
5
5
  "homepage": "https://getstream.io/chat/",
6
6
  "author": {
package/src/channel.ts CHANGED
@@ -6,6 +6,7 @@ import { MessageReceiptsTracker } from './messageDelivery';
6
6
  import {
7
7
  channelHasReadEvents,
8
8
  generateChannelTempCid,
9
+ hasMessagePaginationCursor,
9
10
  invokeEventListener,
10
11
  logChatPromiseExecution,
11
12
  messageSetPagination,
@@ -1639,6 +1640,7 @@ export class Channel {
1639
1640
  const { messageSet, filteredMessageIds } = this._initializeState(
1640
1641
  state,
1641
1642
  messageSetToAddToIfDoesNotExist,
1643
+ !hasMessagePaginationCursor(options?.messages),
1642
1644
  );
1643
1645
  messageSet.pagination = {
1644
1646
  ...messageSet.pagination,
@@ -2410,6 +2412,7 @@ export class Channel {
2410
2412
  _initializeState(
2411
2413
  state: ChannelAPIResponse,
2412
2414
  messageSetToAddToIfDoesNotExist: MessageSetType = 'latest',
2415
+ isNewestPage = true,
2413
2416
  ) {
2414
2417
  const { state: clientState, user, userID } = this.getClient();
2415
2418
 
@@ -2430,6 +2433,9 @@ export class Channel {
2430
2433
  if (!this.state.messages) {
2431
2434
  this.state.initMessages();
2432
2435
  }
2436
+ if (messageSetToAddToIfDoesNotExist === 'latest' && isNewestPage) {
2437
+ this.state._dropLatestMessagesDisjointFrom(messages);
2438
+ }
2433
2439
  const { messageSet, filteredMessageIds } = this.state.addMessagesSorted(
2434
2440
  messages,
2435
2441
  false,
@@ -64,6 +64,12 @@ const messageSetsOverlapByTimestamp = (a: LocalMessage[], b: LocalMessage[]) =>
64
64
  aOverlapsB(a, b) ||
65
65
  aOverlapsB(b, a);
66
66
 
67
+ const isLocalOnlyMessage = (message: LocalMessage) =>
68
+ message.status === 'sending' ||
69
+ message.status === 'failed' ||
70
+ message.type === 'error' ||
71
+ message.type === 'ephemeral';
72
+
67
73
  /**
68
74
  * ChannelState - A container class for the channel state.
69
75
  */
@@ -972,6 +978,29 @@ export class ChannelState {
972
978
  this.messageSets[index].isCurrent = true;
973
979
  }
974
980
 
981
+ /**
982
+ * Empties the latest message set if the newest page is newer than it and shares no messages
983
+ * with it. Merging them would hide the messages in between from pagination.
984
+ * Skipped while the set holds local-only messages, so they are not lost.
985
+ */
986
+ _dropLatestMessagesDisjointFrom(newestPage: (MessageResponse | LocalMessage)[]) {
987
+ const latestSet = this.messageSets.find((s) => s.isLatest);
988
+ const newestLoaded = latestSet?.messages.slice(-1)[0];
989
+ const oldestReturned = newestPage.find((m) => !m.parent_id || m.show_in_channel);
990
+ if (!latestSet || !newestLoaded || !oldestReturned?.created_at) return;
991
+ if (
992
+ new Date(oldestReturned.created_at).getTime() <=
993
+ newestLoaded.created_at.getTime() ||
994
+ this.areMessageSetsOverlap(latestSet.messages, newestPage) ||
995
+ latestSet.messages.some(isLocalOnlyMessage)
996
+ ) {
997
+ return;
998
+ }
999
+
1000
+ latestSet.messages = [];
1001
+ latestSet.pagination = { ...DEFAULT_MESSAGE_SET_PAGINATION };
1002
+ }
1003
+
975
1004
  private areMessageSetsOverlap(
976
1005
  messages1: Array<{ id: string }>,
977
1006
  messages2: Array<{ id: string }>,
package/src/client.ts CHANGED
@@ -2384,7 +2384,7 @@ export class StreamChat {
2384
2384
  ...messageSetPagination({
2385
2385
  parentSet: updatedMessagesSet,
2386
2386
  requestedPageSize:
2387
- queryChannelsOptions?.message_limit ||
2387
+ queryChannelsOptions?.message_limit ??
2388
2388
  DEFAULT_QUERY_CHANNELS_MESSAGE_LIST_PAGE_SIZE,
2389
2389
  returnedPage: channelState.messages,
2390
2390
  filteredReturnedPage: channelState.messages.filter(
@@ -1,4 +1,8 @@
1
- import { isFinishedUpload, isPendingUpload } from '../../attachmentIdentity';
1
+ import {
2
+ isFinishedUpload,
3
+ isLocalUploadAttachment,
4
+ isPendingUpload,
5
+ } from '../../attachmentIdentity';
2
6
  import type { MiddlewareHandlerParams } from '../../../middleware';
3
7
  import type { Attachment } from '../../../types';
4
8
  import type { MessageComposer } from '../../messageComposer';
@@ -16,6 +20,14 @@ const localAttachmentToAttachment = (localAttachment: LocalAttachment) => {
16
20
  return attachment as Attachment;
17
21
  };
18
22
 
23
+ /**
24
+ * An attachment that is not an upload (e.g. a custom one of an edited message or a restored
25
+ * draft) has no upload state and is sendable as it is. Leaving it out would drop it from the
26
+ * message: on an edit, the API removes every attachment missing from the payload.
27
+ */
28
+ const isSendable = (attachment: LocalAttachment) =>
29
+ isFinishedUpload(attachment) || !isLocalUploadAttachment(attachment);
30
+
19
31
  /**
20
32
  * The composition step used by {@link createSendWithPendingUploadsAttachmentsMiddleware}.
21
33
  *
@@ -46,8 +58,7 @@ const composeWithPendingUploads = ({
46
58
  // settles.
47
59
  const relevantAttachments = composer.attachmentManager.attachments.filter(
48
60
  (attachment) =>
49
- isFinishedUpload(attachment) ||
50
- (!composerIsKeptAsDraft && isPendingUpload(attachment)),
61
+ isSendable(attachment) || (!composerIsKeptAsDraft && isPendingUpload(attachment)),
51
62
  );
52
63
 
53
64
  const localAttachments = (state.localMessage.attachments ?? []).concat(
@@ -56,7 +67,7 @@ const composeWithPendingUploads = ({
56
67
  ),
57
68
  );
58
69
  const messageAttachments = (state.message.attachments ?? []).concat(
59
- relevantAttachments.filter(isFinishedUpload).map(localAttachmentToAttachment),
70
+ relevantAttachments.filter(isSendable).map(localAttachmentToAttachment),
60
71
  );
61
72
 
62
73
  if (!localAttachments.length && !messageAttachments.length) return state;
@@ -153,7 +164,7 @@ export const createAttachmentsCompositionMiddleware = (
153
164
  }
154
165
 
155
166
  const attachments = (state.message.attachments ?? []).concat(
156
- attachmentManager.successfulUploads.map(localAttachmentToAttachment),
167
+ attachmentManager.attachments.filter(isSendable).map(localAttachmentToAttachment),
157
168
  );
158
169
 
159
170
  // prevent introducing attachments array into the payload sent to the server
@@ -187,10 +198,10 @@ export const createDraftAttachmentsCompositionMiddleware = (
187
198
  const { attachmentManager } = composer;
188
199
  if (!attachmentManager) return forward();
189
200
 
190
- const successfulUploads = attachmentManager.successfulUploads;
191
- const attachments = successfulUploads.length
201
+ const sendableAttachments = attachmentManager.attachments.filter(isSendable);
202
+ const attachments = sendableAttachments.length
192
203
  ? (state.draft.attachments ?? []).concat(
193
- successfulUploads.map(localAttachmentToAttachment),
204
+ sendableAttachments.map(localAttachmentToAttachment),
194
205
  )
195
206
  : undefined;
196
207
 
@@ -278,9 +278,14 @@ function applyOwnVoteDelta(
278
278
  }
279
279
  voteCount = Math.max(voteCount - 1, 0);
280
280
  if (latestVotesByOption?.[optionId]) {
281
- latestVotesByOption[optionId] = latestVotesByOption[optionId].filter(
281
+ const latestVotes = latestVotesByOption[optionId].filter(
282
282
  (latestVote) => latestVote.id !== vote.id,
283
283
  );
284
+ if (latestVotes.length) {
285
+ latestVotesByOption[optionId] = latestVotes;
286
+ } else {
287
+ delete latestVotesByOption[optionId];
288
+ }
284
289
  }
285
290
  }
286
291
 
package/src/utils.ts CHANGED
@@ -1164,7 +1164,27 @@ const messagePaginationLinear = ({
1164
1164
  return newPagination;
1165
1165
  };
1166
1166
 
1167
+ export const hasMessagePaginationCursor = (options?: MessagePaginationOptions) =>
1168
+ !!(
1169
+ options?.id_lt ||
1170
+ options?.id_lte ||
1171
+ options?.id_gt ||
1172
+ options?.id_gte ||
1173
+ options?.id_around ||
1174
+ options?.created_at_before ||
1175
+ options?.created_at_before_or_equal ||
1176
+ options?.created_at_after ||
1177
+ options?.created_at_after_or_equal ||
1178
+ options?.created_at_around ||
1179
+ options?.offset
1180
+ );
1181
+
1167
1182
  export const messageSetPagination = (params: MessagePaginationUpdatedParams) => {
1183
+ // an empty page asked for says nothing about what else exists
1184
+ if (params.requestedPageSize === 0 && params.parentSet.messages.length > 0) {
1185
+ return params.parentSet.pagination;
1186
+ }
1187
+
1168
1188
  if (
1169
1189
  params.parentSet.messages.length +
1170
1190
  (params.returnedPage.length - params.filteredReturnedPage.length) <