From 487628e5d6529d89c9aedcc035e4ab4d95bd5c4c Mon Sep 17 00:00:00 2001 From: Alfonso Grillo Date: Wed, 28 Dec 2022 15:17:34 +0100 Subject: [PATCH 1/6] Add convenience init for PollAggregator --- MatrixSDK/Room/Polls/PollAggregator.swift | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/MatrixSDK/Room/Polls/PollAggregator.swift b/MatrixSDK/Room/Polls/PollAggregator.swift index e896631d55..134bfa480d 100644 --- a/MatrixSDK/Room/Polls/PollAggregator.swift +++ b/MatrixSDK/Room/Polls/PollAggregator.swift @@ -71,6 +71,25 @@ public class PollAggregator { } } + public convenience init(session: MXSession, room: MXRoom, pollEvent: MXEvent) throws { + var pollStartEventId: String? + + switch pollEvent.eventType { + case .pollStart: + pollStartEventId = pollEvent.eventId + case .pollEnd: + pollStartEventId = pollEvent.relatesTo.eventId + default: + pollStartEventId = nil + } + + guard let pollStartEventId = pollStartEventId else { + throw PollAggregatorError.invalidPollStartEvent + } + + try self.init(session: session, room: room, pollStartEventId: pollStartEventId) + } + public init(session: MXSession, room: MXRoom, pollStartEventId: String) throws { self.session = session self.room = room From b36bab689acb584ac4d4355f1b967ce1ba4f9acf Mon Sep 17 00:00:00 2001 From: Alfonso Grillo Date: Fri, 30 Dec 2022 11:04:14 +0100 Subject: [PATCH 2/6] Add reply support for poll.end --- MatrixSDK/Data/MXRoom.m | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/MatrixSDK/Data/MXRoom.m b/MatrixSDK/Data/MXRoom.m index 201e665931..1375389e42 100644 --- a/MatrixSDK/Data/MXRoom.m +++ b/MatrixSDK/Data/MXRoom.m @@ -2442,6 +2442,11 @@ - (BOOL)canReplyToEvent:(MXEvent *)eventToReply return YES; } + if(eventToReply.eventType == MXEventTypePollEnd) + { + return YES; + } + if(eventToReply.eventType == MXEventTypeBeaconInfo) { return YES; From 930eea4fa9cfed185a1d6e9f342d8e3acfe11554 Mon Sep 17 00:00:00 2001 From: Alfonso Grillo Date: Fri, 30 Dec 2022 11:14:48 +0100 Subject: [PATCH 3/6] Add reply support to poll.end events --- MatrixSDK/Data/MXRoom.m | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/MatrixSDK/Data/MXRoom.m b/MatrixSDK/Data/MXRoom.m index 1375389e42..a2130f0374 100644 --- a/MatrixSDK/Data/MXRoom.m +++ b/MatrixSDK/Data/MXRoom.m @@ -2192,6 +2192,17 @@ - (void)getReplyContentBodiesWithEventToReply:(MXEvent*)eventToReply senderMessageBody = question; } + if (eventToReply.eventType == MXEventTypePollEnd) + { + MXEvent* pollStartEvent = [mxSession.store eventWithEventId:eventToReply.relatesTo.eventId inRoom:self.roomId]; + + if (pollStartEvent) { + NSString *question = [MXEventContentPollStart modelFromJSON:pollStartEvent.content].question; + senderMessageBody = question; + } else { // we need a fallback to avoid crashes. Now is the m.poll.start event id + senderMessageBody = eventToReply.relatesTo.eventId; + } + } else if (eventToReply.eventType == MXEventTypeBeaconInfo) { senderMessageBody = stringLocalizer.senderSentTheirLiveLocation; From 38674c5167f5e98b6b286769f5caac8f26658c37 Mon Sep 17 00:00:00 2001 From: Alfonso Grillo Date: Wed, 4 Jan 2023 14:43:27 +0100 Subject: [PATCH 4/6] Move comment --- MatrixSDK/Data/MXRoom.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MatrixSDK/Data/MXRoom.m b/MatrixSDK/Data/MXRoom.m index a2130f0374..b13b5937b5 100644 --- a/MatrixSDK/Data/MXRoom.m +++ b/MatrixSDK/Data/MXRoom.m @@ -2199,7 +2199,8 @@ - (void)getReplyContentBodiesWithEventToReply:(MXEvent*)eventToReply if (pollStartEvent) { NSString *question = [MXEventContentPollStart modelFromJSON:pollStartEvent.content].question; senderMessageBody = question; - } else { // we need a fallback to avoid crashes. Now is the m.poll.start event id + } else { + // we need a fallback to avoid crashes since the m.poll.start event may be missing. senderMessageBody = eventToReply.relatesTo.eventId; } } From ca2eab757433c08c759bbb7d41be0edc16204ef6 Mon Sep 17 00:00:00 2001 From: Alfonso Grillo Date: Wed, 4 Jan 2023 15:08:13 +0100 Subject: [PATCH 5/6] Add changelog.d file --- changelog.d/pr-1674.change | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/pr-1674.change diff --git a/changelog.d/pr-1674.change b/changelog.d/pr-1674.change new file mode 100644 index 0000000000..36c0645ef7 --- /dev/null +++ b/changelog.d/pr-1674.change @@ -0,0 +1 @@ +Polls: add support for rendering/replying to poll ended events. From 6b348d29d5d2a8f901d7ff32bdc7ce248b8c5dc1 Mon Sep 17 00:00:00 2001 From: Alfonso Grillo Date: Mon, 9 Jan 2023 15:53:03 +0100 Subject: [PATCH 6/6] Cleanup code --- MatrixSDK/Data/MXRoom.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MatrixSDK/Data/MXRoom.m b/MatrixSDK/Data/MXRoom.m index b13b5937b5..020dbe5eef 100644 --- a/MatrixSDK/Data/MXRoom.m +++ b/MatrixSDK/Data/MXRoom.m @@ -2449,17 +2449,17 @@ - (NSString*)replyMessageFormattedBodyFromEventToReply:(MXEvent*)eventToReply - (BOOL)canReplyToEvent:(MXEvent *)eventToReply { - if(eventToReply.eventType == MXEventTypePollStart) + if (eventToReply.eventType == MXEventTypePollStart) { return YES; } - if(eventToReply.eventType == MXEventTypePollEnd) + if (eventToReply.eventType == MXEventTypePollEnd) { return YES; } - if(eventToReply.eventType == MXEventTypeBeaconInfo) + if (eventToReply.eventType == MXEventTypeBeaconInfo) { return YES; }