This commit is contained in:
fmodf 2024-08-08 00:27:47 +02:00
parent 6a167f6c2c
commit a567c210c4
7 changed files with 57 additions and 34 deletions

View file

@ -7,4 +7,6 @@ enum ConversationAction: Codable {
case setReplyText(String)
case sendMediaMessages(from: String, to: String, messagesIds: [String], localFilesNames: [String])
case setArchivedMessagesRequested
}

View file

@ -0,0 +1,47 @@
import Combine
import Foundation
final class ArchivedMessagesMiddleware {
static let shared = ArchivedMessagesMiddleware()
func middleware(state: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
switch action {
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 = chat.participant
if let lastMessage = state.conversationsState.currentMessages.first {
promise(.success(.xmppAction(.xmppLoadArchivedMessages(jid: currentClient.bareJid, to: roster?.bareJid, fromDate: lastMessage.date))))
} else {
let date = Date()
let archivesRequestDate = Calendar.current.date(byAdding: .day, value: -Const.mamRequestLength, to: date) ?? date
promise(.success(.xmppAction(.xmppLoadArchivedMessages(jid: currentClient.bareJid, to: roster?.bareJid, fromDate: archivesRequestDate))))
}
// 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)")))
}
} else {
promise(.success(.info("MessageMiddleware: No client found for account \(chat.account), probably some error here")))
}
}
}
.eraseToAnyPublisher()
}
default:
return Empty().eraseToAnyPublisher()
}
}
}

View file

@ -1,32 +0,0 @@
import Combine
import Foundation
final class MessagesMiddleware {
static let shared = MessagesMiddleware()
func middleware(state: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
switch action {
case .conversationAction(.makeConversationActive(let chat, let roster)):
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)")))
}
} else {
promise(.success(.info("MessageMiddleware: No client found for account \(chat.account), probably some error here")))
}
}
}
.eraseToAnyPublisher()
default:
return Empty().eraseToAnyPublisher()
}
}
}

View file

@ -147,7 +147,7 @@ final class XMPPMiddleware {
return Deferred {
Future<AppAction, Never> { [weak self] promise in
self?.service.requestArchivedMessages(jid: jid, to: to, fromDate: fromDate)
promise(.success(.info("XMPPMiddleware: archived messages requested for \(jid) from \(fromDate)")))
promise(.success(.conversationAction(.setArchivedMessagesRequested)))
}
}
.eraseToAnyPublisher()

View file

@ -17,6 +17,9 @@ extension ConversationState {
state.replyText = text.makeReply
}
case .setArchivedMessagesRequested:
state.archivedMessagesRequested = true
default:
break
}

View file

@ -4,6 +4,8 @@ struct ConversationState: Stateable {
var currentMessages: [Message]
var replyText: String
var archivedMessagesRequested: Bool
}
// MARK: Init
@ -11,5 +13,6 @@ extension ConversationState {
init() {
currentMessages = []
replyText = ""
archivedMessagesRequested = false
}
}

View file

@ -13,7 +13,7 @@ let store = AppStore(
XMPPMiddleware.shared.middleware,
RostersMiddleware.shared.middleware,
ChatsMiddleware.shared.middleware,
MessagesMiddleware.shared.middleware,
ArchivedMessagesMiddleware.shared.middleware,
ConversationMiddleware.shared.middleware,
SharingMiddleware.shared.middleware,
FileMiddleware.shared.middleware