mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.9 KiB
Swift
49 lines
1.9 KiB
Swift
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import Foundation
|
|
|
|
@objc(SNOpenGroupServerIdLookupMigration)
|
|
public class OpenGroupServerIdLookupMigration: OWSDatabaseMigration {
|
|
@objc
|
|
class func migrationId() -> String {
|
|
return "003"
|
|
}
|
|
|
|
override public func runUp(completion: @escaping OWSDatabaseMigrationCompletion) {
|
|
self.doMigrationAsync(completion: completion)
|
|
}
|
|
|
|
private func doMigrationAsync(completion: @escaping OWSDatabaseMigrationCompletion) {
|
|
var lookups: [OpenGroupServerIdLookup] = []
|
|
|
|
Storage.write(with: { transaction in
|
|
TSGroupThread.enumerateCollectionObjects(with: transaction) { object, _ in
|
|
guard let thread: TSGroupThread = object as? TSGroupThread else { return }
|
|
guard let threadId: String = thread.uniqueId else { return }
|
|
guard let openGroup: OpenGroupV2 = Storage.shared.getV2OpenGroup(for: threadId) else { return }
|
|
|
|
thread.enumerateInteractions(with: transaction) { interaction, _ in
|
|
guard let tsMessage: TSMessage = interaction as? TSMessage else { return }
|
|
guard let tsMessageId: String = tsMessage.uniqueId else { return }
|
|
|
|
lookups.append(
|
|
OpenGroupServerIdLookup(
|
|
server: openGroup.server,
|
|
room: openGroup.room,
|
|
serverId: tsMessage.openGroupServerMessageID,
|
|
tsMessageId: tsMessageId
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
lookups.forEach { lookup in
|
|
Storage.shared.addOpenGroupServerIdLookup(lookup, using: transaction)
|
|
}
|
|
self.save(with: transaction) // Intentionally capture self
|
|
}, completion: {
|
|
completion()
|
|
})
|
|
}
|
|
}
|