another.im-ios/ConversationsClassic/AppData/Client/Client.swift

130 lines
4.1 KiB
Swift
Raw Normal View History

2024-08-11 00:28:01 +00:00
import Combine
import Foundation
import Martin
enum ClientState: Equatable {
enum ClientConnectionState {
case connected
case disconnected
}
case disabled
case enabled(ClientConnectionState)
}
final class Client: ObservableObject {
@Published private(set) var state: ClientState = .enabled(.disconnected)
@Published private(set) var credentials: Credentials
private var connection: XMPPClient
private var connectionCancellable: AnyCancellable?
2024-08-11 11:39:17 +00:00
private var rosterManager = ClientMartinRosterManager()
2024-08-11 00:28:01 +00:00
init(credentials: Credentials) {
self.credentials = credentials
state = credentials.isActive ? .enabled(.disconnected) : .disabled
connection = Self.prepareConnection(credentials, rosterManager)
connectionCancellable = connection.$state
.sink { [weak self] state in
guard let self = self else { return }
guard self.credentials.isActive else {
self.state = .disabled
return
}
switch state {
case .connected:
self.state = .enabled(.connected)
default:
self.state = .enabled(.disconnected)
}
}
}
2024-08-11 19:01:48 +00:00
}
2024-08-11 00:28:01 +00:00
2024-08-11 19:01:48 +00:00
extension Client {
2024-08-11 17:07:02 +00:00
func addRoster(_ jid: String, name: String?, groups: [String]) async throws {
2024-08-11 00:28:01 +00:00
_ = try await connection.module(.roster).addItem(
2024-08-11 17:07:02 +00:00
jid: JID(jid),
name: name,
groups: groups
2024-08-11 00:28:01 +00:00
)
}
func deleteRoster(_ roster: Roster) async throws {
_ = try await connection.module(.roster).removeItem(jid: JID(roster.contactBareJid))
}
2024-08-11 19:01:48 +00:00
func connect() async {
guard credentials.isActive, state == .enabled(.disconnected) else {
return
}
try? await connection.loginAndWait()
}
func disconnect() {
_ = connection.disconnect()
}
2024-08-11 00:28:01 +00:00
}
extension Client {
enum ClientLoginResult {
case success(Client)
case failure
}
static func tryLogin(with credentials: Credentials) async -> ClientLoginResult {
let client = Client(credentials: credentials)
do {
try await client.connection.loginAndWait()
return .success(client)
} catch {
return .failure
}
}
}
private extension Client {
static func prepareConnection(_ credentials: Credentials, _ roster: RosterManager) -> XMPPClient {
let client = XMPPClient()
// register modules
// core modules RFC 6120
client.modulesManager.register(StreamFeaturesModule())
client.modulesManager.register(SaslModule())
client.modulesManager.register(AuthModule())
client.modulesManager.register(SessionEstablishmentModule())
client.modulesManager.register(ResourceBinderModule())
client.modulesManager.register(DiscoveryModule(identity: .init(category: "client", type: "iOS", name: Const.appName)))
// messaging modules RFC 6121
client.modulesManager.register(RosterModule(rosterManager: roster))
client.modulesManager.register(PresenceModule())
// client.modulesManager.register(PubSubModule())
// client.modulesManager.register(MessageModule(chatManager: manager))
// client.modulesManager.register(MessageArchiveManagementModule())
// client.modulesManager.register(MessageCarbonsModule())
// file transfer modules
// client.modulesManager.register(HttpFileUploadModule())
// extensions
client.modulesManager.register(SoftwareVersionModule())
client.modulesManager.register(PingModule())
client.connectionConfiguration.userJid = .init(credentials.bareJid)
client.connectionConfiguration.credentials = .password(password: credentials.pass)
// group chats
// client.modulesManager.register(MucModule(roomManager: manager))
// channels
// client.modulesManager.register(MixModule(channelManager: manager))
// add client to clients
return client
}
}