Improve handling of attachment edge cases.

pull/1/head
Matthew Chen 8 years ago
parent a9dca831d3
commit 0abdbffe1f

@ -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

@ -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<NSString *> *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];
}];

Loading…
Cancel
Save