Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Set room to read when last message is an update message #2006

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions NextcloudTalk/NCChatController.m
Original file line number Diff line number Diff line change
Expand Up @@ -470,14 +470,7 @@ - (void)checkForNewMessagesFromMessageId:(NSInteger)messageId
object:self
userInfo:userInfo];

// Messages are already sorted by messageId here
NCChatMessage *lastMessage = [storedMessages lastObject];

// Make sure we update the unread flags for the room (lastMessage can already be set, but there still might be unread flags)
if (lastMessage.timestamp >= self->_room.lastActivity && !lastMessage.isUpdateMessage) {
self->_room.lastActivity = lastMessage.timestamp;
[[NCRoomsManager sharedInstance] updateLastMessage:lastMessage withNoUnreadMessages:YES forRoom:self->_room];
}
[self updateLastMessageIfNeededFromMessages:storedMessages];
}
}

Expand All @@ -501,15 +494,8 @@ - (void)getInitialChatHistory
[[NSNotificationCenter defaultCenter] postNotificationName:NCChatControllerDidReceiveInitialChatHistoryNotification
object:self
userInfo:userInfo];

// Messages are already sorted by messageId here
NCChatMessage *lastMessage = [storedMessages lastObject];

// Make sure we update the unread flags for the room (lastMessage can already be set, but there still might be unread flags)
if (lastMessage.timestamp >= self->_room.lastActivity && !lastMessage.isUpdateMessage) {
self->_room.lastActivity = lastMessage.timestamp;
[[NCRoomsManager sharedInstance] updateLastMessage:lastMessage withNoUnreadMessages:YES forRoom:self->_room];
}

[self updateLastMessageIfNeededFromMessages:storedMessages];
} else {
_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) {
if (self->_stopChatMessagesPoll) {
Expand Down Expand Up @@ -542,6 +528,34 @@ - (void)getInitialChatHistory
}
}

- (void)updateLastMessageIfNeededFromMessages:(NSArray *)storedMessages
{
// Try to find the last non-update message - Messages are already sorted by messageId here
NCChatMessage *lastNonUpdateMessage;
NCChatMessage *lastMessage = [storedMessages lastObject];
NCChatMessage *tempMessage;

for (NSInteger i = (storedMessages.count - 1); i >= 0; i--) {
tempMessage = [storedMessages objectAtIndex:i];

if (![tempMessage isUpdateMessage]) {
lastNonUpdateMessage = tempMessage;
break;
}
}

// Make sure we update the unread flags for the room (lastMessage can already be set, but there still might be unread flags)
if (lastMessage && lastMessage.timestamp >= self->_room.lastActivity) {
// Make sure our local reference to the room also has the correct lastActivity set
if (lastNonUpdateMessage) {
self->_room.lastActivity = lastNonUpdateMessage.timestamp;
}

// 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
[[NCRoomsManager sharedInstance] setNoUnreadMessagesForRoom:self->_room withLastMessage:lastNonUpdateMessage];
}
}

- (void)getInitialChatHistoryForOfflineMode
{
NSMutableDictionary *userInfo = [NSMutableDictionary new];
Expand Down
2 changes: 1 addition & 1 deletion NextcloudTalk/NCRoomsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ typedef void (^SendOfflineMessagesCompletionBlock)(void);
- (void)updateRoom:(NSString *)token withCompletionBlock:(GetRoomCompletionBlock)block;
- (void)updatePendingMessage:(NSString *)message forRoom:(NCRoom *)room;
- (void)updateLastReadMessage:(NSInteger)lastReadMessage forRoom:(NCRoom *)room;
- (void)updateLastMessage:(NCChatMessage *)message withNoUnreadMessages:(BOOL)noUnreadMessages forRoom:(NCRoom *)room;
- (void)updateLastCommonReadMessage:(NSInteger)messageId forRoom:(NCRoom *)room;
- (void)setNoUnreadMessagesForRoom:(NCRoom *)room withLastMessage:(NCChatMessage * _Nullable)lastMessage;
// Chat
- (void)startChatInRoom:(NCRoom *)room;
- (void)leaveChatInRoom:(NSString *)token;
Expand Down
22 changes: 12 additions & 10 deletions NextcloudTalk/NCRoomsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,22 @@ - (void)updateLastReadMessage:(NSInteger)lastReadMessage forRoom:(NCRoom *)room
}];
}

- (void)updateLastMessage:(NCChatMessage *)message withNoUnreadMessages:(BOOL)noUnreadMessages forRoom:(NCRoom *)room
- (void)setNoUnreadMessagesForRoom:(NCRoom *)room withLastMessage:(NCChatMessage * _Nullable)lastMessage
{
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
NCRoom *managedRoom = [NCRoom objectsWhere:@"internalId = %@", room.internalId].firstObject;
if (managedRoom) {
managedRoom.lastMessageId = message.internalId;
managedRoom.lastActivity = message.timestamp;

if (noUnreadMessages) {
managedRoom.unreadMention = NO;
managedRoom.unreadMentionDirect = NO;
managedRoom.unreadMessages = 0;
}
if (!managedRoom) {
return;
}

managedRoom.unreadMention = NO;
managedRoom.unreadMentionDirect = NO;
managedRoom.unreadMessages = 0;

if (lastMessage) {
managedRoom.lastMessageId = lastMessage.internalId;
managedRoom.lastActivity = lastMessage.timestamp;
}
}];
}
Expand Down
Loading