another.im-ios/ConversationsClassic/AppData/Client/Client+MartinMessages.swift

39 lines
1 KiB
Swift
Raw Normal View History

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-03 13:07:53 +00:00
// Process image
if let msg = Message.map(message, context: chat.context) {
2024-08-18 17:20:55 +00:00
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
}
}