refactor: contacts-perfomance (#74)

* refactor: contacts-perfomance

* bump status-go
This commit is contained in:
Richard Ramos 2021-10-05 16:10:45 -04:00 committed by GitHub
parent 45887b1724
commit 964e52c742
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 61 additions and 42 deletions

View File

@ -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)

View File

@ -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())

View File

@ -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)

View File

@ -53,6 +53,5 @@ proc toProfile*(account: Account): Profile =
alias: account.name,
ensName: "",
ensVerified: false,
appearance: 0,
systemTags: @[]
appearance: 0
)

View File

@ -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,

View File

@ -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)
result = self.hasAddedUs

2
vendor/status-go vendored

@ -1 +1 @@
Subproject commit 51493d61bdb71cbac06acbd120d23f434082cfa4
Subproject commit c3ced098390f3d4e8d4850d9c0bf6e7dbd627c20