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

72 lines
2.7 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(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-28 01:48:08 +01:00
public var attachmentID: String?
2020-11-06 05:37:11 +01:00
2020-11-28 01:48:08 +01:00
public var isValid: Bool { title != nil && url != nil && attachmentID != nil }
2020-11-28 01:48:08 +01:00
internal init(title: String?, url: String, attachmentID: String?) {
2020-11-06 05:37:11 +01:00
self.title = title
self.url = url
2020-11-28 01:48:08 +01:00
self.attachmentID = attachmentID
2020-11-06 05:37:11 +01:00
}
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 }
2020-11-08 22:15:44 +01:00
if let url = coder.decodeObject(forKey: "urlString") as! String? { self.url = url }
2020-11-28 01:48:08 +01:00
if let attachmentID = coder.decodeObject(forKey: "attachmentID") as! String? { self.attachmentID = attachmentID }
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")
2020-11-08 22:15:44 +01:00
coder.encode(url, forKey: "urlString")
2020-11-28 01:48:08 +01:00
coder.encode(attachmentID, forKey: "attachmentID")
2020-11-06 05:37:11 +01:00
}
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
2020-11-28 01:48:08 +01:00
return LinkPreview(title: title, url: url, attachmentID: nil)
2020-11-06 05:37:11 +01:00
}
public func toProto() -> SNProtoDataMessagePreview? {
2020-11-28 01:48:08 +01:00
preconditionFailure("Use toProto(using:) instead.")
}
public func toProto(using transaction: YapDatabaseReadWriteTransaction) -> 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) }
2021-02-26 03:42:06 +01:00
if let attachmentID = attachmentID, let stream = TSAttachment.fetch(uniqueId: attachmentID, transaction: transaction) as? TSAttachmentStream,
2020-11-28 01:48:08 +01:00
let attachmentProto = stream.buildProto() {
linkPreviewProto.setImage(attachmentProto)
}
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
}
2020-12-07 05:11:49 +01:00
// MARK: Description
public override var description: String {
"""
LinkPreview(
2021-05-03 07:13:18 +02:00
title: \(title ?? "null"),
url: \(url ?? "null"),
2020-12-07 05:11:49 +01:00
attachmentID: \(attachmentID ?? "null")
)
"""
}
2020-11-05 04:16:45 +01:00
}
}