From 3fbc18cbdaed0e4d86108663dcba02037480bb50 Mon Sep 17 00:00:00 2001 From: fmodf Date: Fri, 8 Nov 2024 14:12:42 +0100 Subject: [PATCH] wip --- AnotherIM/AppData/Client/Client+MartinMAM.swift | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/AnotherIM/AppData/Client/Client+MartinMAM.swift b/AnotherIM/AppData/Client/Client+MartinMAM.swift index a82336e..69149a0 100644 --- a/AnotherIM/AppData/Client/Client+MartinMAM.swift +++ b/AnotherIM/AppData/Client/Client+MartinMAM.swift @@ -7,9 +7,10 @@ private typealias ArchMsg = Martin.MessageArchiveManagementModule.ArchivedMessag final class ClientMartinMAM { private var cancellables: Set = [] - private var processor = ArchiveMessageProcessor() + private var processor: ArchiveMessageProcessor init(_ xmppConnection: XMPPClient) { + processor = ArchiveMessageProcessor(xmppConnection.context) // subscribe to archived messages xmppConnection.module(.mam).archivedMessagesPublisher .sink(receiveValue: { [weak self] archived in @@ -24,8 +25,10 @@ final class ClientMartinMAM { private actor ArchiveMessageProcessor { private var accumulator: [ArchMsg] = [] + private let context: Context? - init() { + init(_ ctx: Context?) { + context = ctx Task { while true { try? await Task.sleep(nanoseconds: 700 * NSEC_PER_MSEC) @@ -49,7 +52,7 @@ private actor ArchiveMessageProcessor { private func handleMessages(_ received: [ArchMsg]) async { if received.isEmpty { return } - try? await Database.shared.dbQueue.write { db in + try? await Database.shared.dbQueue.write { [weak self] db in for recv in received { let message = recv.message let date = recv.timestamp @@ -67,7 +70,7 @@ private actor ArchiveMessageProcessor { print("Date: \(date)") print("---") #endif - if var msg = Message.map(message, context: nil) { + if var msg = Message.map(message, context: self?.context) { msg.date = date try msg.insert(db) }