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
|
|
|
|
}
|
|
|
|
|
2024-08-17 21:12:39 +00:00
|
|
|
enum AttachmentType: Int, Codable, DatabaseValueConvertible {
|
|
|
|
case image
|
|
|
|
case video
|
|
|
|
case audio
|
|
|
|
case file
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Attachment: Codable & Equatable, DatabaseValueConvertible {
|
|
|
|
let type: AttachmentType
|
|
|
|
var localName: String?
|
|
|
|
var thumbnailName: String?
|
|
|
|
var remotePath: String?
|
2024-08-17 22:05:18 +00:00
|
|
|
|
|
|
|
var localPath: URL? {
|
|
|
|
guard let attachmentLocalName = localName else { return nil }
|
|
|
|
return Const.fileFolder.appendingPathComponent(attachmentLocalName)
|
|
|
|
}
|
|
|
|
|
|
|
|
var thumbnailPath: URL? {
|
|
|
|
guard let attachmentThumbnailName = thumbnailName else { return nil }
|
|
|
|
return Const.fileFolder.appendingPathComponent(attachmentThumbnailName)
|
|
|
|
}
|
2024-08-17 21:12:39 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 14:13:00 +00:00
|
|
|
enum MessageContentType: Codable & Equatable, DatabaseValueConvertible {
|
|
|
|
case text
|
|
|
|
case typing
|
2024-08-17 21:12:39 +00:00
|
|
|
case invite
|
|
|
|
case attachment(Attachment)
|
2024-08-18 15:29:44 +00:00
|
|
|
|
|
|
|
var isAttachment: Bool {
|
|
|
|
if case .attachment = self {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2024-08-13 14:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum MessageStatus: Int, Codable, DatabaseValueConvertible {
|
|
|
|
case pending
|
|
|
|
case sent
|
|
|
|
case error
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Message: DBStorable, Equatable {
|
|
|
|
static let databaseTableName = "messages"
|
|
|
|
|
|
|
|
let id: String
|
2024-08-17 22:12:23 +00:00
|
|
|
var type: MessageType
|
2024-08-13 14:13:00 +00:00
|
|
|
let date: Date
|
2024-08-17 21:12:39 +00:00
|
|
|
var contentType: MessageContentType
|
2024-08-17 08:06:28 +00:00
|
|
|
var status: MessageStatus
|
2024-08-13 14:13:00 +00:00
|
|
|
|
2024-08-17 22:12:23 +00:00
|
|
|
var from: String
|
|
|
|
var to: String?
|
2024-08-13 14:13:00 +00:00
|
|
|
|
2024-08-17 22:05:18 +00:00
|
|
|
var body: String?
|
2024-08-17 22:12:23 +00:00
|
|
|
var subject: String?
|
|
|
|
var thread: String?
|
2024-08-17 22:05:18 +00:00
|
|
|
var oobUrl: String?
|
2024-08-13 14:13:00 +00:00
|
|
|
}
|
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-17 22:12:23 +00:00
|
|
|
|
|
|
|
static var blank: Message {
|
|
|
|
Message(
|
|
|
|
id: UUID().uuidString,
|
|
|
|
type: .chat,
|
|
|
|
date: Date(),
|
|
|
|
contentType: .text,
|
|
|
|
status: .pending,
|
|
|
|
from: "",
|
|
|
|
to: nil,
|
|
|
|
body: nil,
|
|
|
|
subject: nil,
|
|
|
|
thread: nil,
|
|
|
|
oobUrl: nil
|
|
|
|
)
|
|
|
|
}
|
2024-08-14 13:48:30 +00:00
|
|
|
}
|