refactor: check mentions in toMessage

This commit is contained in:
Jonathan Rainville 2021-02-02 13:22:38 -05:00 committed by Iuri Matias
parent e57fc2afb7
commit 1616ae255b
3 changed files with 15 additions and 10 deletions

View File

@ -1,20 +1,24 @@
import ../libstatus/settings as status_settings
proc formatChatUpdate(response: JsonNode): (seq[Chat], seq[Message]) =
var chats: seq[Chat] = @[]
var messages: seq[Message] = @[]
let pk = status_settings.getSetting[string](Setting.PublicKey, "0x0")
if response["result"]{"chats"} != nil:
for jsonMsg in response["result"]["messages"]:
messages.add(jsonMsg.toMessage)
messages.add(jsonMsg.toMessage(pk))
if response["result"]{"chats"} != nil:
for jsonChat in response["result"]["chats"]:
chats.add(jsonChat.toChat)
result = (chats, messages)
proc processChatUpdate(self: ChatModel, response: JsonNode): (seq[Chat], seq[Message]) =
let pk = status_settings.getSetting[string](Setting.PublicKey, "0x0")
var chats: seq[Chat] = @[]
var messages: seq[Message] = @[]
if response{"result"}{"chats"} != nil:
for jsonMsg in response["result"]["messages"]:
messages.add(jsonMsg.toMessage)
messages.add(jsonMsg.toMessage(pk))
if response{"result"}{"chats"} != nil:
for jsonChat in response["result"]["chats"]:
let chat = jsonChat.toChat

View File

@ -61,9 +61,7 @@ proc parseChatMessagesResponse*(chatId: string, rpcResult: JsonNode): (string, s
var msg: Message
if rpcResult["messages"].kind != JNull:
for jsonMsg in rpcResult["messages"]:
msg = jsonMsg.toMessage
msg.hasMention = concat(msg.parsedText.map(t => t.children.filter(c => c.textType == "mention" and c.literal == pk))).len > 0
messages.add(msg)
messages.add(jsonMsg.toMessage(pk))
return (rpcResult{"cursor"}.getStr, messages)
proc rpcChatMessages*(chatId: string, cursorVal: JsonNode, limit: int, success: var bool): string =

View File

@ -13,7 +13,7 @@ import types
import web3/conversions
from ../libstatus/utils import parseAddress, wei2Eth
proc toMessage*(jsonMsg: JsonNode): Message
proc toMessage*(jsonMsg: JsonNode, pk: string): Message
proc toChat*(jsonChat: JsonNode): Chat
@ -36,8 +36,7 @@ proc fromEvent*(event: JsonNode): Signal =
if event["event"]{"messages"} != nil:
for jsonMsg in event["event"]["messages"]:
var message = jsonMsg.toMessage
message.hasMention = concat(message.parsedText.map(t => t.children.filter(c => c.textType == "mention" and c.literal == pk))).len > 0
var message = jsonMsg.toMessage(pk)
if message.hasMention:
chatsWithMentions.add(message.chatId)
signal.messages.add(message)
@ -120,6 +119,8 @@ proc toChat*(jsonChat: JsonNode): Chat =
let chatTypeInt = jsonChat{"chatType"}.getInt
let chatType: ChatType = if chatTypeInt >= ord(low(ChatType)) or chatTypeInt <= ord(high(ChatType)): ChatType(chatTypeInt) else: ChatType.Unknown
let pk = status_settings.getSetting[string](Setting.PublicKey, "0x0")
result = Chat(
id: jsonChat{"id"}.getStr,
communityId: jsonChat{"communityId"}.getStr,
@ -141,7 +142,7 @@ proc toChat*(jsonChat: JsonNode): Chat =
result.muted = jsonChat["muted"].getBool
if jsonChat["lastMessage"].kind != JNull:
result.lastMessage = jsonChat{"lastMessage"}.toMessage
result.lastMessage = jsonChat{"lastMessage"}.toMessage(pk)
if result.chatType == ChatType.OneToOne:
result.identicon = generateIdenticon(result.id)
@ -202,7 +203,7 @@ proc toTextItem*(jsonText: JsonNode): TextItem =
result.children.add(child.toTextItem)
proc toMessage*(jsonMsg: JsonNode): Message =
proc toMessage*(jsonMsg: JsonNode, pk: string): Message =
var contentType: ContentType
try:
contentType = ContentType(jsonMsg{"contentType"}.getInt)
@ -277,6 +278,8 @@ proc toMessage*(jsonMsg: JsonNode): Message =
signature: jsonMsg["commandParameters"]["signature"].getStr
)
message.hasMention = concat(message.parsedText.map(t => t.children.filter(c => c.textType == "mention" and c.literal == pk))).len > 0
result = message
proc toReaction*(jsonReaction: JsonNode): Reaction =