From 9c4576f1f68d4d3e3059ce93e4a860e623c3eb90 Mon Sep 17 00:00:00 2001 From: fmodf Date: Mon, 19 Aug 2024 04:38:06 +0200 Subject: [PATCH] wip --- .../AppData/Client/Client+MartinMAM.swift | 40 +++++++++---------- .../AppData/Store/MessagesStore.swift | 2 +- ConversationsClassic/Helpers/Const.swift | 2 +- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/ConversationsClassic/AppData/Client/Client+MartinMAM.swift b/ConversationsClassic/AppData/Client/Client+MartinMAM.swift index 3487893..984790d 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinMAM.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinMAM.swift @@ -31,30 +31,28 @@ final class ClientMartinMAM { private func handleMessage(_ received: Martin.MessageArchiveManagementModule.ArchivedMessageReceived) async { let message = received.message let date = received.timestamp - #if DEBUG - print("---") - print("Archive message received: \(message)") - print("Date: \(date)") - print("---") - #endif + let msgId = received.messageId - // Skip archived message if such message already exists in the database - if let archiveId = message.id { - try? await Database.shared.dbQueue.read { db in - if try Message.fetchOne(db, key: archiveId) != nil { - return + try? await Database.shared.dbQueue.write { db in + if try Message.fetchOne(db, key: msgId) != nil { + #if DEBUG + print("---") + print("Skipping archived message with id \(message.id ?? "???") (message exists)") + print("---") + #endif + return + } else { + #if DEBUG + print("---") + print("Archive message received: \(message)") + print("Date: \(date)") + print("---") + #endif + if var msg = Message.map(message) { + msg.date = received.timestamp + try msg.insert(db) } } } - - if let msg = Message.map(message) { - do { - var msg = msg - msg.date = received.timestamp - try await msg.save() - } catch { - logIt(.error, "Error saving message: \(error)") - } - } } } diff --git a/ConversationsClassic/AppData/Store/MessagesStore.swift b/ConversationsClassic/AppData/Store/MessagesStore.swift index a33614a..fcd4b4b 100644 --- a/ConversationsClassic/AppData/Store/MessagesStore.swift +++ b/ConversationsClassic/AppData/Store/MessagesStore.swift @@ -76,7 +76,7 @@ private extension MessagesStore { // MARK: - Archived messages extension MessagesStore { func requestEarliestArchivedMessages() { - guard let beforeId = messages.first?.id else { return } + guard let beforeId = messages.last?.id else { return } Task { await archiveMessageFetcher.fetchBeforeMessages(roster, client, beforeId: beforeId) } diff --git a/ConversationsClassic/Helpers/Const.swift b/ConversationsClassic/Helpers/Const.swift index c9fd53a..6bb40c2 100644 --- a/ConversationsClassic/Helpers/Const.swift +++ b/ConversationsClassic/Helpers/Const.swift @@ -44,5 +44,5 @@ enum Const { static let attachmentPreviewSize = UIScreen.main.bounds.width * 0.5 // MAM request page size - static let mamRequestLimit = 30 + static let mamRequestLimit = 10 }