Merge pull request #5869 from vector-im/ismail/5488_strip_inreplyto

Strip `in reply to` from thread summaries and latest messages
This commit is contained in:
ismailgulek 2022-03-31 14:46:19 +03:00 committed by GitHub
commit 2051418e55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 20 deletions

View file

@ -26,4 +26,24 @@ extension MXEvent {
} }
return MXMessageType(identifier: messageTypeString) 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
}
}
} }

View file

@ -115,7 +115,7 @@ class ThreadSummaryView: UIView {
room.state { [weak self] roomState in room.state { [weak self] roomState in
guard let self = self else { return } guard let self = self else { return }
let formatterError = UnsafeMutablePointer<MXKEventFormatterError>.allocate(capacity: 1) let formatterError = UnsafeMutablePointer<MXKEventFormatterError>.allocate(capacity: 1)
let lastMessageText = eventFormatter.attributedString(from: lastMessage, let lastMessageText = eventFormatter.attributedString(from: lastMessage.replyStrippedVersion,
with: roomState, with: roomState,
error: formatterError) error: formatterError)

View file

@ -206,25 +206,8 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
guard let message = thread.rootMessage else { guard let message = thread.rootMessage else {
return nil 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<MXKEventFormatterError>.allocate(capacity: 1)
return eventFormatter.attributedString(from: trimmedMessage,
with: roomState,
error: formatterError)
}
let formatterError = UnsafeMutablePointer<MXKEventFormatterError>.allocate(capacity: 1) let formatterError = UnsafeMutablePointer<MXKEventFormatterError>.allocate(capacity: 1)
return eventFormatter.attributedString(from: message, return eventFormatter.attributedString(from: message.replyStrippedVersion,
with: roomState, with: roomState,
error: formatterError) error: formatterError)
} }
@ -238,7 +221,7 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
} }
let formatterError = UnsafeMutablePointer<MXKEventFormatterError>.allocate(capacity: 1) let formatterError = UnsafeMutablePointer<MXKEventFormatterError>.allocate(capacity: 1)
return ( return (
eventFormatter.attributedString(from: message, eventFormatter.attributedString(from: message.replyStrippedVersion,
with: roomState, with: roomState,
error: formatterError), error: formatterError),
eventFormatter.dateString(from: message, withTime: true) eventFormatter.dateString(from: message, withTime: true)

1
changelog.d/5488.change Normal file
View file

@ -0,0 +1 @@
Threads: Strip `ìn reply to` from thread summaries and latest messages.