From 17477b0c45e335f9a8f7104ef74c3e0e1d607287 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 19 Jan 2021 14:07:40 -0400 Subject: [PATCH] fix: code review --- src/app/chat/view.nim | 30 ++++++++++++++++++++++++------ src/status/libstatus/chat.nim | 28 ++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index 667f1b8ceb..0dde25ddeb 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -451,18 +451,36 @@ QtObject: proc asyncMessageLoad*(self: ChatsView, chatId: string) {.slot.} = spawnAndSend(self, "asyncMessageLoaded") do: # Call self.ensResolved(string) when ens is resolved + + var messages: JsonNode + var msgCallSuccess: bool + let msgCallResult = rpcChatMessages(chatId, newJString(""), 20, msgCallSuccess) + if(msgCallSuccess): + messages = msgCallResult.parseJson()["result"] + + var reactions: JsonNode + var reactionsCallSuccess: bool + let reactionsCallResult = rpcReactions(chatId, newJString(""), 20, reactionsCallSuccess) + if(reactionsCallSuccess): + reactions = reactionsCallResult.parseJson()["result"] + + $(%*{ "chatId": chatId, - "messages": callPrivateRPC("chatMessages".prefix, %* [chatId, "", 20]).parseJson()["result"], - "reactions": callPrivateRPC("emojiReactionsByChatID".prefix, %* [chatId, "", 20]).parseJson()["result"] + "messages": messages, + "reactions": reactions }) proc asyncMessageLoaded*(self: ChatsView, rpcResponse: string) {.slot.} = let rpcResponseObj = rpcResponse.parseJson - let chatMessages = parseChatMessagesResponse(rpcResponseObj["chatId"].getStr, rpcResponseObj["messages"]) - let reactions = parseReactionsResponse(rpcResponseObj["chatId"].getStr, rpcResponseObj["reactions"]) - self.status.chat.chatMessages(rpcResponseObj["chatId"].getStr, true, chatMessages[0], chatMessages[1]) - self.status.chat.chatReactions(rpcResponseObj["chatId"].getStr, true, reactions[0], reactions[1]) + + if(rpcResponseObj["messages"].kind != JNull): + let chatMessages = parseChatMessagesResponse(rpcResponseObj["chatId"].getStr, rpcResponseObj["messages"]) + self.status.chat.chatMessages(rpcResponseObj["chatId"].getStr, true, chatMessages[0], chatMessages[1]) + + if(rpcResponseObj["reactions"].kind != JNull): + let reactions = parseReactionsResponse(rpcResponseObj["chatId"].getStr, rpcResponseObj["reactions"]) + self.status.chat.chatReactions(rpcResponseObj["chatId"].getStr, true, reactions[0], reactions[1]) proc hideLoadingIndicator*(self: ChatsView) {.slot.} = self.loadingMessages = false diff --git a/src/status/libstatus/chat.nim b/src/status/libstatus/chat.nim index 2e7baf78f5..3080bd83e8 100644 --- a/src/status/libstatus/chat.nim +++ b/src/status/libstatus/chat.nim @@ -62,6 +62,14 @@ proc parseChatMessagesResponse*(chatId: string, rpcResult: JsonNode): (string, s messages.add(jsonMsg.toMessage) return (rpcResult{"cursor"}.getStr, messages) +proc rpcChatMessages*(chatId: string, cursorVal: JsonNode, limit: int, success: var bool): string = + success = true + try: + result = callPrivateRPC("chatMessages".prefix, %* [chatId, cursorVal, limit]) + except RpcException as e: + success = false + result = e.msg + proc chatMessages*(chatId: string, cursor: string = ""): (string, seq[Message]) = var cursorVal: JsonNode @@ -70,8 +78,10 @@ proc chatMessages*(chatId: string, cursor: string = ""): (string, seq[Message]) else: cursorVal = newJString(cursor) - let callRPCResult = parseJson(callPrivateRPC("chatMessages".prefix, %* [chatId, cursorVal, 20]))["result"] - return parseChatMessagesResponse(chatId, callRPCResult) + var success: bool + let callResult = rpcChatMessages(chatId, cursorVal, 20, success) + if success: + result = parseChatMessagesResponse(chatId, callResult.parseJson()["result"]) proc parseReactionsResponse*(chatId: string, rpcResult: JsonNode): (string, seq[Reaction]) = var reactions: seq[Reaction] = @[] @@ -80,6 +90,14 @@ proc parseReactionsResponse*(chatId: string, rpcResult: JsonNode): (string, seq[ reactions.add(jsonMsg.toReaction) return (rpcResult{"cursor"}.getStr, reactions) +proc rpcReactions*(chatId: string, cursorVal: JsonNode, limit: int, success: var bool): string = + success = true + try: + result = callPrivateRPC("emojiReactionsByChatID".prefix, %* [chatId, cursorVal, limit]) + except RpcException as e: + success = false + result = e.msg + proc getEmojiReactionsByChatId*(chatId: string, cursor: string = ""): (string, seq[Reaction]) = var cursorVal: JsonNode @@ -88,8 +106,10 @@ proc getEmojiReactionsByChatId*(chatId: string, cursor: string = ""): (string, s else: cursorVal = newJString(cursor) - let rpcResult = parseJson(callPrivateRPC("emojiReactionsByChatID".prefix, %* [chatId, cursorVal, 20]))["result"] - return parseReactionsResponse(chatId, rpcResult) + var success: bool + let rpcResult = rpcReactions(chatId, cursorVal, 20, success) + if success: + result = parseReactionsResponse(chatId, rpcResult.parseJson()["result"]) proc addEmojiReaction*(chatId: string, messageId: string, emojiId: int): seq[Reaction] = let rpcResult = parseJson(callPrivateRPC("sendEmojiReaction".prefix, %* [chatId, messageId, emojiId]))["result"]