This commit is contained in:
fmodf 2024-07-23 16:55:38 +02:00
parent 3fc0be729e
commit 822c4fe749
9 changed files with 48 additions and 10 deletions

View file

@ -10,7 +10,6 @@ enum AppAction: Codable {
case rostersAction(_ action: RostersAction)
case chatsAction(_ action: ChatsAction)
case conversationAction(_ action: ConversationAction)
case messagesAction(_ action: MessagesAction)
case sharingAction(_ action: SharingAction)
case fileAction(_ action: FileAction)
}

View file

@ -1,3 +0,0 @@
enum MessagesAction: Codable {
case currentConversationMessagesUpdated(messages: [Message])
}

View file

@ -1,3 +1,5 @@
import Foundation
enum XMPPAction: Codable {
case clientConnectionChanged(jid: String, state: ConnectionStatus)
case xmppMessageReceived(Message)
@ -10,4 +12,6 @@ enum XMPPAction: Codable {
case xmppSharingUploadFailed(msgId: String, reason: String)
case xmppSharingUploadSuccess(msgId: String, attachmentRemotePath: String)
case serverFeaturesLoaded(jid: String, features: [String])
case xmppLoadArchivedMessages(jid: String, to: String?, fromDate: Date)
}

View file

@ -488,7 +488,7 @@ private extension DatabaseMiddleware {
} receiveValue: { messages in
// messages
DispatchQueue.main.async {
store.dispatch(.messagesAction(.currentConversationMessagesUpdated(messages: messages)))
store.dispatch(.conversationAction(.messagesUpdated(messages: messages)))
}
}
.store(in: &conversationCancellables)

View file

@ -4,10 +4,23 @@ import Foundation
final class MessagesMiddleware {
static let shared = MessagesMiddleware()
func middleware(state _: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
func middleware(state: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
switch action {
case .messagesAction(.currentConversationMessagesUpdated(let messages)):
return Just(.conversationAction(.messagesUpdated(messages: messages)))
case .conversationAction(.makeConversationActive(let chat, let roster)):
return 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: -7, 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:

View file

@ -129,6 +129,13 @@ final class XMPPMiddleware {
}
.eraseToAnyPublisher()
case .xmppAction(.xmppLoadArchivedMessages(let jid, let to, let fromDate)):
return 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)")))
}
.eraseToAnyPublisher()
default:
return Empty().eraseToAnyPublisher()
}

View file

@ -13,7 +13,7 @@ extension AppState {
case .startAction(let action):
StartState.reducer(state: &state.startState, action: action)
case .databaseAction, .xmppAction, .fileAction, .info, .messagesAction:
case .databaseAction, .xmppAction, .fileAction, .info:
break // this actions are processed by other middlewares
case .accountsAction(let action):

View file

@ -250,4 +250,19 @@ final class XMPPService: ObservableObject {
}
}
}
func requestArchivedMessages(jid: String, to: String?, fromDate: Date) {
guard let client = getClient(for: jid) else {
return
}
client.module(.mam).queryItems(componentJid: JID(jid), with: JID(to), start: fromDate, end: Date(), queryId: UUID().uuidString) { result in
switch result {
case .success(let response):
print("MAM response: \(response)")
case .failure(let error):
print("MAM error: \(error)")
}
}
}
}

View file

@ -43,4 +43,7 @@ enum Const {
// Size for attachment preview
static let attachmentPreviewSize = UIScreen.main.bounds.width * 0.5
// Lenght in days for MAM request
static let mamRequestLength = 7
}