2024-08-11 00:28:01 +00:00
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
import GRDB
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
final class ClientsStore: ObservableObject {
|
|
|
|
static let shared = ClientsStore()
|
|
|
|
|
|
|
|
@Published private(set) var ready = false
|
|
|
|
@Published private(set) var clients: [Client] = []
|
2024-08-11 22:30:01 +00:00
|
|
|
@Published private(set) var actualRosters: [Roster] = []
|
2024-08-12 19:29:16 +00:00
|
|
|
@Published private(set) var actualChats: [Chat] = []
|
2024-08-11 00:28:01 +00:00
|
|
|
|
2024-08-11 22:30:01 +00:00
|
|
|
private var credentialsCancellable: AnyCancellable?
|
|
|
|
private var rostersCancellable: AnyCancellable?
|
2024-08-12 19:29:16 +00:00
|
|
|
private var chatsCancellable: AnyCancellable?
|
2024-08-11 19:01:48 +00:00
|
|
|
|
2024-08-11 20:33:04 +00:00
|
|
|
init() {
|
2024-08-11 22:30:01 +00:00
|
|
|
credentialsCancellable = ValueObservation
|
|
|
|
.tracking { db in
|
|
|
|
try Credentials.fetchAll(db)
|
|
|
|
}
|
|
|
|
.publisher(in: Database.shared.dbQueue)
|
|
|
|
.catch { _ in Just([]) }
|
|
|
|
.sink { [weak self] creds in
|
|
|
|
self?.processCredentials(creds)
|
|
|
|
}
|
2024-08-11 00:28:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 19:01:48 +00:00
|
|
|
private func processCredentials(_ credentials: [Credentials]) {
|
|
|
|
let existsJids = Set(clients.map { $0.credentials.bareJid })
|
|
|
|
let credentialsJids = Set(credentials.map { $0.bareJid })
|
|
|
|
|
|
|
|
let forAdd = credentials.filter { !existsJids.contains($0.bareJid) }
|
|
|
|
let newClients = forAdd.map { Client(credentials: $0) }
|
|
|
|
|
|
|
|
let forRemove = clients.filter { !credentialsJids.contains($0.credentials.bareJid) }
|
|
|
|
forRemove.forEach { $0.disconnect() }
|
|
|
|
|
|
|
|
var updatedClients = clients.filter { credentialsJids.contains($0.credentials.bareJid) }
|
|
|
|
updatedClients.append(contentsOf: newClients)
|
|
|
|
clients = updatedClients
|
2024-08-11 22:30:01 +00:00
|
|
|
|
|
|
|
if !ready {
|
|
|
|
ready = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resubscribeRosters()
|
2024-08-12 19:29:16 +00:00
|
|
|
resubscribeChats()
|
|
|
|
reconnectAll()
|
2024-08-11 19:01:48 +00:00
|
|
|
}
|
2024-08-11 21:52:01 +00:00
|
|
|
|
|
|
|
private func client(for credentials: Credentials) -> Client? {
|
|
|
|
clients.first { $0.credentials == credentials }
|
|
|
|
}
|
2024-08-11 19:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension ClientsStore {
|
2024-08-11 20:33:04 +00:00
|
|
|
func tryLogin(_ jidStr: String, _ pass: String) async throws {
|
|
|
|
// login with fake timeout
|
|
|
|
async let sleep: Void? = try? await Task.sleep(nanoseconds: 1 * NSEC_PER_SEC)
|
|
|
|
async let request = try await Client.tryLogin(with: .init(bareJid: jidStr, pass: pass, isActive: true))
|
|
|
|
let client = try await(request, sleep).0
|
|
|
|
|
2024-08-11 00:28:01 +00:00
|
|
|
clients.append(client)
|
2024-08-11 20:33:04 +00:00
|
|
|
try? await client.credentials.save()
|
2024-08-11 00:28:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 19:29:16 +00:00
|
|
|
private func reconnectAll() {
|
2024-08-11 19:01:48 +00:00
|
|
|
Task {
|
|
|
|
await withTaskGroup(of: Void.self) { taskGroup in
|
|
|
|
for client in clients {
|
|
|
|
taskGroup.addTask {
|
|
|
|
await client.connect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-11 00:28:01 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-11 20:33:04 +00:00
|
|
|
|
|
|
|
extension ClientsStore {
|
2024-08-11 22:30:01 +00:00
|
|
|
private func resubscribeRosters() {
|
|
|
|
let clientsJids = clients
|
|
|
|
.filter { $0.state != .disabled }
|
|
|
|
.map { $0.credentials.bareJid }
|
|
|
|
|
|
|
|
rostersCancellable = ValueObservation.tracking { db in
|
|
|
|
try Roster
|
|
|
|
.filter(clientsJids.contains(Column("bareJid")))
|
|
|
|
.filter(Column("locallyDeleted") == false)
|
|
|
|
.fetchAll(db)
|
|
|
|
}
|
|
|
|
.publisher(in: Database.shared.dbQueue)
|
|
|
|
.catch { _ in Just([]) }
|
|
|
|
.sink { [weak self] rosters in
|
|
|
|
self?.actualRosters = rosters
|
|
|
|
}
|
|
|
|
}
|
2024-08-11 23:16:09 +00:00
|
|
|
|
|
|
|
func addRoster(_ credentials: Credentials, contactJID: String, name: String?, groups: [String]) async throws {
|
|
|
|
// check that roster exist in db as locally deleted and undelete it
|
|
|
|
let deletedLocally = try await Roster.fetchDeletedLocally()
|
|
|
|
if var roster = deletedLocally.first(where: { $0.contactBareJid == contactJID }) {
|
|
|
|
try await roster.setLocallyDeleted(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new roster
|
|
|
|
guard let client = client(for: credentials) else {
|
|
|
|
throw ClientStoreError.clientNotFound
|
|
|
|
}
|
|
|
|
try await client.addRoster(contactJID, name: name, groups: groups)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteRoster(_ roster: Roster) async throws {
|
|
|
|
guard let client = clients.first(where: { $0.credentials.bareJid == roster.bareJid }) else {
|
|
|
|
throw ClientStoreError.clientNotFound
|
|
|
|
}
|
|
|
|
try await client.deleteRoster(roster)
|
|
|
|
}
|
2024-08-11 20:33:04 +00:00
|
|
|
}
|
2024-08-12 19:29:16 +00:00
|
|
|
|
|
|
|
extension ClientsStore {
|
|
|
|
private func resubscribeChats() {
|
|
|
|
let clientsJids = clients
|
|
|
|
.filter { $0.state != .disabled }
|
|
|
|
.map { $0.credentials.bareJid }
|
|
|
|
|
|
|
|
chatsCancellable = ValueObservation.tracking { db in
|
|
|
|
try Chat
|
|
|
|
.filter(clientsJids.contains(Column("account")))
|
|
|
|
.fetchAll(db)
|
|
|
|
}
|
|
|
|
.publisher(in: Database.shared.dbQueue)
|
|
|
|
.catch { _ in Just([]) }
|
|
|
|
.sink { [weak self] chats in
|
|
|
|
self?.actualChats = chats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-13 08:40:27 +00:00
|
|
|
|
|
|
|
extension ClientsStore {
|
2024-08-17 16:15:05 +00:00
|
|
|
func conversation(for roster: Roster) async throws -> ConversationStore {
|
2024-08-13 08:40:27 +00:00
|
|
|
while !ready {
|
|
|
|
await Task.yield()
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let client = clients.first(where: { $0.credentials.bareJid == roster.bareJid }) else {
|
|
|
|
throw ClientStoreError.clientNotFound
|
|
|
|
}
|
|
|
|
|
2024-08-17 16:15:05 +00:00
|
|
|
return ConversationStore(roster: roster, client: client)
|
2024-08-13 08:40:27 +00:00
|
|
|
}
|
2024-08-14 09:29:51 +00:00
|
|
|
|
2024-08-17 16:15:05 +00:00
|
|
|
func conversation(for chat: Chat) async throws -> ConversationStore {
|
2024-08-14 09:29:51 +00:00
|
|
|
while !ready {
|
|
|
|
await Task.yield()
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let client = clients.first(where: { $0.credentials.bareJid == chat.account }) else {
|
|
|
|
throw ClientStoreError.clientNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
let roster = try await chat.fetchRoster()
|
2024-08-17 16:15:05 +00:00
|
|
|
return ConversationStore(roster: roster, client: client)
|
2024-08-14 09:29:51 +00:00
|
|
|
}
|
2024-08-13 08:40:27 +00:00
|
|
|
}
|