Skip to content

Commit 2ef7e3d

Browse files
committed
fix: handle poll vote limit trespass with notification instead of removing the oldest vote
1 parent c8f6d7c commit 2ef7e3d

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

src/poll.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -324,20 +324,17 @@ export class Poll {
324324
max_votes_allowed && max_votes_allowed === Object.keys(ownVotesByOptionId).length;
325325

326326
if (reachedVoteLimit) {
327-
let oldestVote = Object.values(ownVotesByOptionId)[0];
328-
Object.values(ownVotesByOptionId)
329-
.slice(1)
330-
.forEach((vote) => {
331-
if (
332-
!oldestVote?.created_at ||
333-
new Date(vote.created_at) < new Date(oldestVote.created_at)
334-
) {
335-
oldestVote = vote;
336-
}
337-
});
338-
if (oldestVote?.id) {
339-
await this.removeVote(oldestVote.id, messageId);
340-
}
327+
this.client.notifications.addInfo({
328+
message: 'Reached the vote limit. Remove an existing vote first.',
329+
origin: {
330+
emitter: 'Poll',
331+
context: { messageId, optionId },
332+
},
333+
options: {
334+
type: 'validation:poll:castVote:limit',
335+
},
336+
});
337+
return;
341338
}
342339
return await this.client.castPollVote(messageId, this.id as string, {
343340
option_id: optionId,

test/unit/poll.test.js

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sinon from 'sinon';
22
import { Poll, StreamChat } from '../../src';
33

4-
import { describe, it, afterEach, expect } from 'vitest';
4+
import { describe, it, afterEach, expect, vi } from 'vitest';
55

66
const pollId = 'WD4SBRJvLoGwB4oAoCQGM';
77

@@ -701,26 +701,26 @@ describe('Poll', () => {
701701
getPollStub.restore();
702702
});
703703

704-
it('should remove oldest vote before casting a new one if reached max votes allowed', async () => {
704+
it('should publish a notification and not cast vote if reached max votes allowed', async () => {
705705
const poll = new Poll({
706706
client,
707707
poll: { ...pollResponse, max_votes_allowed: user2Votes.length },
708708
});
709709
const option_id = 'ba933470-c0da-4b6f-a4d2-d2176ac0d4a8';
710710
const messageId = 'XXX';
711-
const removePollVoteStub = sinon.stub(client, 'removePollVote');
712-
const castPollVoteStub = sinon.stub(client, 'castPollVote');
713-
removePollVoteStub.resolves('removed');
714-
castPollVoteStub.resolves({ vote: { id: 'vote1', option_id, user_id: 'user1' } });
711+
const removePollVoteSpy = vi
712+
.spyOn(client, 'removePollVote')
713+
.mockResolvedValue('removed');
714+
const castPollVoteSpy = vi
715+
.spyOn(client, 'castPollVote')
716+
.mockResolvedValue({ vote: { id: 'vote1', option_id, user_id: 'user1' } });
717+
const addInfoNotificationSpy = vi.spyOn(client.notifications, 'addInfo');
715718

716719
await poll.castVote(option_id, messageId);
717720

718-
expect(removePollVoteStub.calledWith(messageId, pollResponse.id, user1Votes[1].id)).to
719-
.be.true;
720-
expect(castPollVoteStub.calledWith(messageId, pollResponse.id, { option_id })).to.be
721-
.true;
722-
removePollVoteStub.restore();
723-
castPollVoteStub.restore();
721+
expect(removePollVoteSpy).not.toHaveBeenCalled();
722+
expect(castPollVoteSpy).not.toHaveBeenCalled();
723+
expect(addInfoNotificationSpy).toHaveBeenCalledTimes(1);
724724
});
725725

726726
it('should not remove oldest vote before casting a new one if not reached max votes allowed', async () => {
@@ -730,18 +730,21 @@ describe('Poll', () => {
730730
});
731731
const option_id = 'ba933470-c0da-4b6f-a4d2-d2176ac0d4a8';
732732
const messageId = 'XXX';
733-
const removePollVoteStub = sinon.stub(client, 'removePollVote');
734-
const castPollVoteStub = sinon.stub(client, 'castPollVote');
735-
removePollVoteStub.resolves('removed');
736-
castPollVoteStub.resolves({ vote: { id: 'vote1', option_id, user_id: 'user1' } });
733+
const removePollVoteSpy = vi
734+
.spyOn(client, 'removePollVote')
735+
.mockResolvedValue('removed');
736+
const castPollVoteSpy = vi
737+
.spyOn(client, 'castPollVote')
738+
.mockResolvedValue({ vote: { id: 'vote1', option_id, user_id: 'user1' } });
739+
const addInfoNotificationSpy = vi.spyOn(client.notifications, 'addInfo');
737740

738741
await poll.castVote(option_id, messageId);
739742

740-
expect(removePollVoteStub.called).to.be.false;
741-
expect(castPollVoteStub.calledWith(messageId, pollResponse.id, { option_id })).to.be
742-
.true;
743-
removePollVoteStub.restore();
744-
castPollVoteStub.restore();
743+
expect(removePollVoteSpy).not.toHaveBeenCalled();
744+
expect(castPollVoteSpy).toHaveBeenCalledWith(messageId, pollResponse.id, {
745+
option_id,
746+
});
747+
expect(addInfoNotificationSpy).not.toHaveBeenCalled();
745748
});
746749

747750
it('should not remove oldest vote before casting a new one if max_votes_allowed is not defined', async () => {
@@ -751,17 +754,20 @@ describe('Poll', () => {
751754
});
752755
const option_id = 'ba933470-c0da-4b6f-a4d2-d2176ac0d4a8';
753756
const messageId = 'XXX';
754-
const removePollVoteStub = sinon.stub(client, 'removePollVote');
755-
const castPollVoteStub = sinon.stub(client, 'castPollVote');
756-
removePollVoteStub.resolves('removed');
757-
castPollVoteStub.resolves({ vote: { id: 'vote1', option_id, user_id: 'user1' } });
757+
const removePollVoteSpy = vi
758+
.spyOn(client, 'removePollVote')
759+
.mockResolvedValue('removed');
760+
const castPollVoteSpy = vi
761+
.spyOn(client, 'castPollVote')
762+
.mockResolvedValue({ vote: { id: 'vote1', option_id, user_id: 'user1' } });
763+
const addInfoNotificationSpy = vi.spyOn(client.notifications, 'addInfo');
758764

759765
await poll.castVote(option_id, messageId);
760766

761-
expect(removePollVoteStub.called).to.be.false;
762-
expect(castPollVoteStub.calledWith(messageId, pollResponse.id, { option_id })).to.be
763-
.true;
764-
removePollVoteStub.restore();
765-
castPollVoteStub.restore();
767+
expect(removePollVoteSpy).not.toHaveBeenCalled();
768+
expect(castPollVoteSpy).toHaveBeenCalledWith(messageId, pollResponse.id, {
769+
option_id,
770+
});
771+
expect(addInfoNotificationSpy).not.toHaveBeenCalled();
766772
});
767773
});

0 commit comments

Comments
 (0)