From 9aba9ae3c107cbe68132f07589adc0bca73e74c7 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 17 Jun 2021 09:51:59 -0400 Subject: [PATCH] refactor: extract ens code from chat view --- src/app/chat/view.nim | 59 ++++-------------- src/app/chat/views/ens.nim | 61 +++++++++++++++++++ .../Chat/components/ProfilePopup.qml | 2 +- ui/shared/ContactsListAndSearch.qml | 6 +- 4 files changed, 77 insertions(+), 51 deletions(-) create mode 100644 src/app/chat/views/ens.nim diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index 7994ff31ed..bebe4cbf74 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -9,7 +9,7 @@ import ../../status/ens as status_ens import ../../status/chat/[chat, message] import ../../status/profile/profile import web3/[conversions, ethtypes] -import views/[channels_list, message_list, chat_item, suggestions_list, reactions, stickers, groups, transactions, communities, community_list, community_item, format_input, activity_notification_list] +import views/[channels_list, message_list, chat_item, suggestions_list, reactions, stickers, groups, transactions, communities, community_list, community_item, format_input, ens, activity_notification_list] import ../utils/image_utils import ../../status/tasks/[qt, task_runner_impl] import ../../status/tasks/marathon/mailserver/worker @@ -31,8 +31,6 @@ type AsyncActivityNotificationLoadTaskArg = ref object of QObjectTaskArg AsyncMessageLoadTaskArg = ref object of QObjectTaskArg chatId: string - ResolveEnsTaskArg = ref object of QObjectTaskArg - ens: string const getLinkPreviewDataTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} = let arg = decode[GetLinkPreviewDataTaskArg](argEncoded) @@ -110,26 +108,12 @@ proc asyncActivityNotificationLoad[T](self: T, slot: string) = ) self.status.tasks.threadpool.start(arg) -const resolveEnsTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} = - let - arg = decode[ResolveEnsTaskArg](argEncoded) - output = %* { "address": status_ens.address(arg.ens), "pubkey": status_ens.pubkey(arg.ens) } - arg.finish(output) - -proc resolveEns[T](self: T, slot: string, ens: string) = - let arg = ResolveEnsTaskArg( - tptr: cast[ByteAddress](resolveEnsTask), - vptr: cast[ByteAddress](self.vptr), - slot: slot, - ens: ens - ) - self.status.tasks.threadpool.start(arg) - QtObject: type ChatsView* = ref object of QAbstractListModel status: Status formatInputView: FormatInputView + ensView: EnsView chats*: ChannelsList currentSuggestions*: SuggestionsList activityNotificationList*: ActivityNotificationList @@ -157,6 +141,7 @@ QtObject: proc delete(self: ChatsView) = self.chats.delete self.formatInputView.delete + self.ensView.delete self.activeChannel.delete self.contextChannel.delete self.currentSuggestions.delete @@ -179,6 +164,7 @@ QtObject: new(result, delete) result.status = status result.formatInputView = newFormatInputView() + result.ensView = newEnsView(status) result.connected = false result.chats = newChannelsList(status) @@ -204,6 +190,14 @@ QtObject: QtProperty[QVariant] formatInputView: read = getFormatInput + proc getEns(self: ChatsView): QVariant {.slot.} = newQVariant(self.ensView) + QtProperty[QVariant] ensView: + read = getEns + + proc getCommunities*(self: ChatsView): QVariant {.slot.} = newQVariant(self.communities) + QtProperty[QVariant] communities: + read = getCommunities + proc getMessageListIndexById(self: ChatsView, id: string): int proc getChannel*(self: ChatsView, index: int): Chat = @@ -243,12 +237,6 @@ QtObject: QtProperty[QVariant] chats: read = getChatsList - proc getCommunities*(self: ChatsView): QVariant {.slot.} = - newQVariant(self.communities) - - QtProperty[QVariant] communities: - read = getCommunities - proc getChannelColor*(self: ChatsView, channel: string): string {.slot.} = if (channel == ""): return let selectedChannel = self.getChannelById(channel) @@ -825,29 +813,6 @@ QtObject: proc deleteMessage*(self: ChatsView, channelId: string, messageId: string) = self.messageList[channelId].deleteMessage(messageId) - proc isEnsVerified*(self: ChatsView, id: string): bool {.slot.} = - if id == "": return false - let contact = self.status.contacts.getContactByID(id) - if contact == nil: - return false - result = contact.ensVerified - - proc formatENSUsername*(self: ChatsView, username: string): string {.slot.} = - result = status_ens.addDomain(username) - - # Resolving a ENS name - proc resolveENS*(self: ChatsView, ens: string) {.slot.} = - self.resolveEns("ensResolved", ens) # Call self.ensResolved(string) when ens is resolved - - proc ensWasResolved*(self: ChatsView, resolvedPubKey: string, resolvedAddress: string) {.signal.} - - proc ensResolved(self: ChatsView, addressPubkeyJson: string) {.slot.} = - var - parsed = addressPubkeyJson.parseJson - address = parsed["address"].to(string) - pubkey = parsed["pubkey"].to(string) - self.ensWasResolved(pubKey, address) - proc isConnected*(self: ChatsView): bool {.slot.} = result = self.status.network.isConnected diff --git a/src/app/chat/views/ens.nim b/src/app/chat/views/ens.nim new file mode 100644 index 0000000000..b404e4e91b --- /dev/null +++ b/src/app/chat/views/ens.nim @@ -0,0 +1,61 @@ +import NimQml, Tables, json, sequtils, chronicles, times, re, sugar, strutils, os, strformat, algorithm + +import ../../../status/[status, contacts] +import ../../../status/ens as status_ens +import ../../../status/tasks/[qt, task_runner_impl] + +logScope: + topics = "ens-view" + +type + ResolveEnsTaskArg = ref object of QObjectTaskArg + ens: string + +const resolveEnsTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} = + let + arg = decode[ResolveEnsTaskArg](argEncoded) + output = %* { "address": status_ens.address(arg.ens), "pubkey": status_ens.pubkey(arg.ens) } + arg.finish(output) + +proc resolveEns[T](self: T, slot: string, ens: string) = + let arg = ResolveEnsTaskArg( + tptr: cast[ByteAddress](resolveEnsTask), + vptr: cast[ByteAddress](self.vptr), + slot: slot, ens: ens + ) + self.status.tasks.threadpool.start(arg) + +QtObject: + type EnsView* = ref object of QObject + status: Status + + proc setup(self: EnsView) = self.QObject.setup + proc delete*(self: EnsView) = self.QObject.delete + + proc newEnsView*(status: Status): EnsView = + new(result, delete) + result.status = status + result.setup + + proc isEnsVerified*(self: EnsView, id: string): bool {.slot.} = + if id == "": return false + let contact = self.status.contacts.getContactByID(id) + if contact == nil: + return false + result = contact.ensVerified + + proc formatENSUsername*(self: EnsView, username: string): string {.slot.} = + result = status_ens.addDomain(username) + + # Resolving a ENS name + proc resolveENS*(self: EnsView, ens: string) {.slot.} = + self.resolveEns("ensResolved", ens) # Call self.ensResolved(string) when ens is resolved + + proc ensWasResolved*(self: EnsView, resolvedPubKey: string, resolvedAddress: string) {.signal.} + + proc ensResolved(self: EnsView, addressPubkeyJson: string) {.slot.} = + var + parsed = addressPubkeyJson.parseJson + address = parsed["address"].to(string) + pubkey = parsed["pubkey"].to(string) + self.ensWasResolved(pubKey, address) diff --git a/ui/app/AppLayouts/Chat/components/ProfilePopup.qml b/ui/app/AppLayouts/Chat/components/ProfilePopup.qml index 2b85fb8158..73f3689529 100644 --- a/ui/app/AppLayouts/Chat/components/ProfilePopup.qml +++ b/ui/app/AppLayouts/Chat/components/ProfilePopup.qml @@ -43,7 +43,7 @@ ModalPopup { fromAuthor = fromAuthorParam || "" identicon = identiconParam || "" text = textParam || "" - isEnsVerified = chatsModel.isEnsVerified(this.fromAuthor) + isEnsVerified = chatsModel.ensView.isEnsVerified(this.fromAuthor) isBlocked = profileModel.contacts.isContactBlocked(this.fromAuthor); alias = chatsModel.alias(this.fromAuthor) || "" diff --git a/ui/shared/ContactsListAndSearch.qml b/ui/shared/ContactsListAndSearch.qml index 7f2f4dc8a6..b960cec162 100644 --- a/ui/shared/ContactsListAndSearch.qml +++ b/ui/shared/ContactsListAndSearch.qml @@ -27,7 +27,7 @@ Item { noContactsRect.visible = false searchResults.loading = true searchResults.showProfileNotFoundMessage = false - chatsModel.resolveENS(ensName) + chatsModel.ensView.resolveENS(ensName) }); function validate() { @@ -75,7 +75,7 @@ Item { textField.anchors.rightMargin: clearBtn.width + Style.current.padding + 2 Connections { - target: chatsModel + target: chatsModel.ensView onEnsWasResolved: { if(chatKey.text == ""){ ensUsername.text = ""; @@ -90,7 +90,7 @@ Item { //% "Can't chat with yourself" validationError = qsTrId("can-t-chat-with-yourself"); } else { - searchResults.username = chatsModel.formatENSUsername(chatKey.text) + searchResults.username = chatsModel.ensView.formatENSUsername(chatKey.text) let userAlias = utilsModel.generateAlias(resolvedPubKey) userAlias = userAlias.length > 20 ? userAlias.substring(0, 19) + "..." : userAlias searchResults.userAlias = userAlias + " • " + Utils.compactAddress(resolvedPubKey, 4)