2024-06-19 15:15:27 +00:00
|
|
|
import Combine
|
|
|
|
|
|
|
|
final class ChatsMiddleware {
|
|
|
|
static let shared = ChatsMiddleware()
|
|
|
|
|
2024-06-20 05:27:13 +00:00
|
|
|
func middleware(state: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
|
2024-06-19 15:15:27 +00:00
|
|
|
switch action {
|
|
|
|
case .databaseAction(.storedChatsLoaded(let chats)):
|
|
|
|
return Just(.chatsAction(.chatsListUpdated(chats: chats)))
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
2024-06-20 05:27:13 +00:00
|
|
|
case .chatsAction(.startChat(accountJid: let accountJid, participantJid: let participantJid)):
|
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
// find existing chat
|
|
|
|
let exist = state.chatsState.chats.first {
|
|
|
|
$0.account == accountJid && $0.participant == participantJid
|
|
|
|
}
|
|
|
|
promise(.success(.empty))
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
2024-06-19 15:15:27 +00:00
|
|
|
default:
|
|
|
|
return Empty().eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|