Skip to content

Commit

Permalink
Merge pull request #1674 from matrix-org/alfogrillo/render_poll_end_e…
Browse files Browse the repository at this point in the history
…vent

Render m.poll.end event (PSG-196)
  • Loading branch information
alfogrillo authored Jan 10, 2023
2 parents da412bc + 6b348d2 commit e74e119
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
21 changes: 19 additions & 2 deletions MatrixSDK/Data/MXRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -2192,6 +2192,18 @@ - (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 since the m.poll.start event may be missing.
senderMessageBody = eventToReply.relatesTo.eventId;
}
}
else if (eventToReply.eventType == MXEventTypeBeaconInfo)
{
senderMessageBody = stringLocalizer.senderSentTheirLiveLocation;
Expand Down Expand Up @@ -2437,12 +2449,17 @@ - (NSString*)replyMessageFormattedBodyFromEventToReply:(MXEvent*)eventToReply

- (BOOL)canReplyToEvent:(MXEvent *)eventToReply
{
if(eventToReply.eventType == MXEventTypePollStart)
if (eventToReply.eventType == MXEventTypePollStart)
{
return YES;
}

if (eventToReply.eventType == MXEventTypePollEnd)
{
return YES;
}

if(eventToReply.eventType == MXEventTypeBeaconInfo)
if (eventToReply.eventType == MXEventTypeBeaconInfo)
{
return YES;
}
Expand Down
19 changes: 19 additions & 0 deletions MatrixSDK/Room/Polls/PollAggregator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-1674.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Polls: add support for rendering/replying to poll ended events.

0 comments on commit e74e119

Please sign in to comment.