From eed175fe1fa54fdfe7d80666e9b656062549aa3d Mon Sep 17 00:00:00 2001 From: fmodf Date: Thu, 8 Aug 2024 12:42:48 +0200 Subject: [PATCH] remove mam for a while --- .../AppCore/Actions/ConversationActions.swift | 2 - .../ArchivedMessagesMiddleware.swift | 70 ++++++++++--------- .../AppCore/Middlewares/XMPPMiddleware.swift | 15 ++-- .../Reducers/ConversationReducer.swift | 3 - .../AppCore/State/ConversationState.swift | 3 - .../AppCore/XMPP/XMPPService.swift | 14 ++-- 6 files changed, 51 insertions(+), 56 deletions(-) diff --git a/ConversationsClassic/AppCore/Actions/ConversationActions.swift b/ConversationsClassic/AppCore/Actions/ConversationActions.swift index 6244b94..6bb8b32 100644 --- a/ConversationsClassic/AppCore/Actions/ConversationActions.swift +++ b/ConversationsClassic/AppCore/Actions/ConversationActions.swift @@ -7,6 +7,4 @@ enum ConversationAction: Codable { case setReplyText(String) case sendMediaMessages(from: String, to: String, messagesIds: [String], localFilesNames: [String]) - - case setArchivedMessagesRequested } diff --git a/ConversationsClassic/AppCore/Middlewares/ArchivedMessagesMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/ArchivedMessagesMiddleware.swift index d4fbfec..b55686f 100644 --- a/ConversationsClassic/AppCore/Middlewares/ArchivedMessagesMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/ArchivedMessagesMiddleware.swift @@ -4,44 +4,46 @@ import Foundation final class ArchivedMessagesMiddleware { static let shared = ArchivedMessagesMiddleware() - func middleware(state: AppState, action: AppAction) -> AnyPublisher { + func middleware(state _: AppState, action: AppAction) -> AnyPublisher { switch action { - case .conversationAction(.messagesUpdated(let messages)): - if state.conversationsState.archivedMessagesRequested { - return Empty().eraseToAnyPublisher() - } else { - guard let chat = state.conversationsState.currentChat else { - return Empty().eraseToAnyPublisher() - } - return Deferred { - Future { promise in - if let currentClient = state.accountsState.accounts.first(where: { $0.bareJid == chat.account }) { - let features = state.accountsState.discoFeatures[currentClient.bareJid] ?? [] - if features.map({ $0.xep }).contains("XEP-0313") { - let roster = chat.participant - if let lastMessage = state.conversationsState.currentMessages.first { - promise(.success(.xmppAction(.xmppLoadArchivedMessages(jid: currentClient.bareJid, to: roster?.bareJid, fromDate: lastMessage.date)))) - } else { - let date = Date() - let archivesRequestDate = Calendar.current.date(byAdding: .day, value: -Const.mamRequestLength, to: date) ?? date - promise(.success(.xmppAction(.xmppLoadArchivedMessages(jid: currentClient.bareJid, to: roster?.bareJid, fromDate: archivesRequestDate)))) - } - // let oldestMessageDate = state.conversationsState.currentMessages.first?.date ?? Date() - // let archivesRequestDate = Calendar.current.date(byAdding: .day, value: -Const.mamRequestLength, to: oldestMessageDate) ?? Date() - // promise(.success(.xmppAction(.xmppLoadArchivedMessages(jid: currentClient.bareJid, to: roster?.bareJid, fromDate: archivesRequestDate)))) - } else { - promise(.success(.info("MessageMiddleware: XEP-0313 not supported for client \(currentClient.bareJid)"))) - } - } else { - promise(.success(.info("MessageMiddleware: No client found for account \(chat.account), probably some error here"))) - } - } - } - .eraseToAnyPublisher() - } + // case .conversationAction(.messagesUpdated(let messages)): + // if state.conversationsState.archivedMessagesRequested { + // return Empty().eraseToAnyPublisher() + // } else { + // guard let chat = state.conversationsState.currentChat else { + // return Empty().eraseToAnyPublisher() + // } + // return Deferred { + // Future { promise in + // if let currentClient = state.accountsState.accounts.first(where: { $0.bareJid == chat.account }) { + // let features = state.accountsState.discoFeatures[currentClient.bareJid] ?? [] + // if features.map({ $0.xep }).contains("XEP-0313") { + // let roster = state.conversationsState.currentRoster + // let date = self.archivesRequestDate(from: messages) + // promise(.success(.xmppAction(.xmppLoadArchivedMessages(jid: currentClient.bareJid, to: roster?.bareJid, fromDate: date)))) + // } else { + // promise(.success(.info("MessageMiddleware: XEP-0313 not supported for client \(currentClient.bareJid)"))) + // } + // } else { + // promise(.success(.info("MessageMiddleware: No client found for account \(chat.account), probably some error here"))) + // } + // } + // } + // .eraseToAnyPublisher() + // } default: return Empty().eraseToAnyPublisher() } } } + +private extension ArchivedMessagesMiddleware { + func archivesRequestDate(from messages: [Message]) -> Date { + if let lastDate = messages.first?.date { + return lastDate + } else { + return Calendar.current.date(byAdding: .day, value: -Const.mamRequestLength, to: Date()) ?? Date() + } + } +} diff --git a/ConversationsClassic/AppCore/Middlewares/XMPPMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/XMPPMiddleware.swift index 3e9cef3..4f98a0b 100644 --- a/ConversationsClassic/AppCore/Middlewares/XMPPMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/XMPPMiddleware.swift @@ -144,13 +144,14 @@ final class XMPPMiddleware { .eraseToAnyPublisher() case .xmppAction(.xmppLoadArchivedMessages(let jid, let to, let fromDate)): - return Deferred { - Future { [weak self] promise in - self?.service.requestArchivedMessages(jid: jid, to: to, fromDate: fromDate) - promise(.success(.conversationAction(.setArchivedMessagesRequested))) - } - } - .eraseToAnyPublisher() + return Empty().eraseToAnyPublisher() + // return Deferred { + // Future { [weak self] promise in + // self?.service.requestArchivedMessages(jid: jid, to: to, fromDate: fromDate) + // promise(.success(.conversationAction(.setArchivedMessagesRequested))) + // } + // } + // .eraseToAnyPublisher() default: return Empty().eraseToAnyPublisher() diff --git a/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift b/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift index da19de4..cc76c62 100644 --- a/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift +++ b/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift @@ -17,9 +17,6 @@ extension ConversationState { state.replyText = text.makeReply } - case .setArchivedMessagesRequested: - state.archivedMessagesRequested = true - default: break } diff --git a/ConversationsClassic/AppCore/State/ConversationState.swift b/ConversationsClassic/AppCore/State/ConversationState.swift index be827d0..9267bbd 100644 --- a/ConversationsClassic/AppCore/State/ConversationState.swift +++ b/ConversationsClassic/AppCore/State/ConversationState.swift @@ -4,8 +4,6 @@ struct ConversationState: Stateable { var currentMessages: [Message] var replyText: String - - var archivedMessagesRequested: Bool } // MARK: Init @@ -13,6 +11,5 @@ extension ConversationState { init() { currentMessages = [] replyText = "" - archivedMessagesRequested = false } } diff --git a/ConversationsClassic/AppCore/XMPP/XMPPService.swift b/ConversationsClassic/AppCore/XMPP/XMPPService.swift index 8e20012..f9109ff 100644 --- a/ConversationsClassic/AppCore/XMPP/XMPPService.swift +++ b/ConversationsClassic/AppCore/XMPP/XMPPService.swift @@ -77,13 +77,13 @@ final class XMPPService: ObservableObject { .store(in: &clientMessagesCancellables) // subscribe to archived messages - client.module(.mam).archivedMessagesPublisher - .sink(receiveValue: { [weak self] archived in - let message = archived.message - message.attribute("archived_date", newValue: "\(archived.timestamp.timeIntervalSince1970)") - self?.clientMessagesPublisher.send((client, message)) - }) - .store(in: &clientMessagesCancellables) + // client.module(.mam).archivedMessagesPublisher + // .sink(receiveValue: { [weak self] archived in + // let message = archived.message + // message.attribute("archived_date", newValue: "\(archived.timestamp.timeIntervalSince1970)") + // self?.clientMessagesPublisher.send((client, message)) + // }) + // .store(in: &clientMessagesCancellables) // enable carbons if available client.module(.messageCarbons).$isAvailable.filter { $0 }