session-ios/SessionMessagingKit/Messages/Visible Message/VisibleMessage+LinkPreview....

47 lines
1.5 KiB
Swift
Raw Normal View History

2020-11-06 05:37:11 +01:00
import SessionUtilities
2020-11-05 04:16:45 +01:00
public extension VisibleMessage {
@objc(SNLinkPreview)
2020-11-05 06:10:49 +01:00
class LinkPreview : NSObject, NSCoding {
2020-11-06 05:37:11 +01:00
public var title: String?
public var url: String?
internal init(title: String, url: String) {
self.title = title
self.url = url
}
2020-11-05 04:16:45 +01:00
public required init?(coder: NSCoder) {
2020-11-06 05:37:11 +01:00
if let title = coder.decodeObject(forKey: "title") as! String? { self.title = title }
if let url = coder.decodeObject(forKey: "url") as! String? { self.url = url }
2020-11-05 04:16:45 +01:00
}
public func encode(with coder: NSCoder) {
2020-11-06 05:37:11 +01:00
coder.encode(title, forKey: "title")
coder.encode(url, forKey: "url")
}
public static func fromProto(_ proto: SNProtoDataMessagePreview) -> LinkPreview? {
guard let title = proto.title else { return nil }
let url = proto.url
return LinkPreview(title: title, url: url)
}
public func toProto() -> SNProtoDataMessagePreview? {
guard let title = title, let url = url else {
SNLog("Couldn't construct link preview proto from: \(self).")
return nil
}
let linkPreviewProto = SNProtoDataMessagePreview.builder(url: url)
linkPreviewProto.setTitle(title)
do {
return try linkPreviewProto.build()
} catch {
SNLog("Couldn't construct link preview proto from: \(self).")
return nil
}
2020-11-05 04:16:45 +01:00
}
}
}