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
package/dist/esm/index.mjs
CHANGED
|
@@ -5687,10 +5687,11 @@ var createLinkPreviewsCompositionMiddleware = (composer) => ({
|
|
|
5687
5687
|
const linkPreviews = linkPreviewsManager.loadingPreviews.length > 0 ? [] : linkPreviewsManager.loadedPreviews.map(
|
|
5688
5688
|
(preview) => LinkPreviewsManager.getPreviewData(preview)
|
|
5689
5689
|
);
|
|
5690
|
-
const
|
|
5690
|
+
const messageAttachments = (state.message.attachments ?? []).concat(
|
|
5691
5691
|
linkPreviews
|
|
5692
5692
|
);
|
|
5693
|
-
|
|
5693
|
+
const localAttachments = (state.localMessage.attachments ?? []).concat(linkPreviews);
|
|
5694
|
+
if (!messageAttachments.length && !localAttachments.length) return forward();
|
|
5694
5695
|
const sendOptions = { ...state.sendOptions };
|
|
5695
5696
|
const skip_enrich_url = !someLinkPreviewsLoading && linkPreviews.length > 0 || someLinkPreviewsDismissed;
|
|
5696
5697
|
if (skip_enrich_url) {
|
|
@@ -5698,14 +5699,14 @@ var createLinkPreviewsCompositionMiddleware = (composer) => ({
|
|
|
5698
5699
|
}
|
|
5699
5700
|
return next({
|
|
5700
5701
|
...state,
|
|
5701
|
-
message: {
|
|
5702
|
+
message: messageAttachments.length ? {
|
|
5702
5703
|
...state.message,
|
|
5703
|
-
attachments
|
|
5704
|
-
},
|
|
5705
|
-
localMessage: {
|
|
5704
|
+
attachments: messageAttachments
|
|
5705
|
+
} : state.message,
|
|
5706
|
+
localMessage: localAttachments.length ? {
|
|
5706
5707
|
...state.localMessage,
|
|
5707
|
-
attachments
|
|
5708
|
-
},
|
|
5708
|
+
attachments: localAttachments
|
|
5709
|
+
} : state.localMessage,
|
|
5709
5710
|
sendOptions
|
|
5710
5711
|
});
|
|
5711
5712
|
}
|
|
@@ -8087,10 +8088,13 @@ var Thread = class extends WithSubscriptions {
|
|
|
8087
8088
|
}));
|
|
8088
8089
|
}
|
|
8089
8090
|
};
|
|
8090
|
-
const
|
|
8091
|
-
|
|
8092
|
-
|
|
8093
|
-
|
|
8091
|
+
const channelResponse = threadData.channel;
|
|
8092
|
+
const channelData = { name: channelResponse.name };
|
|
8093
|
+
const channel = client.channel(
|
|
8094
|
+
threadData.channel.type,
|
|
8095
|
+
threadData.channel.id,
|
|
8096
|
+
channelData
|
|
8097
|
+
);
|
|
8094
8098
|
channel._hydrateMembers({
|
|
8095
8099
|
members: threadData.channel.members ?? [],
|
|
8096
8100
|
overrideCurrentState: false
|
|
@@ -11490,6 +11494,10 @@ var StableWSConnection = class {
|
|
|
11490
11494
|
if (data.type === "health.check") {
|
|
11491
11495
|
this.scheduleNextPing();
|
|
11492
11496
|
}
|
|
11497
|
+
if (data?.type === "connection.error" && data?.error?.code === chatCodes.TOKEN_EXPIRED) {
|
|
11498
|
+
this._log("onmessage() - server reported that the token expired", { wsID });
|
|
11499
|
+
this.tokenExpiredByServer = true;
|
|
11500
|
+
}
|
|
11493
11501
|
this.client.dispatchEvent(data);
|
|
11494
11502
|
this.scheduleConnectionCheck();
|
|
11495
11503
|
};
|
|
@@ -11513,7 +11521,7 @@ var StableWSConnection = class {
|
|
|
11513
11521
|
this.isConnecting = false;
|
|
11514
11522
|
this.rejectPromise?.(this._errorFromWSEvent(event));
|
|
11515
11523
|
this._log(`onclose() - WS connection closed. Calling reconnect ...`, { event });
|
|
11516
|
-
this.
|
|
11524
|
+
this._reconnectAfterFailure();
|
|
11517
11525
|
}
|
|
11518
11526
|
};
|
|
11519
11527
|
this.onerror = (wsID, event) => {
|
|
@@ -11524,7 +11532,7 @@ var StableWSConnection = class {
|
|
|
11524
11532
|
this.isConnecting = false;
|
|
11525
11533
|
this.rejectPromise?.(this._errorFromWSEvent(event));
|
|
11526
11534
|
this._log(`onerror() - WS connection resulted into error`, { event });
|
|
11527
|
-
this.
|
|
11535
|
+
this._reconnectAfterFailure();
|
|
11528
11536
|
};
|
|
11529
11537
|
/**
|
|
11530
11538
|
* _setHealth - Sets the connection to healthy or unhealthy.
|
|
@@ -11577,6 +11585,7 @@ var StableWSConnection = class {
|
|
|
11577
11585
|
*/
|
|
11578
11586
|
this._setupConnectionPromise = () => {
|
|
11579
11587
|
this.isResolved = false;
|
|
11588
|
+
this.tokenExpiredByServer = false;
|
|
11580
11589
|
this.connectionOpen = new Promise((resolve, reject) => {
|
|
11581
11590
|
this.resolvePromise = resolve;
|
|
11582
11591
|
this.rejectPromise = reject;
|
|
@@ -11624,6 +11633,7 @@ var StableWSConnection = class {
|
|
|
11624
11633
|
this.isHealthy = false;
|
|
11625
11634
|
this.wsID = 1;
|
|
11626
11635
|
this.lastEvent = null;
|
|
11636
|
+
this.tokenExpiredByServer = false;
|
|
11627
11637
|
this.pingInterval = 25 * 1e3;
|
|
11628
11638
|
this.connectionCheckTimeout = this.pingInterval + 10 * 1e3;
|
|
11629
11639
|
addConnectionEventListeners(this.onlineStatusChanged);
|
|
@@ -11722,6 +11732,7 @@ var StableWSConnection = class {
|
|
|
11722
11732
|
this.wsID += 1;
|
|
11723
11733
|
this.isConnecting = false;
|
|
11724
11734
|
this.isDisconnected = true;
|
|
11735
|
+
this.tokenExpiredByServer = false;
|
|
11725
11736
|
if (this.healthCheckTimeoutRef) {
|
|
11726
11737
|
clearInterval(this.healthCheckTimeoutRef);
|
|
11727
11738
|
}
|
|
@@ -11854,6 +11865,10 @@ var StableWSConnection = class {
|
|
|
11854
11865
|
this._destroyCurrentWSConnection();
|
|
11855
11866
|
if (options.refreshToken) {
|
|
11856
11867
|
await this.client.tokenManager.loadToken();
|
|
11868
|
+
if (this.isConnecting || this.isHealthy) {
|
|
11869
|
+
this._log("_reconnect() - Abort (4) since already connecting or healthy");
|
|
11870
|
+
return;
|
|
11871
|
+
}
|
|
11857
11872
|
}
|
|
11858
11873
|
try {
|
|
11859
11874
|
await this._connect();
|
|
@@ -11877,6 +11892,21 @@ var StableWSConnection = class {
|
|
|
11877
11892
|
}
|
|
11878
11893
|
this._log("_reconnect() - == END ==");
|
|
11879
11894
|
}
|
|
11895
|
+
/**
|
|
11896
|
+
* _reconnectAfterFailure - Reconnects after the socket closed or failed. If the server
|
|
11897
|
+
* reported that the token expired, the token is refreshed before reconnecting.
|
|
11898
|
+
* Static tokens cannot be refreshed and reconnect as before.
|
|
11899
|
+
*/
|
|
11900
|
+
_reconnectAfterFailure() {
|
|
11901
|
+
if (this.tokenExpiredByServer && !this.client.tokenManager.isStatic()) {
|
|
11902
|
+
this._log(
|
|
11903
|
+
"_reconnectAfterFailure() - token expired, so going to reload token and reconnect"
|
|
11904
|
+
);
|
|
11905
|
+
this._reconnect({ refreshToken: true });
|
|
11906
|
+
return;
|
|
11907
|
+
}
|
|
11908
|
+
this._reconnect();
|
|
11909
|
+
}
|
|
11880
11910
|
/**
|
|
11881
11911
|
* _destroyCurrentWSConnection - Removes the current WS connection
|
|
11882
11912
|
*
|
|
@@ -12934,6 +12964,242 @@ var ThreadManager = class extends WithSubscriptions {
|
|
|
12934
12964
|
}
|
|
12935
12965
|
};
|
|
12936
12966
|
|
|
12967
|
+
// src/poll_optimistic_votes.ts
|
|
12968
|
+
var CATCH_UP_TIMEOUT_MS = 3e3;
|
|
12969
|
+
var OptimisticPollVotes = class {
|
|
12970
|
+
constructor({ client, poll }) {
|
|
12971
|
+
// Own vote changes not confirmed by a WS event yet
|
|
12972
|
+
this.pendingCount = 0;
|
|
12973
|
+
// Own vote changes whose requests succeeded since the server's vote state was last shown
|
|
12974
|
+
this.succeededChanges = [];
|
|
12975
|
+
// Vote requests with this tag are sent one after another. A symbol, so it is unique per instance.
|
|
12976
|
+
this.requestTag = /* @__PURE__ */ Symbol("poll-votes");
|
|
12977
|
+
this.withServerVotes = (pollState) => ({
|
|
12978
|
+
...pollState,
|
|
12979
|
+
...this.serverVoteState
|
|
12980
|
+
});
|
|
12981
|
+
/**
|
|
12982
|
+
* Casts a vote for an option, shown right away through a placeholder vote. In polls with unique
|
|
12983
|
+
* votes, it replaces the existing own votes.
|
|
12984
|
+
*/
|
|
12985
|
+
this.castVote = (optionId, messageId) => {
|
|
12986
|
+
const sendRequest = () => this.client.castPollVote(messageId, this.poll.id, { option_id: optionId });
|
|
12987
|
+
const state = this.poll.data;
|
|
12988
|
+
if (state.ownVotesByOptionId[optionId]) return this.send(sendRequest);
|
|
12989
|
+
const replacedVotes = state.enforce_unique_vote ? Object.values(state.ownVotesByOptionId) : [];
|
|
12990
|
+
this.poll.state.partialNext(
|
|
12991
|
+
applyOwnVoteDelta(state, {
|
|
12992
|
+
add: [this.createPlaceholderVote(optionId)],
|
|
12993
|
+
remove: replacedVotes
|
|
12994
|
+
})
|
|
12995
|
+
);
|
|
12996
|
+
return this.track(sendRequest, (response) => ({
|
|
12997
|
+
optionId,
|
|
12998
|
+
vote: response.vote
|
|
12999
|
+
}));
|
|
13000
|
+
};
|
|
13001
|
+
// Removes a vote, right away.
|
|
13002
|
+
this.removeVote = (voteId, messageId) => {
|
|
13003
|
+
const sendRequest = (id) => this.client.removePollVote(messageId, this.poll.id, id);
|
|
13004
|
+
const state = this.poll.data;
|
|
13005
|
+
const vote = Object.values(state.ownVotesByOptionId).find(({ id }) => id === voteId);
|
|
13006
|
+
if (!vote?.option_id) return this.send(() => sendRequest(voteId));
|
|
13007
|
+
const optionId = vote.option_id;
|
|
13008
|
+
this.poll.state.partialNext(applyOwnVoteDelta(state, { remove: [vote] }));
|
|
13009
|
+
return this.track(
|
|
13010
|
+
// runs when the removal's turn comes, so a preceding cast of the vote has finished
|
|
13011
|
+
() => {
|
|
13012
|
+
const serverVoteId = this.getServerVoteId(vote);
|
|
13013
|
+
return serverVoteId ? sendRequest(serverVoteId) : Promise.resolve(void 0);
|
|
13014
|
+
},
|
|
13015
|
+
() => ({ optionId })
|
|
13016
|
+
);
|
|
13017
|
+
};
|
|
13018
|
+
// Sends a vote request once the previous one has finished, whether it succeeded or not.
|
|
13019
|
+
this.send = async (sendRequest) => {
|
|
13020
|
+
try {
|
|
13021
|
+
return await withoutConcurrency(this.requestTag, sendRequest);
|
|
13022
|
+
} finally {
|
|
13023
|
+
if (!hasPending(this.requestTag)) this.scheduleCatchUp();
|
|
13024
|
+
}
|
|
13025
|
+
};
|
|
13026
|
+
// Stores the vote state of a WS vote event and returns the vote fields to show, if any.
|
|
13027
|
+
this.handleVoteEvent = (poll, {
|
|
13028
|
+
confirmsOwnVote,
|
|
13029
|
+
ownVotesByOptionId
|
|
13030
|
+
}) => {
|
|
13031
|
+
this.serverVoteState = {
|
|
13032
|
+
latest_votes_by_option: poll.latest_votes_by_option,
|
|
13033
|
+
ownVotesByOptionId,
|
|
13034
|
+
vote_count: poll.vote_count,
|
|
13035
|
+
vote_counts_by_option: poll.vote_counts_by_option
|
|
13036
|
+
};
|
|
13037
|
+
if (confirmsOwnVote) this.pendingCount = Math.max(this.pendingCount - 1, 0);
|
|
13038
|
+
return this.pendingCount > 0 ? {} : this.takeServerVoteState();
|
|
13039
|
+
};
|
|
13040
|
+
// Fresh server state (e.g. a refetch) replaces everything, pending changes are dropped.
|
|
13041
|
+
this.reset = (pollState) => {
|
|
13042
|
+
this.serverVoteState = pickVoteState(pollState);
|
|
13043
|
+
this.pendingCount = 0;
|
|
13044
|
+
this.succeededChanges = [];
|
|
13045
|
+
clearTimeout(this.catchUpTimeout);
|
|
13046
|
+
};
|
|
13047
|
+
// Tracks an own vote change until its request settles. `sendRequest` resolves to undefined
|
|
13048
|
+
// when no request was needed.
|
|
13049
|
+
this.track = async (sendRequest, toChange) => {
|
|
13050
|
+
this.pendingCount += 1;
|
|
13051
|
+
clearTimeout(this.catchUpTimeout);
|
|
13052
|
+
let response;
|
|
13053
|
+
try {
|
|
13054
|
+
response = await this.send(async () => {
|
|
13055
|
+
const result = await sendRequest();
|
|
13056
|
+
if (result !== void 0) this.succeededChanges.push(toChange(result));
|
|
13057
|
+
return result;
|
|
13058
|
+
});
|
|
13059
|
+
} catch (error) {
|
|
13060
|
+
this.settleWithoutEvent();
|
|
13061
|
+
throw error;
|
|
13062
|
+
}
|
|
13063
|
+
if (response === void 0) this.settleWithoutEvent();
|
|
13064
|
+
return response;
|
|
13065
|
+
};
|
|
13066
|
+
// A failed or skipped request gets no WS event, so it counts as confirmed right away.
|
|
13067
|
+
this.settleWithoutEvent = () => {
|
|
13068
|
+
this.pendingCount = Math.max(this.pendingCount - 1, 0);
|
|
13069
|
+
if (this.pendingCount === 0) this.poll.state.partialNext(this.takeServerVoteState());
|
|
13070
|
+
};
|
|
13071
|
+
// The vote shown for a cast until the server's vote arrives
|
|
13072
|
+
this.createPlaceholderVote = (optionId) => {
|
|
13073
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
13074
|
+
return {
|
|
13075
|
+
created_at: now,
|
|
13076
|
+
id: `local-${generateUUIDv4()}`,
|
|
13077
|
+
option_id: optionId,
|
|
13078
|
+
poll_id: this.poll.id,
|
|
13079
|
+
updated_at: now,
|
|
13080
|
+
user: this.client.user,
|
|
13081
|
+
user_id: this.client.userID
|
|
13082
|
+
};
|
|
13083
|
+
};
|
|
13084
|
+
/**
|
|
13085
|
+
* The server id of a vote to remove, once everything sent before the removal has finished. Casts
|
|
13086
|
+
* and removals of an option alternate, so the option's last succeeded change tells: a cast is the
|
|
13087
|
+
* vote being removed (e.g. a placeholder), a removal means the cast in between failed. Without
|
|
13088
|
+
* one, the server's own vote of the option is the vote being removed.
|
|
13089
|
+
*/
|
|
13090
|
+
this.getServerVoteId = (vote) => {
|
|
13091
|
+
for (let i = this.succeededChanges.length - 1; i >= 0; i--) {
|
|
13092
|
+
const change = this.succeededChanges[i];
|
|
13093
|
+
if (change.optionId === vote.option_id) return change.vote?.id;
|
|
13094
|
+
}
|
|
13095
|
+
return this.serverVoteState.ownVotesByOptionId[vote.option_id]?.id;
|
|
13096
|
+
};
|
|
13097
|
+
this.takeServerVoteState = () => {
|
|
13098
|
+
clearTimeout(this.catchUpTimeout);
|
|
13099
|
+
this.succeededChanges = [];
|
|
13100
|
+
return {
|
|
13101
|
+
...this.serverVoteState,
|
|
13102
|
+
maxVotedOptionIds: getMaxVotedOptionIds(this.serverVoteState.vote_counts_by_option)
|
|
13103
|
+
};
|
|
13104
|
+
};
|
|
13105
|
+
this.scheduleCatchUp = () => {
|
|
13106
|
+
clearTimeout(this.catchUpTimeout);
|
|
13107
|
+
if (this.pendingCount === 0) return;
|
|
13108
|
+
this.catchUpTimeout = setTimeout(this.applySucceededChanges, CATCH_UP_TIMEOUT_MS);
|
|
13109
|
+
};
|
|
13110
|
+
/**
|
|
13111
|
+
* A WS event of an own vote change got lost, so the server's own votes are stale. All requests
|
|
13112
|
+
* have finished (the timeout starts once nothing is left to send and a new request cancels it),
|
|
13113
|
+
* so applying the succeeded changes in order gives the server's own votes (changes whose events
|
|
13114
|
+
* did arrive end up the same). They are shown with the vote counts of the last WS event, which
|
|
13115
|
+
* contain the other users' votes too.
|
|
13116
|
+
*/
|
|
13117
|
+
this.applySucceededChanges = () => {
|
|
13118
|
+
const { enforce_unique_vote } = this.poll.data;
|
|
13119
|
+
let ownVotesByOptionId = { ...this.serverVoteState.ownVotesByOptionId };
|
|
13120
|
+
for (const { optionId, vote } of this.succeededChanges) {
|
|
13121
|
+
if (!vote) {
|
|
13122
|
+
delete ownVotesByOptionId[optionId];
|
|
13123
|
+
} else if (enforce_unique_vote) {
|
|
13124
|
+
ownVotesByOptionId = { [optionId]: vote };
|
|
13125
|
+
} else {
|
|
13126
|
+
ownVotesByOptionId[optionId] = vote;
|
|
13127
|
+
}
|
|
13128
|
+
}
|
|
13129
|
+
this.serverVoteState = { ...this.serverVoteState, ownVotesByOptionId };
|
|
13130
|
+
this.pendingCount = 0;
|
|
13131
|
+
this.poll.state.partialNext(this.takeServerVoteState());
|
|
13132
|
+
};
|
|
13133
|
+
this.client = client;
|
|
13134
|
+
this.poll = poll;
|
|
13135
|
+
this.serverVoteState = pickVoteState(poll.data);
|
|
13136
|
+
}
|
|
13137
|
+
get serverOwnVotes() {
|
|
13138
|
+
return this.serverVoteState.ownVotesByOptionId;
|
|
13139
|
+
}
|
|
13140
|
+
};
|
|
13141
|
+
function pickVoteState(state) {
|
|
13142
|
+
return {
|
|
13143
|
+
latest_votes_by_option: state.latest_votes_by_option,
|
|
13144
|
+
ownVotesByOptionId: state.ownVotesByOptionId,
|
|
13145
|
+
vote_count: state.vote_count,
|
|
13146
|
+
vote_counts_by_option: state.vote_counts_by_option
|
|
13147
|
+
};
|
|
13148
|
+
}
|
|
13149
|
+
function applyOwnVoteDelta(state, { add = [], remove = [] }) {
|
|
13150
|
+
const ownVotesByOptionId = { ...state.ownVotesByOptionId };
|
|
13151
|
+
const voteCountsByOption = { ...state.vote_counts_by_option };
|
|
13152
|
+
const latestVotesByOption = state.latest_votes_by_option ? { ...state.latest_votes_by_option } : state.latest_votes_by_option;
|
|
13153
|
+
let voteCount = state.vote_count ?? 0;
|
|
13154
|
+
for (const vote of remove) {
|
|
13155
|
+
const optionId = vote.option_id;
|
|
13156
|
+
if (!optionId) continue;
|
|
13157
|
+
delete ownVotesByOptionId[optionId];
|
|
13158
|
+
const optionVoteCount = (voteCountsByOption[optionId] ?? 0) - 1;
|
|
13159
|
+
if (optionVoteCount > 0) {
|
|
13160
|
+
voteCountsByOption[optionId] = optionVoteCount;
|
|
13161
|
+
} else {
|
|
13162
|
+
delete voteCountsByOption[optionId];
|
|
13163
|
+
}
|
|
13164
|
+
voteCount = Math.max(voteCount - 1, 0);
|
|
13165
|
+
if (latestVotesByOption?.[optionId]) {
|
|
13166
|
+
latestVotesByOption[optionId] = latestVotesByOption[optionId].filter(
|
|
13167
|
+
(latestVote) => latestVote.id !== vote.id
|
|
13168
|
+
);
|
|
13169
|
+
}
|
|
13170
|
+
}
|
|
13171
|
+
for (const vote of add) {
|
|
13172
|
+
const optionId = vote.option_id;
|
|
13173
|
+
if (!optionId) continue;
|
|
13174
|
+
ownVotesByOptionId[optionId] = vote;
|
|
13175
|
+
voteCountsByOption[optionId] = (voteCountsByOption[optionId] ?? 0) + 1;
|
|
13176
|
+
voteCount += 1;
|
|
13177
|
+
if (latestVotesByOption && state.voting_visibility !== "anonymous") {
|
|
13178
|
+
latestVotesByOption[optionId] = [vote, ...latestVotesByOption[optionId] ?? []];
|
|
13179
|
+
}
|
|
13180
|
+
}
|
|
13181
|
+
return {
|
|
13182
|
+
latest_votes_by_option: latestVotesByOption,
|
|
13183
|
+
maxVotedOptionIds: getMaxVotedOptionIds(voteCountsByOption),
|
|
13184
|
+
ownVotesByOptionId,
|
|
13185
|
+
vote_count: voteCount,
|
|
13186
|
+
vote_counts_by_option: voteCountsByOption
|
|
13187
|
+
};
|
|
13188
|
+
}
|
|
13189
|
+
function getMaxVotedOptionIds(voteCountsByOption) {
|
|
13190
|
+
let maxVotes = 0;
|
|
13191
|
+
let winningOptions = [];
|
|
13192
|
+
for (const [id, count] of Object.entries(voteCountsByOption ?? {})) {
|
|
13193
|
+
if (count > maxVotes) {
|
|
13194
|
+
winningOptions = [id];
|
|
13195
|
+
maxVotes = count;
|
|
13196
|
+
} else if (count === maxVotes) {
|
|
13197
|
+
winningOptions.push(id);
|
|
13198
|
+
}
|
|
13199
|
+
}
|
|
13200
|
+
return winningOptions;
|
|
13201
|
+
}
|
|
13202
|
+
|
|
12937
13203
|
// src/poll.ts
|
|
12938
13204
|
var isPollUpdatedEvent = (e) => e.type === "poll.updated";
|
|
12939
13205
|
var isPollClosedEventEvent = (e) => e.type === "poll.closed";
|
|
@@ -12943,6 +13209,9 @@ var isPollVoteRemovedEvent = (e) => e.type === "poll.vote_removed";
|
|
|
12943
13209
|
var isVoteAnswer = (vote) => !!vote?.answer_text;
|
|
12944
13210
|
var Poll = class {
|
|
12945
13211
|
constructor({ client, poll }) {
|
|
13212
|
+
// Set whenever server data (WS event or rehydration) confirms that the poll is closed,
|
|
13213
|
+
// so that a failed optimistic close does not reopen a poll that is actually closed.
|
|
13214
|
+
this.closeConfirmed = false;
|
|
12946
13215
|
this.getInitialStateFromPollResponse = (poll) => {
|
|
12947
13216
|
const { own_votes, id, ...pollResponseForState } = poll;
|
|
12948
13217
|
const { ownAnswer, ownVotes } = own_votes?.reduce(
|
|
@@ -12966,25 +13235,55 @@ var Poll = class {
|
|
|
12966
13235
|
ownVotesByOptionId: getOwnVotesByOptionId(ownVotes)
|
|
12967
13236
|
};
|
|
12968
13237
|
};
|
|
13238
|
+
// persists only the server's votes, never the optimistic ones
|
|
12969
13239
|
this.upsertOfflineDb = () => {
|
|
13240
|
+
const serverState = this.optimisticVotes.withServerVotes(this.data);
|
|
12970
13241
|
this.client.offlineDb?.executeQuerySafely(
|
|
12971
|
-
(db) => db.upsertPoll({ poll: mapPollStateToResponse(this) }),
|
|
13242
|
+
(db) => db.upsertPoll({ poll: mapPollStateToResponse(this, serverState) }),
|
|
12972
13243
|
{ method: "upsertPoll" }
|
|
12973
13244
|
);
|
|
12974
13245
|
};
|
|
12975
13246
|
this.reinitializeState = (poll) => {
|
|
12976
|
-
|
|
13247
|
+
if (poll.is_closed) this.closeConfirmed = true;
|
|
13248
|
+
const initialState = this.getInitialStateFromPollResponse(poll);
|
|
13249
|
+
this.optimisticVotes.reset(initialState);
|
|
13250
|
+
this.state.partialNext(initialState);
|
|
13251
|
+
};
|
|
13252
|
+
// Hands the vote state of a WS vote event to the optimistic votes, which decide whether to show it.
|
|
13253
|
+
this.updateFromVoteEvent = (event, ownVotesByOptionId, answerState) => {
|
|
13254
|
+
const isOwnVote = event.poll_vote.user_id === this.client.userID;
|
|
13255
|
+
const shownVoteState = this.optimisticVotes.handleVoteEvent(event.poll, {
|
|
13256
|
+
confirmsOwnVote: isOwnVote && !isVoteAnswer(event.poll_vote) && !!event.poll_vote.option_id,
|
|
13257
|
+
ownVotesByOptionId
|
|
13258
|
+
});
|
|
13259
|
+
this.state.partialNext({
|
|
13260
|
+
...answerState,
|
|
13261
|
+
answers_count: event.poll.answers_count,
|
|
13262
|
+
lastActivityAt: new Date(event.created_at),
|
|
13263
|
+
...shownVoteState
|
|
13264
|
+
});
|
|
13265
|
+
this.upsertOfflineDb();
|
|
12977
13266
|
};
|
|
12978
13267
|
this.handlePollUpdated = (event) => {
|
|
12979
13268
|
if (event.poll?.id && event.poll.id !== this.id) return;
|
|
12980
13269
|
if (!isPollUpdatedEvent(event)) return;
|
|
12981
13270
|
const { id, ...pollData } = extractPollData(event.poll);
|
|
12982
|
-
|
|
13271
|
+
if (pollData.is_closed) this.closeConfirmed = true;
|
|
13272
|
+
this.state.partialNext({
|
|
13273
|
+
...pollData,
|
|
13274
|
+
// Server-owned translations, which `extractPollData` (an update request) leaves out. Copied
|
|
13275
|
+
// even when absent: the event carries the whole poll, and a change made under channel-only
|
|
13276
|
+
// translation drops the stale ones.
|
|
13277
|
+
description_i18n: event.poll.description_i18n,
|
|
13278
|
+
lastActivityAt: new Date(event.created_at),
|
|
13279
|
+
name_i18n: event.poll.name_i18n
|
|
13280
|
+
});
|
|
12983
13281
|
this.upsertOfflineDb();
|
|
12984
13282
|
};
|
|
12985
13283
|
this.handlePollClosed = (event) => {
|
|
12986
13284
|
if (event.poll?.id && event.poll.id !== this.id) return;
|
|
12987
13285
|
if (!isPollClosedEventEvent(event)) return;
|
|
13286
|
+
this.closeConfirmed = true;
|
|
12988
13287
|
this.state.partialNext({
|
|
12989
13288
|
is_closed: true,
|
|
12990
13289
|
lastActivityAt: new Date(event.created_at)
|
|
@@ -12998,8 +13297,7 @@ var Poll = class {
|
|
|
12998
13297
|
const isOwnVote = event.poll_vote.user_id === this.client.userID;
|
|
12999
13298
|
let latestAnswers = [...currentState.latest_answers];
|
|
13000
13299
|
let ownAnswer = currentState.ownAnswer;
|
|
13001
|
-
const ownVotesByOptionId =
|
|
13002
|
-
let maxVotedOptionIds = currentState.maxVotedOptionIds;
|
|
13300
|
+
const ownVotesByOptionId = { ...this.optimisticVotes.serverOwnVotes };
|
|
13003
13301
|
if (isOwnVote) {
|
|
13004
13302
|
if (isVoteAnswer(event.poll_vote)) {
|
|
13005
13303
|
ownAnswer = event.poll_vote;
|
|
@@ -13009,19 +13307,11 @@ var Poll = class {
|
|
|
13009
13307
|
}
|
|
13010
13308
|
if (isVoteAnswer(event.poll_vote)) {
|
|
13011
13309
|
latestAnswers = [event.poll_vote, ...latestAnswers];
|
|
13012
|
-
} else {
|
|
13013
|
-
maxVotedOptionIds = getMaxVotedOptionIds(event.poll.vote_counts_by_option);
|
|
13014
13310
|
}
|
|
13015
|
-
|
|
13016
|
-
this.state.partialNext({
|
|
13017
|
-
...pollEnrichData,
|
|
13311
|
+
this.updateFromVoteEvent(event, ownVotesByOptionId, {
|
|
13018
13312
|
latest_answers: latestAnswers,
|
|
13019
|
-
|
|
13020
|
-
ownAnswer,
|
|
13021
|
-
ownVotesByOptionId,
|
|
13022
|
-
maxVotedOptionIds
|
|
13313
|
+
ownAnswer
|
|
13023
13314
|
});
|
|
13024
|
-
this.upsertOfflineDb();
|
|
13025
13315
|
};
|
|
13026
13316
|
this.handleVoteChanged = (event) => {
|
|
13027
13317
|
if (event.poll?.id && event.poll.id !== this.id) return;
|
|
@@ -13030,8 +13320,7 @@ var Poll = class {
|
|
|
13030
13320
|
const isOwnVote = event.poll_vote.user_id === this.client.userID;
|
|
13031
13321
|
let latestAnswers = [...currentState.latest_answers];
|
|
13032
13322
|
let ownAnswer = currentState.ownAnswer;
|
|
13033
|
-
let ownVotesByOptionId =
|
|
13034
|
-
let maxVotedOptionIds = currentState.maxVotedOptionIds;
|
|
13323
|
+
let ownVotesByOptionId = this.optimisticVotes.serverOwnVotes;
|
|
13035
13324
|
if (isOwnVote) {
|
|
13036
13325
|
if (isVoteAnswer(event.poll_vote)) {
|
|
13037
13326
|
latestAnswers = [
|
|
@@ -13055,23 +13344,14 @@ var Poll = class {
|
|
|
13055
13344
|
if (ownAnswer?.id === event.poll_vote.id) {
|
|
13056
13345
|
ownAnswer = void 0;
|
|
13057
13346
|
}
|
|
13058
|
-
maxVotedOptionIds = getMaxVotedOptionIds(event.poll.vote_counts_by_option);
|
|
13059
13347
|
}
|
|
13060
13348
|
} else if (isVoteAnswer(event.poll_vote)) {
|
|
13061
13349
|
latestAnswers = [event.poll_vote, ...latestAnswers];
|
|
13062
|
-
} else {
|
|
13063
|
-
maxVotedOptionIds = getMaxVotedOptionIds(event.poll.vote_counts_by_option);
|
|
13064
13350
|
}
|
|
13065
|
-
|
|
13066
|
-
this.state.partialNext({
|
|
13067
|
-
...pollEnrichData,
|
|
13351
|
+
this.updateFromVoteEvent(event, ownVotesByOptionId, {
|
|
13068
13352
|
latest_answers: latestAnswers,
|
|
13069
|
-
|
|
13070
|
-
ownAnswer,
|
|
13071
|
-
ownVotesByOptionId,
|
|
13072
|
-
maxVotedOptionIds
|
|
13353
|
+
ownAnswer
|
|
13073
13354
|
});
|
|
13074
|
-
this.upsertOfflineDb();
|
|
13075
13355
|
};
|
|
13076
13356
|
this.handleVoteRemoved = (event) => {
|
|
13077
13357
|
if (event.poll?.id && event.poll.id !== this.id) return;
|
|
@@ -13080,29 +13360,19 @@ var Poll = class {
|
|
|
13080
13360
|
const isOwnVote = event.poll_vote.user_id === this.client.userID;
|
|
13081
13361
|
let latestAnswers = [...currentState.latest_answers];
|
|
13082
13362
|
let ownAnswer = currentState.ownAnswer;
|
|
13083
|
-
const ownVotesByOptionId = { ...
|
|
13084
|
-
let maxVotedOptionIds = currentState.maxVotedOptionIds;
|
|
13363
|
+
const ownVotesByOptionId = { ...this.optimisticVotes.serverOwnVotes };
|
|
13085
13364
|
if (isVoteAnswer(event.poll_vote)) {
|
|
13086
13365
|
latestAnswers = latestAnswers.filter((answer) => answer.id !== event.poll_vote.id);
|
|
13087
13366
|
if (isOwnVote) {
|
|
13088
13367
|
ownAnswer = void 0;
|
|
13089
13368
|
}
|
|
13090
|
-
} else {
|
|
13091
|
-
|
|
13092
|
-
if (isOwnVote && event.poll_vote.option_id) {
|
|
13093
|
-
delete ownVotesByOptionId[event.poll_vote.option_id];
|
|
13094
|
-
}
|
|
13369
|
+
} else if (isOwnVote && event.poll_vote.option_id) {
|
|
13370
|
+
delete ownVotesByOptionId[event.poll_vote.option_id];
|
|
13095
13371
|
}
|
|
13096
|
-
|
|
13097
|
-
this.state.partialNext({
|
|
13098
|
-
...pollEnrichData,
|
|
13372
|
+
this.updateFromVoteEvent(event, ownVotesByOptionId, {
|
|
13099
13373
|
latest_answers: latestAnswers,
|
|
13100
|
-
|
|
13101
|
-
ownAnswer,
|
|
13102
|
-
ownVotesByOptionId,
|
|
13103
|
-
maxVotedOptionIds
|
|
13374
|
+
ownAnswer
|
|
13104
13375
|
});
|
|
13105
|
-
this.upsertOfflineDb();
|
|
13106
13376
|
};
|
|
13107
13377
|
this.query = async (id) => {
|
|
13108
13378
|
const { poll } = await this.client.getPoll(id);
|
|
@@ -13111,11 +13381,33 @@ var Poll = class {
|
|
|
13111
13381
|
};
|
|
13112
13382
|
this.update = async (data) => await this.client.updatePoll({ ...data, id: this.id });
|
|
13113
13383
|
this.partialUpdate = async (partialPollObject) => await this.client.partialUpdatePoll(this.id, partialPollObject);
|
|
13114
|
-
|
|
13384
|
+
/**
|
|
13385
|
+
* Closes the poll. The poll is marked as closed optimistically and the change is
|
|
13386
|
+
* reverted if the request fails, unless the server confirmed the close meanwhile.
|
|
13387
|
+
* Does nothing if the poll is already closed.
|
|
13388
|
+
*/
|
|
13389
|
+
this.close = async () => {
|
|
13390
|
+
if (this.data.is_closed) return;
|
|
13391
|
+
this.closeConfirmed = false;
|
|
13392
|
+
this.state.partialNext({ is_closed: true });
|
|
13393
|
+
try {
|
|
13394
|
+
return await this.client.closePoll(this.id);
|
|
13395
|
+
} catch (error) {
|
|
13396
|
+
if (!this.closeConfirmed) {
|
|
13397
|
+
this.state.partialNext({ is_closed: false });
|
|
13398
|
+
}
|
|
13399
|
+
throw error;
|
|
13400
|
+
}
|
|
13401
|
+
};
|
|
13115
13402
|
this.delete = async () => await this.client.deletePoll(this.id);
|
|
13116
13403
|
this.createOption = async (option) => await this.client.createPollOption(this.id, option);
|
|
13117
13404
|
this.updateOption = async (option) => await this.client.updatePollOption(this.id, option);
|
|
13118
13405
|
this.deleteOption = async (optionId) => await this.client.deletePollOption(this.id, optionId);
|
|
13406
|
+
/**
|
|
13407
|
+
* Casts a vote for an option. The vote is shown right away through a local placeholder vote,
|
|
13408
|
+
* which the server's vote replaces once the WS events of all pending vote changes arrived.
|
|
13409
|
+
* If the request fails, the change is reverted.
|
|
13410
|
+
*/
|
|
13119
13411
|
this.castVote = async (optionId, messageId) => {
|
|
13120
13412
|
const { max_votes_allowed, ownVotesByOptionId } = this.data;
|
|
13121
13413
|
const reachedVoteLimit = max_votes_allowed && max_votes_allowed === Object.keys(ownVotesByOptionId).length;
|
|
@@ -13132,11 +13424,14 @@ var Poll = class {
|
|
|
13132
13424
|
});
|
|
13133
13425
|
return;
|
|
13134
13426
|
}
|
|
13135
|
-
return await this.
|
|
13136
|
-
option_id: optionId
|
|
13137
|
-
});
|
|
13427
|
+
return await this.optimisticVotes.castVote(optionId, messageId);
|
|
13138
13428
|
};
|
|
13139
|
-
|
|
13429
|
+
/**
|
|
13430
|
+
* Removes a vote. The vote is removed right away and restored if the request fails. Vote
|
|
13431
|
+
* requests are sent in order, so removing a vote whose cast is not confirmed yet uses the vote
|
|
13432
|
+
* id from the cast's response; if that cast failed, there is nothing to remove.
|
|
13433
|
+
*/
|
|
13434
|
+
this.removeVote = async (voteId, messageId) => await this.optimisticVotes.removeVote(voteId, messageId);
|
|
13140
13435
|
this.addAnswer = async (answerText, messageId) => await this.client.addPollAnswer(messageId, this.id, answerText);
|
|
13141
13436
|
this.removeAnswer = async (answerId, messageId) => await this.client.removePollVote(messageId, this.id, answerId);
|
|
13142
13437
|
this.queryAnswers = async (params) => await this.client.queryPollAnswers(
|
|
@@ -13154,24 +13449,12 @@ var Poll = class {
|
|
|
13154
13449
|
this.client = client;
|
|
13155
13450
|
this.id = poll.id;
|
|
13156
13451
|
this.state = new StateStore(this.getInitialStateFromPollResponse(poll));
|
|
13452
|
+
this.optimisticVotes = new OptimisticPollVotes({ client: this.client, poll: this });
|
|
13157
13453
|
}
|
|
13158
13454
|
get data() {
|
|
13159
13455
|
return this.state.getLatestValue();
|
|
13160
13456
|
}
|
|
13161
13457
|
};
|
|
13162
|
-
function getMaxVotedOptionIds(voteCountsByOption) {
|
|
13163
|
-
let maxVotes = 0;
|
|
13164
|
-
let winningOptions = [];
|
|
13165
|
-
for (const [id, count] of Object.entries(voteCountsByOption ?? {})) {
|
|
13166
|
-
if (count > maxVotes) {
|
|
13167
|
-
winningOptions = [id];
|
|
13168
|
-
maxVotes = count;
|
|
13169
|
-
} else if (count === maxVotes) {
|
|
13170
|
-
winningOptions.push(id);
|
|
13171
|
-
}
|
|
13172
|
-
}
|
|
13173
|
-
return winningOptions;
|
|
13174
|
-
}
|
|
13175
13458
|
function getOwnVotesByOptionId(ownVotes) {
|
|
13176
13459
|
return !ownVotes ? {} : ownVotes.reduce((acc, vote) => {
|
|
13177
13460
|
if (isVoteAnswer(vote) || !vote.option_id) return acc;
|
|
@@ -13193,7 +13476,7 @@ function extractPollData(pollResponse) {
|
|
|
13193
13476
|
voting_visibility: pollResponse.voting_visibility
|
|
13194
13477
|
};
|
|
13195
13478
|
}
|
|
13196
|
-
function mapPollStateToResponse(poll) {
|
|
13479
|
+
function mapPollStateToResponse(poll, state = poll.data) {
|
|
13197
13480
|
const {
|
|
13198
13481
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
13199
13482
|
lastActivityAt,
|
|
@@ -13202,7 +13485,7 @@ function mapPollStateToResponse(poll) {
|
|
|
13202
13485
|
ownVotesByOptionId,
|
|
13203
13486
|
ownAnswer,
|
|
13204
13487
|
...restState
|
|
13205
|
-
} =
|
|
13488
|
+
} = state;
|
|
13206
13489
|
const ownVotes = [
|
|
13207
13490
|
...Object.values(ownVotesByOptionId),
|
|
13208
13491
|
...ownAnswer ? [ownAnswer] : []
|
|
@@ -14987,7 +15270,7 @@ var StreamChat = class _StreamChat {
|
|
|
14987
15270
|
return this.userAgent;
|
|
14988
15271
|
}
|
|
14989
15272
|
if (!this.cachedUserAgent) {
|
|
14990
|
-
const version = "9.
|
|
15273
|
+
const version = "9.54.1";
|
|
14991
15274
|
const { name: sdkName, version: sdkVersion } = this.sdkIdentifier ?? {};
|
|
14992
15275
|
const { name: appName, version: appVersion } = this.appIdentifier ?? {};
|
|
14993
15276
|
const { os, model: deviceModel } = this.deviceIdentifier ?? {};
|
|
@@ -16576,7 +16859,7 @@ var StreamChat = class _StreamChat {
|
|
|
16576
16859
|
* @param {string} messageId
|
|
16577
16860
|
* @param {string} language
|
|
16578
16861
|
*
|
|
16579
|
-
* @return {MessageResponse} Response that includes the message
|
|
16862
|
+
* @return {APIResponse & { message: MessageResponse }} Response that includes the translated message
|
|
16580
16863
|
*/
|
|
16581
16864
|
async translateMessage(messageId, language) {
|
|
16582
16865
|
return await this.post(
|