stream-chat 9.53.0 → 9.54.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/cjs/index.browser.js +362 -79
- package/dist/cjs/index.browser.js.map +3 -3
- package/dist/cjs/index.node.js +362 -79
- package/dist/cjs/index.node.js.map +3 -3
- package/dist/esm/index.mjs +362 -79
- package/dist/esm/index.mjs.map +3 -3
- package/dist/types/client.d.ts +5 -73
- package/dist/types/connection.d.ts +12 -0
- package/dist/types/poll.d.ts +22 -4
- package/dist/types/poll_optimistic_votes.d.ts +63 -0
- package/dist/types/types.d.ts +36 -9
- package/dist/types/utils.d.ts +1 -3
- package/package.json +6 -1
- package/src/client.ts +3 -3
- package/src/connection.ts +47 -2
- package/src/messageComposer/middleware/messageComposer/linkPreviews.ts +20 -10
- package/src/poll.ts +95 -61
- package/src/poll_optimistic_votes.ts +321 -0
- package/src/thread.ts +11 -4
- package/src/types.ts +41 -9
|
@@ -5910,10 +5910,11 @@ var createLinkPreviewsCompositionMiddleware = (composer) => ({
|
|
|
5910
5910
|
const linkPreviews = linkPreviewsManager.loadingPreviews.length > 0 ? [] : linkPreviewsManager.loadedPreviews.map(
|
|
5911
5911
|
(preview) => LinkPreviewsManager.getPreviewData(preview)
|
|
5912
5912
|
);
|
|
5913
|
-
const
|
|
5913
|
+
const messageAttachments = (state.message.attachments ?? []).concat(
|
|
5914
5914
|
linkPreviews
|
|
5915
5915
|
);
|
|
5916
|
-
|
|
5916
|
+
const localAttachments = (state.localMessage.attachments ?? []).concat(linkPreviews);
|
|
5917
|
+
if (!messageAttachments.length && !localAttachments.length) return forward();
|
|
5917
5918
|
const sendOptions = { ...state.sendOptions };
|
|
5918
5919
|
const skip_enrich_url = !someLinkPreviewsLoading && linkPreviews.length > 0 || someLinkPreviewsDismissed;
|
|
5919
5920
|
if (skip_enrich_url) {
|
|
@@ -5921,14 +5922,14 @@ var createLinkPreviewsCompositionMiddleware = (composer) => ({
|
|
|
5921
5922
|
}
|
|
5922
5923
|
return next({
|
|
5923
5924
|
...state,
|
|
5924
|
-
message: {
|
|
5925
|
+
message: messageAttachments.length ? {
|
|
5925
5926
|
...state.message,
|
|
5926
|
-
attachments
|
|
5927
|
-
},
|
|
5928
|
-
localMessage: {
|
|
5927
|
+
attachments: messageAttachments
|
|
5928
|
+
} : state.message,
|
|
5929
|
+
localMessage: localAttachments.length ? {
|
|
5929
5930
|
...state.localMessage,
|
|
5930
|
-
attachments
|
|
5931
|
-
},
|
|
5931
|
+
attachments: localAttachments
|
|
5932
|
+
} : state.localMessage,
|
|
5932
5933
|
sendOptions
|
|
5933
5934
|
});
|
|
5934
5935
|
}
|
|
@@ -8310,10 +8311,13 @@ var Thread = class extends WithSubscriptions {
|
|
|
8310
8311
|
}));
|
|
8311
8312
|
}
|
|
8312
8313
|
};
|
|
8313
|
-
const
|
|
8314
|
-
|
|
8315
|
-
|
|
8316
|
-
|
|
8314
|
+
const channelResponse = threadData.channel;
|
|
8315
|
+
const channelData = { name: channelResponse.name };
|
|
8316
|
+
const channel = client.channel(
|
|
8317
|
+
threadData.channel.type,
|
|
8318
|
+
threadData.channel.id,
|
|
8319
|
+
channelData
|
|
8320
|
+
);
|
|
8317
8321
|
channel._hydrateMembers({
|
|
8318
8322
|
members: threadData.channel.members ?? [],
|
|
8319
8323
|
overrideCurrentState: false
|
|
@@ -11713,6 +11717,10 @@ var StableWSConnection = class {
|
|
|
11713
11717
|
if (data.type === "health.check") {
|
|
11714
11718
|
this.scheduleNextPing();
|
|
11715
11719
|
}
|
|
11720
|
+
if (data?.type === "connection.error" && data?.error?.code === chatCodes.TOKEN_EXPIRED) {
|
|
11721
|
+
this._log("onmessage() - server reported that the token expired", { wsID });
|
|
11722
|
+
this.tokenExpiredByServer = true;
|
|
11723
|
+
}
|
|
11716
11724
|
this.client.dispatchEvent(data);
|
|
11717
11725
|
this.scheduleConnectionCheck();
|
|
11718
11726
|
};
|
|
@@ -11736,7 +11744,7 @@ var StableWSConnection = class {
|
|
|
11736
11744
|
this.isConnecting = false;
|
|
11737
11745
|
this.rejectPromise?.(this._errorFromWSEvent(event));
|
|
11738
11746
|
this._log(`onclose() - WS connection closed. Calling reconnect ...`, { event });
|
|
11739
|
-
this.
|
|
11747
|
+
this._reconnectAfterFailure();
|
|
11740
11748
|
}
|
|
11741
11749
|
};
|
|
11742
11750
|
this.onerror = (wsID, event) => {
|
|
@@ -11747,7 +11755,7 @@ var StableWSConnection = class {
|
|
|
11747
11755
|
this.isConnecting = false;
|
|
11748
11756
|
this.rejectPromise?.(this._errorFromWSEvent(event));
|
|
11749
11757
|
this._log(`onerror() - WS connection resulted into error`, { event });
|
|
11750
|
-
this.
|
|
11758
|
+
this._reconnectAfterFailure();
|
|
11751
11759
|
};
|
|
11752
11760
|
/**
|
|
11753
11761
|
* _setHealth - Sets the connection to healthy or unhealthy.
|
|
@@ -11800,6 +11808,7 @@ var StableWSConnection = class {
|
|
|
11800
11808
|
*/
|
|
11801
11809
|
this._setupConnectionPromise = () => {
|
|
11802
11810
|
this.isResolved = false;
|
|
11811
|
+
this.tokenExpiredByServer = false;
|
|
11803
11812
|
this.connectionOpen = new Promise((resolve, reject) => {
|
|
11804
11813
|
this.resolvePromise = resolve;
|
|
11805
11814
|
this.rejectPromise = reject;
|
|
@@ -11847,6 +11856,7 @@ var StableWSConnection = class {
|
|
|
11847
11856
|
this.isHealthy = false;
|
|
11848
11857
|
this.wsID = 1;
|
|
11849
11858
|
this.lastEvent = null;
|
|
11859
|
+
this.tokenExpiredByServer = false;
|
|
11850
11860
|
this.pingInterval = 25 * 1e3;
|
|
11851
11861
|
this.connectionCheckTimeout = this.pingInterval + 10 * 1e3;
|
|
11852
11862
|
addConnectionEventListeners(this.onlineStatusChanged);
|
|
@@ -11945,6 +11955,7 @@ var StableWSConnection = class {
|
|
|
11945
11955
|
this.wsID += 1;
|
|
11946
11956
|
this.isConnecting = false;
|
|
11947
11957
|
this.isDisconnected = true;
|
|
11958
|
+
this.tokenExpiredByServer = false;
|
|
11948
11959
|
if (this.healthCheckTimeoutRef) {
|
|
11949
11960
|
clearInterval(this.healthCheckTimeoutRef);
|
|
11950
11961
|
}
|
|
@@ -12077,6 +12088,10 @@ var StableWSConnection = class {
|
|
|
12077
12088
|
this._destroyCurrentWSConnection();
|
|
12078
12089
|
if (options.refreshToken) {
|
|
12079
12090
|
await this.client.tokenManager.loadToken();
|
|
12091
|
+
if (this.isConnecting || this.isHealthy) {
|
|
12092
|
+
this._log("_reconnect() - Abort (4) since already connecting or healthy");
|
|
12093
|
+
return;
|
|
12094
|
+
}
|
|
12080
12095
|
}
|
|
12081
12096
|
try {
|
|
12082
12097
|
await this._connect();
|
|
@@ -12100,6 +12115,21 @@ var StableWSConnection = class {
|
|
|
12100
12115
|
}
|
|
12101
12116
|
this._log("_reconnect() - == END ==");
|
|
12102
12117
|
}
|
|
12118
|
+
/**
|
|
12119
|
+
* _reconnectAfterFailure - Reconnects after the socket closed or failed. If the server
|
|
12120
|
+
* reported that the token expired, the token is refreshed before reconnecting.
|
|
12121
|
+
* Static tokens cannot be refreshed and reconnect as before.
|
|
12122
|
+
*/
|
|
12123
|
+
_reconnectAfterFailure() {
|
|
12124
|
+
if (this.tokenExpiredByServer && !this.client.tokenManager.isStatic()) {
|
|
12125
|
+
this._log(
|
|
12126
|
+
"_reconnectAfterFailure() - token expired, so going to reload token and reconnect"
|
|
12127
|
+
);
|
|
12128
|
+
this._reconnect({ refreshToken: true });
|
|
12129
|
+
return;
|
|
12130
|
+
}
|
|
12131
|
+
this._reconnect();
|
|
12132
|
+
}
|
|
12103
12133
|
/**
|
|
12104
12134
|
* _destroyCurrentWSConnection - Removes the current WS connection
|
|
12105
12135
|
*
|
|
@@ -13157,6 +13187,242 @@ var ThreadManager = class extends WithSubscriptions {
|
|
|
13157
13187
|
}
|
|
13158
13188
|
};
|
|
13159
13189
|
|
|
13190
|
+
// src/poll_optimistic_votes.ts
|
|
13191
|
+
var CATCH_UP_TIMEOUT_MS = 3e3;
|
|
13192
|
+
var OptimisticPollVotes = class {
|
|
13193
|
+
constructor({ client, poll }) {
|
|
13194
|
+
// Own vote changes not confirmed by a WS event yet
|
|
13195
|
+
this.pendingCount = 0;
|
|
13196
|
+
// Own vote changes whose requests succeeded since the server's vote state was last shown
|
|
13197
|
+
this.succeededChanges = [];
|
|
13198
|
+
// Vote requests with this tag are sent one after another. A symbol, so it is unique per instance.
|
|
13199
|
+
this.requestTag = /* @__PURE__ */ Symbol("poll-votes");
|
|
13200
|
+
this.withServerVotes = (pollState) => ({
|
|
13201
|
+
...pollState,
|
|
13202
|
+
...this.serverVoteState
|
|
13203
|
+
});
|
|
13204
|
+
/**
|
|
13205
|
+
* Casts a vote for an option, shown right away through a placeholder vote. In polls with unique
|
|
13206
|
+
* votes, it replaces the existing own votes.
|
|
13207
|
+
*/
|
|
13208
|
+
this.castVote = (optionId, messageId) => {
|
|
13209
|
+
const sendRequest = () => this.client.castPollVote(messageId, this.poll.id, { option_id: optionId });
|
|
13210
|
+
const state = this.poll.data;
|
|
13211
|
+
if (state.ownVotesByOptionId[optionId]) return this.send(sendRequest);
|
|
13212
|
+
const replacedVotes = state.enforce_unique_vote ? Object.values(state.ownVotesByOptionId) : [];
|
|
13213
|
+
this.poll.state.partialNext(
|
|
13214
|
+
applyOwnVoteDelta(state, {
|
|
13215
|
+
add: [this.createPlaceholderVote(optionId)],
|
|
13216
|
+
remove: replacedVotes
|
|
13217
|
+
})
|
|
13218
|
+
);
|
|
13219
|
+
return this.track(sendRequest, (response) => ({
|
|
13220
|
+
optionId,
|
|
13221
|
+
vote: response.vote
|
|
13222
|
+
}));
|
|
13223
|
+
};
|
|
13224
|
+
// Removes a vote, right away.
|
|
13225
|
+
this.removeVote = (voteId, messageId) => {
|
|
13226
|
+
const sendRequest = (id) => this.client.removePollVote(messageId, this.poll.id, id);
|
|
13227
|
+
const state = this.poll.data;
|
|
13228
|
+
const vote = Object.values(state.ownVotesByOptionId).find(({ id }) => id === voteId);
|
|
13229
|
+
if (!vote?.option_id) return this.send(() => sendRequest(voteId));
|
|
13230
|
+
const optionId = vote.option_id;
|
|
13231
|
+
this.poll.state.partialNext(applyOwnVoteDelta(state, { remove: [vote] }));
|
|
13232
|
+
return this.track(
|
|
13233
|
+
// runs when the removal's turn comes, so a preceding cast of the vote has finished
|
|
13234
|
+
() => {
|
|
13235
|
+
const serverVoteId = this.getServerVoteId(vote);
|
|
13236
|
+
return serverVoteId ? sendRequest(serverVoteId) : Promise.resolve(void 0);
|
|
13237
|
+
},
|
|
13238
|
+
() => ({ optionId })
|
|
13239
|
+
);
|
|
13240
|
+
};
|
|
13241
|
+
// Sends a vote request once the previous one has finished, whether it succeeded or not.
|
|
13242
|
+
this.send = async (sendRequest) => {
|
|
13243
|
+
try {
|
|
13244
|
+
return await withoutConcurrency(this.requestTag, sendRequest);
|
|
13245
|
+
} finally {
|
|
13246
|
+
if (!hasPending(this.requestTag)) this.scheduleCatchUp();
|
|
13247
|
+
}
|
|
13248
|
+
};
|
|
13249
|
+
// Stores the vote state of a WS vote event and returns the vote fields to show, if any.
|
|
13250
|
+
this.handleVoteEvent = (poll, {
|
|
13251
|
+
confirmsOwnVote,
|
|
13252
|
+
ownVotesByOptionId
|
|
13253
|
+
}) => {
|
|
13254
|
+
this.serverVoteState = {
|
|
13255
|
+
latest_votes_by_option: poll.latest_votes_by_option,
|
|
13256
|
+
ownVotesByOptionId,
|
|
13257
|
+
vote_count: poll.vote_count,
|
|
13258
|
+
vote_counts_by_option: poll.vote_counts_by_option
|
|
13259
|
+
};
|
|
13260
|
+
if (confirmsOwnVote) this.pendingCount = Math.max(this.pendingCount - 1, 0);
|
|
13261
|
+
return this.pendingCount > 0 ? {} : this.takeServerVoteState();
|
|
13262
|
+
};
|
|
13263
|
+
// Fresh server state (e.g. a refetch) replaces everything, pending changes are dropped.
|
|
13264
|
+
this.reset = (pollState) => {
|
|
13265
|
+
this.serverVoteState = pickVoteState(pollState);
|
|
13266
|
+
this.pendingCount = 0;
|
|
13267
|
+
this.succeededChanges = [];
|
|
13268
|
+
clearTimeout(this.catchUpTimeout);
|
|
13269
|
+
};
|
|
13270
|
+
// Tracks an own vote change until its request settles. `sendRequest` resolves to undefined
|
|
13271
|
+
// when no request was needed.
|
|
13272
|
+
this.track = async (sendRequest, toChange) => {
|
|
13273
|
+
this.pendingCount += 1;
|
|
13274
|
+
clearTimeout(this.catchUpTimeout);
|
|
13275
|
+
let response;
|
|
13276
|
+
try {
|
|
13277
|
+
response = await this.send(async () => {
|
|
13278
|
+
const result = await sendRequest();
|
|
13279
|
+
if (result !== void 0) this.succeededChanges.push(toChange(result));
|
|
13280
|
+
return result;
|
|
13281
|
+
});
|
|
13282
|
+
} catch (error) {
|
|
13283
|
+
this.settleWithoutEvent();
|
|
13284
|
+
throw error;
|
|
13285
|
+
}
|
|
13286
|
+
if (response === void 0) this.settleWithoutEvent();
|
|
13287
|
+
return response;
|
|
13288
|
+
};
|
|
13289
|
+
// A failed or skipped request gets no WS event, so it counts as confirmed right away.
|
|
13290
|
+
this.settleWithoutEvent = () => {
|
|
13291
|
+
this.pendingCount = Math.max(this.pendingCount - 1, 0);
|
|
13292
|
+
if (this.pendingCount === 0) this.poll.state.partialNext(this.takeServerVoteState());
|
|
13293
|
+
};
|
|
13294
|
+
// The vote shown for a cast until the server's vote arrives
|
|
13295
|
+
this.createPlaceholderVote = (optionId) => {
|
|
13296
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
13297
|
+
return {
|
|
13298
|
+
created_at: now,
|
|
13299
|
+
id: `local-${generateUUIDv4()}`,
|
|
13300
|
+
option_id: optionId,
|
|
13301
|
+
poll_id: this.poll.id,
|
|
13302
|
+
updated_at: now,
|
|
13303
|
+
user: this.client.user,
|
|
13304
|
+
user_id: this.client.userID
|
|
13305
|
+
};
|
|
13306
|
+
};
|
|
13307
|
+
/**
|
|
13308
|
+
* The server id of a vote to remove, once everything sent before the removal has finished. Casts
|
|
13309
|
+
* and removals of an option alternate, so the option's last succeeded change tells: a cast is the
|
|
13310
|
+
* vote being removed (e.g. a placeholder), a removal means the cast in between failed. Without
|
|
13311
|
+
* one, the server's own vote of the option is the vote being removed.
|
|
13312
|
+
*/
|
|
13313
|
+
this.getServerVoteId = (vote) => {
|
|
13314
|
+
for (let i = this.succeededChanges.length - 1; i >= 0; i--) {
|
|
13315
|
+
const change = this.succeededChanges[i];
|
|
13316
|
+
if (change.optionId === vote.option_id) return change.vote?.id;
|
|
13317
|
+
}
|
|
13318
|
+
return this.serverVoteState.ownVotesByOptionId[vote.option_id]?.id;
|
|
13319
|
+
};
|
|
13320
|
+
this.takeServerVoteState = () => {
|
|
13321
|
+
clearTimeout(this.catchUpTimeout);
|
|
13322
|
+
this.succeededChanges = [];
|
|
13323
|
+
return {
|
|
13324
|
+
...this.serverVoteState,
|
|
13325
|
+
maxVotedOptionIds: getMaxVotedOptionIds(this.serverVoteState.vote_counts_by_option)
|
|
13326
|
+
};
|
|
13327
|
+
};
|
|
13328
|
+
this.scheduleCatchUp = () => {
|
|
13329
|
+
clearTimeout(this.catchUpTimeout);
|
|
13330
|
+
if (this.pendingCount === 0) return;
|
|
13331
|
+
this.catchUpTimeout = setTimeout(this.applySucceededChanges, CATCH_UP_TIMEOUT_MS);
|
|
13332
|
+
};
|
|
13333
|
+
/**
|
|
13334
|
+
* A WS event of an own vote change got lost, so the server's own votes are stale. All requests
|
|
13335
|
+
* have finished (the timeout starts once nothing is left to send and a new request cancels it),
|
|
13336
|
+
* so applying the succeeded changes in order gives the server's own votes (changes whose events
|
|
13337
|
+
* did arrive end up the same). They are shown with the vote counts of the last WS event, which
|
|
13338
|
+
* contain the other users' votes too.
|
|
13339
|
+
*/
|
|
13340
|
+
this.applySucceededChanges = () => {
|
|
13341
|
+
const { enforce_unique_vote } = this.poll.data;
|
|
13342
|
+
let ownVotesByOptionId = { ...this.serverVoteState.ownVotesByOptionId };
|
|
13343
|
+
for (const { optionId, vote } of this.succeededChanges) {
|
|
13344
|
+
if (!vote) {
|
|
13345
|
+
delete ownVotesByOptionId[optionId];
|
|
13346
|
+
} else if (enforce_unique_vote) {
|
|
13347
|
+
ownVotesByOptionId = { [optionId]: vote };
|
|
13348
|
+
} else {
|
|
13349
|
+
ownVotesByOptionId[optionId] = vote;
|
|
13350
|
+
}
|
|
13351
|
+
}
|
|
13352
|
+
this.serverVoteState = { ...this.serverVoteState, ownVotesByOptionId };
|
|
13353
|
+
this.pendingCount = 0;
|
|
13354
|
+
this.poll.state.partialNext(this.takeServerVoteState());
|
|
13355
|
+
};
|
|
13356
|
+
this.client = client;
|
|
13357
|
+
this.poll = poll;
|
|
13358
|
+
this.serverVoteState = pickVoteState(poll.data);
|
|
13359
|
+
}
|
|
13360
|
+
get serverOwnVotes() {
|
|
13361
|
+
return this.serverVoteState.ownVotesByOptionId;
|
|
13362
|
+
}
|
|
13363
|
+
};
|
|
13364
|
+
function pickVoteState(state) {
|
|
13365
|
+
return {
|
|
13366
|
+
latest_votes_by_option: state.latest_votes_by_option,
|
|
13367
|
+
ownVotesByOptionId: state.ownVotesByOptionId,
|
|
13368
|
+
vote_count: state.vote_count,
|
|
13369
|
+
vote_counts_by_option: state.vote_counts_by_option
|
|
13370
|
+
};
|
|
13371
|
+
}
|
|
13372
|
+
function applyOwnVoteDelta(state, { add = [], remove = [] }) {
|
|
13373
|
+
const ownVotesByOptionId = { ...state.ownVotesByOptionId };
|
|
13374
|
+
const voteCountsByOption = { ...state.vote_counts_by_option };
|
|
13375
|
+
const latestVotesByOption = state.latest_votes_by_option ? { ...state.latest_votes_by_option } : state.latest_votes_by_option;
|
|
13376
|
+
let voteCount = state.vote_count ?? 0;
|
|
13377
|
+
for (const vote of remove) {
|
|
13378
|
+
const optionId = vote.option_id;
|
|
13379
|
+
if (!optionId) continue;
|
|
13380
|
+
delete ownVotesByOptionId[optionId];
|
|
13381
|
+
const optionVoteCount = (voteCountsByOption[optionId] ?? 0) - 1;
|
|
13382
|
+
if (optionVoteCount > 0) {
|
|
13383
|
+
voteCountsByOption[optionId] = optionVoteCount;
|
|
13384
|
+
} else {
|
|
13385
|
+
delete voteCountsByOption[optionId];
|
|
13386
|
+
}
|
|
13387
|
+
voteCount = Math.max(voteCount - 1, 0);
|
|
13388
|
+
if (latestVotesByOption?.[optionId]) {
|
|
13389
|
+
latestVotesByOption[optionId] = latestVotesByOption[optionId].filter(
|
|
13390
|
+
(latestVote) => latestVote.id !== vote.id
|
|
13391
|
+
);
|
|
13392
|
+
}
|
|
13393
|
+
}
|
|
13394
|
+
for (const vote of add) {
|
|
13395
|
+
const optionId = vote.option_id;
|
|
13396
|
+
if (!optionId) continue;
|
|
13397
|
+
ownVotesByOptionId[optionId] = vote;
|
|
13398
|
+
voteCountsByOption[optionId] = (voteCountsByOption[optionId] ?? 0) + 1;
|
|
13399
|
+
voteCount += 1;
|
|
13400
|
+
if (latestVotesByOption && state.voting_visibility !== "anonymous") {
|
|
13401
|
+
latestVotesByOption[optionId] = [vote, ...latestVotesByOption[optionId] ?? []];
|
|
13402
|
+
}
|
|
13403
|
+
}
|
|
13404
|
+
return {
|
|
13405
|
+
latest_votes_by_option: latestVotesByOption,
|
|
13406
|
+
maxVotedOptionIds: getMaxVotedOptionIds(voteCountsByOption),
|
|
13407
|
+
ownVotesByOptionId,
|
|
13408
|
+
vote_count: voteCount,
|
|
13409
|
+
vote_counts_by_option: voteCountsByOption
|
|
13410
|
+
};
|
|
13411
|
+
}
|
|
13412
|
+
function getMaxVotedOptionIds(voteCountsByOption) {
|
|
13413
|
+
let maxVotes = 0;
|
|
13414
|
+
let winningOptions = [];
|
|
13415
|
+
for (const [id, count] of Object.entries(voteCountsByOption ?? {})) {
|
|
13416
|
+
if (count > maxVotes) {
|
|
13417
|
+
winningOptions = [id];
|
|
13418
|
+
maxVotes = count;
|
|
13419
|
+
} else if (count === maxVotes) {
|
|
13420
|
+
winningOptions.push(id);
|
|
13421
|
+
}
|
|
13422
|
+
}
|
|
13423
|
+
return winningOptions;
|
|
13424
|
+
}
|
|
13425
|
+
|
|
13160
13426
|
// src/poll.ts
|
|
13161
13427
|
var isPollUpdatedEvent = (e) => e.type === "poll.updated";
|
|
13162
13428
|
var isPollClosedEventEvent = (e) => e.type === "poll.closed";
|
|
@@ -13166,6 +13432,9 @@ var isPollVoteRemovedEvent = (e) => e.type === "poll.vote_removed";
|
|
|
13166
13432
|
var isVoteAnswer = (vote) => !!vote?.answer_text;
|
|
13167
13433
|
var Poll = class {
|
|
13168
13434
|
constructor({ client, poll }) {
|
|
13435
|
+
// Set whenever server data (WS event or rehydration) confirms that the poll is closed,
|
|
13436
|
+
// so that a failed optimistic close does not reopen a poll that is actually closed.
|
|
13437
|
+
this.closeConfirmed = false;
|
|
13169
13438
|
this.getInitialStateFromPollResponse = (poll) => {
|
|
13170
13439
|
const { own_votes, id, ...pollResponseForState } = poll;
|
|
13171
13440
|
const { ownAnswer, ownVotes } = own_votes?.reduce(
|
|
@@ -13189,25 +13458,55 @@ var Poll = class {
|
|
|
13189
13458
|
ownVotesByOptionId: getOwnVotesByOptionId(ownVotes)
|
|
13190
13459
|
};
|
|
13191
13460
|
};
|
|
13461
|
+
// persists only the server's votes, never the optimistic ones
|
|
13192
13462
|
this.upsertOfflineDb = () => {
|
|
13463
|
+
const serverState = this.optimisticVotes.withServerVotes(this.data);
|
|
13193
13464
|
this.client.offlineDb?.executeQuerySafely(
|
|
13194
|
-
(db) => db.upsertPoll({ poll: mapPollStateToResponse(this) }),
|
|
13465
|
+
(db) => db.upsertPoll({ poll: mapPollStateToResponse(this, serverState) }),
|
|
13195
13466
|
{ method: "upsertPoll" }
|
|
13196
13467
|
);
|
|
13197
13468
|
};
|
|
13198
13469
|
this.reinitializeState = (poll) => {
|
|
13199
|
-
|
|
13470
|
+
if (poll.is_closed) this.closeConfirmed = true;
|
|
13471
|
+
const initialState = this.getInitialStateFromPollResponse(poll);
|
|
13472
|
+
this.optimisticVotes.reset(initialState);
|
|
13473
|
+
this.state.partialNext(initialState);
|
|
13474
|
+
};
|
|
13475
|
+
// Hands the vote state of a WS vote event to the optimistic votes, which decide whether to show it.
|
|
13476
|
+
this.updateFromVoteEvent = (event, ownVotesByOptionId, answerState) => {
|
|
13477
|
+
const isOwnVote = event.poll_vote.user_id === this.client.userID;
|
|
13478
|
+
const shownVoteState = this.optimisticVotes.handleVoteEvent(event.poll, {
|
|
13479
|
+
confirmsOwnVote: isOwnVote && !isVoteAnswer(event.poll_vote) && !!event.poll_vote.option_id,
|
|
13480
|
+
ownVotesByOptionId
|
|
13481
|
+
});
|
|
13482
|
+
this.state.partialNext({
|
|
13483
|
+
...answerState,
|
|
13484
|
+
answers_count: event.poll.answers_count,
|
|
13485
|
+
lastActivityAt: new Date(event.created_at),
|
|
13486
|
+
...shownVoteState
|
|
13487
|
+
});
|
|
13488
|
+
this.upsertOfflineDb();
|
|
13200
13489
|
};
|
|
13201
13490
|
this.handlePollUpdated = (event) => {
|
|
13202
13491
|
if (event.poll?.id && event.poll.id !== this.id) return;
|
|
13203
13492
|
if (!isPollUpdatedEvent(event)) return;
|
|
13204
13493
|
const { id, ...pollData } = extractPollData(event.poll);
|
|
13205
|
-
|
|
13494
|
+
if (pollData.is_closed) this.closeConfirmed = true;
|
|
13495
|
+
this.state.partialNext({
|
|
13496
|
+
...pollData,
|
|
13497
|
+
// Server-owned translations, which `extractPollData` (an update request) leaves out. Copied
|
|
13498
|
+
// even when absent: the event carries the whole poll, and a change made under channel-only
|
|
13499
|
+
// translation drops the stale ones.
|
|
13500
|
+
description_i18n: event.poll.description_i18n,
|
|
13501
|
+
lastActivityAt: new Date(event.created_at),
|
|
13502
|
+
name_i18n: event.poll.name_i18n
|
|
13503
|
+
});
|
|
13206
13504
|
this.upsertOfflineDb();
|
|
13207
13505
|
};
|
|
13208
13506
|
this.handlePollClosed = (event) => {
|
|
13209
13507
|
if (event.poll?.id && event.poll.id !== this.id) return;
|
|
13210
13508
|
if (!isPollClosedEventEvent(event)) return;
|
|
13509
|
+
this.closeConfirmed = true;
|
|
13211
13510
|
this.state.partialNext({
|
|
13212
13511
|
is_closed: true,
|
|
13213
13512
|
lastActivityAt: new Date(event.created_at)
|
|
@@ -13221,8 +13520,7 @@ var Poll = class {
|
|
|
13221
13520
|
const isOwnVote = event.poll_vote.user_id === this.client.userID;
|
|
13222
13521
|
let latestAnswers = [...currentState.latest_answers];
|
|
13223
13522
|
let ownAnswer = currentState.ownAnswer;
|
|
13224
|
-
const ownVotesByOptionId =
|
|
13225
|
-
let maxVotedOptionIds = currentState.maxVotedOptionIds;
|
|
13523
|
+
const ownVotesByOptionId = { ...this.optimisticVotes.serverOwnVotes };
|
|
13226
13524
|
if (isOwnVote) {
|
|
13227
13525
|
if (isVoteAnswer(event.poll_vote)) {
|
|
13228
13526
|
ownAnswer = event.poll_vote;
|
|
@@ -13232,19 +13530,11 @@ var Poll = class {
|
|
|
13232
13530
|
}
|
|
13233
13531
|
if (isVoteAnswer(event.poll_vote)) {
|
|
13234
13532
|
latestAnswers = [event.poll_vote, ...latestAnswers];
|
|
13235
|
-
} else {
|
|
13236
|
-
maxVotedOptionIds = getMaxVotedOptionIds(event.poll.vote_counts_by_option);
|
|
13237
13533
|
}
|
|
13238
|
-
|
|
13239
|
-
this.state.partialNext({
|
|
13240
|
-
...pollEnrichData,
|
|
13534
|
+
this.updateFromVoteEvent(event, ownVotesByOptionId, {
|
|
13241
13535
|
latest_answers: latestAnswers,
|
|
13242
|
-
|
|
13243
|
-
ownAnswer,
|
|
13244
|
-
ownVotesByOptionId,
|
|
13245
|
-
maxVotedOptionIds
|
|
13536
|
+
ownAnswer
|
|
13246
13537
|
});
|
|
13247
|
-
this.upsertOfflineDb();
|
|
13248
13538
|
};
|
|
13249
13539
|
this.handleVoteChanged = (event) => {
|
|
13250
13540
|
if (event.poll?.id && event.poll.id !== this.id) return;
|
|
@@ -13253,8 +13543,7 @@ var Poll = class {
|
|
|
13253
13543
|
const isOwnVote = event.poll_vote.user_id === this.client.userID;
|
|
13254
13544
|
let latestAnswers = [...currentState.latest_answers];
|
|
13255
13545
|
let ownAnswer = currentState.ownAnswer;
|
|
13256
|
-
let ownVotesByOptionId =
|
|
13257
|
-
let maxVotedOptionIds = currentState.maxVotedOptionIds;
|
|
13546
|
+
let ownVotesByOptionId = this.optimisticVotes.serverOwnVotes;
|
|
13258
13547
|
if (isOwnVote) {
|
|
13259
13548
|
if (isVoteAnswer(event.poll_vote)) {
|
|
13260
13549
|
latestAnswers = [
|
|
@@ -13278,23 +13567,14 @@ var Poll = class {
|
|
|
13278
13567
|
if (ownAnswer?.id === event.poll_vote.id) {
|
|
13279
13568
|
ownAnswer = void 0;
|
|
13280
13569
|
}
|
|
13281
|
-
maxVotedOptionIds = getMaxVotedOptionIds(event.poll.vote_counts_by_option);
|
|
13282
13570
|
}
|
|
13283
13571
|
} else if (isVoteAnswer(event.poll_vote)) {
|
|
13284
13572
|
latestAnswers = [event.poll_vote, ...latestAnswers];
|
|
13285
|
-
} else {
|
|
13286
|
-
maxVotedOptionIds = getMaxVotedOptionIds(event.poll.vote_counts_by_option);
|
|
13287
13573
|
}
|
|
13288
|
-
|
|
13289
|
-
this.state.partialNext({
|
|
13290
|
-
...pollEnrichData,
|
|
13574
|
+
this.updateFromVoteEvent(event, ownVotesByOptionId, {
|
|
13291
13575
|
latest_answers: latestAnswers,
|
|
13292
|
-
|
|
13293
|
-
ownAnswer,
|
|
13294
|
-
ownVotesByOptionId,
|
|
13295
|
-
maxVotedOptionIds
|
|
13576
|
+
ownAnswer
|
|
13296
13577
|
});
|
|
13297
|
-
this.upsertOfflineDb();
|
|
13298
13578
|
};
|
|
13299
13579
|
this.handleVoteRemoved = (event) => {
|
|
13300
13580
|
if (event.poll?.id && event.poll.id !== this.id) return;
|
|
@@ -13303,29 +13583,19 @@ var Poll = class {
|
|
|
13303
13583
|
const isOwnVote = event.poll_vote.user_id === this.client.userID;
|
|
13304
13584
|
let latestAnswers = [...currentState.latest_answers];
|
|
13305
13585
|
let ownAnswer = currentState.ownAnswer;
|
|
13306
|
-
const ownVotesByOptionId = { ...
|
|
13307
|
-
let maxVotedOptionIds = currentState.maxVotedOptionIds;
|
|
13586
|
+
const ownVotesByOptionId = { ...this.optimisticVotes.serverOwnVotes };
|
|
13308
13587
|
if (isVoteAnswer(event.poll_vote)) {
|
|
13309
13588
|
latestAnswers = latestAnswers.filter((answer) => answer.id !== event.poll_vote.id);
|
|
13310
13589
|
if (isOwnVote) {
|
|
13311
13590
|
ownAnswer = void 0;
|
|
13312
13591
|
}
|
|
13313
|
-
} else {
|
|
13314
|
-
|
|
13315
|
-
if (isOwnVote && event.poll_vote.option_id) {
|
|
13316
|
-
delete ownVotesByOptionId[event.poll_vote.option_id];
|
|
13317
|
-
}
|
|
13592
|
+
} else if (isOwnVote && event.poll_vote.option_id) {
|
|
13593
|
+
delete ownVotesByOptionId[event.poll_vote.option_id];
|
|
13318
13594
|
}
|
|
13319
|
-
|
|
13320
|
-
this.state.partialNext({
|
|
13321
|
-
...pollEnrichData,
|
|
13595
|
+
this.updateFromVoteEvent(event, ownVotesByOptionId, {
|
|
13322
13596
|
latest_answers: latestAnswers,
|
|
13323
|
-
|
|
13324
|
-
ownAnswer,
|
|
13325
|
-
ownVotesByOptionId,
|
|
13326
|
-
maxVotedOptionIds
|
|
13597
|
+
ownAnswer
|
|
13327
13598
|
});
|
|
13328
|
-
this.upsertOfflineDb();
|
|
13329
13599
|
};
|
|
13330
13600
|
this.query = async (id) => {
|
|
13331
13601
|
const { poll } = await this.client.getPoll(id);
|
|
@@ -13334,11 +13604,33 @@ var Poll = class {
|
|
|
13334
13604
|
};
|
|
13335
13605
|
this.update = async (data) => await this.client.updatePoll({ ...data, id: this.id });
|
|
13336
13606
|
this.partialUpdate = async (partialPollObject) => await this.client.partialUpdatePoll(this.id, partialPollObject);
|
|
13337
|
-
|
|
13607
|
+
/**
|
|
13608
|
+
* Closes the poll. The poll is marked as closed optimistically and the change is
|
|
13609
|
+
* reverted if the request fails, unless the server confirmed the close meanwhile.
|
|
13610
|
+
* Does nothing if the poll is already closed.
|
|
13611
|
+
*/
|
|
13612
|
+
this.close = async () => {
|
|
13613
|
+
if (this.data.is_closed) return;
|
|
13614
|
+
this.closeConfirmed = false;
|
|
13615
|
+
this.state.partialNext({ is_closed: true });
|
|
13616
|
+
try {
|
|
13617
|
+
return await this.client.closePoll(this.id);
|
|
13618
|
+
} catch (error) {
|
|
13619
|
+
if (!this.closeConfirmed) {
|
|
13620
|
+
this.state.partialNext({ is_closed: false });
|
|
13621
|
+
}
|
|
13622
|
+
throw error;
|
|
13623
|
+
}
|
|
13624
|
+
};
|
|
13338
13625
|
this.delete = async () => await this.client.deletePoll(this.id);
|
|
13339
13626
|
this.createOption = async (option) => await this.client.createPollOption(this.id, option);
|
|
13340
13627
|
this.updateOption = async (option) => await this.client.updatePollOption(this.id, option);
|
|
13341
13628
|
this.deleteOption = async (optionId) => await this.client.deletePollOption(this.id, optionId);
|
|
13629
|
+
/**
|
|
13630
|
+
* Casts a vote for an option. The vote is shown right away through a local placeholder vote,
|
|
13631
|
+
* which the server's vote replaces once the WS events of all pending vote changes arrived.
|
|
13632
|
+
* If the request fails, the change is reverted.
|
|
13633
|
+
*/
|
|
13342
13634
|
this.castVote = async (optionId, messageId) => {
|
|
13343
13635
|
const { max_votes_allowed, ownVotesByOptionId } = this.data;
|
|
13344
13636
|
const reachedVoteLimit = max_votes_allowed && max_votes_allowed === Object.keys(ownVotesByOptionId).length;
|
|
@@ -13355,11 +13647,14 @@ var Poll = class {
|
|
|
13355
13647
|
});
|
|
13356
13648
|
return;
|
|
13357
13649
|
}
|
|
13358
|
-
return await this.
|
|
13359
|
-
option_id: optionId
|
|
13360
|
-
});
|
|
13650
|
+
return await this.optimisticVotes.castVote(optionId, messageId);
|
|
13361
13651
|
};
|
|
13362
|
-
|
|
13652
|
+
/**
|
|
13653
|
+
* Removes a vote. The vote is removed right away and restored if the request fails. Vote
|
|
13654
|
+
* requests are sent in order, so removing a vote whose cast is not confirmed yet uses the vote
|
|
13655
|
+
* id from the cast's response; if that cast failed, there is nothing to remove.
|
|
13656
|
+
*/
|
|
13657
|
+
this.removeVote = async (voteId, messageId) => await this.optimisticVotes.removeVote(voteId, messageId);
|
|
13363
13658
|
this.addAnswer = async (answerText, messageId) => await this.client.addPollAnswer(messageId, this.id, answerText);
|
|
13364
13659
|
this.removeAnswer = async (answerId, messageId) => await this.client.removePollVote(messageId, this.id, answerId);
|
|
13365
13660
|
this.queryAnswers = async (params) => await this.client.queryPollAnswers(
|
|
@@ -13377,24 +13672,12 @@ var Poll = class {
|
|
|
13377
13672
|
this.client = client;
|
|
13378
13673
|
this.id = poll.id;
|
|
13379
13674
|
this.state = new StateStore(this.getInitialStateFromPollResponse(poll));
|
|
13675
|
+
this.optimisticVotes = new OptimisticPollVotes({ client: this.client, poll: this });
|
|
13380
13676
|
}
|
|
13381
13677
|
get data() {
|
|
13382
13678
|
return this.state.getLatestValue();
|
|
13383
13679
|
}
|
|
13384
13680
|
};
|
|
13385
|
-
function getMaxVotedOptionIds(voteCountsByOption) {
|
|
13386
|
-
let maxVotes = 0;
|
|
13387
|
-
let winningOptions = [];
|
|
13388
|
-
for (const [id, count] of Object.entries(voteCountsByOption ?? {})) {
|
|
13389
|
-
if (count > maxVotes) {
|
|
13390
|
-
winningOptions = [id];
|
|
13391
|
-
maxVotes = count;
|
|
13392
|
-
} else if (count === maxVotes) {
|
|
13393
|
-
winningOptions.push(id);
|
|
13394
|
-
}
|
|
13395
|
-
}
|
|
13396
|
-
return winningOptions;
|
|
13397
|
-
}
|
|
13398
13681
|
function getOwnVotesByOptionId(ownVotes) {
|
|
13399
13682
|
return !ownVotes ? {} : ownVotes.reduce((acc, vote) => {
|
|
13400
13683
|
if (isVoteAnswer(vote) || !vote.option_id) return acc;
|
|
@@ -13416,7 +13699,7 @@ function extractPollData(pollResponse) {
|
|
|
13416
13699
|
voting_visibility: pollResponse.voting_visibility
|
|
13417
13700
|
};
|
|
13418
13701
|
}
|
|
13419
|
-
function mapPollStateToResponse(poll) {
|
|
13702
|
+
function mapPollStateToResponse(poll, state = poll.data) {
|
|
13420
13703
|
const {
|
|
13421
13704
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
13422
13705
|
lastActivityAt,
|
|
@@ -13425,7 +13708,7 @@ function mapPollStateToResponse(poll) {
|
|
|
13425
13708
|
ownVotesByOptionId,
|
|
13426
13709
|
ownAnswer,
|
|
13427
13710
|
...restState
|
|
13428
|
-
} =
|
|
13711
|
+
} = state;
|
|
13429
13712
|
const ownVotes = [
|
|
13430
13713
|
...Object.values(ownVotesByOptionId),
|
|
13431
13714
|
...ownAnswer ? [ownAnswer] : []
|
|
@@ -15210,7 +15493,7 @@ var StreamChat = class _StreamChat {
|
|
|
15210
15493
|
return this.userAgent;
|
|
15211
15494
|
}
|
|
15212
15495
|
if (!this.cachedUserAgent) {
|
|
15213
|
-
const version = "9.
|
|
15496
|
+
const version = "9.54.1";
|
|
15214
15497
|
const { name: sdkName, version: sdkVersion } = this.sdkIdentifier ?? {};
|
|
15215
15498
|
const { name: appName, version: appVersion } = this.appIdentifier ?? {};
|
|
15216
15499
|
const { os, model: deviceModel } = this.deviceIdentifier ?? {};
|
|
@@ -16799,7 +17082,7 @@ var StreamChat = class _StreamChat {
|
|
|
16799
17082
|
* @param {string} messageId
|
|
16800
17083
|
* @param {string} language
|
|
16801
17084
|
*
|
|
16802
|
-
* @return {MessageResponse} Response that includes the message
|
|
17085
|
+
* @return {APIResponse & { message: MessageResponse }} Response that includes the translated message
|
|
16803
17086
|
*/
|
|
16804
17087
|
async translateMessage(messageId, language) {
|
|
16805
17088
|
return await this.post(
|