From 02efbd306950d12ce02be7f995cc65ed4b0dfcb2 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Mon, 9 Apr 2018 10:42:47 -0400 Subject: [PATCH] Fix blip where thumbnail is initially missing from outgoing message // FREEBIE --- .../src/Messages/OWSMessageSender.m | 45 +++++++++---------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index ff0b1cd83..57b3f03a0 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -287,6 +287,9 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + + __block NSArray *quotedThumbnailAttachments = @[]; + // This method will use a read/write transaction. This transaction // will block until any open read/write transactions are complete. // @@ -300,6 +303,11 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; // So we're using YDB behavior to ensure this invariant, which is a bit // unorthodox. [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + if (message.quotedMessage) { + quotedThumbnailAttachments = + [message.quotedMessage createThumbnailAttachmentsIfNecessaryWithTransaction:transaction]; + } + // All outgoing messages should be saved at the time they are enqueued. [message saveWithTransaction:transaction]; [message updateWithMessageState:TSOutgoingMessageStateAttemptingOut transaction:transaction]; @@ -320,33 +328,20 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; [sendingQueue addOperation:uploadAttachmentOperation]; } + // Though we currently only ever expect at most one thumbnail, the proto data model + // suggests this could change. The logic is intended to work with multiple, but + // if we ever actually want to send multiple, we should do more testing. + OWSAssert(quotedThumbnailAttachments.count <= 1); + for (TSAttachmentStream *thumbnailAttachment in quotedThumbnailAttachments) { + OWSAssert(message.quotedMessage); - if (message.quotedMessage) { - // TODO do we want a different thumbnail size for quotes vs the gallery? This seems reasonable, - // and has the advantage of already having been generated. - __block NSArray *thumbnailAttachments; - [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { - thumbnailAttachments = - [message.quotedMessage createThumbnailAttachmentsIfNecessaryWithTransaction:transaction]; - if (thumbnailAttachments.count > 0) { - [message touchWithTransaction:transaction]; - } - }]; - - // Though we currently only ever expect at most one thumbnail, the proto data model - // suggests this could change. The logic is intended to work with multiple, but - // if we ever actually want to send multiple, we should do more testing. - OWSAssert(thumbnailAttachments.count <= 1); - - for (TSAttachmentStream *thumbnailAttachment in thumbnailAttachments) { - OWSUploadOperation *uploadQuoteThumbnailOperation = - [[OWSUploadOperation alloc] initWithAttachmentId:thumbnailAttachment.uniqueId - dbConnection:self.dbConnection]; + OWSUploadOperation *uploadQuoteThumbnailOperation = + [[OWSUploadOperation alloc] initWithAttachmentId:thumbnailAttachment.uniqueId + dbConnection:self.dbConnection]; - // TODO put attachment uploads on a (lowly) concurrent queue - [sendMessageOperation addDependency:uploadQuoteThumbnailOperation]; - [sendingQueue addOperation:uploadQuoteThumbnailOperation]; - } + // TODO put attachment uploads on a (lowly) concurrent queue + [sendMessageOperation addDependency:uploadQuoteThumbnailOperation]; + [sendingQueue addOperation:uploadQuoteThumbnailOperation]; } [sendingQueue addOperation:sendMessageOperation];