reorg status integration

This commit is contained in:
Richard Ramos 2020-05-13 11:37:12 -04:00 committed by Iuri Matias
parent 0459dd06e5
commit f16863ab2d
8 changed files with 113 additions and 81 deletions

View File

@ -2,7 +2,9 @@ import NimQml
import applicationView
import chats
import state
import status
import status/core as status
import status/test as status_test
proc mainProc() =
# From QT docs:
@ -21,12 +23,12 @@ proc mainProc() =
status.init()
status.setupNewAccount()
discard status.addPeer("enode://2c8de3cbb27a3d30cbb5b3e003bc722b126f5aef82e2052aaef032ca94e0c7ad219e533ba88c70585ebd802de206693255335b100307645ab5170e88620d2a81@47.244.221.14:443")
status_test.setupNewAccount()
discard status_test.addPeer("enode://2c8de3cbb27a3d30cbb5b3e003bc722b126f5aef82e2052aaef032ca94e0c7ad219e533ba88c70585ebd802de206693255335b100307645ab5170e88620d2a81@47.244.221.14:443")
echo status.callPrivateRPC("{\"jsonrpc\":\"2.0\", \"method\":\"wakuext_requestMessages\", \"params\":[{\"topics\": [\"0x7998f3c8\"]}], \"id\": 1}")
# result.accountResult = status.queryAccounts()
status.subscribeToTest()
status_test.subscribeToTest()
let logic = newApplicationView(app, status.callPrivateRPC)
defer: logic.delete

60
src/status/chat.nim Normal file
View File

@ -0,0 +1,60 @@
import libstatus
import json
import utils
proc startMessenger*() =
let payload = %* {
"jsonrpc": "2.0",
"id": 3, #TODO:
"method": "startMessenger".prefix,
"params": []
}
discard $libstatus.callPrivateRPC($payload)
# TODO: create template for error handling
proc loadFilters*(chatId: string) =
let payload = %* {
"jsonrpc": "2.0",
"id": 3, #TODO:
"method": "loadFilters".prefix,
"params": [
[{
"ChatID": chatId,
"OneToOne": false
}]
]
}
discard $libstatus.callPrivateRPC($payload)
proc saveChat*(chatId: string) =
let payload = %* {
"jsonrpc": "2.0",
"id": 4,
"method": "saveChat".prefix,
"params": [ #TODO: determine where do these values come from
{
"lastClockValue": 0,
"color": "#51d0f0",
"name": chatId,
"lastMessage": nil,
"active": true,
"id": chatId,
"unviewedMessagesCount": 0,
"chatType": 2,
"timestamp": 1588940692659
}
]
}
discard $libstatus.callPrivateRPC($payload)
proc chatMessages*(chatId: string) =
let payload = %* {
"jsonrpc": "2.0",
"id": 3, #TODO:
"method": "chatMessages".prefix,
"params": [
chatId, nil, 20
]
}
discard $libstatus.callPrivateRPC($payload)
# TODO: create template for error handling

15
src/status/core.nim Normal file
View File

@ -0,0 +1,15 @@
import libstatus
import signals
import types
proc setSignalHandler(signalHandler: SignalCallback) =
libstatus.setSignalEventCallback(signalHandler)
proc init*() =
setSignalHandler(onSignal)
proc callRPC*(inputJSON: string): string =
return $libstatus.callRPC(inputJSON)
proc callPrivateRPC*(inputJSON: string): string =
return $libstatus.callPrivateRPC(inputJSON)

View File

@ -1,3 +1,5 @@
import types
proc hashMessage*(p0: cstring): cstring {.importc: "HashMessage".}
proc initKeystore*(keydir: cstring): cstring {.importc: "InitKeystore".}
@ -16,6 +18,4 @@ proc callPrivateRPC*(inputJSON: cstring): cstring {.importc: "CallPrivateRPC".}
proc addPeer*(peer: cstring): cstring {.importc: "AddPeer".}
type SignalCallback* = proc(eventMessage: cstring): void
proc setSignalEventCallback*(callback: SignalCallback) {.importc: "SetSignalEventCallback".}

View File

@ -1,9 +1,19 @@
import libstatus
import types
import json
var onSignal*: SignalCallback = proc(p0: cstring): void =
setupForeignThreadGc()
# TODO: Dispatch depending on message type $jsonSignal["type"].getStr
# Consider also have an intermediate object with an enum for type
# So you do not have to deal with json objects but with a nim type
echo $p0
let jsonSignal = ($p0).parseJson
let messageType = $jsonSignal["type"].getStr
case messageType:
of "messages.new":
echo $p0
else:
echo messageType #TODO: do something
tearDownForeignThreadGc()

View File

@ -1,7 +1,12 @@
import os
import core
import libstatus
import json
import signals
import chat
# THIS FILE IS FOR TESTING STATUS FUNCTIONALITY
# ONCE TEST IS DONE, SPLIT THE CODE TO INDIVIDUAL NIM FILES
const datadir = "./data/"
const keystoredir = "./data/keystore/"
@ -12,15 +17,6 @@ proc recreateDir(dirname: string) =
removeDir(dirname)
createDir(dirname)
proc setSignalHandler(signalHandler: SignalCallback) =
libstatus.setSignalEventCallback(signalHandler)
proc init*() =
setSignalHandler(onSignal)
proc queryAccounts*(): string =
var payload = %* {
"jsonrpc": "2.0",
@ -29,63 +25,15 @@ proc queryAccounts*(): string =
[]
]
}
var response = $libstatus.callPrivateRPC($payload)
var response = callPrivateRPC($payload)
echo response
result = parseJson(response)["result"][0].getStr()
proc subscribeToTest*() =
var result = ""
var payload = %* {
"jsonrpc": "2.0",
"id": 3,
"method": "wakuext_startMessenger",
"params": []
}
result = $libstatus.callPrivateRPC($payload)
payload = %* {
"jsonrpc": "2.0",
"id": 3,
"method": "wakuext_loadFilters",
"params": [
[{
"ChatID": "test",
"OneToOne": false
}]
]
}
result = $libstatus.callPrivateRPC($payload)
payload = %* {
"jsonrpc": "2.0",
"id": 4,
"method": "wakuext_saveChat",
"params": [
{
"lastClockValue": 0,
"color": "#51d0f0",
"name": "test",
"lastMessage": nil,
"active": true,
"id": "test",
"unviewedMessagesCount": 0,
"chatType": 2,
"timestamp": 1588940692659
}
]
}
result = $libstatus.callPrivateRPC($payload)
payload = %* {
"jsonrpc": "2.0",
"id": 3,
"method": "wakuext_chatMessages",
"params": [
"test", nil, 20
]
}
result = $libstatus.callPrivateRPC($payload)
proc subscribeToTest*() =
loadFilters("test")
saveChat("test")
chatMessages("test")
proc setupNewAccount*() =
# Deleting directories
@ -356,15 +304,5 @@ proc setupNewAccount*() =
if saveResult["error"].getStr == "":
echo "Account saved succesfully"
proc callRPC*(inputJSON: string): string =
return $libstatus.callRPC(inputJSON)
proc callPrivateRPC*(inputJSON: string): string =
return $libstatus.callPrivateRPC(inputJSON)
proc addPeer*(peer: string): string =
return $libstatus.addPeer(peer)
# proc onMessage*(callback: proc(message: string)): void =
# $libstatus.setSignalEventCallback(callback)

1
src/status/types.nim Normal file
View File

@ -0,0 +1 @@
type SignalCallback* = proc(eventMessage: cstring): void

6
src/status/utils.nim Normal file
View File

@ -0,0 +1,6 @@
proc isWakuEnabled(): bool =
true # TODO:
proc prefix*(methodName: string): string =
result = if isWakuEnabled(): "wakuext_" else: "shhext_"
result = result & methodName