migrate existing attachments to album-compatible gallery schema

This commit is contained in:
Michael Kirk 2018-11-07 13:52:09 -06:00
parent 57681bd6f3
commit e096406e56
6 changed files with 88 additions and 1 deletions

View File

@ -444,6 +444,7 @@
4C4BC6C32102D697004040C9 /* ContactDiscoveryOperationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C4BC6C22102D697004040C9 /* ContactDiscoveryOperationTest.swift */; };
4C63CC00210A620B003AE45C /* SignalTSan.supp in Resources */ = {isa = PBXBuildFile; fileRef = 4C63CBFF210A620B003AE45C /* SignalTSan.supp */; };
4C6F527C20FFE8400097DEEE /* SignalUBSan.supp in Resources */ = {isa = PBXBuildFile; fileRef = 4C6F527B20FFE8400097DEEE /* SignalUBSan.supp */; };
4C7537892193779700DF5E37 /* OWS113MultiAttachmentMediaMessages.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7537882193779700DF5E37 /* OWS113MultiAttachmentMediaMessages.swift */; };
4C858A52212DC5E1001B45D3 /* UIImage+OWS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C858A51212DC5E1001B45D3 /* UIImage+OWS.swift */; };
4C948FF72146EB4800349F0D /* BlockListCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C948FF62146EB4800349F0D /* BlockListCache.swift */; };
4C9CA25D217E676900607C63 /* ZXingObjC.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C9CA25C217E676900607C63 /* ZXingObjC.framework */; };
@ -1142,6 +1143,7 @@
4C4BC6C22102D697004040C9 /* ContactDiscoveryOperationTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ContactDiscoveryOperationTest.swift; path = contact/ContactDiscoveryOperationTest.swift; sourceTree = "<group>"; };
4C63CBFF210A620B003AE45C /* SignalTSan.supp */ = {isa = PBXFileReference; lastKnownFileType = text; path = SignalTSan.supp; sourceTree = "<group>"; };
4C6F527B20FFE8400097DEEE /* SignalUBSan.supp */ = {isa = PBXFileReference; lastKnownFileType = text; path = SignalUBSan.supp; sourceTree = "<group>"; };
4C7537882193779700DF5E37 /* OWS113MultiAttachmentMediaMessages.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OWS113MultiAttachmentMediaMessages.swift; sourceTree = "<group>"; };
4C858A51212DC5E1001B45D3 /* UIImage+OWS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+OWS.swift"; sourceTree = "<group>"; };
4C948FF62146EB4800349F0D /* BlockListCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockListCache.swift; sourceTree = "<group>"; };
4C9CA25C217E676900607C63 /* ZXingObjC.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ZXingObjC.framework; path = ThirdParty/Carthage/Build/iOS/ZXingObjC.framework; sourceTree = "<group>"; };
@ -1639,6 +1641,7 @@
34D5872D208E2C4100D2255A /* OWS109OutgoingMessageState.m */,
349EA07B2162AEA700F7B17F /* OWS111UDAttributesMigration.swift */,
34B6A908218B8824007C4606 /* OWS112TypingIndicatorsMigration.swift */,
4C7537882193779700DF5E37 /* OWS113MultiAttachmentMediaMessages.swift */,
346129931FD1E30000532771 /* OWSDatabaseMigration.h */,
346129941FD1E30000532771 /* OWSDatabaseMigration.m */,
346129E51FD5C0C600532771 /* OWSDatabaseMigrationRunner.h */,
@ -3267,6 +3270,7 @@
34AC0A15211B39EA00997B47 /* ContactsViewHelper.m in Sources */,
346129FF1FD5F31400532771 /* OWS103EnableVideoCalling.m in Sources */,
346129E31FD5C0BE00532771 /* VersionMigrations.m in Sources */,
4C7537892193779700DF5E37 /* OWS113MultiAttachmentMediaMessages.swift in Sources */,
34AC0A16211B39EA00997B47 /* OWSNavigationBar.swift in Sources */,
34AC0A1A211B39EA00997B47 /* CommonStrings.swift in Sources */,
34AC0A19211B39EA00997B47 /* OWSAlerts.swift in Sources */,

View File

@ -640,7 +640,7 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
}
guard let message = attachmentStream.fetchAlbumMessage(with: transaction) else {
owsFailDebug("attachment was unexpectedly empty")
owsFailDebug("message was unexpectedly nil")
return nil
}

View File

@ -0,0 +1,72 @@
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
import SignalServiceKit
@objc
public class OWS113MultiAttachmentMediaMessages: OWSDatabaseMigration {
// MARK: - Dependencies
// MARK: -
// Increment a similar constant for each migration.
@objc
class func migrationId() -> String {
return "113"
}
override public func runUp(completion: @escaping OWSDatabaseMigrationCompletion) {
Logger.debug("")
BenchAsync(title: "\(self.logTag)") { (benchCompletion) in
self.doMigrationAsync(completion: {
benchCompletion()
completion()
})
}
}
private func doMigrationAsync(completion : @escaping OWSDatabaseMigrationCompletion) {
DispatchQueue.global().async {
var legacyAttachments: [(attachmentId: String, messageId: String)] = []
self.dbReadWriteConnection().read { transaction in
TSMessage.enumerateCollectionObjects(with: transaction) { object, _ in
autoreleasepool {
guard let message: TSMessage = object as? TSMessage else {
Logger.debug("ignoring message with type: \(object)")
return
}
guard let messageId = message.uniqueId else {
owsFailDebug("messageId was unexpectedly nil")
return
}
for attachmentId in message.attachmentIds {
legacyAttachments.append((attachmentId: attachmentId as! String, messageId: messageId))
}
}
}
}
self.dbReadWriteConnection().readWrite { transaction in
for (attachmentId, messageId) in legacyAttachments {
autoreleasepool {
guard let attachment = TSAttachment.fetch(uniqueId: attachmentId, transaction: transaction) else {
Logger.warn("missing attachment for messageId: \(messageId)")
return
}
attachment.migrateAlbumMessageId(messageId)
attachment.save(with: transaction)
}
}
self.save(with: transaction)
}
completion()
}
}
}

View File

@ -47,6 +47,7 @@ NS_ASSUME_NONNULL_BEGIN
[[OWS109OutgoingMessageState alloc] initWithPrimaryStorage:primaryStorage],
[[OWS111UDAttributesMigration alloc] initWithPrimaryStorage:primaryStorage],
[[OWS112TypingIndicatorsMigration alloc] initWithPrimaryStorage:primaryStorage],
[[OWS113MultiAttachmentMediaMessages alloc] initWithPrimaryStorage:primaryStorage],
];
}

View File

@ -45,6 +45,11 @@ typedef NS_ENUM(NSUInteger, TSAttachmentType) {
@property (nonatomic, readonly, nullable) NSString *albumMessageId;
- (nullable TSMessage *)fetchAlbumMessageWithTransaction:(YapDatabaseReadTransaction *)transaction;
// `migrateAlbumMessageId` is only used in the migration to the new multi-attachment message scheme,
// and shouldn't be used as a general purpose setter. Instead, `albumMessageId` should be passed as
// an initializer param.
- (void)migrateAlbumMessageId:(NSString *)albumMesssageId;
#pragma mark -
// This constructor is used for new instances of TSAttachmentPointer,

View File

@ -254,6 +254,11 @@ NSUInteger const TSAttachmentSchemaVersion = 4;
return [TSMessage fetchObjectWithUniqueID:self.albumMessageId transaction:transaction];
}
- (void)migrateAlbumMessageId:(NSString *)albumMesssageId
{
_albumMessageId = albumMesssageId;
}
@end
NS_ASSUME_NONNULL_END