2024-08-14 09:29:51 +00:00
|
|
|
import Combine
|
2024-08-14 13:48:30 +00:00
|
|
|
import Foundation
|
2024-08-14 09:29:51 +00:00
|
|
|
import GRDB
|
|
|
|
import Martin
|
|
|
|
|
|
|
|
final class ClientMartinMessagesManager {
|
2024-08-18 11:26:05 +00:00
|
|
|
private var cancellables: Set<AnyCancellable> = []
|
2024-08-14 09:29:51 +00:00
|
|
|
|
|
|
|
init(_ xmppConnection: XMPPClient) {
|
2024-08-18 11:26:05 +00:00
|
|
|
xmppConnection.module(MessageModule.self).messagesPublisher
|
2024-08-14 09:29:51 +00:00
|
|
|
.sink { [weak self] message in
|
2024-08-18 14:43:13 +00:00
|
|
|
self?.handleMessage(message)
|
2024-08-18 17:20:55 +00:00
|
|
|
}
|
2024-08-18 11:26:05 +00:00
|
|
|
.store(in: &cancellables)
|
2024-08-14 09:29:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-18 17:20:55 +00:00
|
|
|
private func handleMessage(_ received: Martin.MessageModule.MessageReceived) {
|
|
|
|
let message = received.message
|
|
|
|
let chat = received.chat
|
2024-08-14 09:29:51 +00:00
|
|
|
#if DEBUG
|
|
|
|
print("---")
|
2024-08-18 11:26:05 +00:00
|
|
|
print("Message received: \(received)")
|
2024-08-18 17:20:55 +00:00
|
|
|
print("Chat: \(chat)")
|
2024-08-14 09:29:51 +00:00
|
|
|
print("---")
|
|
|
|
#endif
|
2024-08-14 13:48:30 +00:00
|
|
|
|
2024-09-02 15:01:20 +00:00
|
|
|
// Try to recognize if message is omemo-encoded and decode it
|
2024-09-02 17:35:08 +00:00
|
|
|
// if let omemo = OMEMO.decode(message: message, chat: chat) {
|
|
|
|
// // If it is omemo-encoded, save it and return
|
|
|
|
// saveMessage(omemo)
|
|
|
|
// return
|
|
|
|
// }
|
2024-09-02 15:01:20 +00:00
|
|
|
|
2024-08-18 17:20:55 +00:00
|
|
|
if let msg = Message.map(message) {
|
|
|
|
Task {
|
|
|
|
do {
|
|
|
|
try await msg.save()
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Error saving message: \(error)")
|
|
|
|
}
|
2024-08-14 13:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-14 09:29:51 +00:00
|
|
|
}
|
|
|
|
}
|