mirror of
https://github.com/status-im/status-lib.git
synced 2025-01-11 13:04:26 +00:00
refactor: contact store return object (#59)
This commit is contained in:
parent
fe7e74459e
commit
374a998fb1
@ -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())
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user