another.im-ios/AnotherIM/AppData/Model/Chat.swift

44 lines
1 KiB
Swift
Raw Permalink Normal View History

import Foundation
import GRDB
enum ConversationType: Int, Codable, DatabaseValueConvertible {
case chat = 0
case room = 1
case channel = 2
}
struct Chat: DBStorable {
static let databaseTableName = "chats"
var id: String
var account: String
var participant: String
var type: ConversationType
2024-09-19 15:14:05 +00:00
var encrypted: Bool
}
extension Chat: Equatable {}
extension Chat {
func fetchRoster() async throws -> Roster {
try await Database.shared.dbQueue.read { db in
guard
let roster = try Roster
.filter(Column("bareJid") == account && Column("contactBareJid") == participant)
.fetchOne(db)
else {
throw AppError.rosterNotFound
}
return roster
}
}
2024-09-20 15:32:10 +00:00
func setEncrypted(_ encrypted: Bool) async throws {
try await Database.shared.dbQueue.write { db in
var chat = self
chat.encrypted = encrypted
try chat.update(db)
}
}
}