Fix crash due to empty authorId on old messages

This commit is contained in:
Michael Kirk 2018-11-19 10:59:10 -06:00
parent 7b3bf8636a
commit 0d0359ee12
7 changed files with 15 additions and 28 deletions

View file

@ -6,6 +6,8 @@
NS_ASSUME_NONNULL_BEGIN
extern NSString *const TSContactThreadPrefix;
@interface TSContactThread : TSThread
@property (nonatomic) BOOL hasDismissedOffers;

View file

@ -13,7 +13,7 @@
NS_ASSUME_NONNULL_BEGIN
#define TSContactThreadPrefix @"c"
NSString *const TSContactThreadPrefix = @"c";
@implementation TSContactThread

View file

@ -77,11 +77,8 @@ NS_ASSUME_NONNULL_BEGIN
// This will be 0 for messages created before we were tracking sourceDeviceId
@property (nonatomic, readonly) UInt32 sourceDeviceId;
// NOTE: Use messageAuthorId instead wherever possible.
@property (nonatomic, readonly) NSString *authorId;
- (NSString *)messageAuthorId;
// convenience method for expiring a message which was just read
- (void)markAsReadNowWithSendReadReceipt:(BOOL)sendReadReceipt
transaction:(YapDatabaseReadWriteTransaction *)transaction;

View file

@ -35,6 +35,11 @@ NS_ASSUME_NONNULL_BEGIN
return self;
}
if (_authorId == nil) {
OWSAssertDebug([self.uniqueThreadId hasPrefix:TSContactThreadPrefix]);
_authorId = [TSContactThread contactIdFromThreadId:self.uniqueThreadId];
}
return self;
}
@ -88,10 +93,9 @@ NS_ASSUME_NONNULL_BEGIN
if ([interaction isKindOfClass:[TSIncomingMessage class]]) {
TSIncomingMessage *message = (TSIncomingMessage *)interaction;
NSString *messageAuthorId = message.messageAuthorId;
OWSAssertDebug(messageAuthorId.length > 0);
OWSAssertDebug(message.authorId > 0);
if ([messageAuthorId isEqualToString:authorId]) {
if ([message.authorId isEqualToString:authorId]) {
foundMessage = message;
}
}
@ -101,22 +105,6 @@ NS_ASSUME_NONNULL_BEGIN
return foundMessage;
}
// TODO get rid of this method and instead populate authorId in initWithCoder:
- (NSString *)messageAuthorId
{
// authorId isn't set on all legacy messages, so we take
// extra measures to ensure we obtain a valid value.
NSString *messageAuthorId;
if (self.authorId) {
// Group Thread
messageAuthorId = self.authorId;
} else {
// Contact Thread
messageAuthorId = [TSContactThread contactIdFromThreadId:self.uniqueThreadId];
}
OWSAssertDebug(messageAuthorId.length > 0);
return messageAuthorId;
}
- (OWSInteractionType)interactionType
{

View file

@ -236,7 +236,7 @@ NS_ASSUME_NONNULL_BEGIN
if ([interaction isKindOfClass:[TSIncomingMessage class]]) {
TSIncomingMessage *incomingMessage = (TSIncomingMessage *)interaction;
return [authorId isEqual:incomingMessage.messageAuthorId];
return [authorId isEqual:incomingMessage.authorId];
} else if ([interaction isKindOfClass:[TSOutgoingMessage class]]) {
return [authorId isEqual:[TSAccountManager localNumber]];
} else {

View file

@ -200,7 +200,7 @@ void AssertIsOnDisappearingMessagesQueue()
if ([message isKindOfClass:[TSIncomingMessage class]]) {
TSIncomingMessage *incomingMessage = (TSIncomingMessage *)message;
remoteContactName =
[contactsManager displayNameForPhoneIdentifier:incomingMessage.messageAuthorId transaction:transaction];
[contactsManager displayNameForPhoneIdentifier:incomingMessage.authorId transaction:transaction];
}
[self becomeConsistentWithDisappearingDuration:message.expiresInSeconds

View file

@ -268,7 +268,7 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
NSString *threadUniqueId = message.uniqueThreadId;
OWSAssertDebug(threadUniqueId.length > 0);
NSString *messageAuthorId = message.messageAuthorId;
NSString *messageAuthorId = message.authorId;
OWSAssertDebug(messageAuthorId.length > 0);
OWSLinkedDeviceReadReceipt *newReadReceipt =
@ -286,7 +286,7 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
self.toLinkedDevicesReadReceiptMap[threadUniqueId] = newReadReceipt;
}
if ([message.messageAuthorId isEqualToString:[TSAccountManager localNumber]]) {
if ([message.authorId isEqualToString:[TSAccountManager localNumber]]) {
OWSLogVerbose(@"Ignoring read receipt for self-sender.");
return;
}
@ -379,7 +379,7 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
OWSAssertDebug(message);
OWSAssertDebug(transaction);
NSString *senderId = message.messageAuthorId;
NSString *senderId = message.authorId;
uint64_t timestamp = message.timestamp;
if (senderId.length < 1 || timestamp < 1) {
OWSFailDebug(@"Invalid incoming message: %@ %llu", senderId, timestamp);