From 964e52c74259a32f763f9a803a59d24f549f913a Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 5 Oct 2021 16:10:45 -0400 Subject: [PATCH] refactor: contacts-perfomance (#74) * refactor: contacts-perfomance * bump status-go --- status/chat.nim | 2 +- status/contacts.nim | 38 +++++++++++++--------------- status/statusgo_backend/contacts.nim | 8 +++--- status/types/account.nim | 3 +-- status/types/message.nim | 30 +++++++++++++++++++--- status/types/profile.nim | 20 +++++++-------- vendor/status-go | 2 +- 7 files changed, 61 insertions(+), 42 deletions(-) diff --git a/status/chat.nim b/status/chat.nim index df7ad1f..43372db 100644 --- a/status/chat.nim +++ b/status/chat.nim @@ -253,7 +253,7 @@ proc init*(self: ChatModel, pubKey: string) = self.publicKey = pubKey var (contacts, _) = status_contacts.getContacts() - contacts = contacts.filter(c => c.systemTags.contains(contactAdded)) + contacts = contacts.filter(c => c.added) var chatList = status_chat.loadChats() chatList.sort(sortChats) diff --git a/status/contacts.nim b/status/contacts.nim index 80427ce..c8ba94d 100644 --- a/status/contacts.nim +++ b/status/contacts.nim @@ -30,20 +30,20 @@ proc saveContact(self: ContactModel, contact: Profile) = thumbnail = contact.identityImage.thumbnail largeImage = contact.identityImage.large - status_contacts.saveContact(contact.id, contact.ensVerified, contact.ensName, contact.alias, contact.identicon, thumbnail, largeImage, contact.systemTags, contact.localNickname) + status_contacts.saveContact(contact.id, contact.ensVerified, contact.ensName, contact.alias, contact.identicon, thumbnail, largeImage, contact.added, contact.blocked, contact.hasAddedUs, contact.localNickname) proc getContactByID*(self: ContactModel, id: string): Profile = return status_contacts.getContactByID(id) proc blockContact*(self: ContactModel, id: string) = var contact = self.getContactByID(id) - contact.systemTags.add(contactBlocked) + contact.blocked = true self.saveContact(contact) self.events.emit("contactBlocked", ContactIdArgs(id: id)) proc unblockContact*(self: ContactModel, id: string) = var contact = self.getContactByID(id) - contact.systemTags.delete(contact.systemTags.find(contactBlocked)) + contact.blocked = false self.saveContact(contact) self.events.emit("contactUnblocked", ContactIdArgs(id: id)) @@ -67,7 +67,9 @@ proc getOrCreateContact*(self: ContactModel, id: string): Profile = ensName: "", ensVerified: false, appearance: 0, - systemTags: @[] + added: false, + blocked: false, + hasAddedUs: false ) proc setNickName*(self: ContactModel, id: string, localNickname: string, accountKeyUID: string) = @@ -88,15 +90,13 @@ proc setNickName*(self: ContactModel, id: string, localNickname: string, account proc addContact*(self: ContactModel, id: string, accountKeyUID: string) = var contact = self.getOrCreateContact(id) - let updating = contact.systemTags.contains(contactAdded) + let updating = contact.added if not updating: - contact.systemTags.add(contactAdded) + contact.added = true discard status_chat.createProfileChat(contact.id) else: - let index = contact.systemTags.find(contactBlocked) - if (index > -1): - contact.systemTags.delete(index) + contact.blocked = false self.saveContact(contact) self.events.emit("contactAdded", Args()) @@ -111,20 +111,17 @@ proc addContact*(self: ContactModel, id: string, accountKeyUID: string) = ensName: contact.ensName, ensVerified: contact.ensVerified, appearance: 0, - systemTags: contact.systemTags, + added: contact.added, + blocked: contact.blocked, + hasAddedUs: contact.hasAddedUs, localNickname: contact.localNickname ) self.events.emit("contactUpdate", ContactUpdateArgs(contacts: @[profile])) proc removeContact*(self: ContactModel, id: string) = let contact = self.getContactByID(id) - var idx = contact.systemTags.find(contactAdded) - if idx >= 0: - contact.systemTags.delete(idx) - - idx = contact.systemTags.find(contactRequest) - if idx >= 0: - contact.systemTags.delete(idx) + contact.added = false + contact.hasAddedUs = false self.saveContact(contact) self.events.emit("contactRemoved", Args()) @@ -132,16 +129,15 @@ proc removeContact*(self: ContactModel, id: string) = proc isAdded*(self: ContactModel, id: string): bool = var contact = self.getContactByID(id) if contact.isNil: return false - contact.systemTags.contains(contactAdded) - + return contact.added proc contactRequestReceived*(self: ContactModel, id: string): bool = var contact = self.getContactByID(id) if contact.isNil: return false - contact.systemTags.contains(contactRequest) + return contact.hasAddedUs proc rejectContactRequest*(self: ContactModel, id: string) = let contact = self.getContactByID(id) - contact.systemTags.delete(contact.systemTags.find(contactRequest)) + contact.hasAddedUs = false self.saveContact(contact) self.events.emit("contactRemoved", Args()) diff --git a/status/statusgo_backend/contacts.nim b/status/statusgo_backend/contacts.nim index 63f5a67..0cddd3e 100644 --- a/status/statusgo_backend/contacts.nim +++ b/status/statusgo_backend/contacts.nim @@ -48,8 +48,8 @@ proc getContactsIndex*(): (Table[string, Profile], bool)= return (contactsIndex, false) proc saveContact*(id: string, ensVerified: bool, ensName: string, alias: string, - identicon: string, thumbnail: string, largeImage: string, systemTags: seq[string], - localNickname: string) = + identicon: string, thumbnail: string, largeImage: string, added: bool, blocked: bool, + hasAddedUs: bool, localNickname: string) = let payload = %* [{ "id": id, "name": ensName, @@ -60,7 +60,9 @@ proc saveContact*(id: string, ensVerified: bool, ensName: string, alias: string, "thumbnail": {"Payload": thumbnail.partition(",")[2]}, "large": {"Payload": largeImage.partition(",")[2]} }, - "systemTags": systemTags, + "added": added, + "blocked": blocked, + "hasAddedUs": hasAddedUs, "localNickname": localNickname }] discard callPrivateRPC("saveContact".prefix, payload) diff --git a/status/types/account.nim b/status/types/account.nim index d1ae153..a9a655b 100644 --- a/status/types/account.nim +++ b/status/types/account.nim @@ -53,6 +53,5 @@ proc toProfile*(account: Account): Profile = alias: account.name, ensName: "", ensVerified: false, - appearance: 0, - systemTags: @[] + appearance: 0 ) diff --git a/status/types/message.nim b/status/types/message.nim index d25d21f..0aa44dd 100644 --- a/status/types/message.nim +++ b/status/types/message.nim @@ -1,6 +1,6 @@ {.used.} -import json, strutils, sequtils, sugar, chronicles +import json, strutils, sequtils, sugar, chronicles, tables import json_serialization import ../utils import ../wallet/account @@ -90,6 +90,11 @@ proc currentUserWalletContainsAddress(address: string): bool = return false + +var identiconIndex = initTable[string, string]() +var aliasIndex = initTable[string, string]() + + proc toMessage*(jsonMsg: JsonNode): Message = let publicChatKey = status_settings.getSetting[string](Setting.PublicKey, "0x0") @@ -100,17 +105,34 @@ proc toMessage*(jsonMsg: JsonNode): Message = warn "Unknown content type received", type = jsonMsg{"contentType"}.getInt contentType = ContentType.Message + let publicKey = jsonMsg{"from"}.getStr + + # TODO: THIS IIS A TEMPORARY SOLUTION. + # IDENTICONS ARE GOING TO BE HANDLED VIA AN HTTP SERVER + # AND ALIAS ARE GOING TO BE REMOVED + var identicon = "" + if identiconIndex.hasKey(publicKey): + identicon = identiconIndex[publicKey] + else: + identicon = generateIdenticon(publicKey) + + var alias = "" + if aliasIndex.hasKey(publicKey): + alias = aliasIndex[publicKey] + else: + alias = generateAlias(publicKey) + var message = Message( - alias: jsonMsg{"alias"}.getStr, + alias: alias, userName: "", localName: "", chatId: jsonMsg{"localChatId"}.getStr, clock: jsonMsg{"clock"}.getInt, contentType: contentType, ensName: jsonMsg{"ensName"}.getStr, - fromAuthor: jsonMsg{"from"}.getStr, + fromAuthor: publicKey, id: jsonMsg{"id"}.getStr, - identicon: jsonMsg{"identicon"}.getStr, + identicon: identicon, lineCount: jsonMsg{"lineCount"}.getInt, localChatId: jsonMsg{"localChatId"}.getStr, messageType: jsonMsg{"messageType"}.getStr, diff --git a/status/types/profile.nim b/status/types/profile.nim index 91f941c..b3a3f67 100644 --- a/status/types/profile.nim +++ b/status/types/profile.nim @@ -17,16 +17,14 @@ type Profile* = ref object currentUserStatus*: int identityImage*: IdentityImage appearance*: int - systemTags*: seq[string] + added*: bool + blocked*: bool + hasAddedUs*: bool proc `$`*(self: Profile): string = - return fmt"Profile(id:{self.id}, username:{self.username}, systemTags: {self.systemTags}, ensName: {self.ensName})" + return fmt"Profile(id:{self.id}, username:{self.username}, added: {self.added}, blocked: {self.blocked}, hasAddedUs: {self.hasAddedUs}, ensName: {self.ensName})" proc toProfile*(jsonNode: JsonNode): Profile = - var systemTags: seq[string] = @[] - if jsonNode["systemTags"].kind != JNull: - systemTags = jsonNode["systemTags"].to(seq[string]) - result = Profile( id: jsonNode["id"].str, username: jsonNode["alias"].str, @@ -37,7 +35,9 @@ proc toProfile*(jsonNode: JsonNode): Profile = ensName: "", ensVerified: jsonNode["ensVerified"].getBool, appearance: 0, - systemTags: systemTags + added: jsonNode["added"].getBool, + blocked: jsonNode["blocked"].getBool, + hasAddedUs: jsonNode["hasAddedUs"].getBool, ) if jsonNode.hasKey("name"): @@ -53,10 +53,10 @@ proc toProfile*(jsonNode: JsonNode): Profile = result.identityImage.large = jsonNode["images"]["large"]["uri"].str proc isContact*(self: Profile): bool = - result = self.systemTags.contains(contactAdded) + result = self.added proc isBlocked*(self: Profile): bool = - result = self.systemTags.contains(contactBlocked) + result = self.blocked proc requestReceived*(self: Profile): bool = - result = self.systemTags.contains(contactRequest) \ No newline at end of file + result = self.hasAddedUs \ No newline at end of file diff --git a/vendor/status-go b/vendor/status-go index 51493d6..c3ced09 160000 --- a/vendor/status-go +++ b/vendor/status-go @@ -1 +1 @@ -Subproject commit 51493d61bdb71cbac06acbd120d23f434082cfa4 +Subproject commit c3ced098390f3d4e8d4850d9c0bf6e7dbd627c20