diff --git a/packages/feeds-client/src/common/Poll.ts b/packages/feeds-client/src/common/Poll.ts index 948f90ff..2cb98f54 100644 --- a/packages/feeds-client/src/common/Poll.ts +++ b/packages/feeds-client/src/common/Poll.ts @@ -1,7 +1,7 @@ import { StateStore } from '@stream-io/state-store'; import type { FeedsClient } from '../feeds-client'; import type { - PollVote, + PollVoteResponseData, QueryPollVotesRequest, PollUpdatedFeedEvent, WSEvent, @@ -10,7 +10,6 @@ import type { PollVoteChangedFeedEvent, PollVoteRemovedFeedEvent, PollResponseData, - Poll as PollType, } from '../gen/models'; const isPollUpdatedEvent = ( @@ -34,7 +33,8 @@ const isPollVoteRemovedEvent = ( ): e is { type: 'feeds.poll.vote_removed' } & PollVoteRemovedFeedEvent => e.type === 'feeds.poll.vote_removed'; -export const isVoteAnswer = (vote: PollVote) => !!vote?.answer_text; +export const isVoteAnswer = (vote: PollVoteResponseData) => + !!vote?.answer_text; export type PollAnswersQueryParams = QueryPollVotesRequest & { poll_id: string; @@ -43,16 +43,16 @@ export type PollAnswersQueryParams = QueryPollVotesRequest & { type OptionId = string; -export type PollState = Omit & { +export type PollState = Omit & { last_activity_at: Date; max_voted_option_ids: OptionId[]; - own_votes_by_option_id: Record; - own_answer?: PollVote; // each user can have only one answer + own_votes_by_option_id: Record; + own_answer?: PollVoteResponseData; // each user can have only one answer }; type PollInitOptions = { client: FeedsClient; - poll: PollType; + poll: PollResponseData; }; export class StreamPoll { @@ -74,8 +74,8 @@ export class StreamPoll { ) => { const { own_votes, id, ...pollResponseForState } = poll; const { ownAnswer, ownVotes } = own_votes?.reduce<{ - ownVotes: PollVote[]; - ownAnswer?: PollVote; + ownVotes: PollVoteResponseData[]; + ownAnswer?: PollVoteResponseData; }>( (acc, voteOrAnswer) => { if (isVoteAnswer(voteOrAnswer)) { @@ -111,7 +111,6 @@ export class StreamPoll { if (event.poll?.id && event.poll.id !== this.id) return; if (!isPollUpdatedEvent(event as WSEvent)) return; const { id, ...pollData } = event.poll; - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed this.state.partialNext({ ...pollData, last_activity_at: new Date(event.created_at), @@ -140,19 +139,14 @@ export class StreamPoll { let maxVotedOptionIds = currentState.max_voted_option_ids; if (isOwnVote) { - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed if (isVoteAnswer(event.poll_vote)) { - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed ownAnswer = event.poll_vote; } else if (event.poll_vote.option_id) { - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed ownVotesByOptionId[event.poll_vote.option_id] = event.poll_vote; } } - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed if (isVoteAnswer(event.poll_vote)) { - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed latestAnswers = [event.poll_vote, ...latestAnswers]; } else { maxVotedOptionIds = getMaxVotedOptionIds( @@ -168,7 +162,6 @@ export class StreamPoll { } = event.poll; this.state.partialNext({ answers_count, - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed latest_votes_by_option, vote_count, vote_counts_by_option, @@ -194,22 +187,18 @@ export class StreamPoll { let maxVotedOptionIds = currentState.max_voted_option_ids; if (isOwnVote) { - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed if (isVoteAnswer(event.poll_vote)) { latestAnswers = [ - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed event.poll_vote, ...latestAnswers.filter((answer) => answer.id !== event.poll_vote.id), ]; - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed ownAnswer = event.poll_vote; } else if (event.poll_vote.option_id) { if (event.poll.enforce_unique_vote) { - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed ownVotesByOptionId = { [event.poll_vote.option_id]: event.poll_vote }; } else { ownVotesByOptionId = Object.entries(ownVotesByOptionId).reduce< - Record + Record >((acc, [optionId, vote]) => { if ( optionId !== event.poll_vote.option_id && @@ -220,7 +209,6 @@ export class StreamPoll { acc[optionId] = vote; return acc; }, {}); - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed ownVotesByOptionId[event.poll_vote.option_id] = event.poll_vote; } @@ -231,9 +219,7 @@ export class StreamPoll { event.poll.vote_counts_by_option, ); } - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed } else if (isVoteAnswer(event.poll_vote)) { - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed latestAnswers = [event.poll_vote, ...latestAnswers]; } else { maxVotedOptionIds = getMaxVotedOptionIds( @@ -249,7 +235,6 @@ export class StreamPoll { } = event.poll; this.state.partialNext({ answers_count, - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed latest_votes_by_option, vote_count, vote_counts_by_option, @@ -273,7 +258,6 @@ export class StreamPoll { const ownVotesByOptionId = { ...currentState.own_votes_by_option_id }; let maxVotedOptionIds = currentState.max_voted_option_ids; - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed if (isVoteAnswer(event.poll_vote)) { latestAnswers = latestAnswers.filter( (answer) => answer.id !== event.poll_vote.id, @@ -298,7 +282,6 @@ export class StreamPoll { } = event.poll; this.state.partialNext({ answers_count, - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed latest_votes_by_option, vote_count, vote_counts_by_option, @@ -327,10 +310,10 @@ function getMaxVotedOptionIds( return winningOptions; } -function getOwnVotesByOptionId(ownVotes: PollVote[]) { +function getOwnVotesByOptionId(ownVotes: PollVoteResponseData[]) { return !ownVotes - ? ({} satisfies Record) - : ownVotes.reduce>((acc, vote) => { + ? ({} satisfies Record) + : ownVotes.reduce>((acc, vote) => { if (isVoteAnswer(vote) || !vote.option_id) return acc; acc[vote.option_id] = vote; return acc; diff --git a/packages/feeds-client/src/feeds-client/feeds-client.ts b/packages/feeds-client/src/feeds-client/feeds-client.ts index 412d5204..f02ba47c 100644 --- a/packages/feeds-client/src/feeds-client/feeds-client.ts +++ b/packages/feeds-client/src/feeds-client/feeds-client.ts @@ -356,11 +356,9 @@ export class FeedsClient extends FeedsApi { const pollResponse = activity.poll; const pollFromCache = this.pollFromState(pollResponse.id); if (!pollFromCache) { - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed const poll = new StreamPoll({ client: this, poll: pollResponse }); this.polls_by_id.set(poll.id, poll); } else { - // @ts-expect-error Incompatibility between PollResponseData and Poll due to teams_role, remove when OpenAPI spec is fixed pollFromCache.reinitializeState(pollResponse); } }