2021-09-20 13:00:10 +00:00
|
|
|
import json, chronicles
|
2021-10-01 16:53:38 +00:00
|
|
|
import ./statusgo_backend/contacts as status_contacts
|
|
|
|
import ./statusgo_backend/accounts as status_accounts
|
|
|
|
import ./statusgo_backend/chat as status_chat
|
|
|
|
import ./types/profile
|
2021-09-08 18:05:39 +00:00
|
|
|
import ../eventemitter
|
|
|
|
|
|
|
|
const DELETE_CONTACT* = "__deleteThisContact__"
|
|
|
|
|
|
|
|
type
|
|
|
|
ContactModel* = ref object
|
|
|
|
events*: EventEmitter
|
|
|
|
|
|
|
|
type
|
|
|
|
ContactUpdateArgs* = ref object of Args
|
|
|
|
contacts*: seq[Profile]
|
|
|
|
|
2021-09-21 13:51:46 +00:00
|
|
|
ContactIdArgs* = ref object of Args
|
|
|
|
id*: string
|
|
|
|
|
2021-09-08 18:05:39 +00:00
|
|
|
proc newContactModel*(events: EventEmitter): ContactModel =
|
|
|
|
result = ContactModel()
|
|
|
|
result.events = events
|
|
|
|
|
2021-09-28 19:21:41 +00:00
|
|
|
proc saveContact(self: ContactModel, contact: Profile) =
|
2021-09-21 18:24:32 +00:00
|
|
|
var
|
|
|
|
thumbnail = ""
|
|
|
|
largeImage = ""
|
2021-09-20 13:00:10 +00:00
|
|
|
if contact.identityImage != nil:
|
|
|
|
thumbnail = contact.identityImage.thumbnail
|
2021-09-21 18:24:32 +00:00
|
|
|
largeImage = contact.identityImage.large
|
2021-09-20 13:00:10 +00:00
|
|
|
|
2021-10-05 20:10:45 +00:00
|
|
|
status_contacts.saveContact(contact.id, contact.ensVerified, contact.ensName, contact.alias, contact.identicon, thumbnail, largeImage, contact.added, contact.blocked, contact.hasAddedUs, contact.localNickname)
|
2021-09-20 13:00:10 +00:00
|
|
|
|
2021-09-08 18:05:39 +00:00
|
|
|
proc getContactByID*(self: ContactModel, id: string): Profile =
|
2021-09-28 19:21:41 +00:00
|
|
|
return status_contacts.getContactByID(id)
|
|
|
|
|
|
|
|
proc blockContact*(self: ContactModel, id: string) =
|
2021-11-23 13:15:06 +00:00
|
|
|
status_contacts.blockContact(id)
|
2021-09-21 13:51:46 +00:00
|
|
|
self.events.emit("contactBlocked", ContactIdArgs(id: id))
|
2021-09-08 18:05:39 +00:00
|
|
|
|
2021-09-28 19:21:41 +00:00
|
|
|
proc unblockContact*(self: ContactModel, id: string) =
|
2021-11-23 13:15:06 +00:00
|
|
|
status_contacts.unblockContact(id)
|
2021-09-21 13:51:46 +00:00
|
|
|
self.events.emit("contactUnblocked", ContactIdArgs(id: id))
|
2021-09-08 18:05:39 +00:00
|
|
|
|
2021-09-20 13:00:10 +00:00
|
|
|
proc getContacts*(self: ContactModel, useCache: bool = true): seq[Profile] =
|
|
|
|
let (contacts, usedCache) = status_contacts.getContacts(useCache)
|
|
|
|
if not usedCache:
|
|
|
|
self.events.emit("contactUpdate", ContactUpdateArgs(contacts: contacts))
|
2021-09-08 18:05:39 +00:00
|
|
|
|
2021-09-20 13:00:10 +00:00
|
|
|
return contacts
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
proc getOrCreateContact*(self: ContactModel, id: string): Profile =
|
|
|
|
result = self.getContactByID(id)
|
|
|
|
if result == nil:
|
|
|
|
let alias = status_accounts.generateAlias(id)
|
|
|
|
result = Profile(
|
|
|
|
id: id,
|
|
|
|
username: alias,
|
|
|
|
localNickname: "",
|
|
|
|
identicon: status_accounts.generateIdenticon(id),
|
|
|
|
alias: alias,
|
|
|
|
ensName: "",
|
|
|
|
ensVerified: false,
|
|
|
|
appearance: 0,
|
2021-10-05 20:10:45 +00:00
|
|
|
added: false,
|
|
|
|
blocked: false,
|
|
|
|
hasAddedUs: false
|
2021-09-08 18:05:39 +00:00
|
|
|
)
|
|
|
|
|
2021-09-28 19:21:41 +00:00
|
|
|
proc setNickName*(self: ContactModel, id: string, localNickname: string, accountKeyUID: string) =
|
2021-09-08 18:05:39 +00:00
|
|
|
var contact = self.getOrCreateContact(id)
|
|
|
|
let nickname =
|
|
|
|
if (localNickname == ""):
|
|
|
|
contact.localNickname
|
|
|
|
elif (localNickname == DELETE_CONTACT):
|
|
|
|
""
|
|
|
|
else:
|
|
|
|
localNickname
|
|
|
|
|
2021-09-20 13:00:10 +00:00
|
|
|
contact.localNickname = nickname
|
2021-11-23 13:15:06 +00:00
|
|
|
status_contacts.setContactLocalNickname(id, nickname);
|
2021-09-08 18:05:39 +00:00
|
|
|
self.events.emit("contactAdded", Args())
|
2021-09-28 19:21:41 +00:00
|
|
|
sendContactUpdate(contact.id, accountKeyUID)
|
2021-09-08 18:05:39 +00:00
|
|
|
|
2021-09-28 19:21:41 +00:00
|
|
|
proc addContact*(self: ContactModel, id: string, accountKeyUID: string) =
|
2021-09-08 18:05:39 +00:00
|
|
|
var contact = self.getOrCreateContact(id)
|
|
|
|
|
2021-10-05 20:10:45 +00:00
|
|
|
let updating = contact.added
|
2021-09-20 13:00:10 +00:00
|
|
|
|
2021-09-08 18:05:39 +00:00
|
|
|
if not updating:
|
2021-10-05 20:10:45 +00:00
|
|
|
contact.added = true
|
2021-09-08 18:05:39 +00:00
|
|
|
else:
|
2021-10-05 20:10:45 +00:00
|
|
|
contact.blocked = false
|
2021-09-08 18:05:39 +00:00
|
|
|
|
2021-09-28 19:21:41 +00:00
|
|
|
self.saveContact(contact)
|
2021-09-08 18:05:39 +00:00
|
|
|
self.events.emit("contactAdded", Args())
|
2021-09-28 19:21:41 +00:00
|
|
|
sendContactUpdate(contact.id, accountKeyUID)
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
if updating:
|
|
|
|
let profile = Profile(
|
|
|
|
id: contact.id,
|
|
|
|
username: contact.alias,
|
|
|
|
identicon: contact.identicon,
|
|
|
|
alias: contact.alias,
|
|
|
|
ensName: contact.ensName,
|
|
|
|
ensVerified: contact.ensVerified,
|
|
|
|
appearance: 0,
|
2021-10-05 20:10:45 +00:00
|
|
|
added: contact.added,
|
|
|
|
blocked: contact.blocked,
|
|
|
|
hasAddedUs: contact.hasAddedUs,
|
2021-09-08 18:05:39 +00:00
|
|
|
localNickname: contact.localNickname
|
|
|
|
)
|
|
|
|
self.events.emit("contactUpdate", ContactUpdateArgs(contacts: @[profile]))
|
|
|
|
|
|
|
|
proc removeContact*(self: ContactModel, id: string) =
|
2021-11-23 13:15:06 +00:00
|
|
|
status_contacts.removeContact(id)
|
2021-09-08 18:05:39 +00:00
|
|
|
self.events.emit("contactRemoved", Args())
|
|
|
|
|
|
|
|
proc isAdded*(self: ContactModel, id: string): bool =
|
|
|
|
var contact = self.getContactByID(id)
|
|
|
|
if contact.isNil: return false
|
2021-10-05 20:10:45 +00:00
|
|
|
return contact.added
|
2021-09-08 18:05:39 +00:00
|
|
|
proc contactRequestReceived*(self: ContactModel, id: string): bool =
|
|
|
|
var contact = self.getContactByID(id)
|
|
|
|
if contact.isNil: return false
|
2021-10-05 20:10:45 +00:00
|
|
|
return contact.hasAddedUs
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
proc rejectContactRequest*(self: ContactModel, id: string) =
|
|
|
|
let contact = self.getContactByID(id)
|
2021-10-05 20:10:45 +00:00
|
|
|
contact.hasAddedUs = false
|
2021-09-08 18:05:39 +00:00
|
|
|
|
2021-09-28 19:21:41 +00:00
|
|
|
self.saveContact(contact)
|
2021-09-08 18:05:39 +00:00
|
|
|
self.events.emit("contactRemoved", Args())
|