This commit is contained in:
fmodf 2024-11-08 14:12:42 +01:00
parent b1ca812c56
commit 3fbc18cbda

View file

@ -7,9 +7,10 @@ private typealias ArchMsg = Martin.MessageArchiveManagementModule.ArchivedMessag
final class ClientMartinMAM {
private var cancellables: Set<AnyCancellable> = []
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)
}