another.im-ios/ConversationsClassic/AppCore/Middlewares/ConversationMiddleware.swift

43 lines
1.8 KiB
Swift
Raw Normal View History

2024-06-21 10:32:16 +00:00
import Combine
final class ConversationMiddleware {
static let shared = ConversationMiddleware()
2024-06-24 13:28:26 +00:00
func middleware(state: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
2024-06-21 10:32:16 +00:00
switch action {
2024-06-21 10:42:50 +00:00
case .chatsAction(.chatStarted(let chat)):
2024-06-26 06:51:56 +00:00
return Future<AppAction, Never> { promise in
let roster = state.rostersState.rosters
.first { $0.bareJid == chat.account && $0.contactBareJid == chat.participant }
promise(.success(.conversationAction(.makeConversationActive(chat: chat, roster: roster))))
}
.eraseToAnyPublisher()
2024-06-21 10:42:50 +00:00
case .conversationAction(.makeConversationActive):
return Just(AppAction.changeFlow(.conversation)).eraseToAnyPublisher()
2024-06-24 13:28:26 +00:00
case .xmppAction(.xmppMessageReceived(let message)):
return Future<AppAction, Never> { promise in
let currentChat = state.conversationsState.currentChat
2024-06-26 06:51:56 +00:00
if message.from == currentChat?.participant, message.to == currentChat?.account, message.contentType != .typing {
2024-06-24 13:28:26 +00:00
promise(.success(.conversationAction(.messageForCurrentConversationReceived(message))))
} else {
promise(.success(.empty))
}
}
.eraseToAnyPublisher()
2024-06-26 06:51:56 +00:00
case .conversationAction(.messageForCurrentConversationReceived(let message)):
return Future<AppAction, Never> { promise in
var currentMessages = state.conversationsState.currentMessages
currentMessages.append(message)
promise(.success(.conversationAction(.messagesUpdated(messages: currentMessages))))
}
.eraseToAnyPublisher()
2024-06-21 10:32:16 +00:00
default:
return Empty().eraseToAnyPublisher()
}
}
}