mv-experiment #1

Merged
fmodf merged 88 commits from mv-experiment into develop 2024-09-03 15:13:59 +00:00
5 changed files with 75 additions and 61 deletions
Showing only changes of commit f3aabc34e8 - Show all commits

View file

@ -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()

View file

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

View file

@ -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()

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