From 30403be9bae86eeaa4bdd647ec6e98c92b4381f6 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 5 Apr 2018 09:56:58 -0400 Subject: [PATCH] Respond to CR. --- SignalServiceKit/src/Messages/OWSMessageUtils.m | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/SignalServiceKit/src/Messages/OWSMessageUtils.m b/SignalServiceKit/src/Messages/OWSMessageUtils.m index 3c7e93fdb..475b9b453 100644 --- a/SignalServiceKit/src/Messages/OWSMessageUtils.m +++ b/SignalServiceKit/src/Messages/OWSMessageUtils.m @@ -151,7 +151,7 @@ NS_ASSUME_NONNULL_BEGIN [TSAttachment fetchObjectWithUniqueID:attachmentId transaction:transaction]; if (attachment) { // If the attachment is "oversize text", try to treat it appropriately. - if (!hasText && [NSObject isNullableObject:attachment.contentType equalTo:OWSMimeTypeOversizeTextMessage] && + if (!hasText && [OWSMimeTypeOversizeTextMessage isEqualToString:attachment.contentType] && [attachment isKindOfClass:[TSAttachmentStream class]]) { hasText = YES; @@ -160,6 +160,10 @@ NS_ASSUME_NONNULL_BEGIN TSAttachmentStream *attachmentStream = (TSAttachmentStream *)attachment; NSData *_Nullable oversizeTextData = [NSData dataWithContentsOfFile:attachmentStream.filePath]; if (oversizeTextData) { + // We don't need to include the entire text body of the message, just + // enough to render a snippet. kOversizeTextMessageSizeThreshold is our + // limit on how long text should be in protos since they'll be stored in + // the database. We apply this constant here for the same reasons. NSString *_Nullable oversizeText = [[NSString alloc] initWithData:oversizeTextData encoding:NSUTF8StringEncoding]; // First, truncate to the rough max characters. @@ -170,17 +174,21 @@ NS_ASSUME_NONNULL_BEGIN while (truncatedText && truncatedText.length > 0 && [truncatedText dataUsingEncoding:NSUTF8StringEncoding].length >= kOversizeTextMessageSizeThreshold) { - // A very coarse binomial search by halving is acceptable, since + // A very coarse binary search by halving is acceptable, since // kOversizeTextMessageSizeThreshold is much longer than our target // length of "three short lines of text on any device we might // display this on. // - // We don't worry much about the search converging because + // The search will always converge since in the worst case (namely + // a single character which in utf-8 is >= 1024 bytes) the loop will + // exit when the string is empty. truncatedText = [truncatedText substringToIndex:oversizeText.length / 2]; } if ([truncatedText dataUsingEncoding:NSUTF8StringEncoding].length < kOversizeTextMessageSizeThreshold) { quotedText = truncatedText; + } else { + OWSFail(@"%@ Missing valid text snippet.", self.logTag); } } } else {