Skip to content

Commit 8df75d4

Browse files
committed
Merge remote-tracking branch 'origin/master' into user_block1
2 parents 8a7a23f + 0d5f87f commit 8df75d4

File tree

8 files changed

+751
-12
lines changed

8 files changed

+751
-12
lines changed

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
## [8.27.0](https://github.com/GetStream/stream-chat-js/compare/v8.26.0...v8.27.0) (2024-04-24)
6+
7+
8+
### Features
9+
10+
* implement queryReactions ([#1279](https://github.com/GetStream/stream-chat-js/issues/1279)) ([ef21c10](https://github.com/GetStream/stream-chat-js/commit/ef21c1042ab9982600c946bb3a965fde3bdaf0da))
11+
12+
## [8.26.0](https://github.com/GetStream/stream-chat-js/compare/v8.25.1...v8.26.0) (2024-04-12)
13+
14+
15+
### Features
16+
17+
* polls feature endpoints ([#1269](https://github.com/GetStream/stream-chat-js/issues/1269)) ([1d81480](https://github.com/GetStream/stream-chat-js/commit/1d8148072af1d899955a3d4b1e9b1957322961ed))
18+
519
### [8.25.1](https://github.com/GetStream/stream-chat-js/compare/v8.25.0...v8.25.1) (2024-03-28)
620

721

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "stream-chat",
3-
"version": "8.25.1",
3+
"version": "8.27.0",
44
"description": "JS SDK for the Stream Chat API",
55
"author": "GetStream",
66
"homepage": "https://getstream.io/chat/",

src/channel.ts

+40
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
UserFilters,
5454
UserResponse,
5555
QueryChannelAPIResponse,
56+
PollVoteData,
5657
SendMessageOptions,
5758
} from './types';
5859
import { Role } from './permissions';
@@ -1158,6 +1159,20 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
11581159
return await this.getClient().post<CreateCallResponse>(this._channelURL() + '/call', options);
11591160
}
11601161

1162+
/**
1163+
* Cast or cancel one or more votes on a poll
1164+
* @param pollId string The poll id
1165+
* @param votes PollVoteData[] The votes that will be casted (or canceled in case of an empty array)
1166+
* @returns {APIResponse & PollVoteResponse} The poll votes
1167+
*/
1168+
async vote(messageId: string, pollId: string, vote: PollVoteData) {
1169+
return await this.getClient().castPollVote(messageId, pollId, vote);
1170+
}
1171+
1172+
async removeVote(messageId: string, pollId: string, voteId: string) {
1173+
return await this.getClient().removePollVote(messageId, pollId, voteId);
1174+
}
1175+
11611176
/**
11621177
* on - Listen to events on this channel.
11631178
*
@@ -1401,6 +1416,31 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
14011416
};
14021417
}
14031418
break;
1419+
case 'poll.updated':
1420+
if (event.poll) {
1421+
channelState.updatePoll(event.poll, event.message?.id || '');
1422+
}
1423+
break;
1424+
case 'poll.vote_casted':
1425+
if (event.poll_vote && event.poll) {
1426+
channelState.addPollVote(event.poll_vote, event.poll, event.message?.id || '');
1427+
}
1428+
break;
1429+
case 'poll.vote_changed':
1430+
if (event.poll_vote && event.poll) {
1431+
channelState.updatePollVote(event.poll_vote, event.poll, event.message?.id || '');
1432+
}
1433+
break;
1434+
case 'poll.vote_removed':
1435+
if (event.poll_vote && event.poll) {
1436+
channelState.removePollVote(event.poll_vote, event.poll, event.message?.id || '');
1437+
}
1438+
break;
1439+
case 'poll.closed':
1440+
if (event.message) {
1441+
channelState.addMessageSorted(event.message, false, false);
1442+
}
1443+
break;
14041444
case 'reaction.new':
14051445
if (event.message && event.reaction) {
14061446
event.message = channelState.addReaction(event.reaction, event.message);

src/channel_state.ts

+92
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
ReactionResponse,
1212
UserResponse,
1313
PendingMessageResponse,
14+
PollVote,
15+
PollResponse,
1416
} from './types';
1517
import { addToMessageList } from './utils';
1618

@@ -486,6 +488,96 @@ export class ChannelState<StreamChatGenerics extends ExtendableGenerics = Defaul
486488
return { removed: result.length < msgArray.length, result };
487489
};
488490

491+
// this handles the case when vote on poll is changed
492+
updatePollVote = (
493+
pollVote: PollVote<StreamChatGenerics>,
494+
poll: PollResponse<StreamChatGenerics>,
495+
messageId: string,
496+
) => {
497+
const message = this.findMessage(messageId);
498+
if (!message) return;
499+
500+
if (message.poll_id !== pollVote.poll_id) return;
501+
502+
const updatedPoll = { ...poll };
503+
let ownVotes = [...(message.poll?.own_votes || [])];
504+
505+
if (pollVote.user_id === this._channel.getClient().userID) {
506+
if (pollVote.option_id && poll.enforce_unique_vote) {
507+
// remove all previous votes where option_id is not empty
508+
ownVotes = ownVotes.filter((vote) => !vote.option_id);
509+
} else if (pollVote.answer_text) {
510+
// remove all previous votes where option_id is empty
511+
ownVotes = ownVotes.filter((vote) => vote.answer_text);
512+
}
513+
514+
ownVotes.push(pollVote);
515+
}
516+
517+
updatedPoll.own_votes = ownVotes as PollVote<StreamChatGenerics>[];
518+
const newMessage = { ...message, poll: updatedPoll };
519+
520+
this.addMessageSorted((newMessage as unknown) as MessageResponse<StreamChatGenerics>, false, false);
521+
};
522+
523+
addPollVote = (pollVote: PollVote<StreamChatGenerics>, poll: PollResponse<StreamChatGenerics>, messageId: string) => {
524+
const message = this.findMessage(messageId);
525+
if (!message) return;
526+
527+
if (message.poll_id !== pollVote.poll_id) return;
528+
529+
const updatedPoll = { ...poll };
530+
const ownVotes = [...(message.poll?.own_votes || [])];
531+
532+
if (pollVote.user_id === this._channel.getClient().userID) {
533+
ownVotes.push(pollVote);
534+
}
535+
536+
updatedPoll.own_votes = ownVotes as PollVote<StreamChatGenerics>[];
537+
const newMessage = { ...message, poll: updatedPoll };
538+
539+
this.addMessageSorted((newMessage as unknown) as MessageResponse<StreamChatGenerics>, false, false);
540+
};
541+
542+
removePollVote = (
543+
pollVote: PollVote<StreamChatGenerics>,
544+
poll: PollResponse<StreamChatGenerics>,
545+
messageId: string,
546+
) => {
547+
const message = this.findMessage(messageId);
548+
if (!message) return;
549+
550+
if (message.poll_id !== pollVote.poll_id) return;
551+
552+
const updatedPoll = { ...poll };
553+
const ownVotes = [...(message.poll?.own_votes || [])];
554+
if (pollVote.user_id === this._channel.getClient().userID) {
555+
const index = ownVotes.findIndex((vote) => vote.option_id === pollVote.option_id);
556+
if (index > -1) {
557+
ownVotes.splice(index, 1);
558+
}
559+
}
560+
561+
updatedPoll.own_votes = ownVotes as PollVote<StreamChatGenerics>[];
562+
563+
const newMessage = { ...message, poll: updatedPoll };
564+
this.addMessageSorted((newMessage as unknown) as MessageResponse<StreamChatGenerics>, false, false);
565+
};
566+
567+
updatePoll = (poll: PollResponse<StreamChatGenerics>, messageId: string) => {
568+
const message = this.findMessage(messageId);
569+
if (!message) return;
570+
571+
const updatedPoll = {
572+
...poll,
573+
own_votes: [...(message.poll?.own_votes || [])],
574+
};
575+
576+
const newMessage = { ...message, poll: updatedPoll };
577+
578+
this.addMessageSorted((newMessage as unknown) as MessageResponse<StreamChatGenerics>, false, false);
579+
};
580+
489581
/**
490582
* Updates the message.user property with updated user object, for messages.
491583
*

0 commit comments

Comments
 (0)