session-ios/SessionMessagingKit/Messages/Visible Message/VisibleMessage+Quote.swift

54 lines
2.0 KiB
Swift
Raw Normal View History

2020-11-09 00:58:47 +01:00
import SessionUtilitiesKit
2020-11-05 04:16:45 +01:00
public extension VisibleMessage {
@objc(SNQuote)
2020-11-05 06:10:49 +01:00
class Quote : NSObject, NSCoding {
2020-11-06 04:58:47 +01:00
public var timestamp: UInt64?
public var publicKey: String?
public var text: String?
public var isValid: Bool { timestamp != nil && publicKey != nil && text != nil }
2020-11-06 04:58:47 +01:00
internal init(timestamp: UInt64, publicKey: String, text: String) {
self.timestamp = timestamp
self.publicKey = publicKey
self.text = text
}
2020-11-05 04:16:45 +01:00
public required init?(coder: NSCoder) {
2020-11-06 04:58:47 +01:00
if let timestamp = coder.decodeObject(forKey: "timestamp") as! UInt64? { self.timestamp = timestamp }
2020-11-08 22:15:44 +01:00
if let publicKey = coder.decodeObject(forKey: "authorId") as! String? { self.publicKey = publicKey }
if let text = coder.decodeObject(forKey: "body") as! String? { self.text = text }
2020-11-05 04:16:45 +01:00
}
public func encode(with coder: NSCoder) {
2020-11-06 04:58:47 +01:00
coder.encode(timestamp, forKey: "timestamp")
2020-11-08 22:15:44 +01:00
coder.encode(publicKey, forKey: "authorId")
coder.encode(text, forKey: "body")
2020-11-06 04:58:47 +01:00
}
public static func fromProto(_ proto: SNProtoDataMessageQuote) -> Quote? {
let timestamp = proto.id
let publicKey = proto.author
guard let text = proto.text else { return nil }
return Quote(timestamp: timestamp, publicKey: publicKey, text: text)
}
public func toProto() -> SNProtoDataMessageQuote? {
2020-11-06 06:01:51 +01:00
guard let timestamp = timestamp, let publicKey = publicKey, let text = text else {
2020-11-06 04:58:47 +01:00
SNLog("Couldn't construct quote proto from: \(self).")
return nil
}
let quoteProto = SNProtoDataMessageQuote.builder(id: timestamp, author: publicKey)
2020-11-06 06:01:51 +01:00
quoteProto.setText(text)
2020-11-06 04:58:47 +01:00
do {
return try quoteProto.build()
} catch {
SNLog("Couldn't construct quote proto from: \(self).")
return nil
}
2020-11-05 04:16:45 +01:00
}
}
}