another.im-ios/AnotherIM/AppData/Client/Client+MartinMAM.swift
2024-10-31 12:43:16 +01:00

80 lines
2.4 KiB
Swift

import Combine
import Foundation
import GRDB
import Martin
private typealias ArchMsg = Martin.MessageArchiveManagementModule.ArchivedMessageReceived
final class ClientMartinMAM {
private var cancellables: Set<AnyCancellable> = []
private var processor = ArchiveMessageProcessor()
init(_ xmppConnection: XMPPClient) {
// subscribe to archived messages
xmppConnection.module(.mam).archivedMessagesPublisher
.sink(receiveValue: { [weak self] archived in
guard let self = self else { return }
Task {
await self.processor.append(archived)
}
})
.store(in: &cancellables)
}
}
private actor ArchiveMessageProcessor {
private var accumulator: [ArchMsg] = []
init() {
Task {
while true {
try? await Task.sleep(nanoseconds: 700 * NSEC_PER_MSEC)
await process()
}
}
}
func append(_ msg: ArchMsg) async {
accumulator.append(msg)
if accumulator.count >= Const.mamRequestPageSize {
await process()
}
}
func process() async {
if accumulator.isEmpty { return }
await handleMessages(accumulator)
accumulator.removeAll()
}
private func handleMessages(_ received: [ArchMsg]) async {
if received.isEmpty { return }
try? await Database.shared.dbQueue.write { db in
for recv in received {
let message = recv.message
let date = recv.timestamp
if let msgId = message.id {
if try Message.fetchOne(db, key: msgId) != nil {
#if DEBUG
print("---")
print("Skipping archived message with id \(msgId) (message exists)")
print("---")
#endif
} else {
#if DEBUG
print("---")
print("Archive message received: \(message)")
print("Date: \(date)")
print("---")
#endif
if var msg = Message.map(message, context: nil) {
msg.date = date
try msg.insert(db)
}
}
}
}
}
}
}