fix: code review

This commit is contained in:
Richard Ramos 2021-01-19 14:07:40 -04:00 committed by Iuri Matias
parent 55466416d6
commit 17477b0c45
2 changed files with 48 additions and 10 deletions

View File

@ -451,18 +451,36 @@ QtObject:
proc asyncMessageLoad*(self: ChatsView, chatId: string) {.slot.} = proc asyncMessageLoad*(self: ChatsView, chatId: string) {.slot.} =
spawnAndSend(self, "asyncMessageLoaded") do: # Call self.ensResolved(string) when ens is resolved 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, "chatId": chatId,
"messages": callPrivateRPC("chatMessages".prefix, %* [chatId, "", 20]).parseJson()["result"], "messages": messages,
"reactions": callPrivateRPC("emojiReactionsByChatID".prefix, %* [chatId, "", 20]).parseJson()["result"] "reactions": reactions
}) })
proc asyncMessageLoaded*(self: ChatsView, rpcResponse: string) {.slot.} = proc asyncMessageLoaded*(self: ChatsView, rpcResponse: string) {.slot.} =
let rpcResponseObj = rpcResponse.parseJson let rpcResponseObj = rpcResponse.parseJson
let chatMessages = parseChatMessagesResponse(rpcResponseObj["chatId"].getStr, rpcResponseObj["messages"])
let reactions = parseReactionsResponse(rpcResponseObj["chatId"].getStr, rpcResponseObj["reactions"]) if(rpcResponseObj["messages"].kind != JNull):
self.status.chat.chatMessages(rpcResponseObj["chatId"].getStr, true, chatMessages[0], chatMessages[1]) let chatMessages = parseChatMessagesResponse(rpcResponseObj["chatId"].getStr, rpcResponseObj["messages"])
self.status.chat.chatReactions(rpcResponseObj["chatId"].getStr, true, reactions[0], reactions[1]) 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.} = proc hideLoadingIndicator*(self: ChatsView) {.slot.} =
self.loadingMessages = false self.loadingMessages = false

View File

@ -62,6 +62,14 @@ proc parseChatMessagesResponse*(chatId: string, rpcResult: JsonNode): (string, s
messages.add(jsonMsg.toMessage) messages.add(jsonMsg.toMessage)
return (rpcResult{"cursor"}.getStr, messages) 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]) = proc chatMessages*(chatId: string, cursor: string = ""): (string, seq[Message]) =
var cursorVal: JsonNode var cursorVal: JsonNode
@ -70,8 +78,10 @@ proc chatMessages*(chatId: string, cursor: string = ""): (string, seq[Message])
else: else:
cursorVal = newJString(cursor) cursorVal = newJString(cursor)
let callRPCResult = parseJson(callPrivateRPC("chatMessages".prefix, %* [chatId, cursorVal, 20]))["result"] var success: bool
return parseChatMessagesResponse(chatId, callRPCResult) let callResult = rpcChatMessages(chatId, cursorVal, 20, success)
if success:
result = parseChatMessagesResponse(chatId, callResult.parseJson()["result"])
proc parseReactionsResponse*(chatId: string, rpcResult: JsonNode): (string, seq[Reaction]) = proc parseReactionsResponse*(chatId: string, rpcResult: JsonNode): (string, seq[Reaction]) =
var reactions: seq[Reaction] = @[] var reactions: seq[Reaction] = @[]
@ -80,6 +90,14 @@ proc parseReactionsResponse*(chatId: string, rpcResult: JsonNode): (string, seq[
reactions.add(jsonMsg.toReaction) reactions.add(jsonMsg.toReaction)
return (rpcResult{"cursor"}.getStr, reactions) 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]) = proc getEmojiReactionsByChatId*(chatId: string, cursor: string = ""): (string, seq[Reaction]) =
var cursorVal: JsonNode var cursorVal: JsonNode
@ -88,8 +106,10 @@ proc getEmojiReactionsByChatId*(chatId: string, cursor: string = ""): (string, s
else: else:
cursorVal = newJString(cursor) cursorVal = newJString(cursor)
let rpcResult = parseJson(callPrivateRPC("emojiReactionsByChatID".prefix, %* [chatId, cursorVal, 20]))["result"] var success: bool
return parseReactionsResponse(chatId, rpcResult) 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] = proc addEmojiReaction*(chatId: string, messageId: string, emojiId: int): seq[Reaction] =
let rpcResult = parseJson(callPrivateRPC("sendEmojiReaction".prefix, %* [chatId, messageId, emojiId]))["result"] let rpcResult = parseJson(callPrivateRPC("sendEmojiReaction".prefix, %* [chatId, messageId, emojiId]))["result"]