Skip to content

Commit

Permalink
Updated handling for anonymous answers in polls (#3236)
Browse files Browse the repository at this point in the history
Co-authored-by: Toomas Vahter <[email protected]>
  • Loading branch information
martinmitrevski and laevandus authored Jun 6, 2024
1 parent ccf9b01 commit bd06e34
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
3 changes: 1 addition & 2 deletions Sources/StreamChat/Database/DTOs/PollVoteDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion Sources/StreamChat/Database/DatabaseSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit bd06e34

Please sign in to comment.