From 2d241623b713fcab4df2a283e83943b83ba60c6b Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 21 Nov 2017 18:17:19 -0500 Subject: [PATCH] Improve handling of edge cases around unread indicator delimiting deleted message(s). --- .../ConversationViewController.m | 6 ++++++ Signal/src/util/ThreadUtil.m | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 6c7a37bc5..a93d6526a 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -3413,6 +3413,12 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { if (lastVisibleViewItem) { uint64_t lastVisibleTimestamp = lastVisibleViewItem.interaction.timestampForSorting; self.lastVisibleTimestamp = MAX(self.lastVisibleTimestamp, lastVisibleTimestamp); + + // If we delete the last unread message (manually or due to disappearing messages) + // we may need to clean up an obsolete unread indicator. + if (lastVisibleViewItem.interaction.interactionType == OWSInteractionType_UnreadIndicator) { + [self ensureDynamicInteractions]; + } } [self ensureScrollDownButton]; diff --git a/Signal/src/util/ThreadUtil.m b/Signal/src/util/ThreadUtil.m index 00b6638ee..2ee2e5206 100644 --- a/Signal/src/util/ThreadUtil.m +++ b/Signal/src/util/ThreadUtil.m @@ -137,7 +137,7 @@ NS_ASSUME_NONNULL_BEGIN dbConnection:(YapDatabaseConnection *)dbConnection hideUnreadMessagesIndicator:(BOOL)hideUnreadMessagesIndicator firstUnseenInteractionTimestamp: - (nullable NSNumber *)firstUnseenInteractionTimestampParameter + (nullable NSNumber *)firstUnseenInteractionTimestampParam maxRangeSize:(int)maxRangeSize { OWSAssert(thread); @@ -232,6 +232,21 @@ NS_ASSUME_NONNULL_BEGIN // have been marked as read. // // IFF this variable is non-null, there are unseen messages in the thread. + // + // Make a local copy of this parameter that we can modify. + NSNumber *_Nullable firstUnseenInteractionTimestampParameter = firstUnseenInteractionTimestampParam; + if (firstUnseenInteractionTimestampParameter) { + // Due to disappearing messages or manual deletion, + // firstUnseenInteractionTimestampParameter may refer to an obsolete + // interaction in which case we want to discard it. + TSInteraction *_Nullable lastInteraction = + [[transaction ext:TSMessageDatabaseViewExtensionName] lastObjectInGroup:thread.uniqueId]; + if (!lastInteraction + || lastInteraction.timestampForSorting + < firstUnseenInteractionTimestampParameter.unsignedLongLongValue) { + firstUnseenInteractionTimestampParameter = nil; + } + } if (firstUnseenInteractionTimestampParameter) { result.firstUnseenInteractionTimestamp = firstUnseenInteractionTimestampParameter; } else {