2021-09-28 19:21:41 +00:00
|
|
|
import tables, json, strmisc, atomics, sequtils, json_serialization, chronicles
|
2021-09-20 13:00:10 +00:00
|
|
|
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]
|
|
|
|
|
2021-09-28 19:21:41 +00:00
|
|
|
proc getContactByID*(id: string): Profile =
|
|
|
|
let response = callPrivateRPC("getContactByID".prefix, %* [id])
|
2021-09-08 18:05:39 +00:00
|
|
|
dirty.store(true)
|
2021-09-28 19:21:41 +00:00
|
|
|
let responseResult = parseJSON($response)["result"]
|
|
|
|
if responseResult == nil or responseResult.kind == JNull:
|
|
|
|
return nil
|
|
|
|
|
2021-10-01 16:53:38 +00:00
|
|
|
return toProfile(parseJSON($response)["result"])
|
2021-09-28 19:21:41 +00:00
|
|
|
|
2021-09-08 18:05:39 +00:00
|
|
|
|
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)
|
|
|
|
|
2021-10-01 16:53:38 +00:00
|
|
|
contacts = map(response["result"].getElems(), proc(x: JsonNode): Profile = x.toProfile())
|
2021-09-20 13:00:10 +00:00
|
|
|
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-28 19:21:41 +00:00
|
|
|
|
|
|
|
discard getContacts()
|
|
|
|
return (contactsIndex, false)
|
2021-09-08 18:05:39 +00:00
|
|
|
|
2021-11-23 13:15:06 +00:00
|
|
|
proc blockContact*(id: string) =
|
|
|
|
discard callPrivateRPC("blockContact".prefix, %* [id])
|
|
|
|
dirty.store(true)
|
|
|
|
|
|
|
|
proc unblockContact*(id: string) =
|
|
|
|
discard callPrivateRPC("unblockContact".prefix, %* [id])
|
|
|
|
dirty.store(true)
|
|
|
|
|
|
|
|
proc removeContact*(id: string) =
|
|
|
|
discard callPrivateRPC("removeContact".prefix, %* [id])
|
|
|
|
dirty.store(true)
|
|
|
|
|
|
|
|
proc rejectContactRequest*(id: string) =
|
|
|
|
let payload = %*[{
|
|
|
|
"id": id
|
|
|
|
}]
|
|
|
|
discard callPrivateRPC("rejectContactRequest".prefix, payload)
|
|
|
|
dirty.store(true)
|
|
|
|
|
|
|
|
proc setContactLocalNickname*(id: string, name: string) =
|
|
|
|
let payload = %* [{
|
|
|
|
"id": id,
|
|
|
|
"nickname": name
|
|
|
|
}]
|
|
|
|
discard callPrivateRPC("setContactLocalNickname".prefix, payload)
|
|
|
|
dirty.store(true)
|
|
|
|
|
2021-09-21 18:24:32 +00:00
|
|
|
proc saveContact*(id: string, ensVerified: bool, ensName: string, alias: string,
|
2021-10-05 20:10:45 +00:00
|
|
|
identicon: string, thumbnail: string, largeImage: string, added: bool, blocked: bool,
|
|
|
|
hasAddedUs: bool, localNickname: string) =
|
2021-11-23 13:15:06 +00:00
|
|
|
# TODO: Most of these method arguments aren't used anymore
|
|
|
|
# as status-go's RPC API became smarter. Should remove those.
|
2021-09-08 18:05:39 +00:00
|
|
|
let payload = %* [{
|
|
|
|
"id": id,
|
2021-11-23 13:15:06 +00:00
|
|
|
"ensName": ensName
|
2021-09-08 18:05:39 +00:00
|
|
|
}]
|
2021-11-23 13:15:06 +00:00
|
|
|
|
|
|
|
discard callPrivateRPC("addContact".prefix, payload)
|
2021-09-08 18:05:39 +00:00
|
|
|
dirty.store(true)
|
|
|
|
|
2021-09-28 19:21:41 +00:00
|
|
|
proc sendContactUpdate*(publicKey: string, accountKeyUID: string) =
|
2021-09-20 13:00:10 +00:00
|
|
|
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)
|
2021-09-28 19:21:41 +00:00
|
|
|
discard callPrivateRPC("sendContactUpdate".prefix, %* [publicKey, ensName, identityImage.thumbnail])
|
2021-09-08 18:05:39 +00:00
|
|
|
dirty.store(true)
|