Fix attachment quoting
This commit is contained in:
parent
896ca8f0bd
commit
e3304a40f9
|
@ -7,43 +7,52 @@ public extension VisibleMessage {
|
|||
public var timestamp: UInt64?
|
||||
public var publicKey: String?
|
||||
public var text: String?
|
||||
public var attachmentID: String?
|
||||
|
||||
public var isValid: Bool { timestamp != nil && publicKey != nil && text != nil }
|
||||
public var isValid: Bool { timestamp != nil && publicKey != nil }
|
||||
|
||||
public override init() { super.init() }
|
||||
|
||||
internal init(timestamp: UInt64, publicKey: String, text: String) {
|
||||
internal init(timestamp: UInt64, publicKey: String, text: String, attachmentID: String?) {
|
||||
self.timestamp = timestamp
|
||||
self.publicKey = publicKey
|
||||
self.text = text
|
||||
self.attachmentID = attachmentID
|
||||
}
|
||||
|
||||
public required init?(coder: NSCoder) {
|
||||
if let timestamp = coder.decodeObject(forKey: "timestamp") as! UInt64? { self.timestamp = timestamp }
|
||||
if let publicKey = coder.decodeObject(forKey: "authorId") as! String? { self.publicKey = publicKey }
|
||||
if let text = coder.decodeObject(forKey: "body") as! String? { self.text = text }
|
||||
if let attachmentID = coder.decodeObject(forKey: "attachmentID") as! String? { self.attachmentID = attachmentID }
|
||||
}
|
||||
|
||||
public func encode(with coder: NSCoder) {
|
||||
coder.encode(timestamp, forKey: "timestamp")
|
||||
coder.encode(publicKey, forKey: "authorId")
|
||||
coder.encode(text, forKey: "body")
|
||||
coder.encode(attachmentID, forKey: "attachmentID")
|
||||
}
|
||||
|
||||
public static func fromProto(_ proto: SNProtoDataMessageQuote) -> Quote? {
|
||||
let timestamp = proto.id
|
||||
let publicKey = proto.author
|
||||
guard let text = proto.text else { return nil }
|
||||
return Quote(timestamp: timestamp, publicKey: publicKey, text: text)
|
||||
return Quote(timestamp: timestamp, publicKey: publicKey, text: text, attachmentID: nil) // TODO: attachmentID
|
||||
}
|
||||
|
||||
public func toProto() -> SNProtoDataMessageQuote? {
|
||||
preconditionFailure("Use toProto(using:) instead.")
|
||||
}
|
||||
|
||||
public func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoDataMessageQuote? {
|
||||
guard let timestamp = timestamp, let publicKey = publicKey, let text = text else {
|
||||
SNLog("Couldn't construct quote proto from: \(self).")
|
||||
return nil
|
||||
}
|
||||
let quoteProto = SNProtoDataMessageQuote.builder(id: timestamp, author: publicKey)
|
||||
quoteProto.setText(text)
|
||||
addAttachmentsIfNeeded(to: quoteProto, using: transaction)
|
||||
do {
|
||||
return try quoteProto.build()
|
||||
} catch {
|
||||
|
@ -51,5 +60,27 @@ public extension VisibleMessage {
|
|||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
private func addAttachmentsIfNeeded(to quoteProto: SNProtoDataMessageQuote.SNProtoDataMessageQuoteBuilder, using transaction: YapDatabaseReadWriteTransaction) {
|
||||
guard let attachmentID = attachmentID else { return }
|
||||
guard let stream = TSAttachmentStream.fetch(uniqueId: attachmentID, transaction: transaction), stream.isUploaded else {
|
||||
#if DEBUG
|
||||
preconditionFailure("Sending a message before all associated attachments have been uploaded.")
|
||||
#endif
|
||||
return
|
||||
}
|
||||
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).")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,6 +59,10 @@ public final class VisibleMessage : Message {
|
|||
dataMessage = SNProtoDataMessage.builder()
|
||||
}
|
||||
if let text = text { dataMessage.setBody(text) }
|
||||
var attachmentIDs = self.attachmentIDs
|
||||
if let quotedAttachmentID = quote?.attachmentID, let index = attachmentIDs.firstIndex(of: quotedAttachmentID) {
|
||||
attachmentIDs.remove(at: index)
|
||||
}
|
||||
let attachments = attachmentIDs.compactMap { TSAttachmentStream.fetch(uniqueId: $0, transaction: transaction) }
|
||||
if !attachments.allSatisfy({ $0.isUploaded }) {
|
||||
#if DEBUG
|
||||
|
@ -67,7 +71,7 @@ public final class VisibleMessage : Message {
|
|||
}
|
||||
let attachmentProtos = attachments.compactMap { $0.buildProto() }
|
||||
dataMessage.setAttachments(attachmentProtos)
|
||||
if let quote = quote, let quoteProto = quote.toProto() { dataMessage.setQuote(quoteProto) }
|
||||
if let quote = quote, let quoteProto = quote.toProto(using: transaction) { dataMessage.setQuote(quoteProto) }
|
||||
if let linkPreview = linkPreview, let linkPreviewProto = linkPreview.toProto() { dataMessage.setPreview([ linkPreviewProto ]) }
|
||||
// TODO: Contact
|
||||
do {
|
||||
|
|
|
@ -8,6 +8,7 @@ extension VisibleMessage.Quote {
|
|||
result.timestamp = quote.timestamp
|
||||
result.publicKey = quote.authorId
|
||||
result.text = quote.body
|
||||
result.attachmentID = quote.attachmentStream?.uniqueId
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,11 +4,15 @@ extension TSQuotedMessage {
|
|||
@objc(from:)
|
||||
public static func from(_ quote: VisibleMessage.Quote?) -> TSQuotedMessage? {
|
||||
guard let quote = quote else { return nil }
|
||||
var attachments: [TSAttachment] = []
|
||||
if let attachmentID = quote.attachmentID, let attachment = TSAttachment.fetch(uniqueId: attachmentID) {
|
||||
attachments.append(attachment)
|
||||
}
|
||||
return TSQuotedMessage(
|
||||
timestamp: quote.timestamp!,
|
||||
authorId: quote.publicKey!,
|
||||
body: quote.text, bodySource: .local,
|
||||
receivedQuotedAttachmentInfos: []
|
||||
body: quote.text,
|
||||
quotedAttachmentsForSending: attachments
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,9 +20,16 @@ extension MessageSender : SharedSenderKeysDelegate {
|
|||
streams.append(stream)
|
||||
stream.write($0.dataSource)
|
||||
stream.save(with: transaction)
|
||||
tsMessage.attachmentIds.add(stream.uniqueId!)
|
||||
}
|
||||
if let quotedMessageThumbnails = tsMessage.quotedMessage?.createThumbnailAttachmentsIfNecessary(with: transaction) {
|
||||
streams += quotedMessageThumbnails
|
||||
}
|
||||
if let linkPreviewAttachmentID = tsMessage.linkPreview?.imageAttachmentId,
|
||||
let stream = TSAttachment.fetch(uniqueId: linkPreviewAttachmentID, transaction: transaction) as? TSAttachmentStream {
|
||||
streams.append(stream)
|
||||
}
|
||||
message.attachmentIDs = streams.map { $0.uniqueId! }
|
||||
tsMessage.attachmentIds.addObjects(from: message.attachmentIDs)
|
||||
tsMessage.save(with: transaction)
|
||||
}
|
||||
|
||||
|
@ -34,6 +41,7 @@ extension MessageSender : SharedSenderKeysDelegate {
|
|||
|
||||
@objc(send:inThread:usingTransaction:)
|
||||
public static func send(_ message: Message, in thread: TSThread, using transaction: YapDatabaseReadWriteTransaction) {
|
||||
if message is VisibleMessage { prep([], for: message, using: transaction) } // To handle quotes & link previews
|
||||
message.threadID = thread.uniqueId!
|
||||
let destination = Message.Destination.from(thread)
|
||||
let job = MessageSendJob(message: message, destination: destination)
|
||||
|
|
Loading…
Reference in New Issue