mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			49 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			49 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Swift
		
	
| 
											5 years ago
										 | import SessionUtilitiesKit | ||
| 
											5 years ago
										 | 
 | ||
|  | public extension VisibleMessage { | ||
|  | 
 | ||
|  |     @objc(SNLinkPreview) | ||
| 
											5 years ago
										 |     class LinkPreview : NSObject, NSCoding { | ||
| 
											5 years ago
										 |         public var title: String? | ||
|  |         public var url: String? | ||
|  | 
 | ||
| 
											5 years ago
										 |         public var isValid: Bool { title != nil && url != nil }  | ||
|  | 
 | ||
| 
											5 years ago
										 |         internal init(title: String?, url: String) { | ||
| 
											5 years ago
										 |             self.title = title | ||
|  |             self.url = url | ||
|  |         } | ||
| 
											5 years ago
										 | 
 | ||
|  |         public required init?(coder: NSCoder) { | ||
| 
											5 years ago
										 |             if let title = coder.decodeObject(forKey: "title") as! String? { self.title = title } | ||
| 
											5 years ago
										 |             if let url = coder.decodeObject(forKey: "urlString") as! String? { self.url = url } | ||
| 
											5 years ago
										 |         } | ||
|  | 
 | ||
|  |         public func encode(with coder: NSCoder) { | ||
| 
											5 years ago
										 |             coder.encode(title, forKey: "title") | ||
| 
											5 years ago
										 |             coder.encode(url, forKey: "urlString") | ||
| 
											5 years ago
										 |         } | ||
|  | 
 | ||
|  |         public static func fromProto(_ proto: SNProtoDataMessagePreview) -> LinkPreview? { | ||
| 
											5 years ago
										 |             let title = proto.title | ||
| 
											5 years ago
										 |             let url = proto.url | ||
|  |             return LinkPreview(title: title, url: url) | ||
|  |         } | ||
|  | 
 | ||
|  |         public func toProto() -> SNProtoDataMessagePreview? { | ||
| 
											5 years ago
										 |             guard let url = url else { | ||
| 
											5 years ago
										 |                 SNLog("Couldn't construct link preview proto from: \(self).") | ||
|  |                 return nil | ||
|  |             } | ||
|  |             let linkPreviewProto = SNProtoDataMessagePreview.builder(url: url) | ||
| 
											5 years ago
										 |             if let title = title { linkPreviewProto.setTitle(title) } | ||
| 
											5 years ago
										 |             do { | ||
|  |                 return try linkPreviewProto.build() | ||
|  |             } catch { | ||
|  |                 SNLog("Couldn't construct link preview proto from: \(self).") | ||
|  |                 return nil | ||
|  |             } | ||
| 
											5 years ago
										 |         } | ||
|  |     } | ||
|  | } |