2021-09-20 13:00:10 +00:00
|
|
|
import tables, json, strmisc, atomics, sugar, sequtils, json_serialization, chronicles
|
|
|
|
import ./core, ./settings, ./accounts, ../utils, ../types/[profile, setting]
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
var
|
2021-09-20 13:00:10 +00:00
|
|
|
contacts {.threadvar.}: seq[Profile]
|
|
|
|
contactsIndex {.threadvar.}: Table[string, Profile]
|
2021-09-08 18:05:39 +00:00
|
|
|
contactsInited {.threadvar.}: bool
|
|
|
|
dirty: Atomic[bool]
|
|
|
|
|
|
|
|
proc getContactByID*(id: string): string =
|
|
|
|
result = callPrivateRPC("getContactByID".prefix, %* [id])
|
|
|
|
dirty.store(true)
|
|
|
|
|
2021-09-20 13:00:10 +00:00
|
|
|
proc getContacts*(useCache: bool = true): (seq[Profile], bool) =
|
|
|
|
let cacheIsDirty = (not useCache) or (not contactsInited) or dirty.load
|
|
|
|
if not cacheIsDirty:
|
|
|
|
return (contacts, true)
|
|
|
|
|
|
|
|
let payload = %* []
|
|
|
|
let response = callPrivateRPC("contacts".prefix, payload).parseJson
|
|
|
|
dirty.store(false)
|
|
|
|
contactsIndex = initTable[string, Profile]()
|
|
|
|
contactsInited = true
|
|
|
|
|
|
|
|
if response["result"].kind == JNull:
|
|
|
|
contacts = @[]
|
|
|
|
return (contacts, false)
|
|
|
|
|
|
|
|
contacts = map(response["result"].getElems(), proc(x: JsonNode): Profile = x.toProfileModel())
|
|
|
|
for contact in contacts:
|
|
|
|
contactsIndex[contact.id] = contact
|
|
|
|
|
|
|
|
return (contacts, false)
|
|
|
|
|
|
|
|
proc getContactsIndex*(): (Table[string, Profile], bool)=
|
2021-09-08 18:05:39 +00:00
|
|
|
let cacheIsDirty = (not contactsInited) or dirty.load
|
2021-09-20 13:00:10 +00:00
|
|
|
|
2021-09-08 18:05:39 +00:00
|
|
|
if not cacheIsDirty:
|
2021-09-20 13:00:10 +00:00
|
|
|
return (contactsIndex, true)
|
2021-09-08 18:05:39 +00:00
|
|
|
else:
|
2021-09-20 13:00:10 +00:00
|
|
|
discard getContacts()
|
|
|
|
return (contactsIndex, false)
|
2021-09-08 18:05:39 +00:00
|
|
|
|
2021-09-21 18:24:32 +00:00
|
|
|
proc saveContact*(id: string, ensVerified: bool, ensName: string, alias: string,
|
|
|
|
identicon: string, thumbnail: string, largeImage: string, systemTags: seq[string],
|
|
|
|
localNickname: string): string =
|
2021-09-08 18:05:39 +00:00
|
|
|
let payload = %* [{
|
|
|
|
"id": id,
|
|
|
|
"name": ensName,
|
|
|
|
"ensVerified": ensVerified,
|
|
|
|
"alias": alias,
|
|
|
|
"identicon": identicon,
|
2021-09-21 18:24:32 +00:00
|
|
|
"images": {
|
|
|
|
"thumbnail": {"Payload": thumbnail.partition(",")[2]},
|
|
|
|
"large": {"Payload": largeImage.partition(",")[2]}
|
|
|
|
},
|
2021-09-08 18:05:39 +00:00
|
|
|
"systemTags": systemTags,
|
|
|
|
"localNickname": localNickname
|
|
|
|
}]
|
|
|
|
# TODO: StatusGoError handling
|
|
|
|
result = callPrivateRPC("saveContact".prefix, payload)
|
|
|
|
dirty.store(true)
|
|
|
|
|
2021-09-20 13:00:10 +00:00
|
|
|
proc sendContactUpdate*(publicKey: string, accountKeyUID: string) : string =
|
|
|
|
let preferredUsername = getSetting[string](Setting.PreferredUsername, "")
|
|
|
|
let usernames = getSetting[seq[string]](Setting.Usernames, @[])
|
|
|
|
var ensName = ""
|
|
|
|
if len(preferredUsername) > 0:
|
|
|
|
ensName = preferredUsername
|
|
|
|
elif len(usernames) >= 1:
|
|
|
|
ensName = usernames[0]
|
|
|
|
|
|
|
|
let identityImage = getIdentityImage(accountKeyUID)
|
|
|
|
result = callPrivateRPC("sendContactUpdate".prefix, %* [publicKey, ensName, identityImage.thumbnail])
|
2021-09-08 18:05:39 +00:00
|
|
|
dirty.store(true)
|