add user config migration with disappearing messages config update

pull/941/head
Ryan Zhao
parent 2d3b42a53a
commit c932b7bd94

@ -40,6 +40,9 @@ enum _015_DisappearingMessagesConfiguration: Migration {
)
}
var contactUpdate: [DisappearingMessagesConfiguration] = []
var legacyGroupUpdate: [DisappearingMessagesConfiguration] = []
try DisappearingMessagesConfiguration
.filter(DisappearingMessagesConfiguration.Columns.isEnabled == true)
.fetchAll(db)
@ -51,13 +54,21 @@ enum _015_DisappearingMessagesConfiguration: Migration {
}
switch thread.variant {
case .contact: try updateDisappearingMessageType(db, id: config.threadId, type: .disappearAfterRead)
case .legacyGroup, .group: try updateDisappearingMessageType(db, id: config.threadId, type: .disappearAfterSend)
case .community: return
case .contact:
try updateDisappearingMessageType(db, id: config.threadId, type: .disappearAfterRead)
contactUpdate.append(config.with(type: .disappearAfterRead))
case .legacyGroup, .group:
try updateDisappearingMessageType(db, id: config.threadId, type: .disappearAfterSend)
legacyGroupUpdate.append(config.with(type: .disappearAfterSend))
case .community:
return
}
}
}
_ = try SessionUtil.updatingDisappearingConfigs(db, contactUpdate)
_ = try SessionUtil.batchUpdate(db, disappearingConfigs: legacyGroupUpdate)
Storage.update(progress: 1, for: self, in: target) // In case this is the last migration
}
}

@ -479,6 +479,60 @@ internal extension SessionUtil {
return updated
}
static func updatingDisappearingConfigs<T>(_ db: Database, _ updated: [T]) throws -> [T] {
guard let updatedDisappearingConfigs: [DisappearingMessagesConfiguration] = updated as? [DisappearingMessagesConfiguration] else { throw StorageError.generic }
// We should only sync disappearing messages configs which are associated to existing contacts
let existingContactIds: [String] = (try? Contact
.filter(ids: updatedDisappearingConfigs.map { $0.id })
.select(.id)
.asRequest(of: String.self)
.fetchAll(db))
.defaulting(to: [])
// If none of the disappearing messages configs are associated with existing contacts then ignore
// the changes (no need to do a config sync)
guard !existingContactIds.isEmpty else { return updated }
// Get the user public key (updating note to self is handled separately)
let userPublicKey: String = getUserHexEncodedPublicKey(db)
let targetDisappearingConfigs: [DisappearingMessagesConfiguration] = updatedDisappearingConfigs
.filter {
$0.id != userPublicKey &&
SessionId(from: $0.id)?.prefix == .standard &&
existingContactIds.contains($0.id)
}
// Update the note to self disappearing messages config first (if needed)
if let updatedUserDisappearingConfig: DisappearingMessagesConfiguration = updatedDisappearingConfigs.first(where: { $0.id == userPublicKey }) {
try SessionUtil.performAndPushChange(
db,
for: .userProfile,
publicKey: userPublicKey
) { conf in
try SessionUtil.updateNoteToSelf(
disappearingMessagesConfig: updatedUserDisappearingConfig,
in: conf
)
}
}
try SessionUtil.performAndPushChange(
db,
for: .contacts,
publicKey: userPublicKey
) { conf in
try SessionUtil
.upsert(
contactData: targetDisappearingConfigs
.map { SyncedContactInfo(id: $0.id, disappearingMessagesConfig: $0) },
in: conf
)
}
return updated
}
}
// MARK: - External Outgoing Changes

@ -697,6 +697,27 @@ public extension SessionUtil {
}
}
static func batchUpdate(
_ db: Database,
disappearingConfigs: [DisappearingMessagesConfiguration]
) throws {
try SessionUtil.performAndPushChange(
db,
for: .userGroups,
publicKey: getUserHexEncodedPublicKey(db)
) { conf in
try SessionUtil.upsert(
legacyGroups: disappearingConfigs.map {
LegacyGroupInfo(
id: $0.id,
disappearingConfig: $0
)
},
in: conf
)
}
}
static func remove(_ db: Database, legacyGroupIds: [String]) throws {
guard !legacyGroupIds.isEmpty else { return }

Loading…
Cancel
Save