From c8efd83c394f605392eba149c2a8967acbd60dcf Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 15 Mar 2017 12:32:58 -0300 Subject: [PATCH] Respond to CR. // FREEBIE --- src/Messages/Attachments/TSAttachment.h | 5 +-- src/Messages/Attachments/TSAttachment.m | 29 ++--------------- .../Attachments/TSAttachmentPointer.m | 32 +++++++++++++++++++ 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/Messages/Attachments/TSAttachment.h b/src/Messages/Attachments/TSAttachment.h index 150fa7199..537ae0a33 100644 --- a/src/Messages/Attachments/TSAttachment.h +++ b/src/Messages/Attachments/TSAttachment.h @@ -6,8 +6,6 @@ NS_ASSUME_NONNULL_BEGIN -@class TSAttachmentPointer; - @interface TSAttachment : TSYapDatabaseObject { @protected @@ -24,7 +22,6 @@ NS_ASSUME_NONNULL_BEGIN @property (atomic, readwrite) UInt64 serverId; @property (atomic, readwrite) NSData *encryptionKey; @property (nonatomic, readonly) NSString *contentType; -// This property @property (atomic, readwrite) BOOL isDownloaded; @@ -40,7 +37,7 @@ NS_ASSUME_NONNULL_BEGIN // This constructor is used for new instances of TSAttachmentStream // that represent downloaded incoming attachments. -- (instancetype)initWithPointer:(TSAttachmentPointer *)pointer; +- (instancetype)initWithPointer:(TSAttachment *)pointer; - (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion; diff --git a/src/Messages/Attachments/TSAttachment.m b/src/Messages/Attachments/TSAttachment.m index 3fcbd3946..cd92bbf38 100644 --- a/src/Messages/Attachments/TSAttachment.m +++ b/src/Messages/Attachments/TSAttachment.m @@ -4,7 +4,6 @@ #import "TSAttachment.h" #import "MIMETypeUtil.h" -#import "TSAttachmentPointer.h" NS_ASSUME_NONNULL_BEGIN @@ -54,7 +53,7 @@ NSUInteger const TSAttachmentSchemaVersion = 3; // This constructor is used for new instances of TSAttachmentStream // that represent downloaded incoming attachments. -- (instancetype)initWithPointer:(TSAttachmentPointer *)pointer +- (instancetype)initWithPointer:(TSAttachment *)pointer { // Once saved, this AttachmentStream will replace the AttachmentPointer in the attachments collection. self = [super initWithUniqueId:pointer.uniqueId]; @@ -70,11 +69,6 @@ NSUInteger const TSAttachmentSchemaVersion = 3; return self; } -- (BOOL)isDecimalNumberText:(NSString *)text -{ - return [text componentsSeparatedByCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]].count == 1; -} - - (nullable instancetype)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; @@ -92,25 +86,8 @@ NSUInteger const TSAttachmentSchemaVersion = 3; - (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion { - // TSAttachment is a base class for TSAttachmentPointer (a yet-to-be-downloaded - // incoming attachment) and TSAttachmentStream (an outgoing or already-downloaded - // incoming attachment). - // - // The attachmentSchemaVersion and serverId properties only apply to - // TSAttachmentPointer, which can be distinguished by the isDownloaded - // property. - if (!_isDownloaded && _attachmentSchemaVersion < 2) { - if (!_serverId) { - OWSAssert([self isDecimalNumberText:self.uniqueId]); - if (![self isDecimalNumberText:self.uniqueId]) { - DDLogError(@"%@ invalid legacy attachment uniqueId: %@.", self.tag, self.uniqueId); - } - _serverId = [self.uniqueId integerValue]; - if (!_serverId) { - DDLogError(@"%@ failed to parse legacy attachment uniqueId: %@.", self.tag, self.uniqueId); - } - } - } + // This method is overridden by the base classes TSAttachmentPointer and + // TSAttachmentStream. } + (NSString *)collection { diff --git a/src/Messages/Attachments/TSAttachmentPointer.m b/src/Messages/Attachments/TSAttachmentPointer.m index 8701335f4..0010c79c8 100644 --- a/src/Messages/Attachments/TSAttachmentPointer.m +++ b/src/Messages/Attachments/TSAttachmentPointer.m @@ -28,6 +28,38 @@ NS_ASSUME_NONNULL_BEGIN return self; } +- (BOOL)isDecimalNumberText:(NSString *)text +{ + return [text componentsSeparatedByCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]].count == 1; +} + +- (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion +{ + // Legacy instances of TSAttachmentPointer apparently used the serverId as their + // uniqueId. + if (attachmentSchemaVersion < 2 && self.serverId == 0) { + OWSAssert([self isDecimalNumberText:self.uniqueId]); + if ([self isDecimalNumberText:self.uniqueId]) { + // For legacy instances, try to parse the serverId from the uniqueId. + self.serverId = [self.uniqueId integerValue]; + } else { + DDLogError(@"%@ invalid legacy attachment uniqueId: %@.", self.tag, self.uniqueId); + } + } +} + +#pragma mark - Logging + ++ (NSString *)tag +{ + return [NSString stringWithFormat:@"[%@]", self.class]; +} + +- (NSString *)tag +{ + return self.class.tag; +} + @end NS_ASSUME_NONNULL_END