2024-06-19 15:15:27 +00:00
|
|
|
import Combine
|
|
|
|
|
|
|
|
final class AccountsMiddleware {
|
|
|
|
static let shared = AccountsMiddleware()
|
|
|
|
|
|
|
|
func middleware(state: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
|
|
|
|
switch action {
|
|
|
|
case .databaseAction(.storedAccountsLoaded(let accounts)):
|
|
|
|
return Just(.accountsAction(.accountsListUpdated(accounts: accounts)))
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
|
|
|
case .xmppAction(.clientConnectionChanged(let jid, let connectionStatus)):
|
|
|
|
return Future<AppAction, Never> { promise in
|
|
|
|
guard let account = state.accountsState.accounts.first(where: { $0.bareJid == jid }) else {
|
2024-07-16 13:01:27 +00:00
|
|
|
promise(.success(.empty("Account not found for jid \(jid)")))
|
2024-06-19 15:15:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if account.isTemp {
|
|
|
|
switch connectionStatus {
|
|
|
|
case .connected:
|
|
|
|
promise(.success(.accountsAction(.makeAccountPermanent(account: account))))
|
|
|
|
|
|
|
|
case .disconnected(let reason):
|
|
|
|
if reason != "No error!" {
|
|
|
|
promise(.success(.accountsAction(.addAccountError(jid: jid, reason: reason))))
|
|
|
|
} else {
|
2024-07-16 13:01:27 +00:00
|
|
|
promise(.success(.empty("Account disconnected with no error")))
|
2024-06-19 15:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2024-07-16 13:01:27 +00:00
|
|
|
promise(.success(.empty("Account connection status changed to \(connectionStatus)")))
|
2024-06-19 15:15:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-16 13:01:27 +00:00
|
|
|
promise(.success(.empty("Account is not temporary, ignoring")))
|
2024-06-19 15:15:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
|
|
|
default:
|
|
|
|
return Empty().eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|