From 3fc0be729ef84c4b0ec8e1d5645a9d3010cc9163 Mon Sep 17 00:00:00 2001 From: fmodf Date: Tue, 23 Jul 2024 11:58:26 +0200 Subject: [PATCH] wip --- .../AppCore/Actions/AppActions.swift | 1 + .../AppCore/Actions/MessagesActions.swift | 3 ++ .../Middlewares/DatabaseMiddleware.swift | 2 +- .../Middlewares/MessagesMiddleware.swift | 17 ++++++++ .../AppCore/Reducers/AppReducer.swift | 2 +- .../AppCore/XMPP/XMPPService.swift | 40 ++++++++++--------- .../ConversationsClassicApp.swift | 1 + 7 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 ConversationsClassic/AppCore/Actions/MessagesActions.swift create mode 100644 ConversationsClassic/AppCore/Middlewares/MessagesMiddleware.swift diff --git a/ConversationsClassic/AppCore/Actions/AppActions.swift b/ConversationsClassic/AppCore/Actions/AppActions.swift index 2cb786d..ea4abb5 100644 --- a/ConversationsClassic/AppCore/Actions/AppActions.swift +++ b/ConversationsClassic/AppCore/Actions/AppActions.swift @@ -10,6 +10,7 @@ enum AppAction: Codable { case rostersAction(_ action: RostersAction) case chatsAction(_ action: ChatsAction) case conversationAction(_ action: ConversationAction) + case messagesAction(_ action: MessagesAction) case sharingAction(_ action: SharingAction) case fileAction(_ action: FileAction) } diff --git a/ConversationsClassic/AppCore/Actions/MessagesActions.swift b/ConversationsClassic/AppCore/Actions/MessagesActions.swift new file mode 100644 index 0000000..d4e3895 --- /dev/null +++ b/ConversationsClassic/AppCore/Actions/MessagesActions.swift @@ -0,0 +1,3 @@ +enum MessagesAction: Codable { + case currentConversationMessagesUpdated(messages: [Message]) +} diff --git a/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift index 3aa6353..1be36df 100644 --- a/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/DatabaseMiddleware.swift @@ -488,7 +488,7 @@ private extension DatabaseMiddleware { } receiveValue: { messages in // messages DispatchQueue.main.async { - store.dispatch(.conversationAction(.messagesUpdated(messages: messages))) + store.dispatch(.messagesAction(.currentConversationMessagesUpdated(messages: messages))) } } .store(in: &conversationCancellables) diff --git a/ConversationsClassic/AppCore/Middlewares/MessagesMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/MessagesMiddleware.swift new file mode 100644 index 0000000..37be04e --- /dev/null +++ b/ConversationsClassic/AppCore/Middlewares/MessagesMiddleware.swift @@ -0,0 +1,17 @@ +import Combine +import Foundation + +final class MessagesMiddleware { + static let shared = MessagesMiddleware() + + func middleware(state _: AppState, action: AppAction) -> AnyPublisher { + switch action { + case .messagesAction(.currentConversationMessagesUpdated(let messages)): + return Just(.conversationAction(.messagesUpdated(messages: messages))) + .eraseToAnyPublisher() + + default: + return Empty().eraseToAnyPublisher() + } + } +} diff --git a/ConversationsClassic/AppCore/Reducers/AppReducer.swift b/ConversationsClassic/AppCore/Reducers/AppReducer.swift index ff67556..3ee765b 100644 --- a/ConversationsClassic/AppCore/Reducers/AppReducer.swift +++ b/ConversationsClassic/AppCore/Reducers/AppReducer.swift @@ -13,7 +13,7 @@ extension AppState { case .startAction(let action): StartState.reducer(state: &state.startState, action: action) - case .databaseAction, .xmppAction, .fileAction, .info: + case .databaseAction, .xmppAction, .fileAction, .info, .messagesAction: break // this actions are processed by other middlewares case .accountsAction(let action): diff --git a/ConversationsClassic/AppCore/XMPP/XMPPService.swift b/ConversationsClassic/AppCore/XMPP/XMPPService.swift index 1b9b405..1c59e4b 100644 --- a/ConversationsClassic/AppCore/XMPP/XMPPService.swift +++ b/ConversationsClassic/AppCore/XMPP/XMPPService.swift @@ -74,27 +74,31 @@ final class XMPPService: ObservableObject { .store(in: &clientMessagesCancellables) // enable carbons if available - client.module(.messageCarbons).$isAvailable.filter { $0 }.sink(receiveValue: { [weak client] _ in - client?.module(.messageCarbons).enable() - }).store(in: &clientMessagesCancellables) + client.module(.messageCarbons).$isAvailable.filter { $0 } + .sink(receiveValue: { [weak client] _ in + client?.module(.messageCarbons).enable() + }) + .store(in: &clientMessagesCancellables) // MAM - client.module(.mam).$availableVersions.sink(receiveValue: { [weak client] versions in - guard !versions.isEmpty, let client = client else { - return - } - print("MAM available versions: \(versions)") - // MessageEventHandler.syncMessagesScheduled(for: client) - }).store(in: &clientMessagesCancellables) + // client.module(.mam).$availableVersions.sink(receiveValue: { [weak client] versions in + // guard !versions.isEmpty, let client = client else { + // return + // } + // print("MAM available versions: \(versions)") + // // MessageEventHandler.syncMessagesScheduled(for: client) + // }).store(in: &clientMessagesCancellables) - client.module(.mam).archivedMessagesPublisher.sink(receiveValue: { archived in - print("MAM archived message: \(archived.message)") - - // guard let conversation = MessageEventHandler.conversationKey(for: archived.message, on: account) else { - // return - // } - // DBChatHistoryStore.instance.append(for: conversation, message: archived.message, source: .archive(source: archived.source, version: archived.query.version, messageId: archived.messageId, timestamp: archived.timestamp)) - }).store(in: &clientMessagesCancellables) + client.module(.mam).archivedMessagesPublisher + .sink(receiveValue: { [weak self] archived in + self?.clientMessagesPublisher.send((client, archived.message)) + // print("MAM archived message: \(archived.message)") + // guard let conversation = MessageEventHandler.conversationKey(for: archived.message, on: account) else { + // return + // } + // DBChatHistoryStore.instance.append(for: conversation, message: archived.message, source: .archive(source: archived.source, version: archived.query.version, messageId: archived.messageId, timestamp: archived.timestamp)) + }) + .store(in: &clientMessagesCancellables) // finally, do login client.login() diff --git a/ConversationsClassic/ConversationsClassicApp.swift b/ConversationsClassic/ConversationsClassicApp.swift index 570f73c..18c6225 100644 --- a/ConversationsClassic/ConversationsClassicApp.swift +++ b/ConversationsClassic/ConversationsClassicApp.swift @@ -13,6 +13,7 @@ let store = AppStore( XMPPMiddleware.shared.middleware, RostersMiddleware.shared.middleware, ChatsMiddleware.shared.middleware, + MessagesMiddleware.shared.middleware, ConversationMiddleware.shared.middleware, SharingMiddleware.shared.middleware, FileMiddleware.shared.middleware