mirror of https://github.com/oxen-io/session-ios
Implement link preview
parent
9dc942772c
commit
39a1a4b3ab
@ -1,15 +1,46 @@
|
|||||||
|
import SessionUtilities
|
||||||
|
|
||||||
public extension VisibleMessage {
|
public extension VisibleMessage {
|
||||||
|
|
||||||
@objc(SNLinkPreview)
|
@objc(SNLinkPreview)
|
||||||
class LinkPreview : NSObject, NSCoding {
|
class LinkPreview : NSObject, NSCoding {
|
||||||
|
public var title: String?
|
||||||
|
public var url: String?
|
||||||
|
|
||||||
|
internal init(title: String, url: String) {
|
||||||
|
self.title = title
|
||||||
|
self.url = url
|
||||||
|
}
|
||||||
|
|
||||||
public required init?(coder: NSCoder) {
|
public required init?(coder: NSCoder) {
|
||||||
fatalError("Not implemented.")
|
if let title = coder.decodeObject(forKey: "title") as! String? { self.title = title }
|
||||||
|
if let url = coder.decodeObject(forKey: "url") as! String? { self.url = url }
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encode(with coder: NSCoder) {
|
public func encode(with coder: NSCoder) {
|
||||||
fatalError("Not implemented.")
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue