From bd06e34e4a0cb96d048bfee64df62b2baf189688 Mon Sep 17 00:00:00 2001 From: Martin Mitrevski Date: Thu, 6 Jun 2024 14:57:41 +0200 Subject: [PATCH] Updated handling for anonymous answers in polls (#3236) Co-authored-by: Toomas Vahter --- .../Controllers/PollController/PollController.swift | 6 +++++- .../PollController/PollVoteListController.swift | 12 ++++++++---- Sources/StreamChat/Database/DTOs/PollVoteDTO.swift | 3 +-- Sources/StreamChat/Database/DatabaseSession.swift | 9 ++++++++- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/Sources/StreamChat/Controllers/PollController/PollController.swift b/Sources/StreamChat/Controllers/PollController/PollController.swift index 6e92e905fa0..fe051d98ca0 100644 --- a/Sources/StreamChat/Controllers/PollController/PollController.swift +++ b/Sources/StreamChat/Controllers/PollController/PollController.swift @@ -174,7 +174,11 @@ public class PollController: DataController, DelegateCallable, DataStoreProvider var deleteExistingVotes = [PollVote]() if poll?.enforceUniqueVote == true && !ownVotes.isEmpty { - deleteExistingVotes = Array(ownVotes) + if optionId != nil { + deleteExistingVotes = Array(ownVotes.filter { !$0.isAnswer }) + } else { + deleteExistingVotes = Array(ownVotes.filter { $0.isAnswer }) + } } pollsRepository.castPollVote( diff --git a/Sources/StreamChat/Controllers/PollController/PollVoteListController.swift b/Sources/StreamChat/Controllers/PollController/PollVoteListController.swift index b31eade2d31..2f204efac0a 100644 --- a/Sources/StreamChat/Controllers/PollController/PollVoteListController.swift +++ b/Sources/StreamChat/Controllers/PollController/PollVoteListController.swift @@ -203,11 +203,15 @@ extension PollVoteListController { extension PollVoteListController: EventsControllerDelegate { public func eventsController(_ controller: EventsController, didReceiveEvent event: any Event) { + var vote: PollVote? if let event = event as? PollVoteCastedEvent { - let vote = event.vote - if vote.isAnswer == true && query.pollId == vote.pollId && query.optionId == nil { - pollsRepository.link(pollVote: vote, to: query) - } + vote = event.vote + } else if let event = event as? PollVoteChangedEvent { + vote = event.vote + } + guard let vote else { return } + if vote.isAnswer == true && query.pollId == vote.pollId && query.optionId == nil { + pollsRepository.link(pollVote: vote, to: query) } } } diff --git a/Sources/StreamChat/Database/DTOs/PollVoteDTO.swift b/Sources/StreamChat/Database/DTOs/PollVoteDTO.swift index 36011f58e2d..488b96c7c0a 100644 --- a/Sources/StreamChat/Database/DTOs/PollVoteDTO.swift +++ b/Sources/StreamChat/Database/DTOs/PollVoteDTO.swift @@ -157,8 +157,7 @@ extension NSManagedObjectContext { if let userId { userDto = user(id: userId) } - let userSuffix = poll.votingVisibility == VotingVisibility.anonymous.rawValue ? nil : userId - let userId = userSuffix ?? "anon" + let userId = userId ?? "anon" let voteId = voteId ?? PollVoteDTO.localVoteId(optionId: optionId, pollId: pollId, userId: userId) let dto = PollVoteDTO.loadOrCreate( voteId: voteId, diff --git a/Sources/StreamChat/Database/DatabaseSession.swift b/Sources/StreamChat/Database/DatabaseSession.swift index b4e08b61b03..9f7fe4a1a8c 100644 --- a/Sources/StreamChat/Database/DatabaseSession.swift +++ b/Sources/StreamChat/Database/DatabaseSession.swift @@ -752,7 +752,14 @@ extension DatabaseSession { let votes = try pollVotes(for: userId, pollId: vote.pollId) for existing in votes { - if vote.id != existing.id { + if vote.id != existing.id && existing.isAnswer == false { + delete(pollVote: existing) + } + } + } else if vote.isAnswer == true { + let votes = try pollVotes(for: userId, pollId: vote.pollId) + for existing in votes { + if vote.id != existing.id && existing.isAnswer == true { delete(pollVote: existing) } }