From 42d14757257ba5f5a8c3fb5208186d6caa50201c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Mu=CC=88ller?= Date: Sun, 7 Mar 2021 13:40:55 +0100 Subject: [PATCH] Add checkForNewMessagesFromMessageId to NCChatController MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcel Mùˆller --- NextcloudTalk/NCChatController.h | 1 + NextcloudTalk/NCChatController.m | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/NextcloudTalk/NCChatController.h b/NextcloudTalk/NCChatController.h index 3476499c..06aee37c 100644 --- a/NextcloudTalk/NCChatController.h +++ b/NextcloudTalk/NCChatController.h @@ -58,6 +58,7 @@ extern NSString * const NCChatControllerDidReceiveDeletedMessageNotification; - (void)getInitialChatHistoryForOfflineMode; - (void)getHistoryBatchFromMessagesId:(NSInteger)messageId; - (void)getHistoryBatchOfflineFromMessagesId:(NSInteger)messageId; +- (void)checkForNewMessagesFromMessageId:(NSInteger)messageId; - (void)updateHistoryInBackgroundWithCompletionBlock:(UpdateHistoryInBackgroundCompletionBlock)block; - (void)startReceivingNewChatMessages; - (void)stopReceivingNewChatMessages; diff --git a/NextcloudTalk/NCChatController.m b/NextcloudTalk/NCChatController.m index 7b30f2cc..0a23a771 100644 --- a/NextcloudTalk/NCChatController.m +++ b/NextcloudTalk/NCChatController.m @@ -335,6 +335,31 @@ NSString * const NCChatControllerDidReceiveDeletedMessageNotification }]; } +- (void)checkForNewMessagesFromMessageId:(NSInteger)messageId +{ + NCChatBlock *lastChatBlock = [self chatBlocksForRoom].lastObject; + NSArray *storedMessages = [self getNewStoredMessagesInBlock:lastChatBlock sinceMessageId:messageId]; + + if (storedMessages.count > 0) { + NSMutableDictionary *userInfo = [NSMutableDictionary new]; + [userInfo setObject:self->_room.token forKey:@"room"]; + [userInfo setObject:storedMessages forKey:@"messages"]; + + [[NSNotificationCenter defaultCenter] postNotificationName:NCChatControllerDidReceiveChatMessagesNotification + 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) { + self->_room.lastActivity = lastMessage.timestamp; + [[NCRoomsManager sharedInstance] updateLastMessage:lastMessage withNoUnreadMessages:YES forRoom:self->_room]; + } + } +} + - (void)getInitialChatHistory { NSMutableDictionary *userInfo = [NSMutableDictionary new];