Skip to content

Commit

Permalink
refactor: remove ownVotes from Poll state
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed Oct 23, 2024
1 parent 734757c commit 0d62e10
Showing 1 changed file with 11 additions and 26 deletions.
37 changes: 11 additions & 26 deletions src/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ export type PollState<SCG extends ExtendableGenerics = DefaultGenerics> = SCG['p
Omit<PollResponse<SCG>, 'own_votes' | 'id'> & {
lastActivityAt: Date; // todo: would be ideal to get this from the BE
maxVotedOptionIds: OptionId[];
ownVotes: PollVote<SCG>[];
ownVotesByOptionId: Record<OptionId, PollVote<SCG>>; // single user can vote only once for the same option
ownVotesByOptionId: Record<OptionId, PollVote<SCG>>;
ownAnswer?: PollAnswer; // each user can have only one answer
};

Expand Down Expand Up @@ -132,7 +131,6 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
),
ownAnswer,
ownVotesByOptionId: getOwnVotesByOptionId(ownVotes),
ownVotes,
};
};

Expand Down Expand Up @@ -163,7 +161,6 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
if (!isPollVoteCastedEvent(event)) return;
const currentState = this.data;
const isOwnVote = event.poll_vote.user_id === this.client.userID;
const ownVotes = [...(currentState?.ownVotes || [])];
let latestAnswers = [...(currentState.latest_answers as PollAnswer[])];
let ownAnswer = currentState.ownAnswer;
const ownVotesByOptionId = currentState.ownVotesByOptionId;
Expand All @@ -172,11 +169,8 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
if (isOwnVote) {
if (isVoteAnswer(event.poll_vote)) {
ownAnswer = event.poll_vote;
} else {
ownVotes.push(event.poll_vote);
if (event.poll_vote.option_id) {
ownVotesByOptionId[event.poll_vote.option_id] = event.poll_vote;
}
} else if (event.poll_vote.option_id) {
ownVotesByOptionId[event.poll_vote.option_id] = event.poll_vote;
}
}

Expand All @@ -193,7 +187,6 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
latest_answers: latestAnswers,
lastActivityAt: new Date(event.created_at),
ownAnswer,
ownVotes,
ownVotesByOptionId,
maxVotedOptionIds,
});
Expand All @@ -205,7 +198,6 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
if (!isPollVoteChangedEvent(event)) return;
const currentState = this.data;
const isOwnVote = event.poll_vote.user_id === this.client.userID;
let ownVotes = [...(currentState?.ownVotes || [])];
let latestAnswers = [...(currentState.latest_answers as PollAnswer[])];
let ownAnswer = currentState.ownAnswer;
let ownVotesByOptionId = currentState.ownVotesByOptionId;
Expand All @@ -217,12 +209,9 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
event.poll_vote,
...latestAnswers.filter((answer) => answer.user_id !== event.poll_vote.user_id),
];
ownVotes = ownVotes.filter((vote) => vote.id !== event.poll_vote.id);
ownAnswer = event.poll_vote;
} else {
// event.poll.enforce_unique_vote === true
ownVotes = [event.poll_vote];
ownVotesByOptionId = { [event.poll_vote.option_id!]: event.poll_vote };
} else if (event.poll_vote.option_id) {
ownVotesByOptionId = { [event.poll_vote.option_id]: event.poll_vote };

if (ownAnswer?.id === event.poll_vote.id) {
ownAnswer = undefined;
Expand All @@ -242,7 +231,6 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
latest_answers: latestAnswers,
lastActivityAt: new Date(event.created_at),
ownAnswer,
ownVotes,
ownVotesByOptionId,
maxVotedOptionIds,
});
Expand All @@ -253,23 +241,21 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
if (!isPollVoteRemovedEvent(event)) return;
const currentState = this.data;
const isOwnVote = event.poll_vote.user_id === this.client.userID;
let ownVotes = [...(currentState?.ownVotes || [])];
let latestAnswers = [...(currentState.latest_answers as PollAnswer[])];
let ownAnswer = currentState.ownAnswer;
const ownVotesByOptionId = { ...currentState.ownVotesByOptionId };
let maxVotedOptionIds = currentState.maxVotedOptionIds;

if (isOwnVote) {
ownVotes = ownVotes.filter((vote) => vote.id !== event.poll_vote.id);
if (event.poll_vote.option_id) {
delete ownVotesByOptionId[event.poll_vote.option_id];
}
}
if (isVoteAnswer(event.poll_vote)) {
latestAnswers = latestAnswers.filter((answer) => answer.id !== event.poll_vote.id);
ownAnswer = undefined;
if (isOwnVote) {
ownAnswer = undefined;
}
} else {
maxVotedOptionIds = getMaxVotedOptionIds(event.poll.vote_counts_by_option);
if (isOwnVote && event.poll_vote.option_id) {
delete ownVotesByOptionId[event.poll_vote.option_id];
}
}

const { latest_answers, own_votes, ...pollEnrichData } = extractPollEnrichedData(event.poll);
Expand All @@ -279,7 +265,6 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
latest_answers: latestAnswers,
lastActivityAt: new Date(event.created_at),
ownAnswer,
ownVotes,
ownVotesByOptionId,
maxVotedOptionIds,
});
Expand Down

0 comments on commit 0d62e10

Please sign in to comment.