|
@@ -17,6 +17,7 @@ const ws_1 = __importDefault(require("ws"));
|
|
|
const contact_item_1 = require("./contact-item");
|
|
const contact_item_1 = require("./contact-item");
|
|
|
const content_item_1 = require("./content-item");
|
|
const content_item_1 = require("./content-item");
|
|
|
const util_1 = require("./util");
|
|
const util_1 = require("./util");
|
|
|
|
|
+const contact_address_1 = require("./contact-address");
|
|
|
class BankClient {
|
|
class BankClient {
|
|
|
constructor(urlBase, ipfsUrlBase, storage, webClient) {
|
|
constructor(urlBase, ipfsUrlBase, storage, webClient) {
|
|
|
this.urlBase = urlBase;
|
|
this.urlBase = urlBase;
|
|
@@ -232,24 +233,24 @@ class BankClient {
|
|
|
yield this.connectWebsocket(peerAddr, topic, connectCallback, messageCallback);
|
|
yield this.connectWebsocket(peerAddr, topic, connectCallback, messageCallback);
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
- getOrCreateContact(peerId, contactAddr) {
|
|
|
|
|
|
|
+ getOrCreateContact(peerId, addressType, addressValue) {
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
- const itemList = yield this.getAllContacts(peerId);
|
|
|
|
|
- // console.log('contact hash for', contact, type, 'is', contactHash);
|
|
|
|
|
- const existing = itemList.filter(item => item.addrs && item.addrs.includes(contactAddr))[0];
|
|
|
|
|
|
|
+ const contactBook = yield this.getContactBook(peerId);
|
|
|
|
|
+ const existing = contactBook.lookupByAddress(addressType, addressValue);
|
|
|
if (existing != null) {
|
|
if (existing != null) {
|
|
|
return existing;
|
|
return existing;
|
|
|
}
|
|
}
|
|
|
const contactId = util_1.uuid();
|
|
const contactId = util_1.uuid();
|
|
|
const newItem = {
|
|
const newItem = {
|
|
|
addrs: [
|
|
addrs: [
|
|
|
- contactAddr
|
|
|
|
|
|
|
+ new contact_address_1.ContactAddress(addressType, addressValue).toPrefixedString()
|
|
|
],
|
|
],
|
|
|
id: contactId
|
|
id: contactId
|
|
|
};
|
|
};
|
|
|
const newItemHash = yield this.uploadSlimJSON(newItem);
|
|
const newItemHash = yield this.uploadSlimJSON(newItem);
|
|
|
yield this.appendPrivate(peerId, '📇', newItemHash);
|
|
yield this.appendPrivate(peerId, '📇', newItemHash);
|
|
|
- return yield this.getContactById(peerId, contactId);
|
|
|
|
|
|
|
+ const contactBook2 = yield this.getContactBook(peerId);
|
|
|
|
|
+ return (yield contactBook2.lookupById(contactId));
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
getAllContacts(peerId) {
|
|
getAllContacts(peerId) {
|
|
@@ -264,26 +265,21 @@ class BankClient {
|
|
|
return new contact_book_1.ContactBook(yield this.getAllContacts(peerId));
|
|
return new contact_book_1.ContactBook(yield this.getAllContacts(peerId));
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
- getContactById(peerId, contactId) {
|
|
|
|
|
|
|
+ updateContact(peerId, contactId, newProperties) {
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
- const itemList = yield this.getAllContacts(peerId);
|
|
|
|
|
- const existing = itemList.filter(item => item.id === contactId)[0];
|
|
|
|
|
|
|
+ const contactBook = yield this.getContactBook(peerId);
|
|
|
|
|
+ const existing = yield contactBook.lookupById(contactId);
|
|
|
if (!existing) {
|
|
if (!existing) {
|
|
|
- throw new Error('Cannot find contact with id ' + contactId);
|
|
|
|
|
|
|
+ throw new Error('missing contact with id ' + contactId);
|
|
|
}
|
|
}
|
|
|
- return new contact_item_1.ContactItem(existing);
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- updateContact(peerId, contactId, newProperties) {
|
|
|
|
|
- return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
|
- const existing = yield this.getContactById(peerId, contactId);
|
|
|
|
|
const newProps = util_1.mergeDeep({}, newProperties);
|
|
const newProps = util_1.mergeDeep({}, newProperties);
|
|
|
delete newProps.id;
|
|
delete newProps.id;
|
|
|
const newItem = util_1.mergeDeep(existing, newProps);
|
|
const newItem = util_1.mergeDeep(existing, newProps);
|
|
|
delete newItem.hash;
|
|
delete newItem.hash;
|
|
|
const newItemHash = yield this.uploadSlimJSON(newItem);
|
|
const newItemHash = yield this.uploadSlimJSON(newItem);
|
|
|
yield this.appendPrivate(peerId, '📇', newItemHash, existing.hash);
|
|
yield this.appendPrivate(peerId, '📇', newItemHash, existing.hash);
|
|
|
- return yield this.getContactById(peerId, contactId);
|
|
|
|
|
|
|
+ const contactBook2 = yield this.getContactBook(peerId);
|
|
|
|
|
+ return (yield contactBook2.lookupById(contactId));
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
getContentItemByHash(hash) {
|
|
getContentItemByHash(hash) {
|