From c53e4aeb92b6f0cf2fe972e446904547603008aa Mon Sep 17 00:00:00 2001 From: Andrey Bocharnikov Date: Tue, 26 Mar 2024 21:13:22 +0700 Subject: [PATCH] (Chat): Display system messages about adding/removing members immediately after an update is performed (#14146) * (Chat): Display system add/remove messages immediately after update fixes #9876 * (Chat): Display system "pinned" message immediately after update fixes #10702 * fix issues from the PR --- src/app_service/service/chat/service.nim | 84 +++++++++++++------ src/app_service/service/community/service.nim | 2 +- src/app_service/service/message/service.nim | 1 + src/app_service/service/stickers/service.nim | 2 +- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/src/app_service/service/chat/service.nim b/src/app_service/service/chat/service.nim index bd6cf8426f..0604368448 100644 --- a/src/app_service/service/chat/service.nim +++ b/src/app_service/service/chat/service.nim @@ -154,7 +154,7 @@ QtObject: proc updateOrAddChat*(self: Service, chat: ChatDto) proc hydrateChannelGroups*(self: Service, data: JsonNode) proc updateOrAddChannelGroup*(self: Service, channelGroup: ChannelGroupDto, isCommunityChannelGroup: bool = false) - proc processMessageUpdateAfterSend*(self: Service, response: RpcResponse[JsonNode]): (seq[ChatDto], seq[MessageDto]) + proc processMessengerResponse*(self: Service, response: RpcResponse[JsonNode]): (seq[ChatDto], seq[MessageDto]) proc doConnect(self: Service) = self.events.on(SignalType.Message.event) do(e: Args): @@ -195,7 +195,7 @@ QtObject: self.events.on(SIGNAL_CHAT_REQUEST_UPDATE_AFTER_SEND) do(e: Args): var args = RpcResponseArgs(e) - discard self.processMessageUpdateAfterSend(args.response) + discard self.processMessengerResponse(args.response) proc getChannelGroups*(self: Service): seq[ChannelGroupDto] = return toSeq(self.channelGroups.values) @@ -389,36 +389,64 @@ QtObject: proc parseChatResponse*(self: Service, response: RpcResponse[JsonNode]): (seq[ChatDto], seq[MessageDto]) = var chats: seq[ChatDto] = @[] var messages: seq[MessageDto] = @[] - if response.result{"messages"} != nil: - for jsonMsg in response.result["messages"]: - messages.add(jsonMsg.toMessageDto) - if response.result{"chats"} != nil: - for jsonChat in response.result["chats"]: - let chat = chat_dto.toChatDto(jsonChat) - # TODO add the channel back to `chat` when it is refactored - self.updateOrAddChat(chat) - chats.add(chat) - result = (chats, messages) + let errDesription = response.result{"error"}.getStr + if errDesription != "": + error "Error in parseGroupChatResponse: ", errDesription + else: + if response.result{"messages"} != nil: + for jsonMsg in response.result["messages"]: + messages.add(jsonMsg.toMessageDto) + if response.result{"chats"} != nil: + for jsonChat in response.result["chats"]: + let chat = chat_dto.toChatDto(jsonChat) + # TODO add the channel back to `chat` when it is refactored + self.updateOrAddChat(chat) + chats.add(chat) + return (chats, messages) - proc processMessageUpdateAfterSend*(self: Service, response: RpcResponse[JsonNode]): (seq[ChatDto], seq[MessageDto]) = - result = self.parseChatResponse(response) - var (chats, messages) = result + proc signalChatsAndMessagesUpdates*(self: Service, chats: seq[ChatDto], messages: seq[MessageDto]) = if chats.len == 0 or messages.len == 0: error "no chats or messages in the parsed response" return - for chat in chats: if (chat.active): self.events.emit(SIGNAL_CHAT_CREATED, CreatedChatArgs(chat: chat)) - + var chatMap: Table[string, ChatDto] = initTable[string, ChatDto]() + for chat in chats: + chatMap[chat.id] = chat for msg in messages: - for chat in chats: - if chat.id == msg.chatId: - self.events.emit(SIGNAL_SENDING_SUCCESS, MessageSendingSuccess(message: msg, chat: chat)) - break + if chatMap.hasKey(msg.chatId): + let chat = chatMap[msg.chatId] + self.events.emit(SIGNAL_SENDING_SUCCESS, MessageSendingSuccess(message: msg, chat: chat)) + + proc processMessengerResponse*(self: Service, response: RpcResponse[JsonNode]): (seq[ChatDto], seq[MessageDto]) = + result = self.parseChatResponse(response) + var (chats, messages) = result + self.signalChatsAndMessagesUpdates(chats, messages) + + proc processGroupChatResponse*(self: Service, response: RpcResponse[JsonNode]) = + proc parseGroupChatResponse(response: RpcResponse[JsonNode]): (ChatDto, seq[MessageDto], string) = + var chat: ChatDto + var messages: seq[MessageDto] = @[] + + let errorDescription = response.result{"error"}.getStr + + if response.result{"chat"} != nil: + chat = chat_dto.toChatDto(response.result["chat"]) + if response.result{"messages"} != nil: + for jsonMsg in response.result["messages"]: + messages.add(jsonMsg.toMessageDto) + return (chat, messages, errorDescription) + + var (chat, messages, errorDescription) = parseGroupChatResponse(response) + if errorDescription != "": + error "Received an error in the ProcessGroupChatResponse: ", errorDescription + else: + self.updateOrAddChat(chat) + self.signalChatsAndMessagesUpdates(@[chat], messages) proc processUpdateForTransaction*(self: Service, messageId: string, response: RpcResponse[JsonNode]) = - var (chats, _) = self.processMessageUpdateAfterSend(response) + var (chats, _) = self.processMessengerResponse(response) # TODO: Signal is not handled anywhere self.events.emit(SIGNAL_MESSAGE_REMOVE, MessageArgs(id: messageId, channel: chats[0].id)) @@ -533,7 +561,7 @@ QtObject: for imagePath in imagePaths: removeFile(imagePath) - discard self.processMessageUpdateAfterSend(response) + discard self.processMessengerResponse(response) except Exception as e: error "Error sending images", msg = e.msg result = fmt"Error sending images: {e.msg}" @@ -560,7 +588,7 @@ QtObject: linkPreviews, communityId) # Only send a community ID for the community invites - let (chats, messages) = self.processMessageUpdateAfterSend(response) + let (chats, messages) = self.processMessengerResponse(response) if chats.len == 0 or messages.len == 0: self.events.emit(SIGNAL_SENDING_FAILED, ChatArgs(chatId: chatId)) except Exception as e: @@ -570,7 +598,7 @@ QtObject: try: let address = if (tokenAddress == ZERO_ADDRESS): "" else: tokenAddress let response = status_chat_commands.requestAddressForTransaction(chatId, fromAddress, amount, address) - discard self.processMessageUpdateAfterSend(response) + discard self.processMessengerResponse(response) except Exception as e: error "Error requesting address for transaction", msg = e.msg @@ -578,7 +606,7 @@ QtObject: try: let address = if (tokenAddress == ZERO_ADDRESS): "" else: tokenAddress let response = status_chat_commands.requestTransaction(chatId, fromAddress, amount, address) - discard self.processMessageUpdateAfterSend(response) + discard self.processMessengerResponse(response) except Exception as e: error "Error requesting transaction", msg = e.msg @@ -606,7 +634,7 @@ QtObject: proc acceptRequestTransaction*(self: Service, transactionHash: string, messageId: string, signature: string) = try: let response = status_chat_commands.acceptRequestTransaction(transactionHash, messageId, signature) - discard self.processMessageUpdateAfterSend(response) + discard self.processMessengerResponse(response) except Exception as e: error "Error requesting transaction", msg = e.msg @@ -665,6 +693,7 @@ QtObject: let response = status_group_chat.addMembers(communityID, chatId, members) if (response.error.isNil): self.events.emit(SIGNAL_CHAT_MEMBERS_ADDED, ChatMembersAddedArgs(chatId: chatId, ids: members)) + self.processGroupChatResponse(response) except Exception as e: error "error while adding group members: ", msg = e.msg @@ -673,6 +702,7 @@ QtObject: let response = status_group_chat.removeMember(communityID, chatId, member) if (response.error.isNil): self.events.emit(SIGNAL_CHAT_MEMBER_REMOVED, ChatMemberRemovedArgs(chatId: chatId, id: member)) + self.processGroupChatResponse(response) except Exception as e: error "error while removing member from group: ", msg = e.msg diff --git a/src/app_service/service/community/service.nim b/src/app_service/service/community/service.nim index 7d01663148..c8128acc26 100644 --- a/src/app_service/service/community/service.nim +++ b/src/app_service/service/community/service.nim @@ -2085,7 +2085,7 @@ QtObject: # We no longer send invites, but merely share the community so # users can request access (with automatic acception) let response = status_go.shareCommunityToUsers(communityId, pubKeys, inviteMessage) - discard self.chatService.processMessageUpdateAfterSend(response) + discard self.chatService.processMessengerResponse(response) except Exception as e: error "Error sharing community", msg = e.msg result = "Error sharing community: " & e.msg diff --git a/src/app_service/service/message/service.nim b/src/app_service/service/message/service.nim index ff564ccc1b..8f7c9ac9e9 100644 --- a/src/app_service/service/message/service.nim +++ b/src/app_service/service/message/service.nim @@ -644,6 +644,7 @@ QtObject: if(not pinned and not pin): self.numOfPinnedMessagesPerChat[chatId] = self.getNumOfPinnedMessages(chatId) - 1 self.events.emit(SIGNAL_MESSAGE_UNPINNED, data) + discard self.chatService.processMessengerResponse(response) except Exception as e: error "error: ", procName="pinUnpinMessage", errName = e.name, errDesription = e.msg diff --git a/src/app_service/service/stickers/service.nim b/src/app_service/service/stickers/service.nim index c55624da2b..5a101a87f2 100644 --- a/src/app_service/service/stickers/service.nim +++ b/src/app_service/service/stickers/service.nim @@ -369,7 +369,7 @@ QtObject: communityId = "", # communityId is not necessary when sending a sticker sticker.hash, sticker.packId) - discard self.chatService.processMessageUpdateAfterSend(response) + discard self.chatService.processMessengerResponse(response) self.addStickerToRecent(sticker) proc removeRecentStickers*(self: Service, packId: string) =