From 3e3ce9d98312bb2eae2ec96d5e978eca66ed386d Mon Sep 17 00:00:00 2001 From: Pete Walters Date: Fri, 30 Aug 2024 14:30:08 -0500 Subject: [PATCH] Set of fixes to enable basic media tier attachment restore --- Signal/AppLaunch/AppDelegate.swift | 1 + .../CellViews/CVAttachmentProgressView.swift | 1 + .../MessageBackupManagerImpl.swift | 3 +++ .../TSResource/Attachment+TSResource.swift | 2 ++ .../TSResource/TSAttachment+TSResource.swift | 4 ++++ .../Attachments/TSResource/TSResource.swift | 2 ++ .../Messages/Attachments/V2/Attachment.swift | 4 ++++ .../AttachmentDownloadManagerImpl.swift | 24 +++++++++++++++++++ 8 files changed, 41 insertions(+) diff --git a/Signal/AppLaunch/AppDelegate.swift b/Signal/AppLaunch/AppDelegate.swift index 931cde0415..4c93437f19 100644 --- a/Signal/AppLaunch/AppDelegate.swift +++ b/Signal/AppLaunch/AppDelegate.swift @@ -1428,6 +1428,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { ExperienceUpgradeFinder.markAllCompleteForNewUser(transaction: transaction.unwrapGrdbWrite) } } + DependenciesBridge.shared.attachmentDownloadManager.beginDownloadingIfNecessary() } Self.updateApplicationShortcutItems(isRegistered: isRegistered) diff --git a/Signal/ConversationView/CellViews/CVAttachmentProgressView.swift b/Signal/ConversationView/CellViews/CVAttachmentProgressView.swift index c51277d737..bf7c093b15 100644 --- a/Signal/ConversationView/CellViews/CVAttachmentProgressView.swift +++ b/Signal/ConversationView/CellViews/CVAttachmentProgressView.swift @@ -408,6 +408,7 @@ public class CVAttachmentProgressView: ManualLayoutView { let wasNotCreatedLocally = outgoingMessage.wasNotCreatedLocally guard !attachmentStream.attachmentStream.isUploadedToTransitTier, + !attachmentStream.attachmentStream.hasMediaTierInfo, !wasNotCreatedLocally, !hasSendFailed else { diff --git a/SignalServiceKit/MessageBackup/MessageBackupManagerImpl.swift b/SignalServiceKit/MessageBackup/MessageBackupManagerImpl.swift index d6731d1b5c..1f2a6a3085 100644 --- a/SignalServiceKit/MessageBackup/MessageBackupManagerImpl.swift +++ b/SignalServiceKit/MessageBackup/MessageBackupManagerImpl.swift @@ -600,12 +600,15 @@ public class MessageBackupManagerImpl: MessageBackupManager { let shouldDownloadAllFullsize = backupAttachmentDownloadStore.getShouldStoreAllMediaLocally(tx: tx) try backupAttachmentDownloadStore.dequeueAndClearTable(tx: tx) { backupDownload in // Every backup attachment gets enqueued for thumbnail download at lower priority. + /* + TODO: Re-enable thumbnail downloading once AttachmentDownloadManager understands thumbnail types attachmentDownloadManager.enqueueDownloadOfAttachment( id: backupDownload.attachmentRowId, priority: .backupRestoreLow, source: .mediaTierThumbnail, tx: tx ) + */ // If its recent media, also download fullsize at higher priority. // Or if "optimize media" is off, download fullsize everything // regardless of date. diff --git a/SignalServiceKit/Messages/Attachments/TSResource/Attachment+TSResource.swift b/SignalServiceKit/Messages/Attachments/TSResource/Attachment+TSResource.swift index 41b42b6ba7..9b98b8116d 100644 --- a/SignalServiceKit/Messages/Attachments/TSResource/Attachment+TSResource.swift +++ b/SignalServiceKit/Messages/Attachments/TSResource/Attachment+TSResource.swift @@ -79,6 +79,8 @@ extension AttachmentStream: TSResource { public var isUploadedToTransitTier: Bool { attachment.isUploadedToTransitTier } + public var hasMediaTierInfo: Bool { attachment.hasMediaTierInfo } + public var mimeType: String { attachment.mimeType } public var concreteType: ConcreteTSResource { attachment.concreteType } diff --git a/SignalServiceKit/Messages/Attachments/TSResource/TSAttachment+TSResource.swift b/SignalServiceKit/Messages/Attachments/TSResource/TSAttachment+TSResource.swift index da261cd6c7..a03b864a82 100644 --- a/SignalServiceKit/Messages/Attachments/TSResource/TSAttachment+TSResource.swift +++ b/SignalServiceKit/Messages/Attachments/TSResource/TSAttachment+TSResource.swift @@ -72,6 +72,10 @@ extension TSAttachment: TSResource { } } + public var hasMediaTierInfo: Bool { + return false + } + public var mimeType: String { return contentType } diff --git a/SignalServiceKit/Messages/Attachments/TSResource/TSResource.swift b/SignalServiceKit/Messages/Attachments/TSResource/TSResource.swift index c6591fe352..9fee147974 100644 --- a/SignalServiceKit/Messages/Attachments/TSResource/TSResource.swift +++ b/SignalServiceKit/Messages/Attachments/TSResource/TSResource.swift @@ -47,6 +47,8 @@ public protocol TSResource { var isUploadedToTransitTier: Bool { get } + var hasMediaTierInfo: Bool { get } + // MARK: - Converters var concreteType: ConcreteTSResource { get } diff --git a/SignalServiceKit/Messages/Attachments/V2/Attachment.swift b/SignalServiceKit/Messages/Attachments/V2/Attachment.swift index 4cd8dbe787..c5e8c01295 100644 --- a/SignalServiceKit/Messages/Attachments/V2/Attachment.swift +++ b/SignalServiceKit/Messages/Attachments/V2/Attachment.swift @@ -223,6 +223,10 @@ public class Attachment { return transitTierInfo != nil } + public var hasMediaTierInfo: Bool { + return mediaTierInfo != nil + } + func asStream() -> AttachmentStream? { return AttachmentStream(attachment: self) } diff --git a/SignalServiceKit/Messages/Attachments/V2/Downloads/AttachmentDownloadManagerImpl.swift b/SignalServiceKit/Messages/Attachments/V2/Downloads/AttachmentDownloadManagerImpl.swift index de74de3da1..5cadadd0f5 100644 --- a/SignalServiceKit/Messages/Attachments/V2/Downloads/AttachmentDownloadManagerImpl.swift +++ b/SignalServiceKit/Messages/Attachments/V2/Downloads/AttachmentDownloadManagerImpl.swift @@ -7,6 +7,7 @@ import Foundation public class AttachmentDownloadManagerImpl: AttachmentDownloadManager { + private let appReadiness: Shims.AppReadiness private let attachmentDownloadStore: AttachmentDownloadStore private let attachmentStore: AttachmentStore private let attachmentUpdater: AttachmentUpdater @@ -16,6 +17,7 @@ public class AttachmentDownloadManagerImpl: AttachmentDownloadManager { private let downloadabilityChecker: DownloadabilityChecker private let progressStates: ProgressStates private let queueLoader: PersistedQueueLoader + private let tsAccountManager: TSAccountManager public init( appReadiness: Shims.AppReadiness, @@ -40,6 +42,7 @@ public class AttachmentDownloadManagerImpl: AttachmentDownloadManager { ) { self.attachmentDownloadStore = attachmentDownloadStore self.attachmentStore = attachmentStore + self.appReadiness = appReadiness self.db = db self.decrypter = Decrypter( attachmentValidator: attachmentValidator, @@ -82,10 +85,27 @@ public class AttachmentDownloadManagerImpl: AttachmentDownloadManager { stickerManager: stickerManager, tsAccountManager: tsAccountManager ) + self.tsAccountManager = tsAccountManager appReadiness.runNowOrWhenMainAppDidBecomeReadyAsync { [weak self] in self?.beginDownloadingIfNecessary() } + + NotificationCenter.default.addObserver( + self, + selector: #selector(registrationStateDidChange), + name: .registrationStateDidChange, + object: nil + ) + } + + @objc + private func registrationStateDidChange() { + AssertIsOnMainThread() + guard tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { return } + appReadiness.runNowOrWhenMainAppDidBecomeReadyAsync { [weak self] in + self?.beginDownloadingIfNecessary() + } } public func downloadBackup(metadata: BackupReadCredential) -> Promise { @@ -214,6 +234,10 @@ public class AttachmentDownloadManagerImpl: AttachmentDownloadManager { guard CurrentAppContext().isMainApp else { return } + guard tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { + return + } + Task { [weak self] in try await self?.queueLoader.loadFromQueueIfAble() }