Fix open group link previews

This commit is contained in:
nielsandriesse 2020-12-01 13:39:27 +11:00
parent 5f2ec37db7
commit a87bad6603
4 changed files with 18 additions and 14 deletions

View File

@ -86,8 +86,8 @@ public final class OpenGroupAPI : DotNetAPI {
}
}
let quote: OpenGroupMessage.Quote?
if let quoteAsJSON = value["quote"] as? JSON, let quotedMessageTimestamp = quoteAsJSON["id"] as? UInt64, let quoteePublicKey = quoteAsJSON["author"] as? String {
let quotedMessageBody = quoteAsJSON["text"] as? String
if let quoteAsJSON = value["quote"] as? JSON, let quotedMessageTimestamp = quoteAsJSON["id"] as? UInt64, let quoteePublicKey = quoteAsJSON["author"] as? String,
let quotedMessageBody = quoteAsJSON["text"] as? String {
let quotedMessageServerID = message["reply_to"] as? UInt64
quote = OpenGroupMessage.Quote(quotedMessageTimestamp: quotedMessageTimestamp, quoteePublicKey: quoteePublicKey, quotedMessageBody: quotedMessageBody,
quotedMessageServerID: quotedMessageServerID)

View File

@ -4,6 +4,7 @@ internal extension OpenGroupMessage {
static func from(_ message: VisibleMessage, for server: String, using transaction: YapDatabaseReadWriteTransaction) -> OpenGroupMessage? {
let storage = Configuration.shared.storage
guard let userPublicKey = storage.getUserPublicKey() else { return nil }
var attachmentIDs = message.attachmentIDs
// Validation
guard message.isValid else { return nil } // Should be valid at this point
// Quote
@ -11,7 +12,11 @@ internal extension OpenGroupMessage {
if let quote = message.quote {
guard quote.isValid else { return nil }
let quotedMessageServerID = TSIncomingMessage.find(withAuthorId: quote.publicKey!, timestamp: quote.timestamp!, transaction: transaction)?.openGroupServerMessageID
return OpenGroupMessage.Quote(quotedMessageTimestamp: quote.timestamp!, quoteePublicKey: quote.publicKey!, quotedMessageBody: quote.text, quotedMessageServerID: quotedMessageServerID)
let quotedMessageBody = quote.text ?? String(quote.timestamp!) // The back-end doesn't accept messages without a body so we use this as a workaround
if let quotedAttachmentID = quote.attachmentID, let index = attachmentIDs.firstIndex(of: quotedAttachmentID) {
attachmentIDs.remove(at: index)
}
return OpenGroupMessage.Quote(quotedMessageTimestamp: quote.timestamp!, quoteePublicKey: quote.publicKey!, quotedMessageBody: quotedMessageBody, quotedMessageServerID: quotedMessageServerID)
} else {
return nil
}
@ -25,13 +30,16 @@ internal extension OpenGroupMessage {
if let linkPreview = message.linkPreview {
guard linkPreview.isValid, let attachmentID = linkPreview.attachmentID,
let attachment = TSAttachmentStream.fetch(uniqueId: attachmentID, transaction: transaction) else { return nil }
if let index = attachmentIDs.firstIndex(of: attachmentID) {
attachmentIDs.remove(at: index)
}
let fileName = attachment.sourceFilename ?? UUID().uuidString
let width = attachment.shouldHaveImageSize() ? attachment.imageSize().width : 0
let height = attachment.shouldHaveImageSize() ? attachment.imageSize().height : 0
let openGroupLinkPreview = OpenGroupMessage.Attachment(
kind: .linkPreview,
server: server,
serverID: 0,
serverID: attachment.serverId,
contentType: attachment.contentType,
size: UInt(attachment.byteCount),
fileName: fileName,
@ -46,7 +54,7 @@ internal extension OpenGroupMessage {
result.attachments.append(openGroupLinkPreview)
}
// Attachments
let attachments: [OpenGroupMessage.Attachment] = message.attachmentIDs.compactMap { attachmentID in
let attachments: [OpenGroupMessage.Attachment] = attachmentIDs.compactMap { attachmentID in
guard let attachment = TSAttachmentStream.fetch(uniqueId: attachmentID, transaction: transaction) else { return nil } // Should never occur
let fileName = attachment.sourceFilename ?? UUID().uuidString
let width = attachment.shouldHaveImageSize() ? attachment.imageSize().width : 0

View File

@ -34,7 +34,7 @@ public final class OpenGroupMessage : NSObject {
public struct Quote {
public let quotedMessageTimestamp: UInt64
public let quoteePublicKey: String
public let quotedMessageBody: String?
public let quotedMessageBody: String
public let quotedMessageServerID: UInt64?
}
@ -93,7 +93,7 @@ public final class OpenGroupMessage : NSObject {
}
@objc public convenience init(senderPublicKey: String, displayName: String, body: String, type: String, timestamp: UInt64,
quotedMessageTimestamp: UInt64, quoteePublicKey: String?, quotedMessageBody: String?, quotedMessageServerID: UInt64,
quotedMessageTimestamp: UInt64, quoteePublicKey: String?, quotedMessageBody: String, quotedMessageServerID: UInt64,
signatureData: Data?, signatureVersion: UInt64, serverTimestamp: UInt64) {
let quote: Quote?
if quotedMessageTimestamp != 0, let quoteeHexEncodedPublicKey = quoteePublicKey {
@ -137,8 +137,7 @@ public final class OpenGroupMessage : NSObject {
internal func toJSON() -> JSON {
var value: JSON = [ "timestamp" : timestamp ]
if let quote = quote {
var quoteAsJSON: JSON = [ "id" : quote.quotedMessageTimestamp, "author" : quote.quoteePublicKey ]
if let quotedMessageBody = quote.quotedMessageBody { quoteAsJSON["text"] = quotedMessageBody }
let quoteAsJSON: JSON = [ "id" : quote.quotedMessageTimestamp, "author" : quote.quoteePublicKey, "text" : quote.quotedMessageBody ]
value["quote"] = quoteAsJSON
}
if let signature = signature {
@ -185,10 +184,7 @@ public final class OpenGroupMessage : NSObject {
private func getValidationData(for signatureVersion: UInt64) -> Data? {
var string = "\(body.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines))\(timestamp)"
if let quote = quote {
string += "\(quote.quotedMessageTimestamp)\(quote.quoteePublicKey)"
if let quotedMessageBody = quote.quotedMessageBody {
string += "\(quotedMessageBody.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines))"
}
string += "\(quote.quotedMessageTimestamp)\(quote.quoteePublicKey)\(quote.quotedMessageBody.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines))"
if let quotedMessageServerID = quote.quotedMessageServerID {
string += "\(quotedMessageServerID)"
}

View File

@ -108,7 +108,7 @@ public final class OpenGroupPoller : NSObject {
// Quote
if let quote = message.quote {
let quoteProto = SNProtoDataMessageQuote.builder(id: quote.quotedMessageTimestamp, author: quote.quoteePublicKey)
if let quotedMessageBody = quote.quotedMessageBody { quoteProto.setText(quotedMessageBody) }
if quote.quotedMessageBody != String(quote.quotedMessageTimestamp) { quoteProto.setText(quote.quotedMessageBody) }
dataMessageProto.setQuote(try! quoteProto.build())
}
// Profile