From 374a998fb1b435c1c1630485a8890bea2b3b8014 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 28 Sep 2021 21:21:41 +0200 Subject: [PATCH] refactor: contact store return object (#59) --- status/contacts.nim | 47 ++++++++++++++-------------- status/statusgo_backend/contacts.nim | 27 +++++++++------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/status/contacts.nim b/status/contacts.nim index 126aa02..16419a2 100644 --- a/status/contacts.nim +++ b/status/contacts.nim @@ -22,7 +22,7 @@ proc newContactModel*(events: EventEmitter): ContactModel = result = ContactModel() result.events = events -proc saveContact(self: ContactModel, contact: Profile): string = +proc saveContact(self: ContactModel, contact: Profile) = var thumbnail = "" largeImage = "" @@ -30,27 +30,21 @@ proc saveContact(self: ContactModel, contact: Profile): string = thumbnail = contact.identityImage.thumbnail largeImage = contact.identityImage.large - return 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.systemTags, contact.localNickname) proc getContactByID*(self: ContactModel, id: string): Profile = - let response = status_contacts.getContactByID(id) - # TODO: change to options - let responseResult = parseJSON($response)["result"] - if responseResult == nil or responseResult.kind == JNull: - result = nil - else: - result = toProfileModel(parseJSON($response)["result"]) - -proc blockContact*(self: ContactModel, id: string): string = + return status_contacts.getContactByID(id) + +proc blockContact*(self: ContactModel, id: string) = var contact = self.getContactByID(id) contact.systemTags.add(contactBlocked) - discard self.saveContact(contact) + self.saveContact(contact) self.events.emit("contactBlocked", ContactIdArgs(id: id)) -proc unblockContact*(self: ContactModel, id: string): string = +proc unblockContact*(self: ContactModel, id: string) = var contact = self.getContactByID(id) contact.systemTags.delete(contact.systemTags.find(contactBlocked)) - discard self.saveContact(contact) + self.saveContact(contact) self.events.emit("contactUnblocked", ContactIdArgs(id: id)) proc getContacts*(self: ContactModel, useCache: bool = true): seq[Profile] = @@ -76,7 +70,7 @@ proc getOrCreateContact*(self: ContactModel, id: string): Profile = systemTags: @[] ) -proc setNickName*(self: ContactModel, id: string, localNickname: string, accountKeyUID: string): string = +proc setNickName*(self: ContactModel, id: string, localNickname: string, accountKeyUID: string) = var contact = self.getOrCreateContact(id) let nickname = if (localNickname == ""): @@ -87,11 +81,11 @@ proc setNickName*(self: ContactModel, id: string, localNickname: string, account localNickname contact.localNickname = nickname - result = self.saveContact(contact) + self.saveContact(contact) self.events.emit("contactAdded", Args()) - discard sendContactUpdate(contact.id, accountKeyUID) + sendContactUpdate(contact.id, accountKeyUID) -proc addContact*(self: ContactModel, id: string, accountKeyUID: string): string = +proc addContact*(self: ContactModel, id: string, accountKeyUID: string) = var contact = self.getOrCreateContact(id) let updating = contact.systemTags.contains(contactAdded) @@ -104,9 +98,9 @@ proc addContact*(self: ContactModel, id: string, accountKeyUID: string): string if (index > -1): contact.systemTags.delete(index) - result = self.saveContact(contact) + self.saveContact(contact) self.events.emit("contactAdded", Args()) - discard sendContactUpdate(contact.id, accountKeyUID) + sendContactUpdate(contact.id, accountKeyUID) if updating: let profile = Profile( @@ -124,10 +118,15 @@ proc addContact*(self: ContactModel, id: string, accountKeyUID: string): string proc removeContact*(self: ContactModel, id: string) = let contact = self.getContactByID(id) - contact.systemTags.delete(contact.systemTags.find(contactAdded)) - contact.systemTags.delete(contact.systemTags.find(contactRequest)) + var idx = contact.systemTags.find(contactAdded) + if idx >= 0: + contact.systemTags.delete(idx) - discard self.saveContact(contact) + idx = contact.systemTags.find(contactRequest) + if idx >= 0: + contact.systemTags.delete(idx) + + self.saveContact(contact) self.events.emit("contactRemoved", Args()) proc isAdded*(self: ContactModel, id: string): bool = @@ -144,5 +143,5 @@ proc rejectContactRequest*(self: ContactModel, id: string) = let contact = self.getContactByID(id) contact.systemTags.delete(contact.systemTags.find(contactRequest)) - discard self.saveContact(contact) + self.saveContact(contact) self.events.emit("contactRemoved", Args()) diff --git a/status/statusgo_backend/contacts.nim b/status/statusgo_backend/contacts.nim index 11f893d..6eb2aa5 100644 --- a/status/statusgo_backend/contacts.nim +++ b/status/statusgo_backend/contacts.nim @@ -1,4 +1,4 @@ -import tables, json, strmisc, atomics, sugar, sequtils, json_serialization, chronicles +import tables, json, strmisc, atomics, sequtils, json_serialization, chronicles import ./core, ./settings, ./accounts, ../utils, ../types/[profile, setting] var @@ -7,9 +7,15 @@ var contactsInited {.threadvar.}: bool dirty: Atomic[bool] -proc getContactByID*(id: string): string = - result = callPrivateRPC("getContactByID".prefix, %* [id]) +proc getContactByID*(id: string): Profile = + let response = callPrivateRPC("getContactByID".prefix, %* [id]) dirty.store(true) + let responseResult = parseJSON($response)["result"] + if responseResult == nil or responseResult.kind == JNull: + return nil + + return toProfileModel(parseJSON($response)["result"]) + proc getContacts*(useCache: bool = true): (seq[Profile], bool) = let cacheIsDirty = (not useCache) or (not contactsInited) or dirty.load @@ -37,13 +43,13 @@ proc getContactsIndex*(): (Table[string, Profile], bool)= if not cacheIsDirty: return (contactsIndex, true) - else: - discard getContacts() - return (contactsIndex, false) + + discard getContacts() + return (contactsIndex, false) proc saveContact*(id: string, ensVerified: bool, ensName: string, alias: string, identicon: string, thumbnail: string, largeImage: string, systemTags: seq[string], - localNickname: string): string = + localNickname: string) = let payload = %* [{ "id": id, "name": ensName, @@ -57,11 +63,10 @@ proc saveContact*(id: string, ensVerified: bool, ensName: string, alias: string, "systemTags": systemTags, "localNickname": localNickname }] - # TODO: StatusGoError handling - result = callPrivateRPC("saveContact".prefix, payload) + discard callPrivateRPC("saveContact".prefix, payload) dirty.store(true) -proc sendContactUpdate*(publicKey: string, accountKeyUID: string) : string = +proc sendContactUpdate*(publicKey: string, accountKeyUID: string) = let preferredUsername = getSetting[string](Setting.PreferredUsername, "") let usernames = getSetting[seq[string]](Setting.Usernames, @[]) var ensName = "" @@ -71,5 +76,5 @@ proc sendContactUpdate*(publicKey: string, accountKeyUID: string) : string = ensName = usernames[0] let identityImage = getIdentityImage(accountKeyUID) - result = callPrivateRPC("sendContactUpdate".prefix, %* [publicKey, ensName, identityImage.thumbnail]) + discard callPrivateRPC("sendContactUpdate".prefix, %* [publicKey, ensName, identityImage.thumbnail]) dirty.store(true)