feat: using threads for resolving ENS usernames

This commit is contained in:
Richard Ramos 2020-07-07 15:03:23 -04:00 committed by Iuri Matias
parent 93d420758f
commit 4ee0d78cb6
5 changed files with 61 additions and 23 deletions

View File

@ -9,6 +9,8 @@ import ../../status/chat/[chat, message]
import ../../status/libstatus/types
import ../../status/profile/profile
import ../../status/threads
import views/channels_list, views/message_list, views/chat_item, views/sticker_pack_list, views/sticker_list
logScope:
@ -234,9 +236,6 @@ QtObject:
if id == "": return false
self.status.contacts.getContactByID(id).ensVerified
proc resolveENS*(self: ChatsView, ens: string): string {.slot.} =
result = status_ens.pubkey(ens)
proc formatENSUsername*(self: ChatsView, username: string): string {.slot.} =
result = status_ens.addDomain(username)
@ -247,3 +246,15 @@ QtObject:
if self.status.chat.contacts.hasKey(pubKey):
return status_ens.userNameOrAlias(self.status.chat.contacts[pubKey])
generateAlias(pubKey)
# Resolving a ENS name
proc resolveENS*(self: ChatsView, ens: string) {.slot.} =
spawnAndSend(self, "ensResolved") do: # Call self.ensResolved(string) when ens is resolved
status_ens.pubkey(ens)
proc ensWasResolved*(self: ChatsView, resolvedPubKey: string) {.signal.}
proc ensResolved(self: ChatsView, pubKey: string) {.slot.} =
self.ensWasResolved(pubKey)

View File

@ -8,7 +8,7 @@ import stew/byteutils
import unicode
import algorithm
let domain* = ".stateofus.eth"
const domain* = ".stateofus.eth"
proc userName*(ensName: string, removeSuffix: bool = false): string =
if ensName != "" and ensName.endsWith(domain):
@ -79,4 +79,4 @@ proc pubkey*(username: string): string =
result = ""
else:
pubkey.removePrefix("0x")
result = "0x04" & pubkey
result = "0x04" & pubkey

12
src/status/threads.nim Normal file
View File

@ -0,0 +1,12 @@
import nimqml
import threadpool
import stew/faux_closures
export fauxClosure
template spawnAndSend*(view: untyped, signalName: string, exprBlock: untyped) =
let viewPtr = cast[pointer](view.vptr)
proc backgroundTask() {.fauxClosure.} =
let data = exprBlock
signal_handler(viewPtr, data, signalName)
spawn backgroundTask()

View File

@ -13,33 +13,32 @@ ModalPopup {
function validate() {
if (!Utils.isChatKey(chatKey.text) && !Utils.isValidETHNamePrefix(chatKey.text)) {
validationError = "This needs to be a valid chat key or ENS username"
pubKey = "";
ensUsername = "";
validationError = "This needs to be a valid chat key or ENS username";
ensUsername.text = "";
} else {
validationError = ""
}
return validationError === ""
}
function onKeyReleased(){
ensUsername.text = "";
if (!validate()) return;
chatKey.text = chatKey.text.trim();
function resolveENS(ensName){
chatsModel.resolveENS(ensName)
}
if(Utils.isChatKey(chatKey.text)){
pubKey = chatKey.text;
function onKeyReleased(){
if (!validate()) {
return;
}
pubKey = chatsModel.resolveENS(chatKey.text)
if(pubKey == ""){
//% "User not found"
ensUsername.text = qsTrId("user-not-found");
} else {
ensUsername.text = chatsModel.formatENSUsername(chatKey.text) + " • " + Utils.compactAddress(pubKey, 4)
chatKey.text = chatKey.text.trim();
if(Utils.isChatKey(chatKey.text)){
pubKey = chatKey.text;
ensUsername.text = "";
return;
}
Qt.callLater(resolveENS, chatKey.text)
}
function doJoin() {
@ -55,7 +54,7 @@ ModalPopup {
onOpened: {
chatKey.text = "";
pubKey = "";
ensUsername = "";
ensUsername.text = "";
chatKey.forceActiveFocus(Qt.MouseFocusReason)
}
@ -69,6 +68,22 @@ ModalPopup {
Keys.onReleased: {
onKeyReleased();
}
Connections {
target: chatsModel
onEnsWasResolved: {
if(chatKey.text == ""){
ensUsername.text == "";
pubKey = "";
} else if(resolvedPubKey == ""){
ensUsername.text = qsTrId("user-not-found");
pubKey = "";
} else {
ensUsername.text = chatsModel.formatENSUsername(chatKey.text) + " • " + Utils.compactAddress(resolvedPubKey, 4)
pubKey = resolvedPubKey;
}
}
}
}
Text {

2
vendor/nim-stew vendored

@ -1 +1 @@
Subproject commit a99dafab420bcbbffee35e9bd847a9014eafaffe
Subproject commit 32b86bfd1ff97764e94447675559bf37a4ffb407