fix: random crash due to no messages availables in signal

This commit is contained in:
Richard Ramos 2020-05-22 10:43:01 -04:00 committed by RichΛrd
parent 63d525e0f4
commit f29191bc59
1 changed files with 24 additions and 23 deletions

View File

@ -4,29 +4,30 @@ import types
proc fromEvent*(event: JsonNode): Signal =
var signal:ChatSignal = ChatSignal()
signal.messages = @[]
for jsonMsg in event["event"]["messages"]:
let msg = Message(
alias: jsonMsg["alias"].getStr,
chatId: jsonMsg["chatId"].getStr,
clock: $jsonMsg["clock"].getInt,
contentType: jsonMsg["contentType"].getInt,
ensName: jsonMsg["ensName"].getStr,
fromAuthor: jsonMsg["from"].getStr,
id: jsonMsg["identicon"].getStr,
identicon: jsonMsg["identicon"].getStr,
lineCount: jsonMsg["lineCount"].getInt,
localChatId: jsonMsg["localChatId"].getStr,
messageType: jsonMsg["messageType"].getStr,
replace: jsonMsg["replace"].getStr,
responseTo: jsonMsg["responseTo"].getStr,
rtl: jsonMsg["rtl"].getBool,
seen: jsonMsg["seen"].getBool,
text: jsonMsg["text"].getStr,
timestamp: $jsonMsg["timestamp"].getInt,
whisperTimestamp: $jsonMsg["whisperTimestamp"].getInt,
isCurrentUser: false # TODO: this must compare the fromAuthor against current user because the messages received from the mailserver will arrive as signals too, and those include the current user messages
)
signal.messages.add(msg)
if event["event"]{"messages"} != nil:
for jsonMsg in event["event"]["messages"]:
let msg = Message(
alias: jsonMsg{"alias"}.getStr,
chatId: jsonMsg{"chatId"}.getStr,
clock: $jsonMsg{"clock"}.getInt,
contentType: jsonMsg{"contentType"}.getInt,
ensName: jsonMsg{"ensName"}.getStr,
fromAuthor: jsonMsg{"from"}.getStr,
id: jsonMsg{"identicon"}.getStr,
identicon: jsonMsg{"identicon"}.getStr,
lineCount: jsonMsg{"lineCount"}.getInt,
localChatId: jsonMsg{"localChatId"}.getStr,
messageType: jsonMsg{"messageType"}.getStr,
replace: jsonMsg{"replace"}.getStr,
responseTo: jsonMsg{"responseTo"}.getStr,
rtl: jsonMsg{"rtl"}.getBool,
seen: jsonMsg{"seen"}.getBool,
text: jsonMsg{"text"}.getStr,
timestamp: $jsonMsg{"timestamp"}.getInt,
whisperTimestamp: $jsonMsg{"whisperTimestamp"}.getInt,
isCurrentUser: false # TODO: this must compare the fromAuthor against current user because the messages received from the mailserver will arrive as signals too, and those include the current user messages
)
signal.messages.add(msg)
result = signal