From bedf3ab44b2dd07456d9a347464615d6c47208b5 Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Thu, 11 Aug 2022 14:41:08 +1000 Subject: [PATCH] WIP: select quote body from original interaction --- .../Database/Models/Quote.swift | 54 +------------------ .../Shared Models/MessageViewModel.swift | 12 ++++- 2 files changed, 13 insertions(+), 53 deletions(-) diff --git a/SessionMessagingKit/Database/Models/Quote.swift b/SessionMessagingKit/Database/Models/Quote.swift index ae4526d60..f24ae3e4f 100644 --- a/SessionMessagingKit/Database/Models/Quote.swift +++ b/SessionMessagingKit/Database/Models/Quote.swift @@ -104,57 +104,7 @@ public extension Quote { self.interactionId = interactionId self.timestampMs = Int64(quoteProto.id) self.authorId = quoteProto.author - - // Prefer to generate the text snippet locally if available. - let quotedInteraction: Interaction? = try? thread - .interactions - .filter(Interaction.Columns.authorId == quoteProto.author) - .filter(Interaction.Columns.timestampMs == Double(quoteProto.id)) - .fetchOne(db) - - guard quotedInteraction != nil else { - self.body = "QUOTED_MESSAGE_NOT_FOUND".localized() - self.attachmentId = nil - return - } - - if let quotedInteraction: Interaction = quotedInteraction, quotedInteraction.body?.isEmpty == false { - self.body = quotedInteraction.body - } - else if let body: String = quoteProto.text, !body.isEmpty { - self.body = body - } - else { - self.body = nil - } - - // We only use the first attachment - if let attachment = quoteProto.attachments.first(where: { $0.thumbnail != nil })?.thumbnail { - self.attachmentId = try quotedInteraction - .map { quotedInteraction -> Attachment? in - // If the quotedInteraction has an attachment then try clone it - if let attachment: Attachment = try? quotedInteraction.attachments.fetchOne(db) { - return attachment.cloneAsQuoteThumbnail() - } - - // Otherwise if the quotedInteraction has a link preview, try clone that - return try? quotedInteraction.linkPreview - .fetchOne(db)? - .attachment - .fetchOne(db)? - .cloneAsQuoteThumbnail() - } - .defaulting(to: Attachment(proto: attachment)) - .inserted(db) - .id - } - else { - self.attachmentId = nil - } - - // Make sure the quote is valid before completing - if self.body == nil && self.attachmentId == nil { - return nil - } + self.body = nil + self.attachmentId = nil } } diff --git a/SessionMessagingKit/Shared Models/MessageViewModel.swift b/SessionMessagingKit/Shared Models/MessageViewModel.swift index bad4cb96e..69b28faff 100644 --- a/SessionMessagingKit/Shared Models/MessageViewModel.swift +++ b/SessionMessagingKit/Shared Models/MessageViewModel.swift @@ -656,7 +656,17 @@ public extension MessageViewModel { LEFT JOIN \(DisappearingMessagesConfiguration.self) ON \(disappearingMessagesConfig[.threadId]) = \(interaction[.threadId]) LEFT JOIN \(OpenGroup.self) ON \(openGroup[.threadId]) = \(interaction[.threadId]) LEFT JOIN \(Profile.self) ON \(profile[.id]) = \(interaction[.authorId]) - LEFT JOIN \(Quote.self) ON \(quote[.interactionId]) = \(interaction[.id]) + LEFT JOIN ( + SELECT \(quote[.interactionId]), + \(quote[.authorId]), + \(quote[.timestampMs]), + FROM \(Quote.self) + LEFT JOIN ( + SELECT \(interaction[.authorId]), + \(interaction[.timestampMs]), + \(interaction[.body]) + ) + ) AS \(ViewModel.quoteKey) ON \(quote[.interactionId]) = \(interaction[.id]) LEFT JOIN \(Attachment.self) AS \(ViewModel.quoteAttachmentKey) ON \(ViewModel.quoteAttachmentKey).\(attachmentIdColumnLiteral) = \(quote[.attachmentId]) LEFT JOIN \(LinkPreview.self) ON ( \(linkPreview[.url]) = \(interaction[.linkPreviewUrl]) AND