From 0abdbffe1f9ebc013cd7a9ef3ef4d98bc2f80788 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 10 Nov 2017 11:22:59 -0500 Subject: [PATCH 1/2] Improve handling of attachment edge cases. --- .../ConversationView/ConversationViewItem.m | 3 ++ .../src/Messages/OWSMessageSender.m | 33 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m index 46196a9ca..c7f9ad396 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m @@ -408,7 +408,10 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType) DDLogVerbose(@"%@ message: %@", self.logTag, message.description); OWSFail(@"%@ Unknown cell type", self.logTag); + // Messages of unknown type (including messages with missing attachments) + // are rendered like empty text messages, but without any interactivity. self.messageCellType = OWSMessageCellType_Unknown; + self.displayableText = [[DisplayableText alloc] initWithFullText:@"" displayText:@"" isTextTruncated:NO]; } - (OWSMessageCellType)messageCellType diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index ad5d71c40..037113c9f 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -1177,6 +1177,37 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; [TSContactThread getOrCreateThreadWithContactId:contactId transaction:transaction]; [cThread saveWithTransaction:transaction]; + // We need to clone any attachments for message sent to self; otherwise deleting + // the incoming or outgoing copy of the message will break the other. + NSMutableArray *attachmentIds = [NSMutableArray new]; + for (NSString *attachmentId in outgoingMessage.attachmentIds) { + TSAttachmentStream *_Nullable outgoingAttachment = + [TSAttachmentStream fetchObjectWithUniqueID:attachmentId]; + OWSAssert(outgoingAttachment); + if (!outgoingAttachment) { + DDLogError(@"%@ Couldn't load outgoing attachment for message sent to self.", self.logTag); + } else { + TSAttachmentStream *incomingAttachment = + [[TSAttachmentStream alloc] initWithContentType:outgoingAttachment.contentType + byteCount:outgoingAttachment.byteCount + sourceFilename:outgoingAttachment.sourceFilename]; + NSError *error; + NSData *_Nullable data = [outgoingAttachment readDataFromFileWithError:&error]; + if (!data || error) { + DDLogError(@"%@ Couldn't load attachment data for message sent to self: %@.", self.logTag, error); + } else { + [incomingAttachment writeData:data error:&error]; + if (error) { + DDLogError( + @"%@ Couldn't copy attachment data for message sent to self: %@.", self.logTag, error); + } else { + [incomingAttachment saveWithTransaction:transaction]; + [attachmentIds addObject:incomingAttachment.uniqueId]; + } + } + } + } + // We want the incoming message to appear after the outgoing message. TSIncomingMessage *incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:(outgoingMessage.timestamp + 1) @@ -1184,7 +1215,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; authorId:[cThread contactIdentifier] sourceDeviceId:[OWSDevice currentDeviceId] messageBody:outgoingMessage.body - attachmentIds:outgoingMessage.attachmentIds + attachmentIds:attachmentIds expiresInSeconds:outgoingMessage.expiresInSeconds]; [incomingMessage saveWithTransaction:transaction]; }]; From cc0e58365ea406ce5956db00454e5e1ba3486061 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 10 Nov 2017 13:12:41 -0500 Subject: [PATCH 2/2] Respond to CR. // FREEBIE --- SignalServiceKit/src/Messages/OWSMessageSender.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 037113c9f..6ce1a2c79 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -1182,7 +1182,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; NSMutableArray *attachmentIds = [NSMutableArray new]; for (NSString *attachmentId in outgoingMessage.attachmentIds) { TSAttachmentStream *_Nullable outgoingAttachment = - [TSAttachmentStream fetchObjectWithUniqueID:attachmentId]; + [TSAttachmentStream fetchObjectWithUniqueID:attachmentId transaction:transaction]; OWSAssert(outgoingAttachment); if (!outgoingAttachment) { DDLogError(@"%@ Couldn't load outgoing attachment for message sent to self.", self.logTag);