From 5088e394f2ca31298b68f3c7abb36af873fdfef3 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Wed, 13 Sep 2023 17:23:18 +1000 Subject: [PATCH 1/3] clean --- Session/Conversations/Message Cells/VisibleMessageCell.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Session/Conversations/Message Cells/VisibleMessageCell.swift b/Session/Conversations/Message Cells/VisibleMessageCell.swift index c264472ea..e1670ea30 100644 --- a/Session/Conversations/Message Cells/VisibleMessageCell.swift +++ b/Session/Conversations/Message Cells/VisibleMessageCell.swift @@ -467,7 +467,6 @@ final class VisibleMessageCell: MessageCell, TappableLabelDelegate { subview.removeFromSuperview() } albumView = nil - albumView = nil bodyTappableLabel = nil // Handle the deleted state first (it's much simpler than the others) From 5f25abc213d1de3b899e83f2b793268feae253f1 Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Thu, 14 Sep 2023 15:12:37 +1000 Subject: [PATCH 2/3] add paged database observer for link preview attachment --- .../Conversations/ConversationViewModel.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Session/Conversations/ConversationViewModel.swift b/Session/Conversations/ConversationViewModel.swift index 0bfef4254..7698a6f25 100644 --- a/Session/Conversations/ConversationViewModel.swift +++ b/Session/Conversations/ConversationViewModel.swift @@ -265,6 +265,24 @@ public class ConversationViewModel: OWSAudioPlayerDelegate { .allCases .filter { $0 != .wasRead } ), + PagedData.ObservedChanges( + table: Attachment.self, + columns: [.state], + joinToPagedType: { + let interaction: TypedTableAlias = TypedTableAlias() + let linkPreview: TypedTableAlias = TypedTableAlias() + let linkPreviewAttachment: TypedTableAlias = TypedTableAlias() + + return SQL(""" + LEFT JOIN \(LinkPreview.self) ON ( + \(linkPreview[.url]) = \(interaction[.linkPreviewUrl]) AND + \(Interaction.linkPreviewFilterLiteral()) + ) + LEFT JOIN \(linkPreviewAttachment) ON \(linkPreviewAttachment[.id]) = \(linkPreview[.attachmentId]) + """ + ) + }() + ), PagedData.ObservedChanges( table: Contact.self, columns: [.isTrusted], From f92579db07617346f0c3f8b7534c6a3cf51ab29f Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Tue, 19 Sep 2023 15:40:58 +1000 Subject: [PATCH 3/3] Fixed a couple more bugs with link previews Fixed an issue where sending a link with a preview wouldn't work if you have a previous "failed" preview for the same link Fixed an issue where receiving a link with a preview could update all existing previews to an invalid state --- .../ConversationVC+Interaction.swift | 38 ++++++++++++++----- .../Database/Models/LinkPreview.swift | 6 +-- .../MessageReceiver+VisibleMessages.swift | 1 - 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index 9d7c469a1..1b513ff95 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -532,16 +532,34 @@ extension ConversationVC: let insertedInteraction: Interaction = try optimisticData.interaction.inserted(db) self?.viewModel.associate(optimisticMessageId: optimisticData.id, to: insertedInteraction.id) - // If there is a LinkPreview and it doesn't match an existing one then add it now - if - let linkPreviewDraft: LinkPreviewDraft = optimisticData.linkPreviewDraft, - (try? insertedInteraction.linkPreview.isEmpty(db)) == true - { - try LinkPreview( - url: linkPreviewDraft.urlString, - title: linkPreviewDraft.title, - attachmentId: try optimisticData.linkPreviewAttachment?.inserted(db).id - ).insert(db) + // If there is a LinkPreview draft then check the state of any existing link previews and + // insert a new one if needed + if let linkPreviewDraft: LinkPreviewDraft = optimisticData.linkPreviewDraft { + let invalidLinkPreviewAttachmentStates: [Attachment.State] = [ + .failedDownload, .pendingDownload, .downloading, .failedUpload, .invalid + ] + let linkPreviewAttachmentId: String? = try? insertedInteraction.linkPreview + .select(.attachmentId) + .asRequest(of: String.self) + .fetchOne(db) + let linkPreviewAttachmentState: Attachment.State = linkPreviewAttachmentId + .map { + try? Attachment + .filter(id: $0) + .select(.state) + .asRequest(of: Attachment.State.self) + .fetchOne(db) + } + .defaulting(to: .invalid) + + // If we don't have a "valid" existing link preview then upsert a new one + if invalidLinkPreviewAttachmentStates.contains(linkPreviewAttachmentState) { + try LinkPreview( + url: linkPreviewDraft.urlString, + title: linkPreviewDraft.title, + attachmentId: try optimisticData.linkPreviewAttachment?.inserted(db).id + ).save(db) + } } // If there is a Quote the insert it now diff --git a/SessionMessagingKit/Database/Models/LinkPreview.swift b/SessionMessagingKit/Database/Models/LinkPreview.swift index b214bc78d..c87740015 100644 --- a/SessionMessagingKit/Database/Models/LinkPreview.swift +++ b/SessionMessagingKit/Database/Models/LinkPreview.swift @@ -77,7 +77,7 @@ public struct LinkPreview: Codable, Equatable, Hashable, FetchableRecord, Persis // MARK: - Protobuf public extension LinkPreview { - init?(_ db: Database, proto: SNProtoDataMessage, body: String?, sentTimestampMs: TimeInterval) throws { + init?(_ db: Database, proto: SNProtoDataMessage, sentTimestampMs: TimeInterval) throws { guard let previewProto = proto.preview.first else { throw LinkPreviewError.noPreview } guard URL(string: previewProto.url) != nil else { throw LinkPreviewError.invalidInput } guard LinkPreview.isValidLinkUrl(previewProto.url) else { throw LinkPreviewError.invalidInput } @@ -86,9 +86,7 @@ public extension LinkPreview { let timestamp: TimeInterval = LinkPreview.timestampFor(sentTimestampMs: sentTimestampMs) let maybeLinkPreview: LinkPreview? = try? LinkPreview .filter(LinkPreview.Columns.url == previewProto.url) - .filter(LinkPreview.Columns.timestamp == LinkPreview.timestampFor( - sentTimestampMs: Double(proto.timestamp) - )) + .filter(LinkPreview.Columns.timestamp == timestamp) .fetchOne(db) if let linkPreview: LinkPreview = maybeLinkPreview { diff --git a/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+VisibleMessages.swift b/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+VisibleMessages.swift index 520002125..3f80ebe04 100644 --- a/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+VisibleMessages.swift +++ b/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+VisibleMessages.swift @@ -276,7 +276,6 @@ extension MessageReceiver { let linkPreview: LinkPreview? = try? LinkPreview( db, proto: dataMessage, - body: message.text, sentTimestampMs: (messageSentTimestamp * 1000) )?.saved(db)