import Combine import Foundation import GRDB import Martin final class ClientMartinMAM { private var cancellables: Set = [] init(_ xmppConnection: XMPPClient) { // subscribe to archived messages xmppConnection.module(.mam).archivedMessagesPublisher .sink(receiveValue: { [weak self] archived in let message = archived.message message.attribute("archived_date", newValue: "\(archived.timestamp.timeIntervalSince1970)") self?.handleMessage(archived) }) .store(in: &cancellables) } private func handleMessage(_ received: Martin.MessageArchiveManagementModule.ArchivedMessageReceived) { let message = received.message let date = received.timestamp #if DEBUG print("---") print("Archive message received: \(message)") print("Date: \(date)") print("---") #endif if let msg = Message.map(message) { Task { do { var msg = msg msg.date = received.timestamp try await msg.save() } catch { logIt(.error, "Error saving message: \(error)") } } } } }