From a567c210c461894dbcd27da47c7d1faafe93112e Mon Sep 17 00:00:00 2001 From: fmodf Date: Thu, 8 Aug 2024 00:27:47 +0200 Subject: [PATCH] wip --- .../AppCore/Actions/ConversationActions.swift | 2 + .../ArchivedMessagesMiddleware.swift | 47 +++++++++++++++++++ .../Middlewares/MessagesMiddleware.swift | 32 ------------- .../AppCore/Middlewares/XMPPMiddleware.swift | 2 +- .../Reducers/ConversationReducer.swift | 3 ++ .../AppCore/State/ConversationState.swift | 3 ++ .../ConversationsClassicApp.swift | 2 +- 7 files changed, 57 insertions(+), 34 deletions(-) create mode 100644 ConversationsClassic/AppCore/Middlewares/ArchivedMessagesMiddleware.swift delete mode 100644 ConversationsClassic/AppCore/Middlewares/MessagesMiddleware.swift diff --git a/ConversationsClassic/AppCore/Actions/ConversationActions.swift b/ConversationsClassic/AppCore/Actions/ConversationActions.swift index 6bb8b32..6244b94 100644 --- a/ConversationsClassic/AppCore/Actions/ConversationActions.swift +++ b/ConversationsClassic/AppCore/Actions/ConversationActions.swift @@ -7,4 +7,6 @@ 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 new file mode 100644 index 0000000..d4fbfec --- /dev/null +++ b/ConversationsClassic/AppCore/Middlewares/ArchivedMessagesMiddleware.swift @@ -0,0 +1,47 @@ +import Combine +import Foundation + +final class ArchivedMessagesMiddleware { + static let shared = ArchivedMessagesMiddleware() + + 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() + } + + default: + return Empty().eraseToAnyPublisher() + } + } +} diff --git a/ConversationsClassic/AppCore/Middlewares/MessagesMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/MessagesMiddleware.swift deleted file mode 100644 index 12aa1b8..0000000 --- a/ConversationsClassic/AppCore/Middlewares/MessagesMiddleware.swift +++ /dev/null @@ -1,32 +0,0 @@ -import Combine -import Foundation - -final class MessagesMiddleware { - static let shared = MessagesMiddleware() - - func middleware(state: AppState, action: AppAction) -> AnyPublisher { - switch action { - case .conversationAction(.makeConversationActive(let chat, let roster)): - 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 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() - - default: - return Empty().eraseToAnyPublisher() - } - } -} diff --git a/ConversationsClassic/AppCore/Middlewares/XMPPMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/XMPPMiddleware.swift index 40a6e95..3e9cef3 100644 --- a/ConversationsClassic/AppCore/Middlewares/XMPPMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/XMPPMiddleware.swift @@ -147,7 +147,7 @@ final class XMPPMiddleware { return Deferred { Future { [weak self] promise in self?.service.requestArchivedMessages(jid: jid, to: to, fromDate: fromDate) - promise(.success(.info("XMPPMiddleware: archived messages requested for \(jid) from \(fromDate)"))) + promise(.success(.conversationAction(.setArchivedMessagesRequested))) } } .eraseToAnyPublisher() diff --git a/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift b/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift index cc76c62..da19de4 100644 --- a/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift +++ b/ConversationsClassic/AppCore/Reducers/ConversationReducer.swift @@ -17,6 +17,9 @@ 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 9267bbd..be827d0 100644 --- a/ConversationsClassic/AppCore/State/ConversationState.swift +++ b/ConversationsClassic/AppCore/State/ConversationState.swift @@ -4,6 +4,8 @@ struct ConversationState: Stateable { var currentMessages: [Message] var replyText: String + + var archivedMessagesRequested: Bool } // MARK: Init @@ -11,5 +13,6 @@ extension ConversationState { init() { currentMessages = [] replyText = "" + archivedMessagesRequested = false } } diff --git a/ConversationsClassic/ConversationsClassicApp.swift b/ConversationsClassic/ConversationsClassicApp.swift index 18c6225..eff8cbd 100644 --- a/ConversationsClassic/ConversationsClassicApp.swift +++ b/ConversationsClassic/ConversationsClassicApp.swift @@ -13,7 +13,7 @@ let store = AppStore( XMPPMiddleware.shared.middleware, RostersMiddleware.shared.middleware, ChatsMiddleware.shared.middleware, - MessagesMiddleware.shared.middleware, + ArchivedMessagesMiddleware.shared.middleware, ConversationMiddleware.shared.middleware, SharingMiddleware.shared.middleware, FileMiddleware.shared.middleware