session-ios/SessionMessagingKit/Messages/Visible Messages/VisibleMessage+Quote.swift

100 lines
4.2 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(SNQuote)
2020-11-05 06:10:49 +01:00
class Quote : NSObject, NSCoding {
2020-11-06 04:58:47 +01:00
public var timestamp: UInt64?
public var publicKey: String?
public var text: String?
2020-11-27 03:52:20 +01:00
public var attachmentID: String?
2020-11-06 04:58:47 +01:00
2020-11-27 03:52:20 +01:00
public var isValid: Bool { timestamp != nil && publicKey != nil }
2020-11-19 05:24:09 +01:00
public override init() { super.init() }
2020-11-27 06:22:15 +01:00
internal init(timestamp: UInt64, publicKey: String, text: String?, attachmentID: String?) {
2020-11-06 04:58:47 +01:00
self.timestamp = timestamp
self.publicKey = publicKey
self.text = text
2020-11-27 03:52:20 +01:00
self.attachmentID = attachmentID
2020-11-06 04:58:47 +01:00
}
2020-11-05 04:16:45 +01:00
public required init?(coder: NSCoder) {
2020-11-06 04:58:47 +01:00
if let timestamp = coder.decodeObject(forKey: "timestamp") as! UInt64? { self.timestamp = timestamp }
2020-11-08 22:15:44 +01:00
if let publicKey = coder.decodeObject(forKey: "authorId") as! String? { self.publicKey = publicKey }
if let text = coder.decodeObject(forKey: "body") as! String? { self.text = text }
2020-11-27 03:52:20 +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 04:58:47 +01:00
coder.encode(timestamp, forKey: "timestamp")
2020-11-08 22:15:44 +01:00
coder.encode(publicKey, forKey: "authorId")
coder.encode(text, forKey: "body")
2020-11-27 03:52:20 +01:00
coder.encode(attachmentID, forKey: "attachmentID")
2020-11-06 04:58:47 +01:00
}
public static func fromProto(_ proto: SNProtoDataMessageQuote) -> Quote? {
let timestamp = proto.id
let publicKey = proto.author
2020-11-27 06:22:15 +01:00
let text = proto.text
return Quote(timestamp: timestamp, publicKey: publicKey, text: text, attachmentID: nil)
2020-11-06 04:58:47 +01:00
}
public func toProto() -> SNProtoDataMessageQuote? {
2020-11-27 03:52:20 +01:00
preconditionFailure("Use toProto(using:) instead.")
}
public func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoDataMessageQuote? {
2020-11-27 05:13:42 +01:00
guard let timestamp = timestamp, let publicKey = publicKey else {
2020-11-06 04:58:47 +01:00
SNLog("Couldn't construct quote proto from: \(self).")
return nil
}
let quoteProto = SNProtoDataMessageQuote.builder(id: timestamp, author: publicKey)
2020-11-27 05:13:42 +01:00
if let text = text { quoteProto.setText(text) }
2020-11-27 03:52:20 +01:00
addAttachmentsIfNeeded(to: quoteProto, using: transaction)
2020-11-06 04:58:47 +01:00
do {
return try quoteProto.build()
} catch {
SNLog("Couldn't construct quote proto from: \(self).")
return nil
}
2020-11-05 04:16:45 +01:00
}
2020-11-27 03:52:20 +01:00
private func addAttachmentsIfNeeded(to quoteProto: SNProtoDataMessageQuote.SNProtoDataMessageQuoteBuilder, using transaction: YapDatabaseReadWriteTransaction) {
guard let attachmentID = attachmentID else { return }
2021-02-26 03:42:06 +01:00
guard let stream = TSAttachment.fetch(uniqueId: attachmentID, transaction: transaction) as? TSAttachmentStream, stream.isUploaded else {
2020-11-27 03:52:20 +01:00
#if DEBUG
preconditionFailure("Sending a message before all associated attachments have been uploaded.")
2020-12-07 05:11:49 +01:00
#else
2020-11-27 03:52:20 +01:00
return
2020-12-07 05:11:49 +01:00
#endif
2020-11-27 03:52:20 +01:00
}
let quotedAttachmentProto = SNProtoDataMessageQuoteQuotedAttachment.builder()
quotedAttachmentProto.setContentType(stream.contentType)
if let fileName = stream.sourceFilename { quotedAttachmentProto.setFileName(fileName) }
guard let attachmentProto = stream.buildProto() else {
return SNLog("Ignoring invalid attachment for quoted message.")
}
quotedAttachmentProto.setThumbnail(attachmentProto)
do {
try quoteProto.addAttachments(quotedAttachmentProto.build())
} catch {
SNLog("Couldn't construct quoted attachment proto from: \(self).")
}
}
2020-12-07 05:11:49 +01:00
// MARK: Description
public override var description: String {
"""
Quote(
2021-05-03 07:13:18 +02:00
timestamp: \(timestamp?.description ?? "null"),
publicKey: \(publicKey ?? "null"),
text: \(text ?? "null"),
2020-12-07 05:11:49 +01:00
attachmentID: \(attachmentID ?? "null")
)
"""
}
2020-11-05 04:16:45 +01:00
}
}