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

33 lines
1.6 KiB
Swift
Raw Normal View History

2024-07-23 09:58:26 +00:00
import Combine
import Foundation
final class MessagesMiddleware {
static let shared = MessagesMiddleware()
2024-07-23 14:55:38 +00:00
func middleware(state: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
2024-07-23 09:58:26 +00:00
switch action {
2024-07-23 14:55:38 +00:00
case .conversationAction(.makeConversationActive(let chat, let roster)):
2024-08-07 12:49:47 +00:00
return Deferred {
Future<AppAction, Never> { 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)")))
}
2024-07-23 14:55:38 +00:00
} else {
2024-08-07 12:49:47 +00:00
promise(.success(.info("MessageMiddleware: No client found for account \(chat.account), probably some error here")))
2024-07-23 14:55:38 +00:00
}
}
}
.eraseToAnyPublisher()
2024-07-23 09:58:26 +00:00
default:
return Empty().eraseToAnyPublisher()
}
}
}