2024-08-07 22:27:47 +00:00
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
final class ArchivedMessagesMiddleware {
|
|
|
|
static let shared = ArchivedMessagesMiddleware()
|
|
|
|
|
2024-08-08 10:42:48 +00:00
|
|
|
func middleware(state _: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
|
2024-08-07 22:27:47 +00:00
|
|
|
switch action {
|
2024-08-08 10:42:48 +00:00
|
|
|
// case .conversationAction(.messagesUpdated(let messages)):
|
|
|
|
// if state.conversationsState.archivedMessagesRequested {
|
|
|
|
// return Empty().eraseToAnyPublisher()
|
|
|
|
// } else {
|
|
|
|
// guard let chat = state.conversationsState.currentChat else {
|
|
|
|
// return Empty().eraseToAnyPublisher()
|
|
|
|
// }
|
|
|
|
// 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 roster = state.conversationsState.currentRoster
|
|
|
|
// let date = self.archivesRequestDate(from: messages)
|
|
|
|
// promise(.success(.xmppAction(.xmppLoadArchivedMessages(jid: currentClient.bareJid, to: roster?.bareJid, fromDate: date))))
|
|
|
|
// } else {
|
|
|
|
// promise(.success(.info("MessageMiddleware: XEP-0313 not supported for client \(currentClient.bareJid)")))
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// promise(.success(.info("MessageMiddleware: No client found for account \(chat.account), probably some error here")))
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// .eraseToAnyPublisher()
|
|
|
|
// }
|
2024-08-07 22:27:47 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return Empty().eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-08 10:42:48 +00:00
|
|
|
|
|
|
|
private extension ArchivedMessagesMiddleware {
|
|
|
|
func archivesRequestDate(from messages: [Message]) -> Date {
|
|
|
|
if let lastDate = messages.first?.date {
|
|
|
|
return lastDate
|
|
|
|
} else {
|
2024-08-09 14:33:22 +00:00
|
|
|
return Calendar.current.date(byAdding: .day, value: -Const.mamRequestDaysLength, to: Date()) ?? Date()
|
2024-08-08 10:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|