40 lines
1.1 KiB
Swift
40 lines
1.1 KiB
Swift
import Combine
|
|
import Foundation
|
|
import GRDB
|
|
import Martin
|
|
|
|
final class ClientMartinMessagesManager {
|
|
private var cancellables: Set<AnyCancellable> = []
|
|
|
|
init(_ xmppConnection: XMPPClient) {
|
|
xmppConnection.module(MessageModule.self).messagesPublisher
|
|
.sink { [weak self] message in
|
|
self?.handleMessage(message)
|
|
}
|
|
.store(in: &cancellables)
|
|
}
|
|
|
|
private func handleMessage(_ received: Martin.MessageModule.MessageReceived) {
|
|
let message = received.message
|
|
let chat = received.chat
|
|
#if DEBUG
|
|
print("---")
|
|
print("Message received: \(received)")
|
|
print("Chat: \(chat)")
|
|
print("---")
|
|
#endif
|
|
|
|
// Try to recognize if message is omemo-encoded and decode it
|
|
|
|
if let msg = Message.map(message) {
|
|
Task {
|
|
do {
|
|
try await msg.save()
|
|
} catch {
|
|
logIt(.error, "Error saving message: \(error)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|