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.
session-ios/SessionMessagingKit/Database/Migrations/_018_GroupsRebuildChanges.s...

107 lines
4.3 KiB
Swift

// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved.
//
// stringlint:disable
import Foundation
import UIKit.UIImage
import GRDB
import SessionSnodeKit
import SessionUtilitiesKit
Merge remote-tracking branch 'upstream/dev' into feature/groups-rebuild # Conflicts: # Session.xcodeproj/project.pbxproj # Session/Conversations/Settings/ThreadSettingsViewModel.swift # Session/Meta/Translations/de.lproj/Localizable.strings # Session/Meta/Translations/en.lproj/Localizable.strings # Session/Meta/Translations/es-ES.lproj/Localizable.strings # Session/Meta/Translations/fa.lproj/Localizable.strings # Session/Meta/Translations/fi.lproj/Localizable.strings # Session/Meta/Translations/fil.lproj/Localizable.strings # Session/Meta/Translations/fr.lproj/Localizable.strings # Session/Meta/Translations/hi.lproj/Localizable.strings # Session/Meta/Translations/hr.lproj/Localizable.strings # Session/Meta/Translations/it.lproj/Localizable.strings # Session/Meta/Translations/ja.lproj/Localizable.strings # Session/Meta/Translations/nl.lproj/Localizable.strings # Session/Meta/Translations/pl.lproj/Localizable.strings # Session/Meta/Translations/pt-BR.lproj/Localizable.strings # Session/Meta/Translations/ru.lproj/Localizable.strings # Session/Meta/Translations/sk.lproj/Localizable.strings # Session/Meta/Translations/sl.lproj/Localizable.strings # Session/Meta/Translations/sv-SE.lproj/Localizable.strings # Session/Meta/Translations/th.lproj/Localizable.strings # Session/Meta/Translations/vi.lproj/Localizable.strings # Session/Meta/Translations/zh-CN.lproj/Localizable.strings # Session/Meta/Translations/zh-TW.lproj/Localizable.strings # SessionMessagingKit/Calls/WebRTCSession.swift # SessionMessagingKit/Configuration.swift # SessionMessagingKit/Database/Migrations/_003_YDBToGRDBMigration.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+VisibleMessages.swift # SessionMessagingKit/SessionUtil/Config Handling/SessionUtil+Contacts.swift # SessionMessagingKit/Utilities/ProfileManager.swift # SessionMessagingKitTests/Jobs/Types/MessageSendJobSpec.swift # SessionMessagingKitTests/LibSessionUtil/LibSessionSpec.swift # SessionMessagingKitTests/LibSessionUtil/SessionUtilSpec.swift # SessionMessagingKitTests/Open Groups/Models/BatchRequestInfoSpec.swift # SessionMessagingKitTests/Open Groups/Models/SOGSMessageSpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupAPISpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupManagerSpec.swift # SessionMessagingKitTests/Open Groups/Types/SOGSEndpointSpec.swift # SessionMessagingKitTests/Sending & Receiving/MessageReceiverDecryptionSpec.swift # SessionMessagingKitTests/Sending & Receiving/MessageSenderEncryptionSpec.swift # SessionMessagingKitTests/Shared Models/SessionThreadViewModelSpec.swift # SessionMessagingKitTests/Utilities/CryptoSMKSpec.swift # SessionTests/Conversations/Settings/ThreadDisappearingMessagesViewModelSpec.swift # SessionTests/Conversations/Settings/ThreadSettingsViewModelSpec.swift # SessionTests/Settings/NotificationContentViewModelSpec.swift # SessionUtilitiesKitTests/Database/Models/IdentitySpec.swift # SessionUtilitiesKitTests/Database/Utilities/PersistableRecordUtilitiesSpec.swift # SessionUtilitiesKitTests/General/DependenciesSpec.swift # SessionUtilitiesKitTests/JobRunner/JobRunnerSpec.swift # _SharedTestUtilities/MockCaches.swift
2 years ago
enum _018_GroupsRebuildChanges: Migration {
static let target: TargetMigrations.Identifier = .messagingKit
static let identifier: String = "GroupsRebuildChanges"
static let needsConfigSync: Bool = false
static let minExpectedRunDuration: TimeInterval = 0.1
static var requirements: [MigrationRequirement] = [.sessionUtilStateLoaded]
static var fetchedTables: [(FetchableRecord & TableRecord).Type] = [
Identity.self, OpenGroup.self
]
static var createdOrAlteredTables: [(FetchableRecord & TableRecord).Type] = [
ClosedGroup.self, OpenGroup.self, GroupMember.self
]
static func migrate(_ db: Database, using dependencies: Dependencies) throws {
try db.alter(table: ClosedGroup.self) { t in
t.add(.groupDescription, .text)
t.add(.displayPictureUrl, .text)
t.add(.displayPictureFilename, .text)
t.add(.displayPictureEncryptionKey, .blob)
t.add(.lastDisplayPictureUpdate, .integer).defaults(to: 0)
t.add(.shouldPoll, .boolean).defaults(to: false)
t.add(.groupIdentityPrivateKey, .blob)
t.add(.authData, .blob)
t.add(.invited, .boolean).defaults(to: false)
}
try db.alter(table: OpenGroup.self) { t in
t.add(.displayPictureFilename, .text)
t.add(.lastDisplayPictureUpdate, .integer).defaults(to: 0)
}
try db.alter(table: GroupMember.self) { t in
t.add(.roleStatus, .integer)
.notNull()
.defaults(to: GroupMember.RoleStatus.accepted)
}
// Update existing groups where the current user is a member to have `shouldPoll` as `true`
try ClosedGroup
.joining(
required: ClosedGroup.members
.filter(GroupMember.Columns.profileId == getUserSessionId(db, using: dependencies).hexString)
)
.updateAll(
db,
ClosedGroup.Columns.shouldPoll.set(to: true)
)
// Move the `imageData` out of the `OpenGroup` table and on to disk to be consistent with
// the other display picture logic
let existingImageInfo: [OpenGroupImageInfo] = try OpenGroup
.filter(OpenGroup.Columns.deprecatedColumn("imageData") != nil)
.select(OpenGroup.Columns.threadId, OpenGroup.Columns.deprecatedColumn("imageData"))
.asRequest(of: OpenGroupImageInfo.self)
.fetchAll(db)
existingImageInfo.forEach { imageInfo in
let fileName: String = DisplayPictureManager.generateFilename()
let filePath: String = DisplayPictureManager.filepath(for: fileName)
// Save the decrypted display picture to disk
try? imageInfo.data.write(to: URL(fileURLWithPath: filePath), options: [.atomic])
guard UIImage(contentsOfFile: filePath) != nil else {
SNLog("[GroupsRebuildChanges] Failed to save Community imageData for \(imageInfo.threadId)")
return
}
// Update the database with the new info
_ = try? OpenGroup
.filter(id: imageInfo.threadId)
.updateAll( // Unsynced so no 'updateAllAndConfig'
db,
OpenGroup.Columns.deprecatedColumn("imageData").set(to: nil),
OpenGroup.Columns.displayPictureFilename.set(to: fileName),
OpenGroup.Columns.lastDisplayPictureUpdate.set(
to: SnodeAPI.currentOffsetTimestampMs(using: dependencies)
)
)
}
Storage.update(progress: 1, for: self, in: target, using: dependencies)
}
struct OpenGroupImageInfo: FetchableRecord, Decodable, ColumnExpressible {
typealias Columns = CodingKeys
enum CodingKeys: String, CodingKey, ColumnExpression, CaseIterable {
case threadId
case data = "imageData"
}
let threadId: String
let data: Data
}
}