Skip to content

Commit c8f0622

Browse files
authored
Merge pull request #2006 from nextcloud/fix/noid/cheat-marked-as-read
fix: Set room to read when last message is an update message
2 parents ba3fbdf + d2879d3 commit c8f0622

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

NextcloudTalk/NCChatController.m

+31-17
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,7 @@ - (void)checkForNewMessagesFromMessageId:(NSInteger)messageId
485485
object:self
486486
userInfo:userInfo];
487487

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];
496489
}
497490
}
498491

@@ -516,15 +509,8 @@ - (void)getInitialChatHistory
516509
[[NSNotificationCenter defaultCenter] postNotificationName:NCChatControllerDidReceiveInitialChatHistoryNotification
517510
object:self
518511
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];
528514
} else {
529515
_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) {
530516
if (self->_stopChatMessagesPoll) {
@@ -557,6 +543,34 @@ - (void)getInitialChatHistory
557543
}
558544
}
559545

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+
560574
- (void)getInitialChatHistoryForOfflineMode
561575
{
562576
NSMutableDictionary *userInfo = [NSMutableDictionary new];

NextcloudTalk/NCRoomsManager.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ typedef void (^SendOfflineMessagesCompletionBlock)(void);
6060
- (void)updateRoom:(NSString *)token withCompletionBlock:(GetRoomCompletionBlock)block;
6161
- (void)updatePendingMessage:(NSString *)message forRoom:(NCRoom *)room;
6262
- (void)updateLastReadMessage:(NSInteger)lastReadMessage forRoom:(NCRoom *)room;
63-
- (void)updateLastMessage:(NCChatMessage *)message withNoUnreadMessages:(BOOL)noUnreadMessages forRoom:(NCRoom *)room;
6463
- (void)updateLastCommonReadMessage:(NSInteger)messageId forRoom:(NCRoom *)room;
64+
- (void)setNoUnreadMessagesForRoom:(NCRoom *)room withLastMessage:(NCChatMessage * _Nullable)lastMessage;
6565
// Chat
6666
- (void)startChatInRoom:(NCRoom *)room;
6767
- (void)leaveChatInRoom:(NSString *)token;

NextcloudTalk/NCRoomsManager.m

+12-10
Original file line numberDiff line numberDiff line change
@@ -315,20 +315,22 @@ - (void)updateLastReadMessage:(NSInteger)lastReadMessage forRoom:(NCRoom *)room
315315
}];
316316
}
317317

318-
- (void)updateLastMessage:(NCChatMessage *)message withNoUnreadMessages:(BOOL)noUnreadMessages forRoom:(NCRoom *)room
318+
- (void)setNoUnreadMessagesForRoom:(NCRoom *)room withLastMessage:(NCChatMessage * _Nullable)lastMessage
319319
{
320320
RLMRealm *realm = [RLMRealm defaultRealm];
321321
[realm transactionWithBlock:^{
322322
NCRoom *managedRoom = [NCRoom objectsWhere:@"internalId = %@", room.internalId].firstObject;
323-
if (managedRoom) {
324-
managedRoom.lastMessageId = message.internalId;
325-
managedRoom.lastActivity = message.timestamp;
326-
327-
if (noUnreadMessages) {
328-
managedRoom.unreadMention = NO;
329-
managedRoom.unreadMentionDirect = NO;
330-
managedRoom.unreadMessages = 0;
331-
}
323+
if (!managedRoom) {
324+
return;
325+
}
326+
327+
managedRoom.unreadMention = NO;
328+
managedRoom.unreadMentionDirect = NO;
329+
managedRoom.unreadMessages = 0;
330+
331+
if (lastMessage) {
332+
managedRoom.lastMessageId = lastMessage.internalId;
333+
managedRoom.lastActivity = lastMessage.timestamp;
332334
}
333335
}];
334336
}

0 commit comments

Comments
 (0)