another.im-ios/ConversationsClassic/AppCore/Models/Message.swift

123 lines
3.4 KiB
Swift
Raw Normal View History

2024-06-19 15:15:27 +00:00
import Foundation
import GRDB
2024-06-24 10:44:55 +00:00
import Martin
2024-06-19 15:15:27 +00:00
enum MessageType: String, Codable, DatabaseValueConvertible {
2024-06-24 10:44:55 +00:00
case chat
case groupchat
2024-06-24 13:28:26 +00:00
case error
2024-06-24 10:44:55 +00:00
}
2024-07-13 13:38:15 +00:00
enum MessageAttachmentType: Int, Stateable, DatabaseValueConvertible {
case movie = 0
case image = 1
case audio = 2
case file = 3
}
2024-06-24 10:44:55 +00:00
enum MessageContentType: String, Codable, DatabaseValueConvertible {
2024-06-19 15:15:27 +00:00
case text
2024-06-24 10:44:55 +00:00
case typing
case invite
2024-07-13 13:38:15 +00:00
case attachment
2024-06-19 15:15:27 +00:00
}
2024-06-26 08:00:59 +00:00
struct Message: DBStorable, Equatable {
2024-06-26 09:29:30 +00:00
static let databaseTableName = "messages"
2024-06-19 15:15:27 +00:00
let id: String
2024-06-24 10:44:55 +00:00
let type: MessageType
2024-06-24 13:28:26 +00:00
let contentType: MessageContentType
let from: String
let to: String?
2024-06-19 15:15:27 +00:00
2024-06-24 13:28:26 +00:00
let body: String?
let subject: String?
let thread: String?
let oobUrl: String?
2024-06-25 11:13:59 +00:00
let date: Date
2024-06-26 07:27:23 +00:00
let pending: Bool
2024-06-26 10:26:04 +00:00
let sentError: Bool
2024-07-10 11:09:59 +00:00
2024-07-13 13:38:15 +00:00
var attachmentType: MessageAttachmentType?
2024-07-14 10:08:51 +00:00
var attachmentLocalName: String?
2024-07-13 13:42:47 +00:00
var attachmentRemotePath: URL?
2024-07-14 10:08:51 +00:00
var attachmentThumbnailName: String?
2024-07-13 13:42:47 +00:00
var attachmentDownloadFailed: Bool = false
2024-06-19 15:15:27 +00:00
}
2024-06-24 10:44:55 +00:00
2024-06-24 13:28:26 +00:00
extension Message {
// Universal mapping from Martin's Message to App Message
static func map(_ martinMessage: Martin.Message) -> Message? {
#if DEBUG
print("---")
print("Message received: \(martinMessage)")
print("---")
#endif
// Check that the message type is supported
let chatTypes: [StanzaType] = [.chat, .groupchat]
guard let mType = martinMessage.type, chatTypes.contains(mType) else {
#if DEBUG
print("Unsupported message type: \(martinMessage.type?.rawValue ?? "nil")")
#endif
return nil
}
// Type
let type = MessageType(rawValue: martinMessage.type?.rawValue ?? "") ?? .chat
// Content type
var contentType: MessageContentType = .text
2024-07-13 13:38:15 +00:00
if martinMessage.oob != nil {
contentType = .attachment
} else if martinMessage.hints.contains(.noStore) {
2024-06-24 13:28:26 +00:00
contentType = .typing
}
// From/To
let from = martinMessage.from?.bareJid.stringValue ?? ""
let to = martinMessage.to?.bareJid.stringValue
// Msg
2024-07-13 13:38:15 +00:00
var msg = Message(
2024-06-24 13:28:26 +00:00
id: martinMessage.id ?? UUID().uuidString,
type: type,
contentType: contentType,
from: from,
to: to,
body: martinMessage.body,
subject: martinMessage.subject,
thread: martinMessage.thread,
2024-06-25 11:13:59 +00:00
oobUrl: martinMessage.oob,
2024-06-26 07:27:23 +00:00
date: Date(),
2024-06-26 10:26:04 +00:00
pending: false,
2024-07-13 13:38:15 +00:00
sentError: false,
attachmentType: nil,
2024-07-14 10:08:51 +00:00
attachmentLocalName: nil,
2024-07-13 13:42:47 +00:00
attachmentRemotePath: nil,
2024-07-14 10:08:51 +00:00
attachmentThumbnailName: nil,
2024-07-13 13:42:47 +00:00
attachmentDownloadFailed: false
2024-06-24 13:28:26 +00:00
)
2024-07-13 13:38:15 +00:00
if let oob = martinMessage.oob {
msg.attachmentType = oob.attachmentType
2024-07-13 13:42:47 +00:00
msg.attachmentRemotePath = URL(string: oob)
2024-07-13 13:38:15 +00:00
}
2024-06-24 13:28:26 +00:00
return msg
}
}
2024-07-14 10:08:51 +00:00
extension Message {
var attachmentLocalPath: URL? {
guard let attachmentLocalName = attachmentLocalName else { return nil }
return FileProcessing.fileFolder.appendingPathComponent(attachmentLocalName)
}
var attachmentThumbnailPath: URL? {
guard let attachmentThumbnailName = attachmentThumbnailName else { return nil }
return FileProcessing.fileFolder.appendingPathComponent(attachmentThumbnailName)
}
}