diff --git a/ConversationsClassic/AppData/Client/Client+MartinCarbons.swift b/ConversationsClassic/AppData/Client/Client+MartinCarbons.swift index 13a1f0d..1e1b0fb 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinCarbons.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinCarbons.swift @@ -34,7 +34,7 @@ final class ClientMartinCarbonsManager { print("---") #endif - if let msg = Message.map(message) { + if let msg = Message.map(message, context: nil) { Task { do { try await msg.save() diff --git a/ConversationsClassic/AppData/Client/Client+MartinMAM.swift b/ConversationsClassic/AppData/Client/Client+MartinMAM.swift index 9eda58a..a82336e 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinMAM.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinMAM.swift @@ -67,7 +67,7 @@ private actor ArchiveMessageProcessor { print("Date: \(date)") print("---") #endif - if var msg = Message.map(message) { + if var msg = Message.map(message, context: nil) { msg.date = date try msg.insert(db) } diff --git a/ConversationsClassic/AppData/Client/Client+MartinMessages.swift b/ConversationsClassic/AppData/Client/Client+MartinMessages.swift index ad553f0..8ee2371 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinMessages.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinMessages.swift @@ -24,14 +24,8 @@ final class ClientMartinMessagesManager { print("---") #endif - // Try to recognize if message is omemo-encoded and decode it - // if let omemo = OMEMO.decode(message: message, chat: chat) { - // // If it is omemo-encoded, save it and return - // saveMessage(omemo) - // return - // } - - if let msg = Message.map(message) { + // Process image + if let msg = Message.map(message, context: chat.context) { Task { do { try await msg.save() diff --git a/ConversationsClassic/AppData/Model/Message+OMEMO.swift b/ConversationsClassic/AppData/Model/Message+OMEMO.swift new file mode 100644 index 0000000..3a9c253 --- /dev/null +++ b/ConversationsClassic/AppData/Model/Message+OMEMO.swift @@ -0,0 +1,71 @@ +import Foundation +import GRDB +import Martin + +extension Message { + static func map(_ martinMessage: Martin.Message, context: Martin.Context?) -> Message? { + // Check that the message type is supported + var martinMessage = martinMessage + let chatTypes: [StanzaType] = [.chat, .groupchat] + guard let mType = martinMessage.type, chatTypes.contains(mType) else { + #if DEBUG + print("Unsupported martinMessage type: \(martinMessage.type?.rawValue ?? "nil")") + #endif + return nil + } + + // Type + let type = MessageType(rawValue: martinMessage.type?.rawValue ?? "") ?? .chat + + // Content type + var contentType: MessageContentType = .text + if let oob = martinMessage.oob { + contentType = .attachment(.init( + type: oob.attachmentType, + localName: nil, + thumbnailName: nil, + remotePath: oob + )) + } else if martinMessage.hints.contains(.noStore) { + contentType = .typing + // skip for now + return nil + } + + // Try to recognize if message is omemo-encoded and decode it + if let omemo = context?.module(.omemo) { + let decodingResult = omemo.decode(message: martinMessage) + switch decodingResult { + case .successMessage(let decodedMessage, _): + martinMessage = decodedMessage + // print(decodedMessage, fingerprint) + + case .successTransportKey: + break + + case .failure(let error): + logIt(.error, "Error decoding omemo message: \(error)") + } + } + + // From/To + let from = martinMessage.from?.bareJid.stringValue ?? "" + let to = martinMessage.to?.bareJid.stringValue + + // Msg + let msg = Message( + id: martinMessage.id ?? UUID().uuidString, + type: type, + date: Date(), + contentType: contentType, + status: .sent, + from: from, + to: to, + body: martinMessage.body, + subject: martinMessage.subject, + thread: martinMessage.thread, + oobUrl: martinMessage.oob + ) + return msg + } +} diff --git a/ConversationsClassic/AppData/Model/Message.swift b/ConversationsClassic/AppData/Model/Message.swift index b393a99..4b51cab 100644 --- a/ConversationsClassic/AppData/Model/Message.swift +++ b/ConversationsClassic/AppData/Model/Message.swift @@ -101,54 +101,3 @@ extension Message { ) } } - -extension Message { - static func map(_ martinMessage: Martin.Message) -> Message? { - // Check that the message type is supported - let chatTypes: [StanzaType] = [.chat, .groupchat] - guard let mType = martinMessage.type, chatTypes.contains(mType) else { - #if DEBUG - print("Unsupported martinMessage type: \(martinMessage.type?.rawValue ?? "nil")") - #endif - return nil - } - - // Type - let type = MessageType(rawValue: martinMessage.type?.rawValue ?? "") ?? .chat - - // Content type - var contentType: MessageContentType = .text - if let oob = martinMessage.oob { - contentType = .attachment(.init( - type: oob.attachmentType, - localName: nil, - thumbnailName: nil, - remotePath: oob - )) - } else if martinMessage.hints.contains(.noStore) { - contentType = .typing - // skip for now - return nil - } - - // From/To - let from = martinMessage.from?.bareJid.stringValue ?? "" - let to = martinMessage.to?.bareJid.stringValue - - // Msg - let msg = Message( - id: martinMessage.id ?? UUID().uuidString, - type: type, - date: Date(), - contentType: contentType, - status: .sent, - from: from, - to: to, - body: martinMessage.body, - subject: martinMessage.subject, - thread: martinMessage.thread, - oobUrl: martinMessage.oob - ) - return msg - } -}