diff --git a/Session/Meta/AppDelegate.swift b/Session/Meta/AppDelegate.swift index 2f5e77c54..d55ccf505 100644 --- a/Session/Meta/AppDelegate.swift +++ b/Session/Meta/AppDelegate.swift @@ -320,6 +320,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD Configuration.performMainSetup() JobRunner.setExecutor(SyncPushTokensJob.self, for: .syncPushTokens) + /// We need to do a clean up for disappear after send messages that are received by push notifications before + /// the app set up the main screen and load initial data to prevent a case when the PagedDatabaseObserver + /// hasn't been setup yet then the conversation screen can show stale (ie. deleted) interactions incorrectly + DisappearingMessagesJob.cleanExpiredMessagesOnLaunch() + // Setup the UI if needed, then trigger any post-UI setup actions self.ensureRootViewController(calledFrom: lifecycleMethod) { [weak self] success in // If we didn't successfully ensure the rootViewController then don't continue as diff --git a/SessionMessagingKit/Jobs/Types/DisappearingMessagesJob.swift b/SessionMessagingKit/Jobs/Types/DisappearingMessagesJob.swift index c41ac60cf..d5e238b9b 100644 --- a/SessionMessagingKit/Jobs/Types/DisappearingMessagesJob.swift +++ b/SessionMessagingKit/Jobs/Types/DisappearingMessagesJob.swift @@ -46,6 +46,24 @@ public enum DisappearingMessagesJob: JobExecutor { } } +// MARK: - Clean expired messages on app launch + +public extension DisappearingMessagesJob { + static func cleanExpiredMessagesOnLaunch() { + let timestampNowMs: TimeInterval = TimeInterval(SnodeAPI.currentOffsetTimestampMs()) + var numDeleted: Int = -1 + + Storage.shared.write { db in + numDeleted = try Interaction + .filter(Interaction.Columns.expiresStartedAtMs != nil) + .filter((Interaction.Columns.expiresStartedAtMs + (Interaction.Columns.expiresInSeconds * 1000)) <= timestampNowMs) + .deleteAll(db) + } + + SNLog("[DisappearingMessagesJob] Deleted \(numDeleted) expired messages on app launch.") + } +} + // MARK: - Convenience public extension DisappearingMessagesJob {