another.im-ios/Monal/another.im/XMPP/Models/Message.swift

57 lines
1.4 KiB
Swift
Raw Normal View History

2024-11-29 17:35:20 +00:00
import Foundation
import monalxmpp
2024-12-06 21:32:17 +00:00
enum MessageStatus {
case pending
case sent
case delivered
case error
case inbound
}
2024-11-29 17:35:20 +00:00
struct Message: Identifiable {
let accountId: Int
let participantJid: String
let dbId: Int
let stanzaId: String
let timestamp: Date
let body: String
let isInbound: Bool
let encrypted: Bool
2024-12-06 21:32:17 +00:00
let status: MessageStatus
2024-11-29 17:35:20 +00:00
var id: String {
"\(accountId)|\(dbId)"
}
init?(_ obj: MLMessage) {
guard let accId = obj.accountID as? Int, let dbId = obj.messageDBId as? Int else { return nil }
accountId = accId
participantJid = obj.participantJid
self.dbId = dbId
stanzaId = obj.stanzaId
timestamp = obj.timestamp
body = obj.messageText
isInbound = obj.inbound
encrypted = obj.encrypted
2024-12-06 21:32:17 +00:00
2024-12-06 23:09:49 +00:00
if obj.inbound {
status = .inbound
} else {
if obj.hasBeenReceived {
2024-12-06 21:32:17 +00:00
status = .delivered
2024-12-06 23:09:49 +00:00
} else if obj.hasBeenSent {
status = .sent
2024-12-06 21:32:17 +00:00
} else {
2024-12-06 23:09:49 +00:00
if !obj.errorReason.isEmpty {
status = .error
} else {
status = .pending
}
2024-12-06 21:32:17 +00:00
}
}
2024-12-06 23:09:49 +00:00
// print("AAAAAA", body, status, "\n", "orig: ", obj.hasBeenReceived, obj.hasBeenSent, " id:", dbId, " stanza:", stanzaId)
2024-11-29 17:35:20 +00:00
}
}