From bd309c4f6f33769239c8f193f05a1729bbe4087f Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 16 Sep 2019 16:36:17 +1000 Subject: [PATCH] Clean --- Signal/Signal-Info.plist | 4 -- .../src/Messages/Attachments/TSAttachment.m | 1 - .../Interactions/OWSLinkPreview.swift | 42 ++++++++----------- .../src/Messages/Interactions/TSMessage.h | 4 +- .../src/Messages/Interactions/TSMessage.m | 20 ++++----- .../src/Messages/OWSMessageSender.m | 2 +- 6 files changed, 28 insertions(+), 45 deletions(-) diff --git a/Signal/Signal-Info.plist b/Signal/Signal-Info.plist index b4ed0ec7c..96a81db1a 100644 --- a/Signal/Signal-Info.plist +++ b/Signal/Signal-Info.plist @@ -6,14 +6,10 @@ CarthageVersion 0.33.0 - DateTime - Fri Sep 13 00:30:48 UTC 2019 OSXVersion 10.14.6 WebRTCCommit 1445d719bf05280270e9f77576f80f973fd847f8 M73 - XCodeVersion - 1000.1030 CFBundleDevelopmentRegion en diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachment.m b/SignalServiceKit/src/Messages/Attachments/TSAttachment.m index ddb74a35c..692f1ac52 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachment.m +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachment.m @@ -290,7 +290,6 @@ NSUInteger const TSAttachmentSchemaVersion = 4; #pragma mark - Relationships - - (nullable TSMessage *)fetchAlbumMessageWithTransaction:(YapDatabaseReadTransaction *)transaction { if (self.albumMessageId == nil) { diff --git a/SignalServiceKit/src/Messages/Interactions/OWSLinkPreview.swift b/SignalServiceKit/src/Messages/Interactions/OWSLinkPreview.swift index d5ccd9c81..5c8c7d78d 100644 --- a/SignalServiceKit/src/Messages/Interactions/OWSLinkPreview.swift +++ b/SignalServiceKit/src/Messages/Interactions/OWSLinkPreview.swift @@ -123,10 +123,7 @@ public class OWSLinkPreview: MTLModel { @objc public class func isInvalidContentError(_ error: Error) -> Bool { - guard let error = error as? LinkPreviewError else { - return false - } - + guard let error = error as? LinkPreviewError else { return false } return error == .invalidContent } @@ -253,12 +250,10 @@ public class OWSLinkPreview: MTLModel { return nil } attachment.save(with: transaction) - + return attachment.uniqueId } - - private func isValid() -> Bool { var hasTitle = false if let titleValue = title { @@ -701,37 +696,36 @@ public class OWSLinkPreview: MTLModel { return promise } - public class func getImagePreview(fromUrl imageUrl: String, transaction: YapDatabaseReadWriteTransaction) -> Promise { - // Get the mime types the url - guard let imageFileExtension = fileExtension(forImageUrl: imageUrl), - let imageMimeType = mimetype(forImageFileExtension: imageFileExtension) else { - return Promise(error: LinkPreviewError.invalidInput) + public class func getImagePreview(from url: String, in transaction: YapDatabaseReadWriteTransaction) -> Promise { + // Get the MIME type + guard let imageFileExtension = fileExtension(forImageUrl: url), let imageMIMEType = mimetype(forImageFileExtension: imageFileExtension) else { + return Promise(error: LinkPreviewError.invalidInput) } - return downloadImage(url: imageUrl).map { data in - // Make sure the downloaded image has the correct mime type - guard let newImageMimeType = NSData(data: data).ows_guessMimeType() else { + return downloadImage(url: url).map { data in + // Make sure the downloaded image has the correct MIME type + guard let newImageMIMEType = NSData(data: data).ows_guessMimeType() else { throw LinkPreviewError.invalidContent } // Save the attachment - guard let attachmentId = saveAttachmentIfPossible(imageData: data, mimeType: newImageMimeType, transaction: transaction) else { - Logger.verbose("Error: Failed to save attachment for \(imageUrl)") + guard let attachmentId = saveAttachmentIfPossible(imageData: data, mimeType: newImageMIMEType, transaction: transaction) else { + Logger.verbose("Failed to save attachment for: \(url).") throw LinkPreviewError.attachmentFailedToSave } - // If we had a GIF and the data we have is not a GIF then we need to render a link preview without attachments - if (imageMimeType == OWSMimeTypeImageGif && newImageMimeType != OWSMimeTypeImageGif) { - return OWSLinkPreview(urlString: imageUrl, title: nil, imageAttachmentId: attachmentId) + // If it's a GIF and the data we have is not a GIF then we need to render a link preview without attachments + if (imageMIMEType == OWSMimeTypeImageGif && newImageMIMEType != OWSMimeTypeImageGif) { + return OWSLinkPreview(urlString: url, title: nil, imageAttachmentId: attachmentId) } - return OWSLinkPreview(urlString: imageUrl, title: nil, imageAttachmentId: attachmentId, isDirectAttachment: true) + return OWSLinkPreview(urlString: url, title: nil, imageAttachmentId: attachmentId, isDirectAttachment: true) } } @objc(getImagePreviewFromUrl:transaction:) - public class func objc_getImagePreview(url imageUrl: String, transaction: YapDatabaseReadWriteTransaction) -> AnyPromise { - return AnyPromise.from(getImagePreview(fromUrl: imageUrl, transaction: transaction)) + public class func objc_getImagePreview(url: String, in transaction: YapDatabaseReadWriteTransaction) -> AnyPromise { + return AnyPromise.from(getImagePreview(from: url, in: transaction)) } public class func downloadImage(url imageUrl: String) -> Promise { @@ -799,7 +793,7 @@ public class OWSLinkPreview: MTLModel { return Promise(error: LinkPreviewError.invalidContent) } - // If we have a gif then don't download it as a jpg and also we need to ensure that it's a valid GIF + // Loki: If it's a GIF then ensure it's validity and don't download it as a JPG if (imageMimeType == OWSMimeTypeImageGif && NSData(data: data).ows_isValidImage(withMimeType: OWSMimeTypeImageGif)) { return Promise.value(data) } let maxImageSize: CGFloat = 1024 diff --git a/SignalServiceKit/src/Messages/Interactions/TSMessage.h b/SignalServiceKit/src/Messages/Interactions/TSMessage.h index 8359d7cdc..d115a4fe0 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSMessage.h +++ b/SignalServiceKit/src/Messages/Interactions/TSMessage.h @@ -67,7 +67,7 @@ typedef NS_ENUM(NSInteger, LKMessageFriendRequestStatus) { - (NSArray *)attachmentsWithTransaction:(YapDatabaseReadTransaction *)transaction; - (NSArray *)mediaAttachmentsWithTransaction:(YapDatabaseReadTransaction *)transaction; - (nullable TSAttachment *)oversizeTextAttachmentWithTransaction:(YapDatabaseReadTransaction *)transaction; -- (void)addAttachmentId:(NSString *)attachmentId transaction:(YapDatabaseReadWriteTransaction *)transaction; +- (void)addAttachmentWithID:(NSString *)attachmentID in:(YapDatabaseReadWriteTransaction *)transaction; - (void)removeAttachment:(TSAttachment *)attachment transaction:(YapDatabaseReadWriteTransaction *)transaction NS_SWIFT_NAME(removeAttachment(_:transaction:)); @@ -98,7 +98,7 @@ typedef NS_ENUM(NSInteger, LKMessageFriendRequestStatus) { - (void)saveGroupChatMessageID:(uint64_t)serverMessageID in:(YapDatabaseReadWriteTransaction *_Nullable)transaction; -#pragma mark - Link preview +#pragma mark - Link Preview - (void)generateLinkPreviewIfNeededFromURL:(NSString *)url; diff --git a/SignalServiceKit/src/Messages/Interactions/TSMessage.m b/SignalServiceKit/src/Messages/Interactions/TSMessage.m index 98ec481f1..75308fe4e 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSMessage.m @@ -250,9 +250,9 @@ static const NSUInteger OWSMessageSchemaVersion = 4; [self saveWithTransaction:transaction]; } -- (void)addAttachmentId:(NSString *)attachmentId transaction:(YapDatabaseReadWriteTransaction *)transaction { +- (void)addAttachmentWithID:(NSString *)attachmentID in:(YapDatabaseReadWriteTransaction *)transaction { if (!self.attachmentIds) { return; } - [self.attachmentIds addObject:attachmentId]; + [self.attachmentIds addObject:attachmentID]; [self saveWithTransaction:transaction]; } @@ -512,13 +512,12 @@ static const NSUInteger OWSMessageSchemaVersion = 4; } } -#pragma mark - Link preview +#pragma mark - Link Preview - (void)generateLinkPreviewIfNeededFromURL:(NSString *)url { - // If we already have previews or attachments then don't bother making link previews + // If we already havea link previews or attachment then don't bother if (self.linkPreview != nil || self.hasAttachments) { return; } - [OWSLinkPreview tryToBuildPreviewInfoObjcWithPreviewUrl:url] .thenOn(dispatch_get_main_queue(), ^(OWSLinkPreviewDraft *linkPreviewDraft) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { @@ -528,7 +527,7 @@ static const NSUInteger OWSMessageSchemaVersion = 4; }]; }) .catchOn(dispatch_get_main_queue(), ^(NSError *error) { - // If we failed to get link preview due to invalid content then maybe it's a link to a direct image? + // If we failed to get a link preview due to an invalid content type error then this could be a direct image link if ([OWSLinkPreview isInvalidContentError:error]) { __block AnyPromise *promise; [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { @@ -536,18 +535,14 @@ static const NSUInteger OWSMessageSchemaVersion = 4; }]; return promise; } - - // Return the error return [AnyPromise promiseWithValue:error]; }) .thenOn(dispatch_get_main_queue(), ^(OWSLinkPreview *linkPreview) { - // If we managed to get direct previews then render them + // If we managed to get a direct image preview then render it [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { if (linkPreview.isDirectAttachment) { if (!self.hasAttachments) { - [self addAttachmentId:linkPreview.imageAttachmentId transaction:transaction]; - - // Set the message id in attachment + [self addAttachmentWithID:linkPreview.imageAttachmentId in:transaction]; TSAttachmentStream *linkPreviewAttachment = [TSAttachmentStream fetchObjectWithUniqueID:linkPreview.imageAttachmentId transaction:transaction]; linkPreviewAttachment.albumMessageId = self.uniqueId; linkPreviewAttachment.isUploaded = true; @@ -557,7 +552,6 @@ static const NSUInteger OWSMessageSchemaVersion = 4; self.linkPreview = linkPreview; [self saveWithTransaction:transaction]; } - }]; }); } diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 50e83aae3..3dbcd588f 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -1240,7 +1240,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; [SignalRecipient markRecipientAsRegisteredAndGet:recipient.recipientId transaction:transaction]; }]; - // Check if we need to generate link previews + // Loki: Check if we need to generate a link preview TSMessage *message = messageSend.message; if (message.linkPreview == nil && !message.hasAttachments) { dispatch_async(dispatch_get_main_queue(), ^{