From c47b6e9c2156c9cb749b043e5d92a7288537056d Mon Sep 17 00:00:00 2001 From: fmodf Date: Tue, 13 Aug 2024 16:13:00 +0200 Subject: [PATCH] wip --- .../AppData/Model/Message.swift | 92 +++++++++++++++++++ .../Services/Database+Migrations.swift | 15 +++ 2 files changed, 107 insertions(+) create mode 100644 ConversationsClassic/AppData/Model/Message.swift diff --git a/ConversationsClassic/AppData/Model/Message.swift b/ConversationsClassic/AppData/Model/Message.swift new file mode 100644 index 0000000..74ba173 --- /dev/null +++ b/ConversationsClassic/AppData/Model/Message.swift @@ -0,0 +1,92 @@ +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 + let status: MessageStatus + + let from: String + let to: String? + + let body: String? + let subject: String? + let thread: String? + let oobUrl: String? +} + +extension Message { + static func map(from 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 + + // Extract date or set current + var date = Date() + if let timestampStr = martinMessage.attribute("archived_date"), let timeInterval = TimeInterval(timestampStr) { + date = Date(timeIntervalSince1970: timeInterval) + } + + // Msg + let msg = Message( + id: martinMessage.id ?? UUID().uuidString, + type: type, + date: date, + contentType: contentType, + status: .sent, + from: from, + to: to, + body: martinMessage.body, + subject: martinMessage.subject, + thread: martinMessage.thread, + oobUrl: martinMessage.oob + ) + return msg + } +} diff --git a/ConversationsClassic/AppData/Services/Database+Migrations.swift b/ConversationsClassic/AppData/Services/Database+Migrations.swift index 3f59936..f1c3cf6 100644 --- a/ConversationsClassic/AppData/Services/Database+Migrations.swift +++ b/ConversationsClassic/AppData/Services/Database+Migrations.swift @@ -42,6 +42,21 @@ extension Database { table.column("participant", .text).notNull() table.column("type", .integer).notNull() } + + // messages + try db.create(table: "messages", options: [.ifNotExists]) { table in + table.column("id", .text).notNull().primaryKey().unique(onConflict: .replace) + table.column("type", .text).notNull() + table.column("date", .datetime).notNull() + table.column("contentType", .text).notNull() + table.column("status", .integer).notNull() + table.column("from", .text).notNull() + table.column("to", .text) + table.column("body", .text) + table.column("subject", .text) + table.column("thread", .text) + table.column("oobUrl", .text) + } } // return migrator