Skip to content

Commit

Permalink
Merge branch 'master' into pin
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton authored Oct 31, 2024
2 parents eb4a180 + 1c064e5 commit 79f6643
Show file tree
Hide file tree
Showing 15 changed files with 1,997 additions and 243 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

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.

## [8.42.0](https://github.com/GetStream/stream-chat-js/compare/v8.41.1...v8.42.0) (2024-10-30)


### Features

* polls rewrite ([#1373](https://github.com/GetStream/stream-chat-js/issues/1373)) ([7c56d21](https://github.com/GetStream/stream-chat-js/commit/7c56d219141aabe1ca0fc45af30b59db91ab4a82))

### [8.41.1](https://github.com/GetStream/stream-chat-js/compare/v8.41.0...v8.41.1) (2024-10-28)


### Bug Fixes

* change subscribeWithSelector API declaration ([#1382](https://github.com/GetStream/stream-chat-js/issues/1382)) ([5b1c7e5](https://github.com/GetStream/stream-chat-js/commit/5b1c7e5b6372259db64c36719c53883be1e1f409))

## [8.41.0](https://github.com/GetStream/stream-chat-js/compare/v8.40.9...v8.41.0) (2024-10-24)

### [8.40.9](https://github.com/GetStream/stream-chat-js/compare/v8.40.8...v8.40.9) (2024-09-19)


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stream-chat",
"version": "8.40.9",
"version": "8.42.0",
"description": "JS SDK for the Stream Chat API",
"author": "GetStream",
"homepage": "https://getstream.io/chat/",
Expand Down
27 changes: 2 additions & 25 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,8 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
}),
};

this.getClient().polls.hydratePollCache(messageSet.messages, true);

const areCapabilitiesChanged =
[...(state.channel.own_capabilities || [])].sort().join() !==
[...(Array.isArray(this.data?.own_capabilities) ? (this.data?.own_capabilities as string[]) : [])].sort().join();
Expand Down Expand Up @@ -1507,31 +1509,6 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
};
}
break;
case 'poll.updated':
if (event.poll) {
channelState.updatePoll(event.poll, event.message?.id || '');
}
break;
case 'poll.vote_casted':
if (event.poll_vote && event.poll) {
channelState.addPollVote(event.poll_vote, event.poll, event.message?.id || '');
}
break;
case 'poll.vote_changed':
if (event.poll_vote && event.poll) {
channelState.updatePollVote(event.poll_vote, event.poll, event.message?.id || '');
}
break;
case 'poll.vote_removed':
if (event.poll_vote && event.poll) {
channelState.removePollVote(event.poll_vote, event.poll, event.message?.id || '');
}
break;
case 'poll.closed':
if (event.message) {
channelState.addMessageSorted(event.message, false, false);
}
break;
case 'reaction.new':
if (event.message && event.reaction) {
event.message = channelState.addReaction(event.reaction, event.message);
Expand Down
92 changes: 0 additions & 92 deletions src/channel_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
MessageSet,
MessageSetType,
PendingMessageResponse,
PollResponse,
PollVote,
ReactionResponse,
UserResponse,
} from './types';
Expand Down Expand Up @@ -493,96 +491,6 @@ export class ChannelState<StreamChatGenerics extends ExtendableGenerics = Defaul
return { removed: result.length < msgArray.length, result };
};

// this handles the case when vote on poll is changed
updatePollVote = (
pollVote: PollVote<StreamChatGenerics>,
poll: PollResponse<StreamChatGenerics>,
messageId: string,
) => {
const message = this.findMessage(messageId);
if (!message) return;

if (message.poll_id !== pollVote.poll_id) return;

const updatedPoll = { ...poll };
let ownVotes = [...(message.poll?.own_votes || [])];

if (pollVote.user_id === this._channel.getClient().userID) {
if (pollVote.option_id && poll.enforce_unique_vote) {
// remove all previous votes where option_id is not empty
ownVotes = ownVotes.filter((vote) => !vote.option_id);
} else if (pollVote.answer_text) {
// remove all previous votes where option_id is empty
ownVotes = ownVotes.filter((vote) => vote.answer_text);
}

ownVotes.push(pollVote);
}

updatedPoll.own_votes = ownVotes as PollVote<StreamChatGenerics>[];
const newMessage = { ...message, poll: updatedPoll };

this.addMessageSorted((newMessage as unknown) as MessageResponse<StreamChatGenerics>, false, false);
};

addPollVote = (pollVote: PollVote<StreamChatGenerics>, poll: PollResponse<StreamChatGenerics>, messageId: string) => {
const message = this.findMessage(messageId);
if (!message) return;

if (message.poll_id !== pollVote.poll_id) return;

const updatedPoll = { ...poll };
const ownVotes = [...(message.poll?.own_votes || [])];

if (pollVote.user_id === this._channel.getClient().userID) {
ownVotes.push(pollVote);
}

updatedPoll.own_votes = ownVotes as PollVote<StreamChatGenerics>[];
const newMessage = { ...message, poll: updatedPoll };

this.addMessageSorted((newMessage as unknown) as MessageResponse<StreamChatGenerics>, false, false);
};

removePollVote = (
pollVote: PollVote<StreamChatGenerics>,
poll: PollResponse<StreamChatGenerics>,
messageId: string,
) => {
const message = this.findMessage(messageId);
if (!message) return;

if (message.poll_id !== pollVote.poll_id) return;

const updatedPoll = { ...poll };
const ownVotes = [...(message.poll?.own_votes || [])];
if (pollVote.user_id === this._channel.getClient().userID) {
const index = ownVotes.findIndex((vote) => vote.option_id === pollVote.option_id);
if (index > -1) {
ownVotes.splice(index, 1);
}
}

updatedPoll.own_votes = ownVotes as PollVote<StreamChatGenerics>[];

const newMessage = { ...message, poll: updatedPoll };
this.addMessageSorted((newMessage as unknown) as MessageResponse<StreamChatGenerics>, false, false);
};

updatePoll = (poll: PollResponse<StreamChatGenerics>, messageId: string) => {
const message = this.findMessage(messageId);
if (!message) return;

const updatedPoll = {
...poll,
own_votes: [...(message.poll?.own_votes || [])],
};

const newMessage = { ...message, poll: updatedPoll };

this.addMessageSorted((newMessage as unknown) as MessageResponse<StreamChatGenerics>, false, false);
};

/**
* Updates the message.user property with updated user object, for messages.
*
Expand Down
Loading

0 comments on commit 79f6643

Please sign in to comment.