From a8d041da3bec620176826203f70e40375ee3032c Mon Sep 17 00:00:00 2001 From: Alfonso Grillo Date: Wed, 18 Jan 2023 12:43:11 +0100 Subject: [PATCH] =?UTF-8?q?Add=20replacement=20logic=20for=20=E2=80=9CEnde?= =?UTF-8?q?d=20poll=E2=80=9D=20text?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Utils/EventFormatter/MXKEventFormatter.m | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m b/Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m index cee8dccec..4a7f6dc0e 100644 --- a/Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m +++ b/Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m @@ -31,6 +31,7 @@ #import "GeneratedInterface-Swift.h" static NSString *const kHTMLATagRegexPattern = @"([^<]*)"; +static NSString *const kEndedPollPattern = @".*
.*
(.*)
"; @interface MXKEventFormatter () { @@ -1808,6 +1809,7 @@ static NSString *const kHTMLATagRegexPattern = @"( } html = [self renderReplyTo:html withRoomState:roomState]; + html = [self renderPollEndedReplyTo:html repliedEvent:repliedEvent]; } // Apply the css style that corresponds to the event state @@ -2014,6 +2016,39 @@ static NSString *const kHTMLATagRegexPattern = @"( return html; } +- (NSString*)renderPollEndedReplyTo:(NSString*)htmlString repliedEvent:(MXEvent*)repliedEvent { + static NSRegularExpression *endedPollRegex; + static dispatch_once_t onceToken; + + dispatch_once(&onceToken, ^{ + endedPollRegex = [NSRegularExpression regularExpressionWithPattern:kEndedPollPattern options:NSRegularExpressionCaseInsensitive error:nil]; + }); + + NSTextCheckingResult* match = [endedPollRegex firstMatchInString:htmlString options:0 range:NSMakeRange(0, htmlString.length)]; + NSString* finalString = htmlString; + + if (!(match && match.numberOfRanges > 1)) { + // no useful match found + return finalString; + } + + NSRange groupRange = [match rangeAtIndex:1]; + NSString* replacementText; + + if (repliedEvent) { + MXEvent* pollStartedEvent = [mxSession.store eventWithEventId:repliedEvent.relatesTo.eventId inRoom:repliedEvent.roomId]; + replacementText = [MXEventContentPollStart modelFromJSON:pollStartedEvent.content].question; + } + + if (replacementText == nil) { + replacementText = VectorL10n.pollTimelineReplyEndedPoll; + } + + finalString = [htmlString stringByReplacingCharactersInRange:groupRange withString:replacementText]; + + return finalString; +} + - (void)postFormatMutableAttributedString:(NSMutableAttributedString*)mutableAttributedString forEvent:(MXEvent*)event andRepliedEvent:(MXEvent*)repliedEvent