Add node model; simplify more; simplify callRPC

Add node model; simplify more; simplify callRPC

refactor wallet to use asset model

move some of wallet logic away from the controller

move ChatMessage to model

move chatItem model

organize models folder

simplify chat message and chat item

rename messageList to message_list

simply addresses in the controller

rename mailservers list

refactor how profile is set

refactor/simplify profile view

refactor/simplify adding mailservers

rename wallet view

simplify wallet assets

rename nodeView to view

extract channel list view

extract channel list view

refactor channel list / chats view

move signals out of app folder

simplify callRPC

add raw rpc method and make node section use it

add node model

move accounts model inside onboard controller (for now)

make events usage consistent among models and controllers; separate model events from app events
This commit is contained in:
Iuri Matias 2020-05-22 19:33:36 -04:00
parent 6e14749904
commit e6199fede6
14 changed files with 127 additions and 138 deletions

View File

@ -9,10 +9,12 @@ type ChatController* = ref object of SignalSubscriber
view*: ChatsView
model*: ChatModel
variant*: QVariant
appEvents*: EventEmitter
proc newController*(events: EventEmitter): ChatController =
proc newController*(appEvents: EventEmitter): ChatController =
result = ChatController()
result.model = newChatModel(events)
result.appEvents = appEvents
result.model = newChatModel()
result.view = newChatsView(result.model)
result.variant = newQVariant(result.view)

View File

@ -1,23 +1,25 @@
import NimQml
import chronicles
import eventemitter
import "../../status/core" as status
import ../../signals/types
import ../../models/node
import view
logScope:
topics = "node"
type NodeController* = ref object of SignalSubscriber
model*: NodeModel
view*: NodeView
variant*: QVariant
appEvents*: EventEmitter
var sendRPCMessage = proc (msg: string): string =
echo "sending RPC message"
status.callPrivateRPC(msg)
proc newController*(): NodeController =
proc newController*(appEvents: EventEmitter): NodeController =
result = NodeController()
result.view = newNodeView(sendRPCMessage)
result.appEvents = appEvents
result.model = newNodeModel()
result.view = newNodeView(result.model)
result.variant = newQVariant(result.view)
proc delete*(self: NodeController) =

View File

@ -1,17 +1,18 @@
import NimQml
import ../../models/node
QtObject:
type NodeView* = ref object of QObject
model: NodeModel
callResult: string
sendRPCMessage: proc (msg: string): string
lastMessage*: string
proc setup(self: NodeView) =
self.QObject.setup
proc newNodeView*(sendRPCMessage: proc): NodeView =
proc newNodeView*(model: NodeModel): NodeView =
new(result)
result.sendRPCMessage = sendRPCMessage
result.model = model
result.callResult = "Use this tool to call JSONRPC methods"
result.lastMessage = ""
result.setup
@ -37,7 +38,7 @@ QtObject:
notify = callResultChanged
proc onSend*(self: NodeView, inputJSON: string) {.slot.} =
self.setCallResult(self.sendRPCMessage(inputJSON))
self.setCallResult(self.model.sendRPCMessageRaw(inputJSON))
echo "Done!: ", self.callResult
proc onMessage*(self: NodeView, message: string) {.slot.} =

View File

@ -1,5 +1,5 @@
import NimQml
import ../../models/accounts as Models
import ../../models/accounts
import ../../signals/types
import eventemitter
import view
@ -8,12 +8,16 @@ type OnboardingController* = ref object of SignalSubscriber
view*: OnboardingView
variant*: QVariant
model*: AccountModel
appEvents*: EventEmitter
proc newController*(model: AccountModel): OnboardingController =
proc newController*(appEvents: EventEmitter): OnboardingController =
result = OnboardingController()
result.model = model
result.appEvents = appEvents
result.model = newAccountModel()
result.view = newOnboardingView(result.model)
result.variant = newQVariant(result.view)
result.model.events.on("accountsReady") do(a: Args):
appEvents.emit("accountsReady", a)
proc delete*(self: OnboardingController) =
delete self.view

View File

@ -1,4 +1,5 @@
import NimQml
import eventemitter
import strformat
import json
import "../../status/core" as status
@ -11,9 +12,11 @@ import ../../models/profile
type ProfileController* = ref object of SignalSubscriber
view*: ProfileView
variant*: QVariant
appEvents*: EventEmitter
proc newController*(): ProfileController =
proc newController*(appEvents: EventEmitter): ProfileController =
result = ProfileController()
result.appEvents = appEvents
result.view = newProfileView()
result.variant = newQVariant(result.view)

View File

@ -12,10 +12,12 @@ type WalletController* = ref object of SignalSubscriber
model: WalletModel
view*: WalletView
variant*: QVariant
appEvents*: EventEmitter
proc newController*(events: EventEmitter): WalletController =
proc newController*(appEvents: EventEmitter): WalletController =
result = WalletController()
result.model = newWalletModel(events)
result.appEvents = appEvents
result.model = newWalletModel()
result.view = newWalletView(result.model)
result.variant = newQVariant(result.view)

View File

@ -18,10 +18,10 @@ type
events*: EventEmitter
channels*: HashSet[string]
proc newChatModel*(events: EventEmitter): ChatModel =
proc newChatModel*(): ChatModel =
result = ChatModel()
result.events = createEventEmitter()
result.channels = initHashSet[string]()
result.events = events
proc delete*(self: ChatModel) =
discard

19
src/models/node.nim Normal file
View File

@ -0,0 +1,19 @@
import eventemitter
# import json
# import strformat
# import strutils
import "../status/core" as status
type NodeModel* = ref object
events*: EventEmitter
proc newNodeModel*(): NodeModel =
result = NodeModel()
result.events = createEventEmitter()
proc delete*(self: NodeModel) =
discard
proc sendRPCMessageRaw*(self: NodeModel, msg: string): string =
echo "sending RPC message"
status.callPrivateRPCRaw(msg)

View File

@ -10,9 +10,9 @@ type Asset* = ref object
type WalletModel* = ref object
events*: EventEmitter
proc newWalletModel*(events: EventEmitter): WalletModel =
proc newWalletModel*(): WalletModel =
result = WalletModel()
result.events = events
result.events = createEventEmitter()
proc delete*(self: WalletModel) =
discard

View File

@ -9,6 +9,7 @@ import app/onboarding/core as onboarding
import state
import json
import status/accounts as status_accounts
import status/core as status_core
import status/chat as status_chat
import status/types as types
import status/libstatus
@ -27,7 +28,7 @@ proc mainProc() =
let app = newQApplication()
let engine = newQQmlApplicationEngine()
let signalController = signals.newController(app)
let events = createEventEmitter()
let appEvents = createEventEmitter()
defer: # Defer will run this just before mainProc() function ends
app.delete()
@ -41,28 +42,29 @@ proc mainProc() =
var appState = state.newAppState()
debug "Application State", title=appState.title
var wallet = wallet.newController(events)
var wallet = wallet.newController(appEvents)
engine.setRootContextProperty("assetsModel", wallet.variant)
var chat = chat.newController(events)
var chat = chat.newController(appEvents)
chat.init()
engine.setRootContextProperty("chatsModel", chat.variant)
var node = node.newController()
var node = node.newController(appEvents)
node.init()
engine.setRootContextProperty("nodeModel", node.variant)
var profile = profile.newController()
var profile = profile.newController(appEvents)
engine.setRootContextProperty("profileModel", profile.variant)
var accountsModel = newAccountModel()
accountsModel.events.on("accountsReady") do(a: Args):
# var accountsModel = newAccountModel()
appEvents.on("accountsReady") do(a: Args):
var args = AccountArgs(a)
status_chat.startMessenger()
status_core.startMessenger()
wallet.init()
profile.init(args.account) # TODO: use correct account
var onboarding = onboarding.newController(accountsModel)
# var onboarding = onboarding.newController(accountsModel)
var onboarding = onboarding.newController(appEvents)
onboarding.init()
engine.setRootContextProperty("onboardingModel", onboarding.variant)
@ -78,7 +80,8 @@ proc mainProc() =
chat.load(channel.name)
)
accountsModel.events.on("accountsReady") do(a: Args):
# accountsModel.appEvents.on("accountsReady") do(a: Args):
appEvents.on("accountsReady") do(a: Args):
appState.addChannel("test")
appState.addChannel("test2")
appState.addChannel("status")

View File

@ -8,15 +8,7 @@ import os
import uuids
proc queryAccounts*(): string =
var payload = %* {
"jsonrpc": "2.0",
"method": "eth_accounts",
"params": [
[]
]
}
var response = callPrivateRPC($payload)
echo response
var response = callPrivateRPC("eth_accounts")
result = parseJson(response)["result"][0].getStr()
proc generateAddresses*(): string =

View File

@ -1,37 +1,17 @@
import libstatus
import core
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, oneToOne = false) =
let payload = %* {
"jsonrpc": "2.0",
"id": 3, #TODO:
"method": "loadFilters".prefix,
"params": [
discard callPrivateRPC("loadFilters".prefix, %* [
[{
"ChatID": chatId,
"OneToOne": oneToOne
}]
]
}
discard $libstatus.callPrivateRPC($payload)
])
proc saveChat*(chatId: string, oneToOne = false) =
let payload = %* {
"jsonrpc": "2.0",
"id": 4,
"method": "saveChat".prefix,
"params": [ #TODO: determine where do these values come from
discard callPrivateRPC("saveChat".prefix, %* [
{
"lastClockValue": 0,
"color": "#51d0f0",
@ -43,29 +23,13 @@ proc saveChat*(chatId: string, oneToOne = false) =
"chatType": if oneToOne: 1 else: 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
discard callPrivateRPC("chatMessages".prefix, %* [chatId, nil, 20])
proc sendChatMessage*(chatId: string, msg: string): string =
let payload = %* {
"jsonrpc": "2.0",
"id": 40,
"method": "sendChatMessage".prefix,
"params": [
callPrivateRPC("sendChatMessage".prefix, %* [
{
"chatId": chatId,
"text": msg,
@ -74,6 +38,4 @@ proc sendChatMessage*(chatId: string, msg: string): string =
"sticker": nil,
"contentType": 1
}
]
}
$libstatus.callPrivateRPC($payload)
])

View File

@ -1,16 +1,30 @@
import json
import libstatus
import chat
import nimcrypto
proc startMessenger*() =
chat.startMessenger()
import utils
proc callRPC*(inputJSON: string): string =
return $libstatus.callRPC(inputJSON)
proc callPrivateRPC*(inputJSON: string): string =
proc callPrivateRPCRaw*(inputJSON: string): string =
return $libstatus.callPrivateRPC(inputJSON)
proc callPrivateRPC*(methodName: string, payload = %* []): string =
try:
let inputJSON = %* {
"jsonrpc": "2.0",
"method": methodName,
"params": %payload
}
echo inputJSON
result = $libstatus.callPrivateRPC($inputJSON)
except:
echo "error doing rpc request"
echo methodName
proc sendTransaction*(inputJSON: string, password: string): string =
var hashed_password = "0x" & $keccak_256.digest(password)
return $libstatus.sendTransaction(inputJSON, hashed_password)
proc startMessenger*() =
discard callPrivateRPC("startMessenger".prefix)

View File

@ -7,14 +7,7 @@ import stint
import strutils
proc getAccounts*(): seq[string] =
var payload = %* {
"jsonrpc": "2.0",
"method": "eth_accounts",
"params": [
[]
]
}
var response = status.callPrivateRPC($payload)
var response = callPrivateRPC("eth_accounts")
result = parseJson(response)["result"].to(seq[string])
proc getAccount*(): string =
@ -39,16 +32,8 @@ proc getPrice*(crypto: string, fiat: string): string =
$parseJson(response.body)["USD"]
proc getBalance*(address: string): string =
let payload = %* {
"jsonrpc": "2.0",
"id": 50,
"method": "eth_getBalance",
"params": [
address,
"latest"
]
}
parseJson(status.callPrivateRPC($payload))["result"].str
let payload = %* [address, "latest"]
parseJson(status.callPrivateRPC("eth_getBalance", payload))["result"].str
proc hex2Eth*(input: string): string =
var value = fromHex(Stuint[256], input)