Skip to content

Commit

Permalink
refactor: contacts-perfomance (#74)
Browse files Browse the repository at this point in the history
* refactor: contacts-perfomance

* bump status-go
  • Loading branch information
richard-ramos authored Oct 5, 2021
1 parent 45887b1 commit 964e52c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 42 deletions.
2 changes: 1 addition & 1 deletion status/chat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
38 changes: 17 additions & 21 deletions status/contacts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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) =
Expand All @@ -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())
Expand All @@ -111,37 +111,33 @@ 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())

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())
8 changes: 5 additions & 3 deletions status/statusgo_backend/contacts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions status/types/account.nim
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,5 @@ proc toProfile*(account: Account): Profile =
alias: account.name,
ensName: "",
ensVerified: false,
appearance: 0,
systemTags: @[]
appearance: 0
)
30 changes: 26 additions & 4 deletions status/types/message.nim
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")

Expand All @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions status/types/profile.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"):
Expand All @@ -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 changes: 1 addition & 1 deletion vendor/status-go
Submodule status-go updated 42 files
+1 −1 VERSION
+50 −50 appdatabase/migrations/bindata.go
+1 −1 go.mod
+2 −2 go.sum
+6 −6 mailserver/migrations/bindata.go
+7 −7 multiaccounts/migrations/bindata.go
+3 −3 protocol/anonmetrics/migrations/migrations.go
+3 −0 protocol/chat.go
+7 −0 protocol/communities/community.go
+4 −0 protocol/communities/community_test.go
+5 −0 protocol/communities_messenger_test.go
+14 −71 protocol/contact.go
+17 −17 protocol/encryption/migrations/migrations.go
+1 −1 protocol/identity_images.go
+13 −22 protocol/messenger.go
+1 −0 protocol/messenger_chats.go
+5 −1 protocol/messenger_communities.go
+4 −6 protocol/messenger_contact_update_test.go
+9 −11 protocol/messenger_contacts.go
+6 −8 protocol/messenger_handler.go
+1 −1 protocol/messenger_handler_test.go
+1 −1 protocol/messenger_identity_image_test.go
+4 −4 protocol/messenger_installations_test.go
+0 −18 protocol/messenger_pin_messages.go
+13 −50 protocol/messenger_test.go
+93 −45 protocol/migrations/migrations.go
+35 −0 protocol/migrations/sqlite/1632303896_modify_contacts_table.up.sql
+1 −0 protocol/migrations/sqlite/1633349838_add_emoji_column_in_chats.up.sql
+46 −59 protocol/persistence.go
+37 −28 protocol/protobuf/chat_identity.pb.go
+2 −0 protocol/protobuf/chat_identity.proto
+11 −12 protocol/push_notification_test.go
+7 −7 protocol/pushnotificationclient/migrations/migrations.go
+5 −5 protocol/pushnotificationserver/migrations/migrations.go
+2 −0 protocol/requests/create_community_request.go
+5 −5 protocol/transport/migrations/migrations.go
+1 −1 services/wallet/on_ramp.go
+18 −18 static/bindata.go
+3 −3 t/bindata.go
+30 −19 vendor/github.com/status-im/go-waku/waku/v2/node/wakunode2.go
+1 −1 vendor/modules.txt
+17 −8 wakuv2/waku.go

0 comments on commit 964e52c

Please sign in to comment.