Skip to content

Commit

Permalink
Merge pull request #1012 from tchapgouv/832-ne-pas-citer-la-totalite-…
Browse files Browse the repository at this point in the history
…dun-message-precedent

Limiter la longueur du message d'origine à l'affichage d'une réponse …
  • Loading branch information
NicolasBuquet authored Apr 10, 2024
2 parents 619fdb0 + db8f720 commit 6af4fe2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,9 @@ - (NSString*)renderReplyTo:(NSString*)htmlString withRoomState:(MXRoomState*)roo
}
}

// Tchap: truncate quoted reply if necessary.
html = [self tchapTruncatedQuotedReplyFrom:html];

// Replace <mx-reply><blockquote><a href=\"__permalink__\">In reply to</a>
// By <mx-reply><blockquote><a href=\"__permalink__\">['In reply to' from resources]</a>
// To localize the "In reply to" string
Expand All @@ -2042,6 +2045,42 @@ - (NSString*)renderReplyTo:(NSString*)htmlString withRoomState:(MXRoomState*)roo
return html;
}

// Tchap: truncate long quoted reply
- (NSString *)tchapTruncatedQuotedReplyFrom:(NSString *)fullQuotedReply {
static NSRegularExpression *htmlQuotedTextRegex;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
htmlQuotedTextRegex = [NSRegularExpression regularExpressionWithPattern:kRepliedTextPattern
options:NSRegularExpressionCaseInsensitive
error:nil];
});

NSTextCheckingResult * regexResults = [htmlQuotedTextRegex firstMatchInString:fullQuotedReply
options:0
range:NSMakeRange(0, fullQuotedReply.length)];

// Check if a quoted text is present.
if( regexResults.numberOfRanges < 1 )
{
// No reply found.
return fullQuotedReply;
}

NSRange quotedTextRange = [regexResults rangeAtIndex:1];

NSUInteger quotedTextMaxLength = 60; // Max length of quoted text

if( quotedTextRange.location != NSNotFound && quotedTextRange.length > quotedTextMaxLength )
{
NSRange truncatedRange = NSMakeRange(quotedTextRange.location + quotedTextMaxLength, quotedTextRange.length - quotedTextMaxLength);
return [fullQuotedReply stringByReplacingCharactersInRange:truncatedRange withString:@""];
}

// The quoted reply is not found or already short. Return it as is.
return fullQuotedReply;
}

- (NSString*)renderPollEndedReplyTo:(NSString*)htmlString repliedEvent:(MXEvent*)repliedEvent {
static NSRegularExpression *endedPollRegex;
static dispatch_once_t onceToken;
Expand Down
1 change: 1 addition & 0 deletions changelog.d/832.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Limiter la longueur du message d'origine à l'affichage d'une réponse avec citation.

0 comments on commit 6af4fe2

Please sign in to comment.