From df1199060d1d1d6d8510e7f93f15b95a035cb57e Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 18 May 2020 16:06:04 -0400 Subject: [PATCH] Apply polymorphism to signals, instead of using json strings --- src/app/chat/core.nim | 7 ++--- src/app/signals/core.nim | 10 +++----- src/app/signals/messages.nim | 30 ++++++++++++++++++++-- src/app/signals/signalSubscriber.nim | 35 ------------------------- src/app/signals/types.nim | 38 ++++++++++++++++++++++++++++ src/app/wallet/core.nim | 4 +-- 6 files changed, 75 insertions(+), 49 deletions(-) delete mode 100644 src/app/signals/signalSubscriber.nim create mode 100644 src/app/signals/types.nim diff --git a/src/app/chat/core.nim b/src/app/chat/core.nim index 65d8c875ee..f3fbe5a1f8 100644 --- a/src/app/chat/core.nim +++ b/src/app/chat/core.nim @@ -1,7 +1,7 @@ import NimQml import ../../status/chat as status_chat import chatView -import ../signals/signalSubscriber +import ../signals/types var sendMessage = proc (msg: string): string = echo "sending public message" @@ -40,5 +40,6 @@ proc load*(self: ChatController): seq[string] = result = @["test"] method onSignal(self: ChatController, data: Signal) = - echo "Received a signal in the chat module" - # result.view.pushMessage(signal) \ No newline at end of file + var chatSignal = cast[ChatSignal](data) + for message in chatSignal.messages: + self.view.pushMessage(message) diff --git a/src/app/signals/core.nim b/src/app/signals/core.nim index cc314b3201..7363e78cc0 100644 --- a/src/app/signals/core.nim +++ b/src/app/signals/core.nim @@ -2,7 +2,7 @@ import NimQml import ../../status/types as status_types import tables import json -import signalSubscriber +import types import messages QtObject: @@ -21,7 +21,6 @@ QtObject: result.setup() result.variant = newQVariant(result) - proc setup(self: SignalsController) = self.QObject.setup @@ -37,15 +36,12 @@ QtObject: proc processSignal(self: SignalsController) = let jsonSignal = (self.statusSignal).parseJson let signalType = $jsonSignal["type"].getStr - # TODO: ideally the signal would receive an object - # formatted for easier usage so the controllers dont - # have to parse the signal themselves + case signalType: of "messages.new": self.signalSubscribers[SignalType.Message].onSignal(messages.fromEvent(jsonSignal)) of "wallet": - var wMsg = WalletMessage(content: $jsonSignal) - self.signalSubscribers[SignalType.Wallet].onSignal(wMsg) + self.signalSubscribers[SignalType.Wallet].onSignal(WalletSignal(content: $jsonSignal)) else: # TODO: log error? discard diff --git a/src/app/signals/messages.nim b/src/app/signals/messages.nim index 3ec2b83c8f..18ea0abf98 100644 --- a/src/app/signals/messages.nim +++ b/src/app/signals/messages.nim @@ -1,5 +1,31 @@ import json -import signalSubscriber +import types proc fromEvent*(event: JsonNode): Signal = - result = Message() \ No newline at end of file + 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 + ) + + signal.messages.add(msg) + + result = signal \ No newline at end of file diff --git a/src/app/signals/signalSubscriber.nim b/src/app/signals/signalSubscriber.nim deleted file mode 100644 index 49cfd55b47..0000000000 --- a/src/app/signals/signalSubscriber.nim +++ /dev/null @@ -1,35 +0,0 @@ -type SignalSubscriber* = ref object of RootObj - -type Signal* = ref object of RootObj - -type WalletMessage* = ref object of Signal - content*: string - -type Message* = ref object of Signal - alias: string - chatId: string - clock: uint - # commandParameters: # ??? - contentType: uint # ??? - ensName: string # ??? - fromAuthor: string - id: string - identicon: string - lineCount: uint - localChatId: string - messageType: string # ??? - # parsedText: # ??? - # quotedMessage: # ??? - replace: string # ??? - responseTo: string # ??? - rtl: bool # ??? - seen: bool - # sticker: # ??? - text: string - timestamp: uint - whisperTimestamp: uint - - -# Override this method -method onSignal*(self: SignalSubscriber, data: Signal) {.base.} = - echo "Received a signal" # TODO: log signal received \ No newline at end of file diff --git a/src/app/signals/types.nim b/src/app/signals/types.nim new file mode 100644 index 0000000000..32a3bff456 --- /dev/null +++ b/src/app/signals/types.nim @@ -0,0 +1,38 @@ +type SignalSubscriber* = ref object of RootObj + +type Signal* = ref object of RootObj + +type WalletSignal* = ref object of Signal + content*: string + +type Message* = object + alias*: string + chatId*: string + clock*: string + # commandParameters*: # ??? + contentType*: int # ??? + ensName*: string # ??? + fromAuthor*: string + id*: string + identicon*: string + lineCount*: int + localChatId*: string + messageType*: string # ??? + # parsedText: # ??? + # quotedMessage: # ??? + replace*: string # ??? + responseTo*: string # ??? + rtl*: bool # ??? + seen*: bool + # sticker: # ??? + text*: string + timestamp*: string + whisperTimestamp*: string + + +type ChatSignal* = ref object of Signal + messages*: seq[Message] + +# Override this method +method onSignal*(self: SignalSubscriber, data: Signal) {.base.} = + echo "Received a signal" # TODO: log signal received \ No newline at end of file diff --git a/src/app/wallet/core.nim b/src/app/wallet/core.nim index 4d338dc01b..503e65554c 100644 --- a/src/app/wallet/core.nim +++ b/src/app/wallet/core.nim @@ -4,7 +4,7 @@ import strutils import walletView import ../../status/wallet as status_wallet -import ../signals/signalSubscriber +import ../signals/types type WalletController* = ref object of SignalSubscriber view*: WalletView @@ -41,5 +41,5 @@ proc init*(self: WalletController) = self.view.addAssetToList("Ethereum", symbol, fmt"{eth_value:.6}", "$" & fmt"{usd_balance:.6}", fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg") method onSignal(self: WalletController, data: Signal) = - var msg = cast[WalletMessage](data) + var msg = cast[WalletSignal](data) self.view.setLastMessage(msg.content) \ No newline at end of file