From 752eda50a4bd9f7be904bd4f95b2a1179ee7ee11 Mon Sep 17 00:00:00 2001 From: Mikhail Rogachev Date: Tue, 30 Jan 2024 21:52:35 +0400 Subject: [PATCH] fix(Chat): Replace chat fetching in CR flows with message parsing (#13361) Close #11854 --- src/app_service/service/contacts/service.nim | 22 ++++++++++++++------ src/app_service/service/message/service.nim | 20 +++++++++++++----- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/app_service/service/contacts/service.nim b/src/app_service/service/contacts/service.nim index cf543e8c0b..5d2af267af 100644 --- a/src/app_service/service/contacts/service.nim +++ b/src/app_service/service/contacts/service.nim @@ -55,8 +55,9 @@ type RpcResponseArgs* = ref object of Args response*: RpcResponse[JsonNode] - ReloadOneToOneArgs* = ref object of Args - sectionId*: string + AppendChatMessagesArgs* = ref object of Args + chatId*: string + messages*: JsonNode # Signals which may be emitted by this service: const SIGNAL_ENS_RESOLVED* = "ensResolved" @@ -80,7 +81,7 @@ const SIGNAL_CONTACT_VERIFICATION_ACCEPTED* = "contactVerificationRequestAccepte const SIGNAL_CONTACT_VERIFICATION_ADDED* = "contactVerificationRequestAdded" const SIGNAL_CONTACT_VERIFICATION_UPDATED* = "contactVerificationRequestUpdated" const SIGNAL_CONTACT_INFO_REQUEST_FINISHED* = "contactInfoRequestFinished" -const SIGNAL_RELOAD_ONE_TO_ONE_CHAT* = "reloadOneToOneChat" +const SIGNAL_APPEND_CHAT_MESSAGES* = "appendChatMessages" type ContactsGroup* {.pure.} = enum @@ -470,7 +471,10 @@ QtObject: return self.parseContactsResponse(response) - self.events.emit(SIGNAL_RELOAD_ONE_TO_ONE_CHAT, ReloadOneToOneArgs(sectionId: publicKey)) + self.events.emit(SIGNAL_APPEND_CHAT_MESSAGES, AppendChatMessagesArgs( + chatId: publicKey, + messages: response.result{"messages"} + )) checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) except Exception as e: @@ -490,7 +494,10 @@ QtObject: return self.parseContactsResponse(response) - self.events.emit(SIGNAL_RELOAD_ONE_TO_ONE_CHAT, ReloadOneToOneArgs(sectionId: publicKey)) + self.events.emit(SIGNAL_APPEND_CHAT_MESSAGES, AppendChatMessagesArgs( + chatId: publicKey, + messages: response.result{"messages"} + )) checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) except Exception as e: @@ -556,7 +563,10 @@ QtObject: error "error removing contact ", msg = response.error.message return - self.events.emit(SIGNAL_RELOAD_ONE_TO_ONE_CHAT, ReloadOneToOneArgs(sectionId: publicKey)) + self.events.emit(SIGNAL_APPEND_CHAT_MESSAGES, AppendChatMessagesArgs( + chatId: publicKey, + messages: response.result{"messages"} + )) self.parseContactsResponse(response) checkAndEmitACNotificationsFromResponse(self.events, response.result{"activityCenterNotifications"}) diff --git a/src/app_service/service/message/service.nim b/src/app_service/service/message/service.nim index 94b125c3ca..0069ed24f7 100644 --- a/src/app_service/service/message/service.nim +++ b/src/app_service/service/message/service.nim @@ -269,7 +269,7 @@ QtObject: let data = MessagesLoadedArgs(chatId: chatId, messages: @[], reactions: @[]) - + self.events.emit(SIGNAL_MESSAGES_LOADED, data) return @@ -398,10 +398,20 @@ QtObject: let data = EnvelopeExpiredArgs(messagesIds: receivedData.messageIds) self.events.emit(SIGNAL_ENVELOPE_EXPIRED, data) - self.events.on(SIGNAL_RELOAD_ONE_TO_ONE_CHAT) do(e: Args): - let args = ReloadOneToOneArgs(e) - self.resetMessageCursor(args.sectionId) - self.asyncLoadMoreMessagesForChat(args.sectionId) + self.events.on(SIGNAL_APPEND_CHAT_MESSAGES) do(e: Args): + let args = AppendChatMessagesArgs(e) + + if args.messages != nil and args.messages.kind != JNull: + var messages: seq[MessageDto] + messages = map(args.messages.getElems(), proc(x: JsonNode): MessageDto = x.toMessageDto()) + + self.bulkReplacePubKeysWithDisplayNames(messages) + + self.events.emit(SIGNAL_MESSAGES_LOADED, MessagesLoadedArgs( + chatId: args.chatId, + messages: messages, + reactions: @[], + )) self.events.on(SignalType.Message.event) do(e: Args): var receivedData = MessageSignal(e)