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?
2020-11-06 06:01:51 +01:00
internal init(title: String?, url: String) {
2020-11-06 05:37:11 +01:00
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? {
2020-11-06 06:01:51 +01:00
let title = proto.title
2020-11-06 05:37:11 +01:00
let url = proto.url
return LinkPreview(title: title, url: url)
}
public func toProto() -> SNProtoDataMessagePreview? {
2020-11-06 06:01:51 +01:00
guard let url = url else {
2020-11-06 05:37:11 +01:00
SNLog("Couldn't construct link preview proto from: \(self).")
return nil
}
let linkPreviewProto = SNProtoDataMessagePreview.builder(url: url)
2020-11-06 06:01:51 +01:00
if let title = title { linkPreviewProto.setTitle(title) }
2020-11-06 05:37:11 +01:00
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
}
}
}