@@ -485,14 +485,7 @@ - (void)checkForNewMessagesFromMessageId:(NSInteger)messageId
485
485
object: self
486
486
userInfo: userInfo];
487
487
488
- // Messages are already sorted by messageId here
489
- NCChatMessage *lastMessage = [storedMessages lastObject ];
490
-
491
- // Make sure we update the unread flags for the room (lastMessage can already be set, but there still might be unread flags)
492
- if (lastMessage.timestamp >= self->_room .lastActivity && !lastMessage.isUpdateMessage ) {
493
- self->_room .lastActivity = lastMessage.timestamp ;
494
- [[NCRoomsManager sharedInstance ] updateLastMessage: lastMessage withNoUnreadMessages: YES forRoom: self ->_room];
495
- }
488
+ [self updateLastMessageIfNeededFromMessages: storedMessages];
496
489
}
497
490
}
498
491
@@ -516,15 +509,8 @@ - (void)getInitialChatHistory
516
509
[[NSNotificationCenter defaultCenter ] postNotificationName: NCChatControllerDidReceiveInitialChatHistoryNotification
517
510
object: self
518
511
userInfo: userInfo];
519
-
520
- // Messages are already sorted by messageId here
521
- NCChatMessage *lastMessage = [storedMessages lastObject ];
522
-
523
- // Make sure we update the unread flags for the room (lastMessage can already be set, but there still might be unread flags)
524
- if (lastMessage.timestamp >= self->_room .lastActivity && !lastMessage.isUpdateMessage ) {
525
- self->_room .lastActivity = lastMessage.timestamp ;
526
- [[NCRoomsManager sharedInstance ] updateLastMessage: lastMessage withNoUnreadMessages: YES forRoom: self ->_room];
527
- }
512
+
513
+ [self updateLastMessageIfNeededFromMessages: storedMessages];
528
514
} else {
529
515
_pullMessagesTask = [[NCAPIController sharedInstance ] receiveChatMessagesOfRoom: _room.token fromLastMessageId: lastReadMessageId history: YES includeLastMessage: YES timeout: NO lastCommonReadMessage: _room.lastCommonReadMessage setReadMarker: YES markNotificationsAsRead: YES forAccount: _account withCompletionBlock: ^(NSArray *messages, NSInteger lastKnownMessage, NSInteger lastCommonReadMessage, NSError *error, NSInteger statusCode) {
530
516
if (self->_stopChatMessagesPoll ) {
@@ -557,6 +543,34 @@ - (void)getInitialChatHistory
557
543
}
558
544
}
559
545
546
+ - (void )updateLastMessageIfNeededFromMessages : (NSArray *)storedMessages
547
+ {
548
+ // Try to find the last non-update message - Messages are already sorted by messageId here
549
+ NCChatMessage *lastNonUpdateMessage;
550
+ NCChatMessage *lastMessage = [storedMessages lastObject ];
551
+ NCChatMessage *tempMessage;
552
+
553
+ for (NSInteger i = (storedMessages.count - 1 ); i >= 0 ; i--) {
554
+ tempMessage = [storedMessages objectAtIndex: i];
555
+
556
+ if (![tempMessage isUpdateMessage ]) {
557
+ lastNonUpdateMessage = tempMessage;
558
+ break ;
559
+ }
560
+ }
561
+
562
+ // Make sure we update the unread flags for the room (lastMessage can already be set, but there still might be unread flags)
563
+ if (lastMessage && lastMessage.timestamp >= self->_room .lastActivity ) {
564
+ // Make sure our local reference to the room also has the correct lastActivity set
565
+ if (lastNonUpdateMessage) {
566
+ self->_room .lastActivity = lastNonUpdateMessage.timestamp ;
567
+ }
568
+
569
+ // We always want to set the room to have no unread messages, optionally we also want to update the last message, if there's one
570
+ [[NCRoomsManager sharedInstance ] setNoUnreadMessagesForRoom: self ->_room withLastMessage: lastNonUpdateMessage];
571
+ }
572
+ }
573
+
560
574
- (void )getInitialChatHistoryForOfflineMode
561
575
{
562
576
NSMutableDictionary *userInfo = [NSMutableDictionary new ];
0 commit comments