This commit is contained in:
fmodf 2024-07-23 11:58:26 +02:00
parent 7ada932664
commit 3fc0be729e
7 changed files with 46 additions and 20 deletions

View file

@ -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)
}

View file

@ -0,0 +1,3 @@
enum MessagesAction: Codable {
case currentConversationMessagesUpdated(messages: [Message])
}

View file

@ -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)

View file

@ -0,0 +1,17 @@
import Combine
import Foundation
final class MessagesMiddleware {
static let shared = MessagesMiddleware()
func middleware(state _: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
switch action {
case .messagesAction(.currentConversationMessagesUpdated(let messages)):
return Just(.conversationAction(.messagesUpdated(messages: messages)))
.eraseToAnyPublisher()
default:
return Empty().eraseToAnyPublisher()
}
}
}

View file

@ -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):

View file

@ -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).$isAvailable.filter { $0 }
.sink(receiveValue: { [weak client] _ in
client?.module(.messageCarbons).enable()
}).store(in: &clientMessagesCancellables)
})
.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).archivedMessagesPublisher.sink(receiveValue: { archived in
print("MAM archived message: \(archived.message)")
// 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: { [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)
})
.store(in: &clientMessagesCancellables)
// finally, do login
client.login()

View file

@ -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