39 lines
1 KiB
Swift
39 lines
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
|
||
|
|
||
|
// Process image
|
||
|
if let msg = Message.map(message, context: chat.context) {
|
||
|
Task {
|
||
|
do {
|
||
|
try await msg.save()
|
||
|
} catch {
|
||
|
logIt(.error, "Error saving message: \(error)")
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|