refactor: signals (#1)

This commit is contained in:
RichΛrd 2021-09-08 16:55:37 -04:00 committed by GitHub
parent 0b24d7a341
commit a044a941e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 89 additions and 60 deletions

View File

@ -4,7 +4,6 @@ from status_go import multiAccountGenerateAndDeriveAddresses, generateAlias, ide
import core
import ../utils as utils
import ../types/[account, fleet, rpc_response]
import ../signals/[base]
import accounts/constants
proc getNetworkConfig(currentNetwork: string): JsonNode =

View File

@ -7,8 +7,7 @@ import
import
./core, ./accounts/constants, ../utils
import ../types/[setting, network, fleet]
import ../signals/[base]
import ../types/[setting, network, fleet, rpc_response]
from status_go import nil

View File

@ -5,8 +5,7 @@ import
import libstatus/settings as libstatus_settings
import ../eventemitter
import ./types/[fleet, network, setting]
import ./signals/[base]
import ./types/[fleet, network, setting, rpc_response]
type
SettingsModel* = ref object

26
status/signals.nim Normal file
View File

@ -0,0 +1,26 @@
import json, json_serialization, strutils
import signals/[base, community, discovery_summary, envelope, expired, mailserver, messages, signal_type, stats, wallet, whisper_filter]
export base, community, discovery_summary, envelope, expired, mailserver, messages, signal_type, stats, wallet, whisper_filter
proc decode*(jsonSignal: JsonNode): Signal =
let signalString = jsonSignal{"type"}.getStr
var signalType: SignalType
try:
signalType = parseEnum[SignalType](signalString)
except:
raise newException(ValueError, "Unknown signal received: " & signalString)
result = case signalType:
of SignalType.Message: MessageSignal.fromEvent(jsonSignal)
of SignalType.EnvelopeSent: EnvelopeSentSignal.fromEvent(jsonSignal)
of SignalType.EnvelopeExpired: EnvelopeExpiredSignal.fromEvent(jsonSignal)
of SignalType.WhisperFilterAdded: WhisperFilterSignal.fromEvent(jsonSignal)
of SignalType.Wallet: WalletSignal.fromEvent(jsonSignal)
of SignalType.NodeLogin: Json.decode($jsonSignal, NodeSignal)
of SignalType.DiscoverySummary: DiscoverySummarySignal.fromEvent(jsonSignal)
of SignalType.MailserverRequestCompleted: MailserverRequestCompletedSignal.fromEvent(jsonSignal)
of SignalType.MailserverRequestExpired: MailserverRequestExpiredSignal.fromEvent(jsonSignal)
of SignalType.CommunityFound: CommunitySignal.fromEvent(jsonSignal)
of SignalType.Stats: StatsSignal.fromEvent(jsonSignal)
else: Signal()

View File

@ -1,5 +1,8 @@
import json
import json_serialization
import signal_type
import ../status
import ../types/rpc_response
import ../../eventemitter
@ -7,9 +10,7 @@ export signal_type
type Signal* = ref object of Args
signalType* {.serializedFieldName("type").}: SignalType
type StatusGoError* = object
error*: string
signalTypeStr*: string
type NodeSignal* = ref object of Signal
event*: StatusGoError
event*: StatusGoError

View File

@ -3,11 +3,12 @@ import json
import base
import status/types/community
import signal_type
type CommunitySignal* = ref object of Signal
community*: Community
proc fromEvent*(event: JsonNode): Signal =
var signal: CommunitySignal = CommunitySignal()
signal.community = event["event"].toCommunity()
result = signal
proc fromEvent*(T: type CommunitySignal, event: JsonNode): CommunitySignal =
result = CommunitySignal()
result.signalType = SignalType.CommunityFound
result.community = event["event"].toCommunity()

View File

@ -1,13 +1,14 @@
import json
import base
import signal_type
type DiscoverySummarySignal* = ref object of Signal
enodes*: seq[string]
proc fromEvent*(jsonSignal: JsonNode): Signal =
var signal:DiscoverySummarySignal = DiscoverySummarySignal()
proc fromEvent*(T: type DiscoverySummarySignal, jsonSignal: JsonNode): DiscoverySummarySignal =
result = DiscoverySummarySignal()
result.signalType = SignalType.DiscoverySummary
if jsonSignal["event"].kind != JNull:
for discoveryItem in jsonSignal["event"]:
signal.enodes.add(discoveryItem["enode"].getStr)
result = signal
result.enodes.add(discoveryItem["enode"].getStr)

View File

@ -1,14 +1,15 @@
import json
import base
import signal_type
type EnvelopeSentSignal* = ref object of Signal
messageIds*: seq[string]
proc fromEvent*(jsonSignal: JsonNode): Signal =
var signal:EnvelopeSentSignal = EnvelopeSentSignal()
proc fromEvent*(T: type EnvelopeSentSignal, jsonSignal: JsonNode): EnvelopeSentSignal =
result = EnvelopeSentSignal()
result.signalType = SignalType.EnvelopeSent
if jsonSignal["event"].kind != JNull and jsonSignal["event"].hasKey("ids") and jsonSignal["event"]["ids"].kind != JNull:
for messageId in jsonSignal["event"]["ids"]:
signal.messageIds.add(messageId.getStr)
result = signal
result.messageIds.add(messageId.getStr)

View File

@ -1,14 +1,15 @@
import json
import base
import signal_type
type EnvelopeExpiredSignal* = ref object of Signal
messageIds*: seq[string]
proc fromEvent*(jsonSignal: JsonNode): Signal =
var signal:EnvelopeExpiredSignal = EnvelopeExpiredSignal()
proc fromEvent*(T: type EnvelopeExpiredSignal, jsonSignal: JsonNode): EnvelopeExpiredSignal =
result = EnvelopeExpiredSignal()
result.signalType = SignalType.EnvelopeExpired
if jsonSignal["event"].kind != JNull and jsonSignal["event"].hasKey("ids") and jsonSignal["event"]["ids"].kind != JNull:
for messageId in jsonSignal["event"]["ids"]:
signal.messageIds.add(messageId.getStr)
result = signal
result.messageIds.add(messageId.getStr)

View File

@ -1,6 +1,7 @@
import json
import base
import signal_type
type MailserverRequestCompletedSignal* = ref object of Signal
requestID*: string
@ -12,18 +13,17 @@ type MailserverRequestCompletedSignal* = ref object of Signal
type MailserverRequestExpiredSignal* = ref object of Signal
# TODO
proc fromCompletedEvent*(jsonSignal: JsonNode): Signal =
var signal:MailserverRequestCompletedSignal = MailserverRequestCompletedSignal()
proc fromEvent*(T: type MailserverRequestCompletedSignal, jsonSignal: JsonNode): MailserverRequestCompletedSignal =
result = MailserverRequestCompletedSignal()
result.signalType = SignalType.MailserverRequestCompleted
if jsonSignal["event"].kind != JNull:
signal.requestID = jsonSignal["event"]{"requestID"}.getStr()
signal.lastEnvelopeHash = jsonSignal["event"]{"lastEnvelopeHash"}.getStr()
signal.cursor = jsonSignal["event"]{"cursor"}.getStr()
signal.errorMessage = jsonSignal["event"]{"errorMessage"}.getStr()
signal.error = signal.errorMessage != ""
result = signal
result.requestID = jsonSignal["event"]{"requestID"}.getStr()
result.lastEnvelopeHash = jsonSignal["event"]{"lastEnvelopeHash"}.getStr()
result.cursor = jsonSignal["event"]{"cursor"}.getStr()
result.errorMessage = jsonSignal["event"]{"errorMessage"}.getStr()
result.error = result.errorMessage != ""
proc fromExpiredEvent*(jsonSignal: JsonNode): Signal =
var signal:MailserverRequestExpiredSignal = MailserverRequestExpiredSignal()
proc fromEvent*(T: type MailserverRequestExpiredSignal, jsonSignal: JsonNode): MailserverRequestExpiredSignal =
# TODO: parse signal
result = signal
result = MailserverRequestExpiredSignal()
result.signalType = SignalType.MailserverRequestExpired

View File

@ -1,4 +1,4 @@
import json, chronicles
import json
import base
@ -18,7 +18,7 @@ type MessageSignal* = ref object of Signal
statusUpdates*: seq[StatusUpdate]
deletedMessages*: seq[RemovedMessage]
proc fromEvent*(event: JsonNode): Signal =
proc fromEvent*(T: type MessageSignal, event: JsonNode): MessageSignal =
var signal:MessageSignal = MessageSignal()
signal.messages = @[]
signal.contacts = @[]
@ -78,7 +78,6 @@ proc fromEvent*(event: JsonNode): Signal =
try:
contentType = ContentType(jsonPinnedMessage{"contentType"}.getInt)
except:
warn "Unknown content type received", type = jsonPinnedMessage{"contentType"}.getInt
contentType = ContentType.Message
signal.pinnedMessages.add(Message(
id: jsonPinnedMessage{"message_id"}.getStr,
@ -92,4 +91,5 @@ proc fromEvent*(event: JsonNode): Signal =
contentType: contentType
))
result = signal
result = signal

View File

@ -1,6 +1,7 @@
import json
import base
import signal_type
type Stats* = object
uploadRate*: uint64
@ -15,7 +16,7 @@ proc toStats(jsonMsg: JsonNode): Stats =
downloadRate: uint64(jsonMsg{"downloadRate"}.getBiggestInt())
)
proc fromEvent*(event: JsonNode): Signal =
var signal:StatsSignal = StatsSignal()
signal.stats = event["event"].toStats
result = signal
proc fromEvent*(T: type StatsSignal, event: JsonNode): StatsSignal =
result = StatsSignal()
result.signalType = SignalType.Stats
result.stats = event["event"].toStats

View File

@ -1,6 +1,7 @@
import json
import base
import signal_type
type WalletSignal* = ref object of Signal
content*: string
@ -10,16 +11,16 @@ type WalletSignal* = ref object of Signal
# newTransactions*: ???
erc20*: bool
proc fromEvent*(jsonSignal: JsonNode): Signal =
var signal:WalletSignal = WalletSignal()
signal.content = $jsonSignal
proc fromEvent*(T: type WalletSignal, jsonSignal: JsonNode): WalletSignal =
result = WalletSignal()
result.signalType = SignalType.Wallet
result.content = $jsonSignal
if jsonSignal["event"].kind != JNull:
signal.eventType = jsonSignal["event"]["type"].getStr
signal.blockNumber = jsonSignal["event"]{"blockNumber"}.getInt
signal.erc20 = jsonSignal["event"]{"erc20"}.getBool
signal.accounts = @[]
result.eventType = jsonSignal["event"]["type"].getStr
result.blockNumber = jsonSignal["event"]{"blockNumber"}.getInt
result.erc20 = jsonSignal["event"]{"erc20"}.getBool
result.accounts = @[]
if jsonSignal["event"]["accounts"].kind != JNull:
for account in jsonSignal["event"]["accounts"]:
signal.accounts.add(account.getStr)
result = signal
result.accounts.add(account.getStr)

View File

@ -23,11 +23,7 @@ proc toFilter(jsonMsg: JsonNode): Filter =
topic: jsonMsg{"topic"}.getStr,
)
proc fromEvent*(event: JsonNode): Signal =
var signal:WhisperFilterSignal = WhisperFilterSignal()
proc fromEvent*(T: type WhisperFilterSignal, event: JsonNode): WhisperFilterSignal =
if event["event"]{"filters"} != nil:
for jsonMsg in event["event"]["filters"]:
signal.filters.add(jsonMsg.toFilter)
result = signal
result.filters.add(jsonMsg.toFilter)

View File

@ -22,6 +22,9 @@ type
id*: int
error*: RpcError
type StatusGoError* = object
error*: string
type
StatusGoException* = object of CatchableError