mirror of
https://github.com/status-im/status-lib.git
synced 2025-02-05 17:14:32 +00:00
refactor: signals (#1)
This commit is contained in:
parent
0b24d7a341
commit
a044a941e6
@ -4,7 +4,6 @@ from status_go import multiAccountGenerateAndDeriveAddresses, generateAlias, ide
|
|||||||
import core
|
import core
|
||||||
import ../utils as utils
|
import ../utils as utils
|
||||||
import ../types/[account, fleet, rpc_response]
|
import ../types/[account, fleet, rpc_response]
|
||||||
import ../signals/[base]
|
|
||||||
import accounts/constants
|
import accounts/constants
|
||||||
|
|
||||||
proc getNetworkConfig(currentNetwork: string): JsonNode =
|
proc getNetworkConfig(currentNetwork: string): JsonNode =
|
||||||
|
@ -7,8 +7,7 @@ import
|
|||||||
import
|
import
|
||||||
./core, ./accounts/constants, ../utils
|
./core, ./accounts/constants, ../utils
|
||||||
|
|
||||||
import ../types/[setting, network, fleet]
|
import ../types/[setting, network, fleet, rpc_response]
|
||||||
import ../signals/[base]
|
|
||||||
|
|
||||||
from status_go import nil
|
from status_go import nil
|
||||||
|
|
||||||
|
@ -5,8 +5,7 @@ import
|
|||||||
|
|
||||||
import libstatus/settings as libstatus_settings
|
import libstatus/settings as libstatus_settings
|
||||||
import ../eventemitter
|
import ../eventemitter
|
||||||
import ./types/[fleet, network, setting]
|
import ./types/[fleet, network, setting, rpc_response]
|
||||||
import ./signals/[base]
|
|
||||||
|
|
||||||
type
|
type
|
||||||
SettingsModel* = ref object
|
SettingsModel* = ref object
|
||||||
|
26
status/signals.nim
Normal file
26
status/signals.nim
Normal 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()
|
@ -1,5 +1,8 @@
|
|||||||
|
import json
|
||||||
import json_serialization
|
import json_serialization
|
||||||
import signal_type
|
import signal_type
|
||||||
|
import ../status
|
||||||
|
import ../types/rpc_response
|
||||||
|
|
||||||
import ../../eventemitter
|
import ../../eventemitter
|
||||||
|
|
||||||
@ -7,9 +10,7 @@ export signal_type
|
|||||||
|
|
||||||
type Signal* = ref object of Args
|
type Signal* = ref object of Args
|
||||||
signalType* {.serializedFieldName("type").}: SignalType
|
signalType* {.serializedFieldName("type").}: SignalType
|
||||||
|
signalTypeStr*: string
|
||||||
type StatusGoError* = object
|
|
||||||
error*: string
|
|
||||||
|
|
||||||
type NodeSignal* = ref object of Signal
|
type NodeSignal* = ref object of Signal
|
||||||
event*: StatusGoError
|
event*: StatusGoError
|
@ -3,11 +3,12 @@ import json
|
|||||||
import base
|
import base
|
||||||
|
|
||||||
import status/types/community
|
import status/types/community
|
||||||
|
import signal_type
|
||||||
|
|
||||||
type CommunitySignal* = ref object of Signal
|
type CommunitySignal* = ref object of Signal
|
||||||
community*: Community
|
community*: Community
|
||||||
|
|
||||||
proc fromEvent*(event: JsonNode): Signal =
|
proc fromEvent*(T: type CommunitySignal, event: JsonNode): CommunitySignal =
|
||||||
var signal: CommunitySignal = CommunitySignal()
|
result = CommunitySignal()
|
||||||
signal.community = event["event"].toCommunity()
|
result.signalType = SignalType.CommunityFound
|
||||||
result = signal
|
result.community = event["event"].toCommunity()
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
import base
|
import base
|
||||||
|
import signal_type
|
||||||
|
|
||||||
type DiscoverySummarySignal* = ref object of Signal
|
type DiscoverySummarySignal* = ref object of Signal
|
||||||
enodes*: seq[string]
|
enodes*: seq[string]
|
||||||
|
|
||||||
proc fromEvent*(jsonSignal: JsonNode): Signal =
|
proc fromEvent*(T: type DiscoverySummarySignal, jsonSignal: JsonNode): DiscoverySummarySignal =
|
||||||
var signal:DiscoverySummarySignal = DiscoverySummarySignal()
|
result = DiscoverySummarySignal()
|
||||||
|
result.signalType = SignalType.DiscoverySummary
|
||||||
if jsonSignal["event"].kind != JNull:
|
if jsonSignal["event"].kind != JNull:
|
||||||
for discoveryItem in jsonSignal["event"]:
|
for discoveryItem in jsonSignal["event"]:
|
||||||
signal.enodes.add(discoveryItem["enode"].getStr)
|
result.enodes.add(discoveryItem["enode"].getStr)
|
||||||
result = signal
|
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
import base
|
import base
|
||||||
|
import signal_type
|
||||||
|
|
||||||
type EnvelopeSentSignal* = ref object of Signal
|
type EnvelopeSentSignal* = ref object of Signal
|
||||||
messageIds*: seq[string]
|
messageIds*: seq[string]
|
||||||
|
|
||||||
proc fromEvent*(jsonSignal: JsonNode): Signal =
|
proc fromEvent*(T: type EnvelopeSentSignal, jsonSignal: JsonNode): EnvelopeSentSignal =
|
||||||
var signal:EnvelopeSentSignal = EnvelopeSentSignal()
|
result = EnvelopeSentSignal()
|
||||||
|
result.signalType = SignalType.EnvelopeSent
|
||||||
if jsonSignal["event"].kind != JNull and jsonSignal["event"].hasKey("ids") and jsonSignal["event"]["ids"].kind != JNull:
|
if jsonSignal["event"].kind != JNull and jsonSignal["event"].hasKey("ids") and jsonSignal["event"]["ids"].kind != JNull:
|
||||||
for messageId in jsonSignal["event"]["ids"]:
|
for messageId in jsonSignal["event"]["ids"]:
|
||||||
signal.messageIds.add(messageId.getStr)
|
result.messageIds.add(messageId.getStr)
|
||||||
result = signal
|
|
||||||
|
|
@ -1,14 +1,15 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
import base
|
import base
|
||||||
|
import signal_type
|
||||||
|
|
||||||
type EnvelopeExpiredSignal* = ref object of Signal
|
type EnvelopeExpiredSignal* = ref object of Signal
|
||||||
messageIds*: seq[string]
|
messageIds*: seq[string]
|
||||||
|
|
||||||
proc fromEvent*(jsonSignal: JsonNode): Signal =
|
proc fromEvent*(T: type EnvelopeExpiredSignal, jsonSignal: JsonNode): EnvelopeExpiredSignal =
|
||||||
var signal:EnvelopeExpiredSignal = EnvelopeExpiredSignal()
|
result = EnvelopeExpiredSignal()
|
||||||
|
result.signalType = SignalType.EnvelopeExpired
|
||||||
if jsonSignal["event"].kind != JNull and jsonSignal["event"].hasKey("ids") and jsonSignal["event"]["ids"].kind != JNull:
|
if jsonSignal["event"].kind != JNull and jsonSignal["event"].hasKey("ids") and jsonSignal["event"]["ids"].kind != JNull:
|
||||||
for messageId in jsonSignal["event"]["ids"]:
|
for messageId in jsonSignal["event"]["ids"]:
|
||||||
signal.messageIds.add(messageId.getStr)
|
result.messageIds.add(messageId.getStr)
|
||||||
result = signal
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
import base
|
import base
|
||||||
|
import signal_type
|
||||||
|
|
||||||
type MailserverRequestCompletedSignal* = ref object of Signal
|
type MailserverRequestCompletedSignal* = ref object of Signal
|
||||||
requestID*: string
|
requestID*: string
|
||||||
@ -12,18 +13,17 @@ type MailserverRequestCompletedSignal* = ref object of Signal
|
|||||||
type MailserverRequestExpiredSignal* = ref object of Signal
|
type MailserverRequestExpiredSignal* = ref object of Signal
|
||||||
# TODO
|
# TODO
|
||||||
|
|
||||||
proc fromCompletedEvent*(jsonSignal: JsonNode): Signal =
|
proc fromEvent*(T: type MailserverRequestCompletedSignal, jsonSignal: JsonNode): MailserverRequestCompletedSignal =
|
||||||
var signal:MailserverRequestCompletedSignal = MailserverRequestCompletedSignal()
|
result = MailserverRequestCompletedSignal()
|
||||||
|
result.signalType = SignalType.MailserverRequestCompleted
|
||||||
if jsonSignal["event"].kind != JNull:
|
if jsonSignal["event"].kind != JNull:
|
||||||
signal.requestID = jsonSignal["event"]{"requestID"}.getStr()
|
result.requestID = jsonSignal["event"]{"requestID"}.getStr()
|
||||||
signal.lastEnvelopeHash = jsonSignal["event"]{"lastEnvelopeHash"}.getStr()
|
result.lastEnvelopeHash = jsonSignal["event"]{"lastEnvelopeHash"}.getStr()
|
||||||
signal.cursor = jsonSignal["event"]{"cursor"}.getStr()
|
result.cursor = jsonSignal["event"]{"cursor"}.getStr()
|
||||||
signal.errorMessage = jsonSignal["event"]{"errorMessage"}.getStr()
|
result.errorMessage = jsonSignal["event"]{"errorMessage"}.getStr()
|
||||||
signal.error = signal.errorMessage != ""
|
result.error = result.errorMessage != ""
|
||||||
result = signal
|
|
||||||
|
|
||||||
proc fromExpiredEvent*(jsonSignal: JsonNode): Signal =
|
proc fromEvent*(T: type MailserverRequestExpiredSignal, jsonSignal: JsonNode): MailserverRequestExpiredSignal =
|
||||||
var signal:MailserverRequestExpiredSignal = MailserverRequestExpiredSignal()
|
|
||||||
# TODO: parse signal
|
# TODO: parse signal
|
||||||
result = signal
|
result = MailserverRequestExpiredSignal()
|
||||||
|
result.signalType = SignalType.MailserverRequestExpired
|
@ -1,4 +1,4 @@
|
|||||||
import json, chronicles
|
import json
|
||||||
|
|
||||||
import base
|
import base
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ type MessageSignal* = ref object of Signal
|
|||||||
statusUpdates*: seq[StatusUpdate]
|
statusUpdates*: seq[StatusUpdate]
|
||||||
deletedMessages*: seq[RemovedMessage]
|
deletedMessages*: seq[RemovedMessage]
|
||||||
|
|
||||||
proc fromEvent*(event: JsonNode): Signal =
|
proc fromEvent*(T: type MessageSignal, event: JsonNode): MessageSignal =
|
||||||
var signal:MessageSignal = MessageSignal()
|
var signal:MessageSignal = MessageSignal()
|
||||||
signal.messages = @[]
|
signal.messages = @[]
|
||||||
signal.contacts = @[]
|
signal.contacts = @[]
|
||||||
@ -78,7 +78,6 @@ proc fromEvent*(event: JsonNode): Signal =
|
|||||||
try:
|
try:
|
||||||
contentType = ContentType(jsonPinnedMessage{"contentType"}.getInt)
|
contentType = ContentType(jsonPinnedMessage{"contentType"}.getInt)
|
||||||
except:
|
except:
|
||||||
warn "Unknown content type received", type = jsonPinnedMessage{"contentType"}.getInt
|
|
||||||
contentType = ContentType.Message
|
contentType = ContentType.Message
|
||||||
signal.pinnedMessages.add(Message(
|
signal.pinnedMessages.add(Message(
|
||||||
id: jsonPinnedMessage{"message_id"}.getStr,
|
id: jsonPinnedMessage{"message_id"}.getStr,
|
||||||
@ -93,3 +92,4 @@ proc fromEvent*(event: JsonNode): Signal =
|
|||||||
))
|
))
|
||||||
|
|
||||||
result = signal
|
result = signal
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
import base
|
import base
|
||||||
|
import signal_type
|
||||||
|
|
||||||
type Stats* = object
|
type Stats* = object
|
||||||
uploadRate*: uint64
|
uploadRate*: uint64
|
||||||
@ -15,7 +16,7 @@ proc toStats(jsonMsg: JsonNode): Stats =
|
|||||||
downloadRate: uint64(jsonMsg{"downloadRate"}.getBiggestInt())
|
downloadRate: uint64(jsonMsg{"downloadRate"}.getBiggestInt())
|
||||||
)
|
)
|
||||||
|
|
||||||
proc fromEvent*(event: JsonNode): Signal =
|
proc fromEvent*(T: type StatsSignal, event: JsonNode): StatsSignal =
|
||||||
var signal:StatsSignal = StatsSignal()
|
result = StatsSignal()
|
||||||
signal.stats = event["event"].toStats
|
result.signalType = SignalType.Stats
|
||||||
result = signal
|
result.stats = event["event"].toStats
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
import base
|
import base
|
||||||
|
import signal_type
|
||||||
|
|
||||||
type WalletSignal* = ref object of Signal
|
type WalletSignal* = ref object of Signal
|
||||||
content*: string
|
content*: string
|
||||||
@ -10,16 +11,16 @@ type WalletSignal* = ref object of Signal
|
|||||||
# newTransactions*: ???
|
# newTransactions*: ???
|
||||||
erc20*: bool
|
erc20*: bool
|
||||||
|
|
||||||
proc fromEvent*(jsonSignal: JsonNode): Signal =
|
proc fromEvent*(T: type WalletSignal, jsonSignal: JsonNode): WalletSignal =
|
||||||
var signal:WalletSignal = WalletSignal()
|
result = WalletSignal()
|
||||||
signal.content = $jsonSignal
|
result.signalType = SignalType.Wallet
|
||||||
|
result.content = $jsonSignal
|
||||||
if jsonSignal["event"].kind != JNull:
|
if jsonSignal["event"].kind != JNull:
|
||||||
signal.eventType = jsonSignal["event"]["type"].getStr
|
result.eventType = jsonSignal["event"]["type"].getStr
|
||||||
signal.blockNumber = jsonSignal["event"]{"blockNumber"}.getInt
|
result.blockNumber = jsonSignal["event"]{"blockNumber"}.getInt
|
||||||
signal.erc20 = jsonSignal["event"]{"erc20"}.getBool
|
result.erc20 = jsonSignal["event"]{"erc20"}.getBool
|
||||||
signal.accounts = @[]
|
result.accounts = @[]
|
||||||
if jsonSignal["event"]["accounts"].kind != JNull:
|
if jsonSignal["event"]["accounts"].kind != JNull:
|
||||||
for account in jsonSignal["event"]["accounts"]:
|
for account in jsonSignal["event"]["accounts"]:
|
||||||
signal.accounts.add(account.getStr)
|
result.accounts.add(account.getStr)
|
||||||
result = signal
|
|
||||||
|
|
@ -23,11 +23,7 @@ proc toFilter(jsonMsg: JsonNode): Filter =
|
|||||||
topic: jsonMsg{"topic"}.getStr,
|
topic: jsonMsg{"topic"}.getStr,
|
||||||
)
|
)
|
||||||
|
|
||||||
proc fromEvent*(event: JsonNode): Signal =
|
proc fromEvent*(T: type WhisperFilterSignal, event: JsonNode): WhisperFilterSignal =
|
||||||
var signal:WhisperFilterSignal = WhisperFilterSignal()
|
|
||||||
|
|
||||||
if event["event"]{"filters"} != nil:
|
if event["event"]{"filters"} != nil:
|
||||||
for jsonMsg in event["event"]["filters"]:
|
for jsonMsg in event["event"]["filters"]:
|
||||||
signal.filters.add(jsonMsg.toFilter)
|
result.filters.add(jsonMsg.toFilter)
|
||||||
|
|
||||||
result = signal
|
|
||||||
|
@ -22,6 +22,9 @@ type
|
|||||||
id*: int
|
id*: int
|
||||||
error*: RpcError
|
error*: RpcError
|
||||||
|
|
||||||
|
type StatusGoError* = object
|
||||||
|
error*: string
|
||||||
|
|
||||||
type
|
type
|
||||||
StatusGoException* = object of CatchableError
|
StatusGoException* = object of CatchableError
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user