diff --git a/Riot/Categories/MXEvent.swift b/Riot/Categories/MXEvent.swift index 5ba0c1c7d..5b1a85263 100644 --- a/Riot/Categories/MXEvent.swift +++ b/Riot/Categories/MXEvent.swift @@ -26,4 +26,24 @@ extension MXEvent { } return MXMessageType(identifier: messageTypeString) } + + /// Lightweight version of the receiver, in which reply-specific keys are stripped. Returns the same event with the receiver if not a reply event. + /// Should be used only to update formatting behavior. + var replyStrippedVersion: MXEvent { + if self.isReply(), let newMessage = self.copy() as? MXEvent { + var jsonDict = newMessage.isEncrypted ? newMessage.clear?.jsonDictionary() : newMessage.jsonDictionary() + if var content = jsonDict?["content"] as? [String: Any] { + content.removeValue(forKey: "format") + content.removeValue(forKey: "formatted_body") + content.removeValue(forKey: kMXEventRelationRelatesToKey) + if let replyText = MXReplyEventParser().parse(newMessage)?.bodyParts.replyText { + content["body"] = replyText + } + jsonDict?["content"] = content + } + return MXEvent(fromJSON: jsonDict) + } else { + return self + } + } } diff --git a/Riot/Modules/Room/TimelineDecorations/Threads/Summary/ThreadSummaryView.swift b/Riot/Modules/Room/TimelineDecorations/Threads/Summary/ThreadSummaryView.swift index a57888aeb..61489868b 100644 --- a/Riot/Modules/Room/TimelineDecorations/Threads/Summary/ThreadSummaryView.swift +++ b/Riot/Modules/Room/TimelineDecorations/Threads/Summary/ThreadSummaryView.swift @@ -115,7 +115,7 @@ class ThreadSummaryView: UIView { room.state { [weak self] roomState in guard let self = self else { return } let formatterError = UnsafeMutablePointer.allocate(capacity: 1) - let lastMessageText = eventFormatter.attributedString(from: lastMessage, + let lastMessageText = eventFormatter.attributedString(from: lastMessage.replyStrippedVersion, with: roomState, error: formatterError) diff --git a/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift b/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift index e63a63703..af2459446 100644 --- a/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift +++ b/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift @@ -206,25 +206,8 @@ final class ThreadListViewModel: ThreadListViewModelProtocol { guard let message = thread.rootMessage else { return nil } - if message.isReply(), let newMessage = message.copy() as? MXEvent { - var jsonDict = newMessage.isEncrypted ? newMessage.clear?.jsonDictionary() : newMessage.jsonDictionary() - if var content = jsonDict?["content"] as? [String: Any] { - content.removeValue(forKey: "format") - content.removeValue(forKey: "formatted_body") - content.removeValue(forKey: kMXEventRelationRelatesToKey) - if let replyText = MXReplyEventParser().parse(newMessage)?.bodyParts.replyText { - content["body"] = replyText - } - jsonDict?["content"] = content - } - let trimmedMessage = MXEvent(fromJSON: jsonDict) - let formatterError = UnsafeMutablePointer.allocate(capacity: 1) - return eventFormatter.attributedString(from: trimmedMessage, - with: roomState, - error: formatterError) - } let formatterError = UnsafeMutablePointer.allocate(capacity: 1) - return eventFormatter.attributedString(from: message, + return eventFormatter.attributedString(from: message.replyStrippedVersion, with: roomState, error: formatterError) } @@ -238,7 +221,7 @@ final class ThreadListViewModel: ThreadListViewModelProtocol { } let formatterError = UnsafeMutablePointer.allocate(capacity: 1) return ( - eventFormatter.attributedString(from: message, + eventFormatter.attributedString(from: message.replyStrippedVersion, with: roomState, error: formatterError), eventFormatter.dateString(from: message, withTime: true) diff --git a/changelog.d/5488.change b/changelog.d/5488.change new file mode 100644 index 000000000..e1b4dc675 --- /dev/null +++ b/changelog.d/5488.change @@ -0,0 +1 @@ +Threads: Strip `ìn reply to` from thread summaries and latest messages.