From 2d0047846739f076510378327ecbff85b05154f3 Mon Sep 17 00:00:00 2001 From: Mikhail Rogachev Date: Mon, 25 Dec 2023 16:18:13 +0700 Subject: [PATCH] feat: update chat unseen counters from the Activity Center (#13020) Close #12857 * feat: update chat unseen counters from the Activity Center * feat: mark all notifications read also updates chats * chore: review fixes --- .../service/activity_center/service.nim | 33 +++++++++++++++++-- .../service/message/async_tasks.nim | 26 ++++++++++----- .../message/dto/seen_unseen_messages.nim | 16 +++++++++ src/app_service/service/message/service.nim | 2 +- src/backend/messages.nim | 2 +- vendor/status-go | 2 +- 6 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 src/app_service/service/message/dto/seen_unseen_messages.nim diff --git a/src/app_service/service/activity_center/service.nim b/src/app_service/service/activity_center/service.nim index 75d02bbafa..308aadbec5 100644 --- a/src/app_service/service/activity_center/service.nim +++ b/src/app_service/service/activity_center/service.nim @@ -11,6 +11,8 @@ import ../../../backend/response_type import ./dto/notification import ../../common/activity_center +import ../message/service +import ../message/dto/seen_unseen_messages export notification @@ -205,7 +207,23 @@ QtObject: proc markActivityCenterNotificationRead*(self: Service, notificationId: string) = try: let notificationIds = @[notificationId] - discard backend.markActivityCenterNotificationsRead(notificationIds) + let response = backend.markActivityCenterNotificationsRead(notificationIds) + + var seenAndUnseenMessagesBatch: JsonNode = newJObject() + discard response.result.getProp("seenAndUnseenMessages", seenAndUnseenMessagesBatch) + + if seenAndUnseenMessagesBatch.len > 0: + for seenAndUnseenMessagesRaw in seenAndUnseenMessagesBatch: + let seenAndUnseenMessages = seenAndUnseenMessagesRaw.toSeenUnseenMessagesDto() + + let data = MessagesMarkedAsReadArgs( + chatId: seenAndUnseenMessages.chatId, + allMessagesMarked: false, + messagesIds: notificationIds, + messagesCount: seenAndUnseenMessages.count, + messagesWithMentionsCount: seenAndUnseenMessages.countWithMentions) + self.events.emit(SIGNAL_MESSAGES_MARKED_AS_READ, data) + self.events.emit(SIGNAL_ACTIVITY_CENTER_MARK_NOTIFICATIONS_AS_READ, ActivityCenterNotificationIdsArgs(notificationIds: notificationIds)) except Exception as e: error "Error marking as read", msg = e.msg @@ -220,7 +238,18 @@ QtObject: proc markAllActivityCenterNotificationsRead*(self: Service) = try: - discard backend.markAllActivityCenterNotificationsRead() + let response = backend.markAllActivityCenterNotificationsRead() + + var seenAndUnseenMessagesBatch: JsonNode = newJObject() + discard response.result.getProp("seenAndUnseenMessages", seenAndUnseenMessagesBatch) + + if seenAndUnseenMessagesBatch.len > 0: + for seenAndUnseenMessagesRaw in seenAndUnseenMessagesBatch: + let seenAndUnseenMessages = seenAndUnseenMessagesRaw.toSeenUnseenMessagesDto() + + let data = MessagesMarkedAsReadArgs(chatId: seenAndUnseenMessages.chatId, allMessagesMarked: true) + self.events.emit(SIGNAL_MESSAGES_MARKED_AS_READ, data) + self.events.emit(SIGNAL_ACTIVITY_CENTER_MARK_ALL_NOTIFICATIONS_AS_READ, Args()) except Exception as e: error "Error marking all as read", msg = e.msg diff --git a/src/app_service/service/message/async_tasks.nim b/src/app_service/service/message/async_tasks.nim index fc99cb1904..d19a72c318 100644 --- a/src/app_service/service/message/async_tasks.nim +++ b/src/app_service/service/message/async_tasks.nim @@ -6,6 +6,15 @@ import ../../../backend/chat as status_go_chat import ../../../app/core/custom_urls/urls_manager +import dto/seen_unseen_messages + +proc getCountAndCountWithMentionsFromResponse(chatId: string, seenAndUnseenMessagesBatch: JsonNode): (int, int) = + if seenAndUnseenMessagesBatch.len > 0: + for seenAndUnseenMessagesRaw in seenAndUnseenMessagesBatch: + let seenAndUnseenMessages = seenAndUnseenMessagesRaw.toSeenUnseenMessagesDto() + if seenAndUnseenMessages.chatId == chatId: + return (seenAndUnseenMessages.count, seenAndUnseenMessages.countWithMentions) + return (0, 0) ################################################# # Async load messages @@ -149,11 +158,9 @@ const asyncMarkCertainMessagesReadTask: Task = proc(argEncoded: string) {.gcsafe let response = status_go.markCertainMessagesFromChatWithIdAsRead(arg.chatId, arg.messagesIds) - var count: int - discard response.result.getProp("count", count) - - var countWithMentions: int - discard response.result.getProp("countWithMentions", countWithMentions) + var seenAndUnseenMessagesBatch: JsonNode = newJObject() + discard response.result.getProp("seenAndUnseenMessages", seenAndUnseenMessagesBatch) + let (count, countWithMentions) = getCountAndCountWithMentionsFromResponse(arg.chatId, seenAndUnseenMessagesBatch) var activityCenterNotifications: JsonNode = newJObject() discard response.result.getProp("activityCenterNotifications", activityCenterNotifications) @@ -319,10 +326,13 @@ const asyncMarkMessageAsUnreadTask: Task = proc(argEncoded: string) {.gcsafe, ni var activityCenterNotifications: JsonNode = newJObject() discard response.result.getProp("activityCenterNotifications", activityCenterNotifications) - - responseJson["messagesCount"] = %response.result["count"] responseJson["activityCenterNotifications"] = activityCenterNotifications - responseJson["messagesWithMentionsCount"] = %response.result["countWithMentions"] + + var seenAndUnseenMessagesBatch: JsonNode = newJObject() + discard response.result.getProp("seenAndUnseenMessages", seenAndUnseenMessagesBatch) + let (count, countWithMentions) = getCountAndCountWithMentionsFromResponse(arg.chatId, seenAndUnseenMessagesBatch) + responseJson["messagesCount"] = %count + responseJson["messagesWithMentionsCount"] = %countWithMentions if response.error != nil: responseJson["error"] = %response.error diff --git a/src/app_service/service/message/dto/seen_unseen_messages.nim b/src/app_service/service/message/dto/seen_unseen_messages.nim new file mode 100644 index 0000000000..3cc002e0c1 --- /dev/null +++ b/src/app_service/service/message/dto/seen_unseen_messages.nim @@ -0,0 +1,16 @@ +import json + +include ../../../common/json_utils + +type SeenUnseenMessagesDto* = object + chatId*: string + count*: int + countWithMentions*: int + seen*: bool + +proc toSeenUnseenMessagesDto*(jsonObj: JsonNode): SeenUnseenMessagesDto = + result = SeenUnseenMessagesDto() + discard jsonObj.getProp("chatId", result.chatId) + discard jsonObj.getProp("count", result.count) + discard jsonObj.getProp("countWithMentions", result.countWithMentions) + discard jsonObj.getProp("seen", result.seen) diff --git a/src/app_service/service/message/service.nim b/src/app_service/service/message/service.nim index e30d52967f..94b125c3ca 100644 --- a/src/app_service/service/message/service.nim +++ b/src/app_service/service/message/service.nim @@ -828,7 +828,7 @@ QtObject: discard responseObj.getProp("countWithMentions", countWithMentions) let data = MessagesMarkedAsReadArgs( - chatId: chatId, + chatId: chatId, allMessagesMarked: false, messagesIds: messagesIds, messagesCount: count, diff --git a/src/backend/messages.nim b/src/backend/messages.nim index 208f52c253..1cf3f6e263 100644 --- a/src/backend/messages.nim +++ b/src/backend/messages.nim @@ -61,7 +61,7 @@ proc markAllMessagesFromChatWithIdAsRead*(chatId: string): RpcResponse[JsonNode] proc markCertainMessagesFromChatWithIdAsRead*(chatId: string, messageIds: seq[string]): RpcResponse[JsonNode] {.raises: [Exception].} = let payload = %* [chatId, messageIds] - result = callPrivateRPC("markMessagesSeen".prefix, payload) + result = callPrivateRPC("markMessagesRead".prefix, payload) proc deleteMessageAndSend*(messageID: string): RpcResponse[JsonNode] {.raises: [Exception].} = result = callPrivateRPC("deleteMessageAndSend".prefix, %* [messageID]) diff --git a/vendor/status-go b/vendor/status-go index a35236e3ed..843bae5659 160000 --- a/vendor/status-go +++ b/vendor/status-go @@ -1 +1 @@ -Subproject commit a35236e3ed77e012f540c4ef7d62db046f355387 +Subproject commit 843bae56597ecf3811d724504bd0216b867979f7