@@ -11,6 +11,8 @@ import {
11
11
ReactionResponse ,
12
12
UserResponse ,
13
13
PendingMessageResponse ,
14
+ PollVote ,
15
+ PollResponse ,
14
16
} from './types' ;
15
17
import { addToMessageList } from './utils' ;
16
18
@@ -486,6 +488,96 @@ export class ChannelState<StreamChatGenerics extends ExtendableGenerics = Defaul
486
488
return { removed : result . length < msgArray . length , result } ;
487
489
} ;
488
490
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
+
489
581
/**
490
582
* Updates the message.user property with updated user object, for messages.
491
583
*
0 commit comments