2024-08-13 14:13:00 +00:00
|
|
|
import Foundation
|
|
|
|
import GRDB
|
|
|
|
import Martin
|
|
|
|
|
|
|
|
enum MessageType: String, Codable, DatabaseValueConvertible {
|
|
|
|
case chat
|
|
|
|
case groupchat
|
|
|
|
case error
|
|
|
|
}
|
|
|
|
|
|
|
|
enum MessageContentType: Codable & Equatable, DatabaseValueConvertible {
|
|
|
|
case text
|
|
|
|
case typing
|
|
|
|
}
|
|
|
|
|
|
|
|
enum MessageStatus: Int, Codable, DatabaseValueConvertible {
|
|
|
|
case pending
|
|
|
|
case sent
|
|
|
|
case error
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Message: DBStorable, Equatable {
|
|
|
|
static let databaseTableName = "messages"
|
|
|
|
|
|
|
|
let id: String
|
|
|
|
let type: MessageType
|
|
|
|
let date: Date
|
|
|
|
let contentType: MessageContentType
|
2024-08-17 08:06:28 +00:00
|
|
|
var status: MessageStatus
|
2024-08-13 14:13:00 +00:00
|
|
|
|
|
|
|
let from: String
|
|
|
|
let to: String?
|
|
|
|
|
|
|
|
let body: String?
|
|
|
|
let subject: String?
|
|
|
|
let thread: String?
|
|
|
|
let oobUrl: String?
|
|
|
|
}
|
2024-08-14 13:48:30 +00:00
|
|
|
|
|
|
|
extension Message {
|
|
|
|
func save() async throws {
|
|
|
|
try await Database.shared.dbQueue.write { db in
|
|
|
|
try self.insert(db)
|
|
|
|
}
|
|
|
|
}
|
2024-08-17 08:06:28 +00:00
|
|
|
|
|
|
|
func setStatus(_ status: MessageStatus) async throws {
|
|
|
|
try await Database.shared.dbQueue.write { db in
|
|
|
|
var updatedMessage = self
|
|
|
|
updatedMessage.status = status
|
|
|
|
try updatedMessage.update(db, columns: ["status"])
|
|
|
|
}
|
|
|
|
}
|
2024-08-14 13:48:30 +00:00
|
|
|
}
|