session-ios/SessionMessagingKit/Database/Migrations/_016_DisappearingMessagesCo...

85 lines
3.9 KiB
Swift
Raw Normal View History

2022-10-24 08:22:34 +02:00
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import GRDB
import SessionUtilitiesKit
enum _016_DisappearingMessagesConfiguration: Migration {
2022-10-24 08:22:34 +02:00
static let target: TargetMigrations.Identifier = .messagingKit
static let identifier: String = "DisappearingMessagesWithTypes"
2022-10-24 08:22:34 +02:00
static let needsConfigSync: Bool = false
static let minExpectedRunDuration: TimeInterval = 0.1
static var requirements: [MigrationRequirement] = [.sessionUtilStateLoaded]
2022-10-24 08:22:34 +02:00
static func migrate(_ db: Database, using dependencies: Dependencies) throws {
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)
}
try db.alter(table: Contact.self) { t in
t.add(.lastKnownClientVersion, .integer)
}
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.
try db.createIndex(
on: Interaction.self,
columns: [.wasRead, .variant]
2023-01-10 05:19:37 +01: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 {
return Storage.update(progress: 1, for: self, in: target, using: dependencies)
}
// 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-26 01:50:07 +02:00
}
// Process any existing disappearing message settings
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
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)
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
// Update the configs so the settings are synced
_ = try SessionUtil.updatingDisappearingConfigsOneToOne(db, contactUpdate, using: dependencies)
_ = try SessionUtil.batchUpdate(db, disappearingConfigs: legacyGroupUpdate, using: dependencies)
Storage.update(progress: 1, for: self, in: target, using: dependencies)
2022-10-24 08:22:34 +02:00
}
}