mirror of
https://github.com/vector-im/element-ios.git
synced 2024-09-28 23:32:41 +00:00
created the replace formatted text function
This commit is contained in:
parent
e09451373b
commit
a1c53040f0
2 changed files with 62 additions and 21 deletions
|
@ -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<String?>) -> 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<String?>) -> 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)
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue