From a1c53040f0dc9d9048c6d2d84743f60316e92686 Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Mon, 10 Oct 2022 15:52:34 +0200 Subject: [PATCH] created the replace formatted text function --- .../Room/DataSources/RoomDataSource.swift | 49 ++++++++++++++----- Riot/Modules/Room/RoomViewController.swift | 34 ++++++++++--- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/Riot/Modules/Room/DataSources/RoomDataSource.swift b/Riot/Modules/Room/DataSources/RoomDataSource.swift index 4401b3215..d5f74d058 100644 --- a/Riot/Modules/Room/DataSources/RoomDataSource.swift +++ b/Riot/Modules/Room/DataSources/RoomDataSource.swift @@ -21,7 +21,7 @@ extension RoomDataSource { private enum Constants { static let emoteMessageSlashCommandPrefix = String(format: "%@ ", kMXKSlashCmdEmote) } - + // MARK: - NSAttributedString Sending /// Send a text message to the room. /// While sending, a fake event will be echoed in the messages list. @@ -33,7 +33,7 @@ extension RoomDataSource { func sendAttributedTextMessage(_ attributedText: NSAttributedString, completion: @escaping (MXResponse) -> Void) { var localEcho: MXEvent? - + let isEmote = isAttributedTextMessageAnEmote(attributedText) let sanitized = sanitizedAttributedMessageText(attributedText) let rawText: String @@ -43,7 +43,7 @@ extension RoomDataSource { } else { rawText = sanitized.string } - + if isEmote { room.sendEmote(rawText, formattedText: html, @@ -57,7 +57,7 @@ extension RoomDataSource { localEcho: &localEcho, completion: completion) } - + if localEcho != nil { self.queueEvent(forProcessing: localEcho, with: self.roomState, direction: .forwards) self.processQueuedEvents(nil) @@ -88,7 +88,7 @@ extension RoomDataSource { self.processQueuedEvents(nil) } } - + /// Send a reply to an event with text message to the room. /// /// While sending, a fake event will be echoed in the messages list. @@ -102,7 +102,7 @@ extension RoomDataSource { withAttributedTextMessage attributedText: NSAttributedString, completion: @escaping (MXResponse) -> Void) { var localEcho: MXEvent? - + let sanitized = sanitizedAttributedMessageText(attributedText) let rawText: String let html: String? = htmlMessageFromSanitizedAttributedText(sanitized) @@ -111,9 +111,9 @@ extension RoomDataSource { } else { rawText = sanitized.string } - + let stringLocalizer: MXSendReplyEventStringLocalizerProtocol = MXKSendReplyEventStringLocalizer() - + room.sendReply(to: eventToReply, textMessage: rawText, formattedTextMessage: html, @@ -121,13 +121,13 @@ extension RoomDataSource { threadId: self.threadId, localEcho: &localEcho, completion: completion) - + if localEcho != nil { self.queueEvent(forProcessing: localEcho, with: self.roomState, direction: .forwards) self.processQueuedEvents(nil) } } - + /// Replace a text in an event. /// /// - Parameters: @@ -147,10 +147,33 @@ extension RoomDataSource { } else { rawText = sanitized.string } - + + handleReplaceFormattedMessage(for: event, rawText: rawText, html: html, success: success, failure: failure) + } + + /// Replace a formatted html text in an event + /// + /// - Parameters: + /// - event: The event to replace + /// - rawText: The new rawText + /// - html: The new html text + /// - success: A block object called when the operation succeeds. It returns the event id of the event generated on the homeserver + /// - failure: A block object called when the operation fails + func replaceFormattedTextMessage( for event: MXEvent, + rawText: String, + html: String, + success: @escaping ((String?) -> Void), + failure: @escaping ((Error?) -> Void)) { + handleReplaceFormattedMessage(for: event, rawText: rawText, html: html, success: success, failure: failure) + } + + private func handleReplaceFormattedMessage( for event: MXEvent, + rawText: String, + html: String?, + success: @escaping ((String?) -> Void), + failure: @escaping ((Error?) -> Void)) { let eventBody = event.content[kMXMessageBodyKey] as? String let eventFormattedBody = event.content["formatted_body"] as? String - if rawText != eventBody && (eventFormattedBody == nil || html != eventFormattedBody) { self.mxSession.aggregations.replaceTextMessageEvent( event, @@ -159,7 +182,7 @@ extension RoomDataSource { localEcho: { localEcho in // Apply the local echo to the timeline self.updateEvent(withReplace: localEcho) - + // Integrate the replace local event into the timeline like when sending a message // This also allows to manage read receipt on this replace event self.queueEvent(forProcessing: localEcho, with: self.roomState, direction: .forwards) diff --git a/Riot/Modules/Room/RoomViewController.swift b/Riot/Modules/Room/RoomViewController.swift index 223b283c4..f81e3e3ef 100644 --- a/Riot/Modules/Room/RoomViewController.swift +++ b/Riot/Modules/Room/RoomViewController.swift @@ -52,18 +52,36 @@ extension RoomViewController { } - /// Send given attributed text message to the room + /// Send the formatted text message and its raw counterpat to the room /// - /// - Parameter attributedTextMsg: the attributed text message + /// - Parameter rawTextMsg: the raw text message + /// - Parameter htmlMsg: the html text message @objc func sendFormattedTextMessage(_ rawTextMsg: String, htmlMsg: String) { + let eventModified = self.roomDataSource.event(withEventId: customizedRoomDataSource?.selectedEventId) self.setupRoomDataSource { roomDataSource in guard let roomDataSource = roomDataSource as? RoomDataSource else { return } - roomDataSource.sendFormattedTextMessage(rawTextMsg, html: htmlMsg) { response in - switch response { - case .success: - break - case .failure: - MXLog.error("[RoomViewController] sendFormattedTextMessage failed") + + if self.inputToolbar?.sendMode == .edit, let eventModified = eventModified { + roomDataSource.replaceFormattedTextMessage( + for: eventModified, + rawText: rawTextMsg, + html: htmlMsg, + success: { _ in + // + }, + failure: { _ in + MXLog.error("[RoomViewController] sendFormattedTextMessage failed while updating event", context: [ + "event_id": eventModified.eventId + ]) + }) + } else { + roomDataSource.sendFormattedTextMessage(rawTextMsg, html: htmlMsg) { response in + switch response { + case .success: + break + case .failure: + MXLog.error("[RoomViewController] sendFormattedTextMessage failed") + } } }