2024-11-21 16:03:55 +00:00
|
|
|
import Foundation
|
2024-11-22 13:12:09 +00:00
|
|
|
import monalxmpp
|
|
|
|
import SwiftUI
|
2024-11-21 16:03:55 +00:00
|
|
|
|
2024-11-22 13:12:09 +00:00
|
|
|
// MARK: - Account
|
2024-11-21 16:03:55 +00:00
|
|
|
enum AccountsAvailability {
|
|
|
|
case noAccounts
|
|
|
|
case allDisabled
|
|
|
|
case someEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Account: Identifiable {
|
|
|
|
let id: Int
|
|
|
|
let local: String
|
|
|
|
let domain: String
|
|
|
|
let resource: String
|
|
|
|
let isEnabled: Bool
|
|
|
|
|
|
|
|
var jid: String {
|
|
|
|
"\(local)@\(domain)"
|
|
|
|
}
|
|
|
|
|
|
|
|
init?(_ dict: NSDictionary) {
|
|
|
|
guard let id = dict.value(forKey: "account_id") as? Int else { return nil }
|
|
|
|
guard let local = dict.value(forKey: "username") as? String else { return nil }
|
|
|
|
guard let domain = dict.value(forKey: "domain") as? String else { return nil }
|
|
|
|
guard let resource = dict.value(forKey: "resource") as? String else { return nil }
|
|
|
|
guard let isEnabled = dict.value(forKey: "enabled") as? Bool else { return nil }
|
|
|
|
|
|
|
|
self.id = id
|
|
|
|
self.local = local
|
|
|
|
self.domain = domain
|
|
|
|
self.resource = resource
|
|
|
|
self.isEnabled = isEnabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-22 13:12:09 +00:00
|
|
|
extension Account: UniversalInputSelectionElement {
|
|
|
|
var icon: Image? { nil }
|
|
|
|
var text: String? { jid }
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Contact
|
2024-11-21 16:03:55 +00:00
|
|
|
struct Contact: Identifiable {
|
2024-11-22 13:12:09 +00:00
|
|
|
let ownerId: Int
|
|
|
|
let contactJid: String
|
|
|
|
let name: String?
|
2024-11-21 16:03:55 +00:00
|
|
|
|
2024-11-22 13:12:09 +00:00
|
|
|
var id: String { contactJid }
|
|
|
|
|
|
|
|
init?(_ obj: MLContact) {
|
|
|
|
ownerId = obj.accountID.intValue
|
|
|
|
contactJid = obj.contactJid
|
2024-11-22 16:34:56 +00:00
|
|
|
name = obj.nickName.isEmpty ? nil : obj.nickName
|
2024-11-22 13:12:09 +00:00
|
|
|
}
|
2024-11-21 16:03:55 +00:00
|
|
|
}
|
2024-11-23 16:23:56 +00:00
|
|
|
|
|
|
|
// MARK: - Chat
|
|
|
|
struct Chat: Identifiable {
|
|
|
|
let accountId: Int
|
|
|
|
let participantJid: String
|
|
|
|
let participantName: String?
|
|
|
|
|
|
|
|
var id: String {
|
|
|
|
"\(accountId)_\(participantJid)"
|
|
|
|
}
|
|
|
|
|
|
|
|
var name: String {
|
|
|
|
if let participantName, !participantName.isEmpty {
|
|
|
|
return participantName
|
|
|
|
} else {
|
|
|
|
return participantJid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init?(_ obj: MLContact) {
|
|
|
|
guard let accId = obj.accountID as? Int else { return nil }
|
|
|
|
accountId = accId
|
|
|
|
participantJid = obj.contactJid
|
|
|
|
participantName = obj.nickName
|
|
|
|
}
|
|
|
|
}
|
2024-11-24 18:42:46 +00:00
|
|
|
|
|
|
|
// MARK: Message
|
|
|
|
struct Message: Identifiable {
|
|
|
|
let accountId: Int
|
|
|
|
let participantJid: String
|
|
|
|
let dbId: Int
|
2024-11-27 15:49:36 +00:00
|
|
|
let stanzaId: String
|
2024-11-24 18:42:46 +00:00
|
|
|
let timestamp: Date
|
|
|
|
let body: String
|
2024-11-25 12:02:36 +00:00
|
|
|
let isInbound: Bool
|
2024-11-28 15:50:59 +00:00
|
|
|
let encrypted: Bool
|
2024-11-24 18:42:46 +00:00
|
|
|
|
|
|
|
var id: String {
|
|
|
|
"\(accountId)|\(dbId)"
|
|
|
|
}
|
|
|
|
|
|
|
|
init?(_ obj: MLMessage) {
|
|
|
|
guard let accId = obj.accountID as? Int, let dbId = obj.messageDBId as? Int else { return nil }
|
|
|
|
accountId = accId
|
|
|
|
participantJid = obj.participantJid
|
|
|
|
self.dbId = dbId
|
2024-11-27 15:49:36 +00:00
|
|
|
stanzaId = obj.stanzaId
|
2024-11-24 18:42:46 +00:00
|
|
|
timestamp = obj.timestamp
|
|
|
|
body = obj.messageText
|
2024-11-25 12:02:36 +00:00
|
|
|
isInbound = obj.inbound
|
2024-11-28 15:50:59 +00:00
|
|
|
encrypted = obj.encrypted
|
2024-11-24 18:42:46 +00:00
|
|
|
}
|
|
|
|
}
|