2024-09-03 15:13:58 +00:00
|
|
|
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
|
2024-09-11 12:37:54 +00:00
|
|
|
var secure = false
|
2024-09-03 15:13:58 +00:00
|
|
|
if let omemo = context?.module(.omemo) {
|
|
|
|
let decodingResult = omemo.decode(message: martinMessage)
|
|
|
|
switch decodingResult {
|
|
|
|
case .successMessage(let decodedMessage, _):
|
|
|
|
martinMessage = decodedMessage
|
2024-09-08 16:16:01 +00:00
|
|
|
if let oob = martinMessage.oob {
|
|
|
|
contentType = .attachment(.init(
|
|
|
|
type: oob.attachmentType,
|
|
|
|
localName: nil,
|
|
|
|
thumbnailName: nil,
|
|
|
|
remotePath: oob
|
|
|
|
))
|
|
|
|
}
|
2024-09-11 12:37:54 +00:00
|
|
|
secure = true
|
2024-09-03 15:13:58 +00:00
|
|
|
|
|
|
|
case .successTransportKey:
|
2024-09-19 15:14:05 +00:00
|
|
|
return nil
|
2024-09-03 15:13:58 +00:00
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
logIt(.error, "Error decoding omemo message: \(error)")
|
2024-09-18 12:26:08 +00:00
|
|
|
logIt(.error, "Message: \(martinMessage)")
|
2024-09-03 15:13:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip for non-visible messages
|
|
|
|
if martinMessage.body == nil, martinMessage.oob == nil, martinMessage.type == .chat {
|
|
|
|
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,
|
2024-09-11 12:37:54 +00:00
|
|
|
oobUrl: martinMessage.oob,
|
|
|
|
secure: secure
|
2024-09-03 15:13:58 +00:00
|
|
|
)
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
}
|