|
@@ -7,6 +7,7 @@ import { Storage } from './storage';
|
|
|
import { UploadItemParameters } from './upload-item-parameters';
|
|
import { UploadItemParameters } from './upload-item-parameters';
|
|
|
import { encodeHex, mergeDeep, uuid } from './util';
|
|
import { encodeHex, mergeDeep, uuid } from './util';
|
|
|
import { IWebClient } from './webclient';
|
|
import { IWebClient } from './webclient';
|
|
|
|
|
+import { ContactItem } from './contact-item';
|
|
|
|
|
|
|
|
export class BankClient {
|
|
export class BankClient {
|
|
|
|
|
|
|
@@ -227,39 +228,41 @@ export class BankClient {
|
|
|
await this.connectWebsocket(peerAddr, topic, connectCallback, messageCallback);
|
|
await this.connectWebsocket(peerAddr, topic, connectCallback, messageCallback);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public async getOrCreateContact(peerId: string, contactAddr: string) {
|
|
|
|
|
|
|
+ public async getOrCreateContact(peerId: string, contactAddr: string): Promise<ContactItem> {
|
|
|
const itemList = await this.getAllContacts(peerId);
|
|
const itemList = await this.getAllContacts(peerId);
|
|
|
// console.log('contact hash for', contact, type, 'is', contactHash);
|
|
// console.log('contact hash for', contact, type, 'is', contactHash);
|
|
|
const existing = itemList.filter(item => item.addrs && item.addrs.includes(contactAddr))[0];
|
|
const existing = itemList.filter(item => item.addrs && item.addrs.includes(contactAddr))[0];
|
|
|
if (existing != null) {
|
|
if (existing != null) {
|
|
|
return existing;
|
|
return existing;
|
|
|
}
|
|
}
|
|
|
|
|
+ const contactId = uuid();
|
|
|
const newItem = {
|
|
const newItem = {
|
|
|
addrs: [
|
|
addrs: [
|
|
|
contactAddr
|
|
contactAddr
|
|
|
],
|
|
],
|
|
|
- id: uuid()
|
|
|
|
|
|
|
+ id: contactId
|
|
|
};
|
|
};
|
|
|
const newItemHash = await this.uploadSlimJSON(newItem);
|
|
const newItemHash = await this.uploadSlimJSON(newItem);
|
|
|
await this.appendPrivate(true, peerId, '📇', newItemHash);
|
|
await this.appendPrivate(true, peerId, '📇', newItemHash);
|
|
|
- return newItem;
|
|
|
|
|
|
|
+ return await this.getContactById(peerId, contactId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public async getAllContacts(peerId: string) {
|
|
|
|
|
|
|
+ public async getAllContacts(peerId: string): Promise<ContactItem[]> {
|
|
|
const contactList = await this.retrievePrivate(peerId, '📇');
|
|
const contactList = await this.retrievePrivate(peerId, '📇');
|
|
|
- return await this.getItemsForCommaList(contactList);
|
|
|
|
|
|
|
+ const items = await this.getItemsForCommaList(contactList);
|
|
|
|
|
+ return items.map(data => new ContactItem(data));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public async getContactById(peerId: string, contactId: string) {
|
|
|
|
|
|
|
+ public async getContactById(peerId: string, contactId: string): Promise<ContactItem> {
|
|
|
const itemList = await this.getAllContacts(peerId);
|
|
const itemList = await this.getAllContacts(peerId);
|
|
|
const existing = itemList.filter(item => item.id === contactId)[0];
|
|
const existing = itemList.filter(item => item.id === contactId)[0];
|
|
|
if (!existing) {
|
|
if (!existing) {
|
|
|
throw new Error('Cannot find contact with id ' + contactId);
|
|
throw new Error('Cannot find contact with id ' + contactId);
|
|
|
}
|
|
}
|
|
|
- return existing;
|
|
|
|
|
|
|
+ return new ContactItem(existing);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public async updateContact(peerId: string, contactId: string, newProperties: any) {
|
|
|
|
|
|
|
+ public async updateContact(peerId: string, contactId: string, newProperties: any): Promise<ContactItem> {
|
|
|
const existing = await this.getContactById(peerId, contactId);
|
|
const existing = await this.getContactById(peerId, contactId);
|
|
|
const newProps: any = mergeDeep({}, newProperties);
|
|
const newProps: any = mergeDeep({}, newProperties);
|
|
|
delete newProps.id;
|
|
delete newProps.id;
|