Add replacement logic for “Ended poll” text

This commit is contained in:
Alfonso Grillo 2023-01-18 12:43:11 +01:00
parent 8ad5489a5e
commit a8d041da3b

View file

@ -31,6 +31,7 @@
#import "GeneratedInterface-Swift.h" #import "GeneratedInterface-Swift.h"
static NSString *const kHTMLATagRegexPattern = @"<a href=(?:'|\")(.*?)(?:'|\")>([^<]*)</a>"; static NSString *const kHTMLATagRegexPattern = @"<a href=(?:'|\")(.*?)(?:'|\")>([^<]*)</a>";
static NSString *const kEndedPollPattern = @"<mx-reply>.*<blockquote>.*<br>(.*)</blockquote></mx-reply>";
@interface MXKEventFormatter () @interface MXKEventFormatter ()
{ {
@ -1808,6 +1809,7 @@ static NSString *const kHTMLATagRegexPattern = @"<a href=(?:'|\")(.*?)(?:'|\")>(
} }
html = [self renderReplyTo:html withRoomState:roomState]; html = [self renderReplyTo:html withRoomState:roomState];
html = [self renderPollEndedReplyTo:html repliedEvent:repliedEvent];
} }
// Apply the css style that corresponds to the event state // Apply the css style that corresponds to the event state
@ -2014,6 +2016,39 @@ static NSString *const kHTMLATagRegexPattern = @"<a href=(?:'|\")(.*?)(?:'|\")>(
return html; 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 - (void)postFormatMutableAttributedString:(NSMutableAttributedString*)mutableAttributedString
forEvent:(MXEvent*)event forEvent:(MXEvent*)event
andRepliedEvent:(MXEvent*)repliedEvent andRepliedEvent:(MXEvent*)repliedEvent