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]) = proc formatChatUpdate(response: JsonNode): (seq[Chat], seq[Message]) =
var chats: seq[Chat] = @[] var chats: seq[Chat] = @[]
var messages: seq[Message] = @[] var messages: seq[Message] = @[]
let pk = status_settings.getSetting[string](Setting.PublicKey, "0x0")
if response["result"]{"chats"} != nil: if response["result"]{"chats"} != nil:
for jsonMsg in response["result"]["messages"]: for jsonMsg in response["result"]["messages"]:
messages.add(jsonMsg.toMessage) messages.add(jsonMsg.toMessage(pk))
if response["result"]{"chats"} != nil: if response["result"]{"chats"} != nil:
for jsonChat in response["result"]["chats"]: for jsonChat in response["result"]["chats"]:
chats.add(jsonChat.toChat) chats.add(jsonChat.toChat)
result = (chats, messages) result = (chats, messages)
proc processChatUpdate(self: ChatModel, response: JsonNode): (seq[Chat], seq[Message]) = proc processChatUpdate(self: ChatModel, response: JsonNode): (seq[Chat], seq[Message]) =
let pk = status_settings.getSetting[string](Setting.PublicKey, "0x0")
var chats: seq[Chat] = @[] var chats: seq[Chat] = @[]
var messages: seq[Message] = @[] var messages: seq[Message] = @[]
if response{"result"}{"chats"} != nil: if response{"result"}{"chats"} != nil:
for jsonMsg in response["result"]["messages"]: for jsonMsg in response["result"]["messages"]:
messages.add(jsonMsg.toMessage) messages.add(jsonMsg.toMessage(pk))
if response{"result"}{"chats"} != nil: if response{"result"}{"chats"} != nil:
for jsonChat in response["result"]["chats"]: for jsonChat in response["result"]["chats"]:
let chat = jsonChat.toChat let chat = jsonChat.toChat

View File

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

View File

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