Skip to content

Commit

Permalink
Fix showing the unread marker for large number of unread messages
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Müller <[email protected]>
  • Loading branch information
SystemKeeper committed Nov 19, 2024
1 parent 18f50b6 commit 6903643
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions NextcloudTalk/NCChatController.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ - (NSArray *)chatBlocksForRoom
return sortedBlocks;
}

- (NSArray *)getBatchOfMessagesInBlock:(NCChatBlock *)chatBlock fromMessageId:(NSInteger)messageId included:(BOOL)included
- (NSArray *)getBatchOfMessagesInBlock:(NCChatBlock *)chatBlock fromMessageId:(NSInteger)messageId included:(BOOL)included enforceLimit:(BOOL)enforceLimit
{
NSInteger fromMessageId = messageId > 0 ? messageId : chatBlock.newestMessageId;
NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@ AND token = %@ AND messageId >= %ld AND messageId < %ld", _account.accountId, _room.token, (long)chatBlock.oldestMessageId, (long)fromMessageId];
Expand All @@ -82,7 +82,12 @@ - (NSArray *)getBatchOfMessagesInBlock:(NCChatBlock *)chatBlock fromMessageId:(N
RLMResults *managedSortedMessages = [managedMessages sortedResultsUsingKeyPath:@"messageId" ascending:YES];
// Create an unmanaged copy of the messages
NSMutableArray *sortedMessages = [NSMutableArray new];
NSInteger startingIndex = managedSortedMessages.count - kReceivedChatMessagesLimit;
NSInteger startingIndex = 0;

if (enforceLimit) {
startingIndex = managedSortedMessages.count - kReceivedChatMessagesLimit;
}

startingIndex = (startingIndex < 0) ? 0 : startingIndex;
for (NSInteger i = startingIndex; i < managedSortedMessages.count; i++) {
NCChatMessage *sortedMessage = [[NCChatMessage alloc] initWithValue:managedSortedMessages[i]];
Expand Down Expand Up @@ -470,8 +475,8 @@ - (void)getInitialChatHistory
}

NCChatBlock *lastChatBlock = [self chatBlocksForRoom].lastObject;
if (lastChatBlock.newestMessageId > 0 && lastChatBlock.newestMessageId >= lastReadMessageId) {
NSArray *storedMessages = [self getBatchOfMessagesInBlock:lastChatBlock fromMessageId:lastChatBlock.newestMessageId included:YES];
if (lastChatBlock.newestMessageId > 0 && lastReadMessageId >= lastChatBlock.oldestMessageId && lastChatBlock.newestMessageId >= lastReadMessageId) {
NSArray *storedMessages = [self getBatchOfMessagesInBlock:lastChatBlock fromMessageId:lastChatBlock.newestMessageId included:YES enforceLimit:NO];
[userInfo setObject:storedMessages forKey:@"messages"];
[[NSNotificationCenter defaultCenter] postNotificationName:NCChatControllerDidReceiveInitialChatHistoryNotification
object:self
Expand Down Expand Up @@ -504,7 +509,7 @@ - (void)getInitialChatHistory
if (messages.count > 0) {
[self storeMessages:messages];
NCChatBlock *lastChatBlock = [self chatBlocksForRoom].lastObject;
NSArray *storedMessages = [self getBatchOfMessagesInBlock:lastChatBlock fromMessageId:lastReadMessageId included:YES];
NSArray *storedMessages = [self getBatchOfMessagesInBlock:lastChatBlock fromMessageId:lastReadMessageId included:YES enforceLimit:NO];
[userInfo setObject:storedMessages forKey:@"messages"];
}
}
Expand All @@ -523,7 +528,7 @@ - (void)getInitialChatHistoryForOfflineMode
[userInfo setObject:_room.token forKey:@"room"];

NCChatBlock *lastChatBlock = [self chatBlocksForRoom].lastObject;
NSArray *storedMessages = [self getBatchOfMessagesInBlock:lastChatBlock fromMessageId:lastChatBlock.newestMessageId included:YES];
NSArray *storedMessages = [self getBatchOfMessagesInBlock:lastChatBlock fromMessageId:lastChatBlock.newestMessageId included:YES enforceLimit:NO];
[userInfo setObject:storedMessages forKey:@"messages"];
[[NSNotificationCenter defaultCenter] postNotificationName:NCChatControllerDidReceiveInitialChatHistoryOfflineNotification
object:self
Expand All @@ -537,7 +542,7 @@ - (void)getHistoryBatchFromMessagesId:(NSInteger)messageId

NCChatBlock *lastChatBlock = [self chatBlocksForRoom].lastObject;
if (lastChatBlock && lastChatBlock.oldestMessageId < messageId) {
NSArray *storedMessages = [self getBatchOfMessagesInBlock:lastChatBlock fromMessageId:messageId included:NO];
NSArray *storedMessages = [self getBatchOfMessagesInBlock:lastChatBlock fromMessageId:messageId included:NO enforceLimit:YES];
[userInfo setObject:storedMessages forKey:@"messages"];
[[NSNotificationCenter defaultCenter] postNotificationName:NCChatControllerDidReceiveChatHistoryNotification
object:self
Expand All @@ -563,7 +568,7 @@ - (void)getHistoryBatchFromMessagesId:(NSInteger)messageId
if (messages.count > 0) {
[self storeMessages:messages];
NCChatBlock *lastChatBlock = [self chatBlocksForRoom].lastObject;
NSArray *historyBatch = [self getBatchOfMessagesInBlock:lastChatBlock fromMessageId:messageId included:NO];
NSArray *historyBatch = [self getBatchOfMessagesInBlock:lastChatBlock fromMessageId:messageId included:NO enforceLimit:YES];
[userInfo setObject:historyBatch forKey:@"messages"];
}
}
Expand All @@ -586,7 +591,7 @@ - (void)getHistoryBatchOfflineFromMessagesId:(NSInteger)messageId
NCChatBlock *currentBlock = chatBlocks[i];
BOOL noMoreMessagesToRetrieveInBlock = NO;
if (currentBlock.oldestMessageId < messageId) {
NSArray *storedMessages = [self getBatchOfMessagesInBlock:currentBlock fromMessageId:messageId included:NO];
NSArray *storedMessages = [self getBatchOfMessagesInBlock:currentBlock fromMessageId:messageId included:NO enforceLimit:YES];
historyBatch = [[NSMutableArray alloc] initWithArray:storedMessages];
if (storedMessages.count > 0) {
break;
Expand All @@ -598,7 +603,7 @@ - (void)getHistoryBatchOfflineFromMessagesId:(NSInteger)messageId
}
if (i > 0 && (currentBlock.oldestMessageId == messageId || noMoreMessagesToRetrieveInBlock)) {
NCChatBlock *previousBlock = chatBlocks[i - 1];
NSArray *storedMessages = [self getBatchOfMessagesInBlock:previousBlock fromMessageId:previousBlock.newestMessageId included:YES];
NSArray *storedMessages = [self getBatchOfMessagesInBlock:previousBlock fromMessageId:previousBlock.newestMessageId included:YES enforceLimit:YES];
historyBatch = [[NSMutableArray alloc] initWithArray:storedMessages];
[userInfo setObject:@(YES) forKey:@"shouldAddBlockSeparator"];
break;
Expand Down

0 comments on commit 6903643

Please sign in to comment.