diff --git a/Signal/src/ViewControllers/ConversationView/MessagesViewController.m b/Signal/src/ViewControllers/ConversationView/MessagesViewController.m index d6c0f2d6d..d24c61223 100644 --- a/Signal/src/ViewControllers/ConversationView/MessagesViewController.m +++ b/Signal/src/ViewControllers/ConversationView/MessagesViewController.m @@ -538,6 +538,8 @@ typedef enum : NSUInteger { - (void)viewWillAppear:(BOOL)animated { + DDLogDebug(@"%@ viewWillAppear", self.tag); + // We need to update the dynamic interactions before we do any layout. [self ensureDynamicInteractions]; @@ -546,8 +548,6 @@ typedef enum : NSUInteger { [self ensureBannerState]; - [self resetContentAndLayout]; - [super viewWillAppear:animated]; // In case we're dismissing a CNContactViewController which requires default system appearance @@ -567,6 +567,9 @@ typedef enum : NSUInteger { [self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [self.messageMappings updateWithTransaction:transaction]; }]; + [self updateMessageMappingRangeOptions]; + + [self resetContentAndLayout]; [self toggleObservers:YES]; @@ -987,6 +990,8 @@ typedef enum : NSUInteger { - (void)viewWillDisappear:(BOOL)animated { + DDLogDebug(@"%@ viewWillDisappear", self.tag); + [super viewWillDisappear:animated]; [self toggleObservers:NO]; @@ -2566,7 +2571,7 @@ typedef enum : NSUInteger { DDLogInfo(@"%@ Blocking an unknown user.", self.tag); [self.blockingManager addBlockedPhoneNumber:errorMessage.contactId]; // Delete the block offer. - [self.storageManager.dbConnection + [self.editingDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [errorMessage removeWithTransaction:transaction]; }]; @@ -2689,6 +2694,8 @@ typedef enum : NSUInteger { const int currentMaxRangeSize = (int)(self.page + 1) * kYapDatabasePageSize; const int maxRangeSize = MAX(initialMaxRangeSize, currentMaxRangeSize); + // `ensureDynamicInteractionsForThread` should operate on the latest thread contents, so + // we should _read_ from uiDatabaseConnection and _write_ to `editingDatabaseConnection`. self.dynamicInteractions = [ThreadUtil ensureDynamicInteractionsForThread:self.thread contactsManager:self.contactsManager @@ -3206,8 +3213,8 @@ typedef enum : NSUInteger { [self setNavigationTitle]; } - if (! - [[self.uiDatabaseConnection ext:TSMessageDatabaseViewExtensionName] hasChangesForNotifications:notifications]) { + if (![[self.uiDatabaseConnection ext:TSMessageDatabaseViewExtensionName] hasChangesForGroup:self.thread.uniqueId + inNotifications:notifications]) { [self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [self.messageMappings updateWithTransaction:transaction]; }]; @@ -3223,12 +3230,24 @@ typedef enum : NSUInteger { NSArray *messageRowChanges = nil; NSArray *sectionChanges = nil; - [[self.uiDatabaseConnection ext:TSMessageDatabaseViewExtensionName] getSectionChanges:§ionChanges - rowChanges:&messageRowChanges - forNotifications:notifications - withMappings:self.messageMappings]; + [[self.uiDatabaseConnection ext:TSMessageDatabaseViewExtensionName] + getSectionChanges:§ionChanges + rowChanges:&messageRowChanges + forNotifications:notifications + withMappings:self.messageMappings]; + + if ([sectionChanges count] == 0 && [messageRowChanges count] == 0) { + // YapDatabase will ignore insertions within the message mapping's + // range that are not within the current mapping's contents. We + // may need to extend the mapping's contents to reflect the current + // range. + [self updateMessageMappingRangeOptions]; + + [self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { + [self.messageMappings updateWithTransaction:transaction]; + }]; + [self resetContentAndLayout]; - if ([sectionChanges count] == 0 & [messageRowChanges count] == 0) { return; } diff --git a/Signal/src/util/ThreadUtil.m b/Signal/src/util/ThreadUtil.m index 7dfb85018..6ae76e02a 100644 --- a/Signal/src/util/ThreadUtil.m +++ b/Signal/src/util/ThreadUtil.m @@ -347,7 +347,11 @@ NS_ASSUME_NONNULL_BEGIN const int kUnreadIndicatorOfferOffset = -1; if (existingBlockOffer && !shouldHaveBlockOffer) { - DDLogInfo(@"Removing block offer"); + DDLogInfo(@"%@ Removing block offer: %@ (%llu)", + self.tag, + existingBlockOffer.uniqueId, + existingBlockOffer.timestampForSorting); + ; [existingBlockOffer removeWithTransaction:transaction]; } else if (!existingBlockOffer && shouldHaveBlockOffer) { DDLogInfo(@"Creating block offer for unknown contact"); @@ -363,10 +367,18 @@ NS_ASSUME_NONNULL_BEGIN thread:thread contactId:recipientId]; [offerMessage saveWithTransaction:transaction]; + + DDLogInfo(@"%@ Creating block offer: %@ (%llu)", + self.tag, + offerMessage.uniqueId, + offerMessage.timestampForSorting); } if (existingAddToContactsOffer && !shouldHaveAddToContactsOffer) { - DDLogInfo(@"Removing 'add to contacts' offer"); + DDLogInfo(@"%@ Removing 'add to contacts' offer: %@ (%llu)", + self.tag, + existingAddToContactsOffer.uniqueId, + existingAddToContactsOffer.timestampForSorting); [existingAddToContactsOffer removeWithTransaction:transaction]; } else if (!existingAddToContactsOffer && shouldHaveAddToContactsOffer) { @@ -383,6 +395,11 @@ NS_ASSUME_NONNULL_BEGIN thread:thread contactId:recipientId]; [offerMessage saveWithTransaction:transaction]; + + DDLogInfo(@"%@ Creating 'add to contacts' offer: %@ (%llu)", + self.tag, + offerMessage.uniqueId, + offerMessage.timestampForSorting); } BOOL shouldHaveUnreadIndicator