104 lines
2.4 KiB
Swift
104 lines
2.4 KiB
Swift
import Foundation
|
|
import GRDB
|
|
import Martin
|
|
|
|
enum MessageType: String, Codable, DatabaseValueConvertible {
|
|
case chat
|
|
case groupchat
|
|
case error
|
|
}
|
|
|
|
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?
|
|
|
|
var localPath: URL? {
|
|
guard let attachmentLocalName = localName else { return nil }
|
|
return FolderWrapper.shared.fileFolder.appendingPathComponent(attachmentLocalName)
|
|
}
|
|
|
|
var thumbnailPath: URL? {
|
|
guard let attachmentThumbnailName = thumbnailName else { return nil }
|
|
return FolderWrapper.shared.fileFolder.appendingPathComponent(attachmentThumbnailName)
|
|
}
|
|
}
|
|
|
|
enum MessageContentType: Codable & Equatable, DatabaseValueConvertible {
|
|
case text
|
|
case typing
|
|
case invite
|
|
case attachment(Attachment)
|
|
|
|
var isAttachment: Bool {
|
|
if case .attachment = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
enum MessageStatus: Int, Codable, DatabaseValueConvertible {
|
|
case pending
|
|
case sent
|
|
case error
|
|
}
|
|
|
|
struct Message: DBStorable, Equatable {
|
|
static let databaseTableName = "messages"
|
|
|
|
let id: String
|
|
var type: MessageType
|
|
var date: Date
|
|
var contentType: MessageContentType
|
|
var status: MessageStatus
|
|
|
|
var from: String
|
|
var to: String?
|
|
|
|
var body: String?
|
|
var subject: String?
|
|
var thread: String?
|
|
var oobUrl: String?
|
|
}
|
|
|
|
extension Message {
|
|
func save() async throws {
|
|
try await Database.shared.dbQueue.write { db in
|
|
try self.insert(db)
|
|
}
|
|
}
|
|
|
|
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"])
|
|
}
|
|
}
|
|
|
|
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
|
|
)
|
|
}
|
|
}
|