Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Refreshing the poll when receiving pollEnd can break the chronological order in the store. #1776

Merged
merged 6 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions MatrixSDK/Aggregations/MXAggregations.m
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,6 @@ - (void)registerListener
[self.beaconAggregations handleBeaconWithEvent:event];
}
break;
case MXEventTypePollEnd:
[self.aggregatedPollsUpdater refreshPollAfter:event];
default:
break;
}
Expand Down
42 changes: 27 additions & 15 deletions MatrixSDK/Room/Polls/PollAggregator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class PollAggregator {
private var events: [MXEvent] = []
private var hasBeenEdited = false

public private(set) var poll: PollProtocol! {
public private(set) var poll: PollProtocol? {
didSet {
delegate?.pollAggregatorDidUpdateData(self)
}
Expand Down Expand Up @@ -88,10 +88,10 @@ public class PollAggregator {
throw PollAggregatorError.invalidPollStartEvent
}

try self.init(session: session, room: room, pollStartEventId: pollStartEventId, delegate: delegate)
self.init(session: session, room: room, pollStartEventId: pollStartEventId, delegate: delegate)
}

public init(session: MXSession, room: MXRoom, pollStartEventId: String, delegate: PollAggregatorDelegate? = nil) throws {
public init(session: MXSession, room: MXRoom, pollStartEventId: String, delegate: PollAggregatorDelegate? = nil) {
self.session = session
self.room = room
self.pollStartEventId = pollStartEventId
Expand All @@ -100,9 +100,11 @@ public class PollAggregator {

NotificationCenter.default.addObserver(self, selector: #selector(handleRoomDataFlush), name: .mxRoomDidFlushData, object: self.room)
setupEditListener()
try buildPollStartContent()
try? buildPollStartContent()

reloadPollData()
}

alfogrillo marked this conversation as resolved.
Show resolved Hide resolved
private func setupEditListener() {
editEventsListener = session.aggregations.listenToEditsUpdate(inRoom: self.room.roomId) { [weak self] event in
guard let self = self,
Expand All @@ -111,11 +113,7 @@ public class PollAggregator {
return
}

do {
try self.buildPollStartContent()
} catch {
self.delegate?.pollAggregator(self, didFailWithError: PollAggregatorError.invalidPollStartEvent)
}
try? self.buildPollStartContent()
}
}

Expand All @@ -137,10 +135,8 @@ public class PollAggregator {
events: events,
currentUserIdentifier: session.myUserId,
hasBeenEdited: hasBeenEdited)

reloadPollData()
}

alfogrillo marked this conversation as resolved.
Show resolved Hide resolved
@objc private func handleRoomDataFlush(sender: Notification) {
guard let room = sender.object as? MXRoom, room == self.room else {
return
Expand All @@ -153,7 +149,23 @@ public class PollAggregator {
delegate?.pollAggregatorDidStartLoading(self)

session.aggregations.referenceEvents(forEvent: pollStartEventId, inRoom: room.roomId, from: nil, limit: -1) { [weak self] response in
guard let self = self else {
guard let self else {
return
}

guard let event = response.originalEvent else {
// we didn't found the start event
self.delegate?.pollAggregator(self, didFailWithError: PollAggregatorError.invalidPollStartEvent)
return
}

if let pollStartEventContent = MXEventContentPollStart(fromJSON: event.content), pollStartEventContent.answerOptions.count >= Constants.minAnswerOptionCount {
pollStartedEvent = event
self.pollStartEventContent = pollStartEventContent
hasBeenEdited = (event.unsignedData.relations?.replace != nil)
} else {
// we were not able to decrypt the start event content
self.delegate?.pollAggregator(self, didFailWithError: PollAggregatorError.invalidPollStartEvent)
alfogrillo marked this conversation as resolved.
Show resolved Hide resolved
return
}

Expand All @@ -179,7 +191,7 @@ public class PollAggregator {
currentUserIdentifier: self.session.myUserId,
hasBeenEdited: self.hasBeenEdited)
} as Any

self.poll = self.pollBuilder.build(pollStartEventContent: self.pollStartEventContent,
pollStartEvent: self.pollStartedEvent,
events: self.events,
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-1776.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Poll: Refreshing the poll when receiving pollEnd can break the chronological order in the store.