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
|
|
|
}
|
|
|
|
|
|
|
|
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-06-19 15:15:27 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 13:28:26 +00:00
|
|
|
struct Message: Stateable, Identifiable, DatabaseValueConvertible {
|
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-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
|
|
|
|
if martinMessage.hints.contains(.noStore) {
|
|
|
|
contentType = .typing
|
|
|
|
}
|
|
|
|
|
|
|
|
// From/To
|
|
|
|
let from = martinMessage.from?.bareJid.stringValue ?? ""
|
|
|
|
let to = martinMessage.to?.bareJid.stringValue
|
|
|
|
|
|
|
|
// Msg
|
|
|
|
let msg = Message(
|
|
|
|
id: martinMessage.id ?? UUID().uuidString,
|
|
|
|
type: type,
|
|
|
|
contentType: contentType,
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
body: martinMessage.body,
|
|
|
|
subject: martinMessage.subject,
|
|
|
|
thread: martinMessage.thread,
|
|
|
|
oobUrl: martinMessage.oob
|
|
|
|
)
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
}
|