user 6 lat temu
rodzic
commit
0dbef2a2b8

+ 11 - 0
lib/contact-item.d.ts

@@ -0,0 +1,11 @@
+import { ContactParams } from "./contact-params";
+export declare class ContactItem {
+    readonly data: ContactParams;
+    readonly id: string;
+    readonly hash: string;
+    readonly addrs: string[];
+    readonly groups: string[];
+    constructor(props: ContactParams);
+    matchesAddressOrName(search: string): boolean;
+    matchesGroup(group: string): boolean;
+}

+ 25 - 0
lib/contact-item.js

@@ -0,0 +1,25 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+class ContactItem {
+    constructor(props) {
+        this.data = props;
+        this.hash = props.hash;
+        this.id = props.id;
+        this.addrs = props.addrs || [];
+        this.groups = props.groups || [];
+    }
+    matchesAddressOrName(search) {
+        if (this.addrs.filter(x => x.indexOf(search.trim()) >= 0)[0].length > 0) {
+            return true;
+        }
+        return false;
+    }
+    matchesGroup(group) {
+        if (this.groups.filter(x => x === group.trim())[0].length > 0) {
+            return true;
+        }
+        return false;
+    }
+}
+exports.ContactItem = ContactItem;
+//# sourceMappingURL=contact-item.js.map

Plik diff jest za duży
+ 1 - 0
lib/contact-item.js.map


+ 6 - 0
lib/contact-params.d.ts

@@ -0,0 +1,6 @@
+export interface ContactParams {
+    id: string;
+    hash: string;
+    addrs: string[];
+    groups: string[];
+}

+ 3 - 0
lib/contact-params.js

@@ -0,0 +1,3 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+//# sourceMappingURL=contact-params.js.map

+ 1 - 0
lib/contact-params.js.map

@@ -0,0 +1 @@
+{"version":3,"file":"contact-params.js","sourceRoot":"","sources":["../src/contact-params.ts"],"names":[],"mappings":""}

+ 5 - 4
lib/index.d.ts

@@ -2,6 +2,7 @@ import { ContentItem } from './content-item';
 import { Storage } from './storage';
 import { UploadItemParameters } from './upload-item-parameters';
 import { IWebClient } from './webclient';
+import { ContactItem } from './contact-item';
 export declare class BankClient {
     private urlBase;
     private ipfsUrlBase;
@@ -27,10 +28,10 @@ export declare class BankClient {
     appendPrivate(dontLogToAll: boolean, peerAddr: string, topic: string, hash?: string, replaceHash?: string, deleteHash?: string): Promise<void>;
     retrievePrivate(peerAddr: string, topic: string): Promise<string>;
     subscribePrivate(peerAddr: string, topic: string, connectCallback: () => void, messageCallback: (data: any) => void): Promise<void>;
-    getOrCreateContact(peerId: string, contactAddr: string): Promise<any>;
-    getAllContacts(peerId: string): Promise<any[]>;
-    getContactById(peerId: string, contactId: string): Promise<any>;
-    updateContact(peerId: string, contactId: string, newProperties: any): Promise<any>;
+    getOrCreateContact(peerId: string, contactAddr: string): Promise<ContactItem>;
+    getAllContacts(peerId: string): Promise<ContactItem[]>;
+    getContactById(peerId: string, contactId: string): Promise<ContactItem>;
+    updateContact(peerId: string, contactId: string, newProperties: any): Promise<ContactItem>;
     getContentItemByHash(hash: string): Promise<ContentItem>;
     getItemsForCommaList(commaList: string): Promise<any[]>;
     parseItemHash(itemHash: string): {

+ 7 - 4
lib/index.js

@@ -15,6 +15,7 @@ const crypto = require("libp2p-crypto");
 const ws_1 = __importDefault(require("ws"));
 const content_item_1 = require("./content-item");
 const util_1 = require("./util");
+const contact_item_1 = require("./contact-item");
 class BankClient {
     constructor(urlBase, ipfsUrlBase, storage, webClient) {
         this.urlBase = urlBase;
@@ -241,21 +242,23 @@ class BankClient {
             if (existing != null) {
                 return existing;
             }
+            const contactId = util_1.uuid();
             const newItem = {
                 addrs: [
                     contactAddr
                 ],
-                id: util_1.uuid()
+                id: contactId
             };
             const newItemHash = yield this.uploadSlimJSON(newItem);
             yield this.appendPrivate(true, peerId, '📇', newItemHash);
-            return newItem;
+            return yield this.getContactById(peerId, contactId);
         });
     }
     getAllContacts(peerId) {
         return __awaiter(this, void 0, void 0, function* () {
             const contactList = yield this.retrievePrivate(peerId, '📇');
-            return yield this.getItemsForCommaList(contactList);
+            const items = yield this.getItemsForCommaList(contactList);
+            return items.map(data => new contact_item_1.ContactItem(data));
         });
     }
     getContactById(peerId, contactId) {
@@ -265,7 +268,7 @@ class BankClient {
             if (!existing) {
                 throw new Error('Cannot find contact with id ' + contactId);
             }
-            return existing;
+            return new contact_item_1.ContactItem(existing);
         });
     }
     updateContact(peerId, contactId, newProperties) {

Plik diff jest za duży
+ 1 - 1
lib/index.js.map


+ 32 - 0
src/contact-item.ts

@@ -0,0 +1,32 @@
+import { ContactParams } from "./contact-params";
+
+export class ContactItem {
+
+  public readonly data: ContactParams;
+  public readonly id: string;
+  public readonly hash: string;
+  public readonly addrs: string[];
+  public readonly groups: string[];
+  
+  constructor(props: ContactParams) {
+    this.data = props;
+    this.hash = props.hash;
+    this.id = props.id;
+    this.addrs = props.addrs || [];
+    this.groups = props.groups || [];
+  }
+
+  public matchesAddressOrName(search: string) {
+      if (this.addrs.filter(x => x.indexOf(search.trim()) >= 0)[0].length > 0) {
+          return true;
+      }
+      return false;
+  }
+
+  public matchesGroup(group: string) {
+    if (this.groups.filter(x => x === group.trim())[0].length > 0) {
+        return true;
+    }
+    return false;
+  }
+}

+ 6 - 0
src/contact-params.ts

@@ -0,0 +1,6 @@
+export interface ContactParams {
+    id: string;
+    hash: string;
+    addrs: string[];
+    groups: string[];
+}

+ 11 - 8
src/index.ts

@@ -7,6 +7,7 @@ import { Storage } from './storage';
 import { UploadItemParameters } from './upload-item-parameters';
 import { encodeHex, mergeDeep, uuid } from './util';
 import { IWebClient } from './webclient';
+import { ContactItem } from './contact-item';
 
 export class BankClient {
 
@@ -227,39 +228,41 @@ export class BankClient {
       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);
       // console.log('contact hash for', contact, type, 'is', contactHash);
       const existing = itemList.filter(item => item.addrs && item.addrs.includes(contactAddr))[0];
       if (existing != null) {
         return existing;
       }
+      const contactId = uuid();
       const newItem = {
         addrs: [
           contactAddr
         ],
-        id: uuid()
+        id: contactId
       };
       const newItemHash = await this.uploadSlimJSON(newItem);
       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, '📇');
-      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 existing = itemList.filter(item => item.id === contactId)[0];
       if (!existing) {
         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 newProps: any = mergeDeep({}, newProperties);
       delete newProps.id;