stream-chat 10.1.1 → 10.3.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/cjs/index.browser.js +4736 -2819
- package/dist/cjs/index.browser.js.map +4 -4
- package/dist/cjs/index.node.js +4757 -2819
- package/dist/cjs/index.node.js.map +4 -4
- package/dist/esm/index.mjs +4736 -2819
- package/dist/esm/index.mjs.map +4 -4
- package/dist/types/channel.d.ts +62 -3
- package/dist/types/client.d.ts +9 -1
- package/dist/types/configuration/types.d.ts +2 -0
- package/dist/types/connection/ConnectionRecoveryManager.d.ts +1 -0
- package/dist/types/gen/chat/ChatApi.d.ts +12 -1
- package/dist/types/gen/models/index.d.ts +659 -11
- package/dist/types/gen/moderation/ModerationApi.d.ts +3 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/messageComposer/attachmentManager.d.ts +2 -0
- package/dist/types/messageComposer/index.d.ts +1 -0
- package/dist/types/messageComposer/linkPreviewsManager.d.ts +1 -0
- package/dist/types/messageComposer/messageComposer.d.ts +33 -7
- package/dist/types/messageComposer/middleware/messageComposer/compositionValidation.d.ts +1 -2
- package/dist/types/messageComposer/middleware/messageComposer/index.d.ts +1 -0
- package/dist/types/messageComposer/middleware/messageComposer/scheduledMessage.d.ts +4 -0
- package/dist/types/messageComposer/middleware/textComposer/commandUtils.d.ts +1 -0
- package/dist/types/messageComposer/scheduledMessageComposition.d.ts +32 -0
- package/dist/types/messageMarks/MessageMarkManager.d.ts +44 -0
- package/dist/types/messageMarks/index.d.ts +1 -0
- package/dist/types/messageOperations/MessageOperations.d.ts +12 -1
- package/dist/types/messageOperations/index.d.ts +1 -0
- package/dist/types/messageOperations/reflectMessageMarkEvent.d.ts +9 -0
- package/dist/types/messageOperations/sendOrdering.d.ts +4 -3
- package/dist/types/messageOperations/types.d.ts +6 -2
- package/dist/types/notifications/types.d.ts +4 -0
- package/dist/types/offline-support/offline_support_api.d.ts +19 -1
- package/dist/types/offline-support/types.d.ts +19 -1
- package/dist/types/pagination/PaginatorView.d.ts +55 -0
- package/dist/types/pagination/index.d.ts +1 -0
- package/dist/types/pagination/paginators/BasePaginator.d.ts +49 -0
- package/dist/types/pagination/paginators/ChannelPaginator.d.ts +0 -9
- package/dist/types/pagination/paginators/MessageIntervalPaginator.d.ts +6 -0
- package/dist/types/pagination/paginators/MessageMarkPaginator.d.ts +61 -0
- package/dist/types/pagination/paginators/ScheduledMessagePaginator.d.ts +83 -0
- package/dist/types/pagination/paginators/index.d.ts +2 -0
- package/dist/types/scheduledMessages/ScheduledMessagePaginatorsManager.d.ts +47 -0
- package/dist/types/scheduledMessages/ScheduledMessageStatePolicy.d.ts +12 -0
- package/dist/types/scheduledMessages/ScheduledMessagesView.d.ts +38 -0
- package/dist/types/scheduledMessages/composition.d.ts +5 -0
- package/dist/types/scheduledMessages/constants.d.ts +56 -0
- package/dist/types/scheduledMessages/editResolvers.d.ts +4 -0
- package/dist/types/scheduledMessages/errors.d.ts +3 -0
- package/dist/types/scheduledMessages/eventHandlers.d.ts +8 -0
- package/dist/types/scheduledMessages/index.d.ts +7 -0
- package/dist/types/scheduledMessages/limits.d.ts +6 -0
- package/dist/types/scheduledMessages/types.d.ts +65 -0
- package/dist/types/thread.d.ts +12 -1
- package/dist/types/utils.d.ts +7 -0
- package/package.json +1 -1
- package/src/channel.ts +231 -7
- package/src/client.ts +46 -2
- package/src/configuration/keys.ts +2 -0
- package/src/configuration/shape.ts +15 -0
- package/src/configuration/types.ts +3 -1
- package/src/connection/ConnectionRecoveryManager.ts +7 -0
- package/src/errors.ts +11 -0
- package/src/gen/chat/ChatApi.ts +60 -3
- package/src/gen/models/index.ts +794 -23
- package/src/gen/moderation/ModerationApi.ts +24 -3
- package/src/index.ts +2 -0
- package/src/messageComposer/attachmentManager.ts +43 -24
- package/src/messageComposer/index.ts +1 -0
- package/src/messageComposer/messageComposer.ts +140 -32
- package/src/messageComposer/middleware/messageComposer/MessageComposerMiddlewareExecutor.ts +2 -0
- package/src/messageComposer/middleware/messageComposer/compositionValidation.ts +40 -47
- package/src/messageComposer/middleware/messageComposer/index.ts +1 -0
- package/src/messageComposer/middleware/messageComposer/scheduledMessage.ts +38 -0
- package/src/messageComposer/middleware/textComposer/commandUtils.ts +8 -0
- package/src/messageComposer/scheduledMessageComposition.ts +324 -0
- package/src/messageMarks/MessageMarkManager.ts +145 -0
- package/src/messageMarks/index.ts +1 -0
- package/src/messageOperations/MessageOperationStatePolicy.ts +7 -0
- package/src/messageOperations/MessageOperations.ts +154 -1
- package/src/messageOperations/index.ts +1 -0
- package/src/messageOperations/reflectMessageMarkEvent.ts +47 -0
- package/src/messageOperations/reflectReactionEvent.ts +3 -0
- package/src/messageOperations/sendOrdering.ts +11 -4
- package/src/messageOperations/types.ts +6 -2
- package/src/notifications/types.ts +4 -0
- package/src/offline-support/offline_support_api.ts +61 -1
- package/src/offline-support/queueableOperations.ts +6 -0
- package/src/offline-support/types.ts +28 -0
- package/src/pagination/PaginatorView.ts +103 -0
- package/src/pagination/index.ts +1 -0
- package/src/pagination/paginators/BasePaginator.ts +204 -29
- package/src/pagination/paginators/ChannelPaginator.ts +8 -28
- package/src/pagination/paginators/MessageIntervalPaginator.ts +14 -1
- package/src/pagination/paginators/MessageMarkPaginator.ts +222 -0
- package/src/pagination/paginators/ScheduledMessagePaginator.ts +352 -0
- package/src/pagination/paginators/index.ts +2 -0
- package/src/scheduledMessages/ScheduledMessagePaginatorsManager.ts +268 -0
- package/src/scheduledMessages/ScheduledMessageStatePolicy.ts +42 -0
- package/src/scheduledMessages/ScheduledMessagesView.ts +57 -0
- package/src/scheduledMessages/composition.ts +129 -0
- package/src/scheduledMessages/constants.ts +79 -0
- package/src/scheduledMessages/editResolvers.ts +51 -0
- package/src/scheduledMessages/errors.ts +45 -0
- package/src/scheduledMessages/eventHandlers.ts +58 -0
- package/src/scheduledMessages/index.ts +7 -0
- package/src/scheduledMessages/limits.ts +32 -0
- package/src/scheduledMessages/types.ts +124 -0
- package/src/thread.ts +41 -17
- package/src/utils.ts +18 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { BasePaginator, ZERO_PAGE_CURSOR } from './BasePaginator';
|
|
2
|
+
import type {
|
|
3
|
+
ExecuteQueryReturnValue,
|
|
4
|
+
PaginationQueryParams,
|
|
5
|
+
PaginationQueryReturnValue,
|
|
6
|
+
PaginatorOptions,
|
|
7
|
+
PaginatorState,
|
|
8
|
+
PlaceQueryPageParams,
|
|
9
|
+
} from './BasePaginator';
|
|
10
|
+
import type {
|
|
11
|
+
MessageMarkWithUserResponse,
|
|
12
|
+
QueryMessageMarksFilter,
|
|
13
|
+
QueryMessageMarksRequest,
|
|
14
|
+
} from '../../types';
|
|
15
|
+
import type { StreamChat } from '../../client';
|
|
16
|
+
import { ComparisonResult } from '../types.normalization';
|
|
17
|
+
import { generateUUIDv4 } from '../../utils';
|
|
18
|
+
import { chatLoggerSystem } from '../../logger';
|
|
19
|
+
|
|
20
|
+
// A mark has no ID of its own: a user marks a message once per type.
|
|
21
|
+
const getMarkId = (mark: MessageMarkWithUserResponse) =>
|
|
22
|
+
`${mark.message_id}:${mark.user_id}:${mark.type}`;
|
|
23
|
+
|
|
24
|
+
// Newest first, as the server returns them. Ties need equal nanosecond timestamps; the ID keeps
|
|
25
|
+
// the order total. The paginator checks for exactly -1 and 1, so no differences.
|
|
26
|
+
const compareMarks = (
|
|
27
|
+
a: MessageMarkWithUserResponse,
|
|
28
|
+
b: MessageMarkWithUserResponse,
|
|
29
|
+
): ComparisonResult => {
|
|
30
|
+
if (a.created_at !== b.created_at) {
|
|
31
|
+
return a.created_at > b.created_at
|
|
32
|
+
? ComparisonResult.A_PRECEDES_B
|
|
33
|
+
: ComparisonResult.A_COMES_AFTER_B;
|
|
34
|
+
}
|
|
35
|
+
const idA = getMarkId(a);
|
|
36
|
+
const idB = getMarkId(b);
|
|
37
|
+
if (idA === idB) return ComparisonResult.A_IS_EQUAL_TO_B;
|
|
38
|
+
return idA < idB ? ComparisonResult.A_PRECEDES_B : ComparisonResult.A_COMES_AFTER_B;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type MessageMarkPaginatorOptions = {
|
|
42
|
+
/** Identifies the list in `client.messageMarks`. Generated when left out. */
|
|
43
|
+
id?: string;
|
|
44
|
+
/** The message whose marks are listed. */
|
|
45
|
+
messageId: string;
|
|
46
|
+
/** Lists only the marks it matches, for example `{ type: 'played' }`. */
|
|
47
|
+
filter?: QueryMessageMarksFilter;
|
|
48
|
+
/** How the list is fetched, for example its `pageSize`. */
|
|
49
|
+
paginatorOptions?: PaginatorOptions<
|
|
50
|
+
MessageMarkWithUserResponse,
|
|
51
|
+
QueryMessageMarksRequest
|
|
52
|
+
>;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Lists the marks on one message, with the users who made them, newest first. The API pages
|
|
57
|
+
* forward only, so the headward cursor starts exhausted (`null`). New marks are added at the head
|
|
58
|
+
* through `ingestItem`, which `client.messageMarks` calls for `message.mark_added`.
|
|
59
|
+
*
|
|
60
|
+
* With offline support, the first page opens with the marks the offline database holds while the
|
|
61
|
+
* request runs, and keeps them when the request fails.
|
|
62
|
+
*/
|
|
63
|
+
export class MessageMarkPaginator extends BasePaginator<
|
|
64
|
+
MessageMarkWithUserResponse,
|
|
65
|
+
QueryMessageMarksRequest
|
|
66
|
+
> {
|
|
67
|
+
private client: StreamChat;
|
|
68
|
+
private readonly _id: string;
|
|
69
|
+
readonly messageId: string;
|
|
70
|
+
readonly filter: QueryMessageMarksFilter | undefined;
|
|
71
|
+
private activeRefCount = 0;
|
|
72
|
+
private _isStateStale = false;
|
|
73
|
+
|
|
74
|
+
constructor(
|
|
75
|
+
client: StreamChat,
|
|
76
|
+
{ id, messageId, filter, paginatorOptions }: MessageMarkPaginatorOptions,
|
|
77
|
+
) {
|
|
78
|
+
super({
|
|
79
|
+
initialCursor: { ...ZERO_PAGE_CURSOR, headward: null },
|
|
80
|
+
...paginatorOptions,
|
|
81
|
+
});
|
|
82
|
+
this.client = client;
|
|
83
|
+
this._id = id ?? `message-mark-paginator-${generateUUIDv4()}`;
|
|
84
|
+
this.messageId = messageId;
|
|
85
|
+
this.filter = filter;
|
|
86
|
+
this.sortComparator = compareMarks;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
get initialState(): PaginatorState<MessageMarkWithUserResponse> {
|
|
90
|
+
return {
|
|
91
|
+
...super.initialState,
|
|
92
|
+
hasMoreHead: false,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
get id() {
|
|
97
|
+
return this._id;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
getItemId(item: MessageMarkWithUserResponse): string {
|
|
101
|
+
return getMarkId(item);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** See {@link MessageMarkPaginator.activate}. */
|
|
105
|
+
get active() {
|
|
106
|
+
return this.activeRefCount > 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Whether marks may have been missed, for example while the connection was down. */
|
|
110
|
+
get isStateStale() {
|
|
111
|
+
return this._isStateStale;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Call this when a screen starts showing the list, and call the returned function when it stops.
|
|
116
|
+
* A stale list reloads as soon as it is shown. The list counts as shown until every caller has
|
|
117
|
+
* released it, and releasing twice does nothing.
|
|
118
|
+
*/
|
|
119
|
+
activate = (): (() => void) => {
|
|
120
|
+
this.activeRefCount += 1;
|
|
121
|
+
if (this.activeRefCount === 1) this.reloadIfStale();
|
|
122
|
+
|
|
123
|
+
let released = false;
|
|
124
|
+
return () => {
|
|
125
|
+
if (released) return;
|
|
126
|
+
released = true;
|
|
127
|
+
this.activeRefCount -= 1;
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Flags the loaded marks as possibly incomplete. An active list reloads now, an inactive one when
|
|
133
|
+
* it is next activated.
|
|
134
|
+
*/
|
|
135
|
+
markStale = () => {
|
|
136
|
+
this._isStateStale = true;
|
|
137
|
+
if (this.active) this.reloadIfStale();
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
private reloadIfStale() {
|
|
141
|
+
if (!this._isStateStale) return;
|
|
142
|
+
this._isStateStale = false;
|
|
143
|
+
// Nothing loaded yet: the first page will be fresh anyway.
|
|
144
|
+
if (typeof this.items === 'undefined') return;
|
|
145
|
+
void this.reload();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
override get isOfflineSupportEnabled() {
|
|
149
|
+
return !!this.client.offlineDb;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
override preloadFirstPageFromOfflineDb = async (): Promise<
|
|
153
|
+
MessageMarkWithUserResponse[] | undefined
|
|
154
|
+
> => {
|
|
155
|
+
const { offlineDb } = this.client;
|
|
156
|
+
if (!offlineDb) return undefined;
|
|
157
|
+
try {
|
|
158
|
+
const marks = await offlineDb.getMessageMarks({
|
|
159
|
+
filter: this.filter,
|
|
160
|
+
limit: this.pageSize,
|
|
161
|
+
messageId: this.messageId,
|
|
162
|
+
});
|
|
163
|
+
return marks?.length ? marks : undefined;
|
|
164
|
+
} catch (error) {
|
|
165
|
+
chatLoggerSystem
|
|
166
|
+
.getLogger('offline-db')
|
|
167
|
+
.warn('Reading the stored message marks failed.', { error });
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// When the request fails, the stored marks are all the list has. It pages no further and counts
|
|
173
|
+
// as stale, so reconnecting or showing it again reloads it.
|
|
174
|
+
override placeQueryPage(
|
|
175
|
+
params: PlaceQueryPageParams<MessageMarkWithUserResponse, QueryMessageMarksRequest>,
|
|
176
|
+
): ExecuteQueryReturnValue<MessageMarkWithUserResponse> {
|
|
177
|
+
const placed = super.placeQueryPage(params);
|
|
178
|
+
if (!params.results && this.showsOfflinePage) {
|
|
179
|
+
this._isStateStale = true;
|
|
180
|
+
this.state.partialNext({ hasMoreTail: false });
|
|
181
|
+
}
|
|
182
|
+
return placed;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
protected override buildMatchFilters() {
|
|
186
|
+
return { message_id: this.messageId, ...this.filter };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// The cursor stays out of the shape: `fetchQueryResponse` applies it.
|
|
190
|
+
protected getNextQueryShape(): QueryMessageMarksRequest {
|
|
191
|
+
return {
|
|
192
|
+
limit: this.pageSize,
|
|
193
|
+
...(this.filter ? { filter: this.filter } : {}),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
protected override fetchQueryResponse = async ({
|
|
198
|
+
direction,
|
|
199
|
+
queryShape,
|
|
200
|
+
}: PaginationQueryParams<QueryMessageMarksRequest>): Promise<
|
|
201
|
+
PaginationQueryReturnValue<MessageMarkWithUserResponse>
|
|
202
|
+
> => {
|
|
203
|
+
if (direction === 'headward') {
|
|
204
|
+
return { items: [] };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const next = direction ? (this.cursor?.tailward ?? undefined) : undefined;
|
|
208
|
+
const { marks: items, next: tailward } = await this.client.queryMessageMarks(
|
|
209
|
+
{ id: this.messageId },
|
|
210
|
+
{ ...(queryShape ?? this.getNextQueryShape()), ...(next ? { next } : {}) },
|
|
211
|
+
);
|
|
212
|
+
return { items, tailward };
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
/** The extracting step of the query pipeline: the response is the page. */
|
|
216
|
+
protected override extractQueryPage(
|
|
217
|
+
response: PaginationQueryReturnValue<MessageMarkWithUserResponse>,
|
|
218
|
+
_params: PaginationQueryParams<QueryMessageMarksRequest>,
|
|
219
|
+
) {
|
|
220
|
+
return response;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PaginationQueryParams,
|
|
3
|
+
PaginationQueryReturnValue,
|
|
4
|
+
PaginatorOptions,
|
|
5
|
+
} from './BasePaginator';
|
|
6
|
+
import { BasePaginator, ZERO_PAGE_CURSOR } from './BasePaginator';
|
|
7
|
+
import { StoreBackedItemIndex } from '../../entityStore/StoreBackedItemIndex';
|
|
8
|
+
import type { EntityStore, EntityStoreChangeBatch } from '../../entityStore/EntityStore';
|
|
9
|
+
import type { StreamChat } from '../../client';
|
|
10
|
+
import type {
|
|
11
|
+
QueryScheduledMessagesRequest,
|
|
12
|
+
ScheduledMessageResponse,
|
|
13
|
+
ScheduledMessagesFilter,
|
|
14
|
+
} from '../../gen/models';
|
|
15
|
+
import { SCHEDULED_MESSAGE_ACTIVE_STATUSES } from '../../scheduledMessages/constants';
|
|
16
|
+
import { generateUUIDv4 } from '../../utils';
|
|
17
|
+
|
|
18
|
+
export type ScheduledMessagePaginatorOptions = {
|
|
19
|
+
client: StreamChat;
|
|
20
|
+
/**
|
|
21
|
+
* The query filter, also deciding which ingested messages the list takes. `status` defaults to all
|
|
22
|
+
* the query accepts. Changed later through the `filter` setter.
|
|
23
|
+
*/
|
|
24
|
+
filter?: ScheduledMessagesFilter;
|
|
25
|
+
/** Generated when omitted. */
|
|
26
|
+
id?: string;
|
|
27
|
+
paginatorOptions?: PaginatorOptions<
|
|
28
|
+
ScheduledMessageResponse,
|
|
29
|
+
QueryScheduledMessagesRequest
|
|
30
|
+
>;
|
|
31
|
+
/** Defaults to `client.scheduledMessageStore`. */
|
|
32
|
+
store?: EntityStore<ScheduledMessageResponse>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const SCHEDULED_MESSAGES_PAGE_SIZE = 100;
|
|
36
|
+
|
|
37
|
+
const getScheduledMessageId = ({ id }: ScheduledMessageResponse) => id;
|
|
38
|
+
|
|
39
|
+
const compareValues = <TValue extends number | string>(first: TValue, second: TValue) =>
|
|
40
|
+
first < second ? -1 : first > second ? 1 : 0;
|
|
41
|
+
|
|
42
|
+
// Earliest first. Failed messages need no rule of their own: they failed at their send time, so they
|
|
43
|
+
// precede the pending ones. Ties break on `created_at` then `id`, both stable under edits.
|
|
44
|
+
const defaultScheduledMessagesComparator = (
|
|
45
|
+
first: ScheduledMessageResponse,
|
|
46
|
+
second: ScheduledMessageResponse,
|
|
47
|
+
) =>
|
|
48
|
+
compareValues<number>(first.scheduled_at, second.scheduled_at) ||
|
|
49
|
+
compareValues<number>(first.created_at, second.created_at) ||
|
|
50
|
+
compareValues(first.id, second.id);
|
|
51
|
+
|
|
52
|
+
/** The current user's scheduled messages, earliest `scheduled_at` first. */
|
|
53
|
+
export class ScheduledMessagePaginator extends BasePaginator<
|
|
54
|
+
ScheduledMessageResponse,
|
|
55
|
+
QueryScheduledMessagesRequest
|
|
56
|
+
> {
|
|
57
|
+
private readonly client: StreamChat;
|
|
58
|
+
private readonly _id: string;
|
|
59
|
+
private _filter: ScheduledMessagesFilter;
|
|
60
|
+
|
|
61
|
+
constructor({
|
|
62
|
+
client,
|
|
63
|
+
filter = {},
|
|
64
|
+
id,
|
|
65
|
+
paginatorOptions,
|
|
66
|
+
store,
|
|
67
|
+
}: ScheduledMessagePaginatorOptions) {
|
|
68
|
+
const entityStore = store ?? client.scheduledMessageStore;
|
|
69
|
+
super(
|
|
70
|
+
{
|
|
71
|
+
initialCursor: ZERO_PAGE_CURSOR,
|
|
72
|
+
createItemIndex: (owner) =>
|
|
73
|
+
new StoreBackedItemIndex<ScheduledMessageResponse>({
|
|
74
|
+
getEntityId: getScheduledMessageId,
|
|
75
|
+
holderName: 'scheduled-message-paginator',
|
|
76
|
+
owner: owner as ScheduledMessagePaginator,
|
|
77
|
+
store: entityStore,
|
|
78
|
+
}),
|
|
79
|
+
...paginatorOptions,
|
|
80
|
+
},
|
|
81
|
+
{ newItemsAnchorBySortPosition: true, pageSize: SCHEDULED_MESSAGES_PAGE_SIZE },
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
this.client = client;
|
|
85
|
+
this._id = id ?? `scheduled-message-paginator-${generateUUIDv4()}`;
|
|
86
|
+
this._filter = filter;
|
|
87
|
+
this.sortComparator = defaultScheduledMessagesComparator;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
get id() {
|
|
91
|
+
return this._id;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The query filter, sent as given: without a `status` the server lists all it lists. */
|
|
95
|
+
get filter(): ScheduledMessagesFilter {
|
|
96
|
+
return this._filter;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Replaces the filter and resets the list; load it again with `reload()`. */
|
|
100
|
+
set filter(filter: ScheduledMessagesFilter) {
|
|
101
|
+
this._filter = filter;
|
|
102
|
+
this.resetState();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
get cid() {
|
|
106
|
+
return this.filter.cid;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public getItemId(item: ScheduledMessageResponse): string {
|
|
110
|
+
return getScheduledMessageId(item);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
protected getNextQueryShape(): QueryScheduledMessagesRequest {
|
|
114
|
+
return { filter: this.filter, limit: this.pageSize };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The query filter, with the server's default statuses when it names none: matching on `status`
|
|
119
|
+
* is what takes sent and canceled messages out of the list. The matcher reads the array as any of
|
|
120
|
+
* them.
|
|
121
|
+
*/
|
|
122
|
+
protected buildMatchFilters(): ScheduledMessagesFilter {
|
|
123
|
+
const { cid, status } = this.filter;
|
|
124
|
+
const statuses = status?.length ? status : SCHEDULED_MESSAGE_ACTIVE_STATUSES;
|
|
125
|
+
|
|
126
|
+
// an undefined `cid` would match nothing
|
|
127
|
+
return cid ? { cid, status: statuses } : { status: statuses };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
protected override fetchQueryResponse = async ({
|
|
131
|
+
direction,
|
|
132
|
+
queryShape,
|
|
133
|
+
}: PaginationQueryParams<QueryScheduledMessagesRequest>): Promise<
|
|
134
|
+
PaginationQueryReturnValue<ScheduledMessageResponse>
|
|
135
|
+
> => {
|
|
136
|
+
const cursor = direction === 'tailward' ? this.cursor?.tailward : undefined;
|
|
137
|
+
const request: QueryScheduledMessagesRequest = {
|
|
138
|
+
...(queryShape ?? this.getNextQueryShape()),
|
|
139
|
+
...(cursor ? { next: cursor } : {}),
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const { next, scheduled_messages: items } =
|
|
143
|
+
await this.client.queryScheduledMessages(request);
|
|
144
|
+
|
|
145
|
+
return { headward: null, items, tailward: next || null };
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
protected override extractQueryPage(
|
|
149
|
+
response: PaginationQueryReturnValue<ScheduledMessageResponse>,
|
|
150
|
+
) {
|
|
151
|
+
return response;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public filterQueryResults(items: ScheduledMessageResponse[]) {
|
|
155
|
+
return items.filter((item) => this.matchesFilter(item));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public ingestItem(item: ScheduledMessageResponse): boolean {
|
|
159
|
+
// the list already holds this copy and placed it when it was stored (`onEntitiesChanged` places
|
|
160
|
+
// copies other lists store): nothing to place
|
|
161
|
+
if (this.getItem(item.id) === item) return false;
|
|
162
|
+
|
|
163
|
+
return this.placeItem(item);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Places `item` by the list's filter and order: moves it, or takes it out when it no longer matches. */
|
|
167
|
+
private placeItem(item: ScheduledMessageResponse) {
|
|
168
|
+
const changed = super.ingestItem(item);
|
|
169
|
+
|
|
170
|
+
if (!this.matchesFilter(item)) this._itemIndex.remove(item.id);
|
|
171
|
+
|
|
172
|
+
return changed;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The IDs the list holds. */
|
|
176
|
+
public getItemIds(): ReadonlySet<string> {
|
|
177
|
+
return new Set(this._itemIndex.values().map(getScheduledMessageId));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** A page loaded and nothing more to load either way: the list holds every message it matches. */
|
|
181
|
+
private get holdsCompleteList() {
|
|
182
|
+
const { hasMoreHead, hasMoreTail } = this.state.getLatestValue();
|
|
183
|
+
return this.hasLoadedPage && !hasMoreHead && !hasMoreTail;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Applies a complete set (e.g. a channel query's `scheduled_messages`) through the paginator's own
|
|
188
|
+
* operations: removes what was held at `itemIdsAtRequest` and is missing, and ingests the rest. A
|
|
189
|
+
* list that does not hold a complete set yet is replaced by it instead. Messages taken since the
|
|
190
|
+
* snapshot stay. Publishes once.
|
|
191
|
+
*/
|
|
192
|
+
public hydrate(
|
|
193
|
+
scheduledMessages: ScheduledMessageResponse[],
|
|
194
|
+
itemIdsAtRequest: ReadonlySet<string> = new Set(),
|
|
195
|
+
) {
|
|
196
|
+
const snapshot = this.filterQueryResults(scheduledMessages);
|
|
197
|
+
|
|
198
|
+
if (!this.holdsCompleteList) {
|
|
199
|
+
this.replaceWithFirstPage({ itemIdsAtRequest, page: snapshot, tailward: null });
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const snapshotIds = new Set(snapshot.map(getScheduledMessageId));
|
|
204
|
+
this.batch(
|
|
205
|
+
() => {
|
|
206
|
+
// held when the request went out and missing from the response: gone on the server
|
|
207
|
+
itemIdsAtRequest.forEach((id) => {
|
|
208
|
+
if (!snapshotIds.has(id)) this.removeItem({ id });
|
|
209
|
+
});
|
|
210
|
+
snapshot.forEach((item) => this.ingestItem(item));
|
|
211
|
+
},
|
|
212
|
+
{ coalesce: true },
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Re-queries the first page and applies it in place: the current messages stay until the response
|
|
218
|
+
* lands, unlike the base `reload()`, which empties the list first. A complete response is applied
|
|
219
|
+
* as {@link hydrate} does; a longer list is replaced by its first page. Messages taken by events
|
|
220
|
+
* while the request was out stay. A failed query keeps the list and records `lastQueryError`; a
|
|
221
|
+
* success clears it. Only the first load publishes `isLoading`. A call while one is in flight
|
|
222
|
+
* joins it.
|
|
223
|
+
*/
|
|
224
|
+
reload = (): Promise<void> => {
|
|
225
|
+
if (!this.reloadInFlight) {
|
|
226
|
+
const inFlight = this.reloadFirstPage().finally(() => {
|
|
227
|
+
if (this.reloadInFlight === inFlight) this.reloadInFlight = undefined;
|
|
228
|
+
});
|
|
229
|
+
this.reloadInFlight = inFlight;
|
|
230
|
+
}
|
|
231
|
+
return this.reloadInFlight;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
private reloadInFlight?: Promise<void>;
|
|
235
|
+
private reloadGeneration = 0;
|
|
236
|
+
|
|
237
|
+
private reloadWasSuperseded(generation: number) {
|
|
238
|
+
return generation !== this.reloadGeneration;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private reloadFirstPage = async () => {
|
|
242
|
+
const generation = this.reloadGeneration;
|
|
243
|
+
const isFirstLoad = !this.hasLoadedPage;
|
|
244
|
+
const itemIdsAtRequest = this.getItemIds();
|
|
245
|
+
if (isFirstLoad) this.state.partialNext({ isLoading: true });
|
|
246
|
+
let applied = false;
|
|
247
|
+
try {
|
|
248
|
+
const fetchParams = {
|
|
249
|
+
queryShape: this.getNextQueryShape(),
|
|
250
|
+
retryCount: this.config.retryCount,
|
|
251
|
+
};
|
|
252
|
+
const response = await this.fetchQueryResponseWithRetries(fetchParams);
|
|
253
|
+
if (!response || this.reloadWasSuperseded(generation)) return;
|
|
254
|
+
const { items, tailward } = this.extractQueryPage(response);
|
|
255
|
+
if (tailward) {
|
|
256
|
+
this.replaceWithFirstPage({
|
|
257
|
+
itemIdsAtRequest,
|
|
258
|
+
page: this.filterQueryResults(items),
|
|
259
|
+
tailward,
|
|
260
|
+
});
|
|
261
|
+
} else {
|
|
262
|
+
this.hydrate(items, itemIdsAtRequest);
|
|
263
|
+
}
|
|
264
|
+
applied = true;
|
|
265
|
+
} finally {
|
|
266
|
+
if (!this.reloadWasSuperseded(generation)) {
|
|
267
|
+
const clearError = applied && this.lastQueryError;
|
|
268
|
+
if (isFirstLoad || clearError) {
|
|
269
|
+
this.state.partialNext({
|
|
270
|
+
...(isFirstLoad ? { isLoading: false } : {}),
|
|
271
|
+
...(clearError ? { lastQueryError: undefined } : {}),
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Replaces the list with `page` as its first page. Messages taken by events since the request went
|
|
280
|
+
* out (held now, not at `itemIdsAtRequest`) stay; the others the page leaves out go. Publishes once.
|
|
281
|
+
*/
|
|
282
|
+
private replaceWithFirstPage({
|
|
283
|
+
itemIdsAtRequest,
|
|
284
|
+
page,
|
|
285
|
+
tailward,
|
|
286
|
+
}: {
|
|
287
|
+
itemIdsAtRequest: ReadonlySet<string>;
|
|
288
|
+
page: ScheduledMessageResponse[];
|
|
289
|
+
tailward: string | null | undefined;
|
|
290
|
+
}) {
|
|
291
|
+
const pageIds = new Set(page.map(getScheduledMessageId));
|
|
292
|
+
const takenSinceRequest = this._itemIndex
|
|
293
|
+
.values()
|
|
294
|
+
.filter(({ id }) => !pageIds.has(id) && !itemIdsAtRequest.has(id));
|
|
295
|
+
const keptIds = new Set([
|
|
296
|
+
...pageIds,
|
|
297
|
+
...takenSinceRequest.map(getScheduledMessageId),
|
|
298
|
+
]);
|
|
299
|
+
|
|
300
|
+
this.batch(
|
|
301
|
+
() => {
|
|
302
|
+
this.setIntervals([]);
|
|
303
|
+
this.setActiveInterval(undefined);
|
|
304
|
+
this.clearIntervalViews();
|
|
305
|
+
this._itemIndex.values().forEach(({ id }) => {
|
|
306
|
+
if (!keptIds.has(id)) this._itemIndex.remove(id);
|
|
307
|
+
});
|
|
308
|
+
this.setItems({
|
|
309
|
+
cursor: tailward
|
|
310
|
+
? { headward: null, tailward }
|
|
311
|
+
: { headward: null, tailward: null },
|
|
312
|
+
isFirstPage: true,
|
|
313
|
+
isLastPage: !tailward,
|
|
314
|
+
valueOrFactory: [...page, ...takenSinceRequest].sort(this.sortComparator),
|
|
315
|
+
});
|
|
316
|
+
},
|
|
317
|
+
{ coalesce: true },
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Another list stored a newer copy of items this one holds. A change can move a scheduled message
|
|
323
|
+
* (`scheduled_at`) or take it out of this list (`status`), so each changed item is placed by this
|
|
324
|
+
* list's filter and order.
|
|
325
|
+
*/
|
|
326
|
+
public onEntitiesChanged({ changedIds }: EntityStoreChangeBatch) {
|
|
327
|
+
this.batch(
|
|
328
|
+
() => {
|
|
329
|
+
changedIds.forEach((id) => {
|
|
330
|
+
const item = this.getItem(id);
|
|
331
|
+
if (item) this.placeItem(item);
|
|
332
|
+
});
|
|
333
|
+
},
|
|
334
|
+
{ coalesce: true },
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
public onEntityRemoved(id: string, item: unknown) {
|
|
339
|
+
this.removeItem({ id, item: item as ScheduledMessageResponse });
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
public flushState() {
|
|
343
|
+
this.flushPendingPublishes();
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
public resetState() {
|
|
347
|
+
super.resetState();
|
|
348
|
+
this._itemIndex.clear();
|
|
349
|
+
this.reloadInFlight = undefined;
|
|
350
|
+
this.reloadGeneration += 1;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
@@ -2,7 +2,9 @@ export * from './BasePaginator';
|
|
|
2
2
|
export * from './ChannelPaginator';
|
|
3
3
|
export { MessageIntervalPaginator } from './MessageIntervalPaginator';
|
|
4
4
|
export * from './MessagePaginator';
|
|
5
|
+
export * from './MessageMarkPaginator';
|
|
5
6
|
export * from './PinnedMessagePaginator';
|
|
6
7
|
export * from './ReminderPaginator';
|
|
8
|
+
export * from './ScheduledMessagePaginator';
|
|
7
9
|
export * from './UserGroupPaginator';
|
|
8
10
|
export * from './ThreadPaginator';
|