2022-10-24 08:22:34 +02:00
|
|
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import GRDB
|
|
|
|
import SessionUtilitiesKit
|
|
|
|
|
2023-08-14 03:08:04 +02:00
|
|
|
enum _016_DisappearingMessagesConfiguration: Migration {
|
2022-10-24 08:22:34 +02:00
|
|
|
static let target: TargetMigrations.Identifier = .messagingKit
|
2022-12-02 06:59:08 +01:00
|
|
|
static let identifier: String = "DisappearingMessagesWithTypes"
|
2022-10-24 08:22:34 +02:00
|
|
|
static let needsConfigSync: Bool = false
|
|
|
|
static let minExpectedRunDuration: TimeInterval = 0.1
|
2023-08-04 02:36:20 +02:00
|
|
|
static var requirements: [MigrationRequirement] = [.sessionUtilStateLoaded]
|
2022-10-24 08:22:34 +02:00
|
|
|
|
2023-08-31 02:35:03 +02:00
|
|
|
static func migrate(_ db: Database, using dependencies: Dependencies) throws {
|
2022-10-25 07:16:08 +02:00
|
|
|
try db.alter(table: DisappearingMessagesConfiguration.self) { t in
|
|
|
|
t.add(.type, .integer)
|
2022-11-02 03:03:41 +01:00
|
|
|
t.add(.lastChangeTimestampMs, .integer)
|
|
|
|
.defaults(to: 0)
|
2022-10-25 07:16:08 +02:00
|
|
|
}
|
|
|
|
|
2022-12-02 06:59:08 +01:00
|
|
|
try db.alter(table: Contact.self) { t in
|
2022-12-05 01:10:44 +01:00
|
|
|
t.add(.lastKnownClientVersion, .integer)
|
2022-12-02 06:59:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-10 05:19:37 +01:00
|
|
|
/// Add index on interaction table for wasRead and variant
|
|
|
|
///
|
|
|
|
/// This is due to new disappearing messages will need some info messages to be able to be unread,
|
|
|
|
/// but we only want to count the unread message number by incoming visible messages and call messages.
|
2023-08-04 02:36:20 +02:00
|
|
|
try db.createIndex(
|
|
|
|
on: Interaction.self,
|
|
|
|
columns: [.wasRead, .variant]
|
2023-01-10 05:19:37 +01:00
|
|
|
)
|
|
|
|
|
2023-08-04 02:36:20 +02:00
|
|
|
// If there isn't already a user account then we can just finish here (there will be no
|
|
|
|
// threads/configs to update and the configs won't be setup which would cause this to crash
|
|
|
|
guard Identity.userExists(db) else {
|
2023-09-01 05:40:50 +02:00
|
|
|
return Storage.update(progress: 1, for: self, in: target, using: dependencies)
|
2023-08-04 02:36:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience function to set the disappearing messages type per conversation
|
2022-10-26 01:50:07 +02:00
|
|
|
func updateDisappearingMessageType(_ db: GRDB.Database, id: String, type: DisappearingMessagesConfiguration.DisappearingMessageType) throws {
|
|
|
|
_ = try DisappearingMessagesConfiguration
|
|
|
|
.filter(DisappearingMessagesConfiguration.Columns.threadId == id)
|
|
|
|
.updateAll(
|
|
|
|
db,
|
|
|
|
DisappearingMessagesConfiguration.Columns.type.set(to: type)
|
2022-10-25 07:16:08 +02:00
|
|
|
)
|
2022-10-26 01:50:07 +02:00
|
|
|
}
|
|
|
|
|
2023-08-04 02:36:20 +02:00
|
|
|
// Process any existing disappearing message settings
|
2023-06-26 06:44:02 +02:00
|
|
|
var contactUpdate: [DisappearingMessagesConfiguration] = []
|
|
|
|
var legacyGroupUpdate: [DisappearingMessagesConfiguration] = []
|
|
|
|
|
2022-10-26 01:50:07 +02:00
|
|
|
try DisappearingMessagesConfiguration
|
|
|
|
.filter(DisappearingMessagesConfiguration.Columns.isEnabled == true)
|
|
|
|
.fetchAll(db)
|
|
|
|
.forEach { config in
|
2023-08-04 02:36:20 +02:00
|
|
|
guard let thread: SessionThread = try? SessionThread.fetchOne(db, id: config.threadId) else { return }
|
|
|
|
guard !thread.isNoteToSelf(db) else {
|
|
|
|
try updateDisappearingMessageType(db, id: config.threadId, type: .disappearAfterSend)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch thread.variant {
|
|
|
|
case .contact:
|
|
|
|
try updateDisappearingMessageType(db, id: config.threadId, type: .disappearAfterRead)
|
|
|
|
contactUpdate.append(config.with(type: .disappearAfterRead))
|
|
|
|
|
|
|
|
case .legacyGroup, .group:
|
2022-10-26 01:50:07 +02:00
|
|
|
try updateDisappearingMessageType(db, id: config.threadId, type: .disappearAfterSend)
|
2023-08-04 02:36:20 +02:00
|
|
|
legacyGroupUpdate.append(config.with(type: .disappearAfterSend))
|
|
|
|
|
|
|
|
case .community: return
|
2022-10-26 01:50:07 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-24 08:22:34 +02:00
|
|
|
|
2023-08-04 02:36:20 +02:00
|
|
|
// Update the configs so the settings are synced
|
2023-08-31 02:35:03 +02:00
|
|
|
_ = try SessionUtil.updatingDisappearingConfigsOneToOne(db, contactUpdate, using: dependencies)
|
|
|
|
_ = try SessionUtil.batchUpdate(db, disappearingConfigs: legacyGroupUpdate, using: dependencies)
|
2023-06-26 06:44:02 +02:00
|
|
|
|
2023-09-01 05:40:50 +02:00
|
|
|
Storage.update(progress: 1, for: self, in: target, using: dependencies)
|
2022-10-24 08:22:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|