This commit is contained in:
fmodf 2024-09-03 15:07:53 +02:00
parent 561ed99f41
commit f3aabc34e8
5 changed files with 75 additions and 61 deletions

View file

@ -34,7 +34,7 @@ final class ClientMartinCarbonsManager {
print("---") print("---")
#endif #endif
if let msg = Message.map(message) { if let msg = Message.map(message, context: nil) {
Task { Task {
do { do {
try await msg.save() try await msg.save()

View file

@ -67,7 +67,7 @@ private actor ArchiveMessageProcessor {
print("Date: \(date)") print("Date: \(date)")
print("---") print("---")
#endif #endif
if var msg = Message.map(message) { if var msg = Message.map(message, context: nil) {
msg.date = date msg.date = date
try msg.insert(db) try msg.insert(db)
} }

View file

@ -24,14 +24,8 @@ final class ClientMartinMessagesManager {
print("---") print("---")
#endif #endif
// Try to recognize if message is omemo-encoded and decode it // Process image
// if let omemo = OMEMO.decode(message: message, chat: chat) { if let msg = Message.map(message, context: chat.context) {
// // If it is omemo-encoded, save it and return
// saveMessage(omemo)
// return
// }
if let msg = Message.map(message) {
Task { Task {
do { do {
try await msg.save() try await msg.save()

View file

@ -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
}
}

View file

@ -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
}
}