user 6 lat temu
rodzic
commit
b4d4c3935c

+ 20 - 0
lib/content-item.d.ts

@@ -0,0 +1,20 @@
+import { ContentParams } from "./content-params";
+export declare class ContentItem {
+    data: ContentParams;
+    private id;
+    private type;
+    private myTagsWithoutAll;
+    private allLinks;
+    private userLinks;
+    private systemLinks;
+    private repliable;
+    private price?;
+    private priceFormatted?;
+    private fromTarget?;
+    private toTarget?;
+    private fromContactId?;
+    private toContactId?;
+    private media?;
+    constructor(props: ContentParams);
+    private formatCents;
+}

+ 45 - 0
lib/content-item.js

@@ -0,0 +1,45 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+class ContentItem {
+    constructor(props) {
+        this.data = props;
+        this.id = props.id;
+        this.type = props.type;
+        if (props.myTags) {
+            this.myTagsWithoutAll = props.myTags.filter(x => x !== 'all');
+        }
+        else {
+            this.myTagsWithoutAll = [];
+        }
+        if (props.price != null && !isNaN(Number(props.price))) {
+            this.price = Number(props.price);
+            this.priceFormatted = this.formatCents(this.price);
+        }
+        this.allLinks = props.links;
+        this.userLinks = props.links.filter(x => !x.rel.startsWith('.'));
+        this.systemLinks = props.links.filter(x => x.rel.startsWith('.'));
+        this.fromTarget = props.links.filter(x => x.rel === '.from').map(x => x.target)[0];
+        this.toTarget = props.links.filter(x => x.rel === '.to').map(x => x.target)[0];
+        this.fromContactId = props.links.filter(x => x.rel === '.from-contact-id').map(x => x.target)[0];
+        this.toContactId = props.links.filter(x => x.rel === '.to-contact-id').map(x => x.target)[0];
+        this.media = props.links.filter(x => x.rel === '.media').map(x => x.target)[0];
+        if (this.fromTarget && this.toTarget && this.media) {
+            this.repliable = true;
+        }
+        else {
+            this.repliable = false;
+        }
+    }
+    formatCents(price) {
+        if (price == null || price === 0) {
+            return 'free';
+        }
+        let dollars = `${price / 100}`;
+        if (dollars.match(/\..$/)) {
+            dollars += '0';
+        }
+        return '$' + dollars;
+    }
+}
+exports.ContentItem = ContentItem;
+//# sourceMappingURL=content-item.js.map

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


+ 5 - 0
lib/content-link.d.ts

@@ -0,0 +1,5 @@
+export interface ContentLink {
+    rel: string;
+    target: string;
+    text?: string;
+}

+ 3 - 0
lib/content-link.js

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

+ 1 - 0
lib/content-link.js.map

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

+ 8 - 0
lib/content-params.d.ts

@@ -0,0 +1,8 @@
+import { ContentLink } from "./content-link";
+export interface ContentParams {
+    id: string;
+    type: string;
+    links: ContentLink[];
+    myTags?: string[];
+    price?: number;
+}

+ 3 - 0
lib/content-params.js

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

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

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

+ 2 - 1
lib/index.d.ts

@@ -1,6 +1,7 @@
 import { Storage } from './storage';
 import { IWebClient } from './webclient';
 import { UploadItemParameters } from './upload-item-parameters';
+import { ContentItem } from './content-item';
 export declare class BankClient {
     private urlBase;
     private ipfsUrlBase;
@@ -30,7 +31,7 @@ export declare class BankClient {
     getOrCreateContact(peerId: string, contactAddr: string): Promise<any>;
     getContactById(peerId: string, contactId: string): Promise<any>;
     updateContact(peerId: string, contactId: string, newProperties: any): Promise<any>;
-    getContentItemByHash(hash: string): Promise<any>;
+    getContentItemByHash(hash: string): Promise<ContentItem>;
     private getItemsForCommaList;
     private getPriv;
     private makePlaintextPayload;

+ 4 - 2
lib/index.js

@@ -18,6 +18,7 @@ const TextEncoder = util.TextEncoder;
 const ws_1 = __importDefault(require("ws"));
 const util_1 = require("./util");
 const webclient_node_1 = __importDefault(require("./webclient-node"));
+const content_item_1 = require("./content-item");
 class BankClient {
     constructor(urlBase, ipfsUrlBase, storage = new storage_1.Storage('bankClient'), webClient = new webclient_node_1.default()) {
         this.urlBase = urlBase;
@@ -316,10 +317,11 @@ class BankClient {
             if (!hash.startsWith('/ipfs/')) {
                 hash = '/ipfs/' + hash;
             }
-            return this.webClient.requestJSON({
+            const contentParams = (yield this.webClient.requestJSON({
                 method: 'get',
                 url: this.ipfsUrlBase + hash + '/content.json'
-            });
+            }));
+            return new content_item_1.ContentItem(contentParams);
         });
     }
     getItemsForCommaList(commaList) {

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


+ 65 - 0
src/content-item.ts

@@ -0,0 +1,65 @@
+import { ContentLink } from "./content-link";
+import { ContentParams } from "./content-params";
+
+export class ContentItem {
+
+  public data: ContentParams;
+  private id: string;
+  private type: string;
+  
+  private myTagsWithoutAll: string[];
+  private allLinks: ContentLink[];
+  private userLinks: ContentLink[];
+  private systemLinks: ContentLink[];
+  private repliable: boolean;
+
+  private price?: number;
+  private priceFormatted?: string;
+  private fromTarget?: string;
+  private toTarget?: string;
+  private fromContactId?: string;
+  private toContactId?: string;
+  private media?: string;
+  
+  constructor(props: ContentParams) {
+    this.data = props;
+    this.id = props.id;
+    this.type = props.type;
+    
+    if (props.myTags) {
+      this.myTagsWithoutAll = props.myTags.filter(x => x !== 'all');
+    } else {
+      this.myTagsWithoutAll = [];
+    }
+    if (props.price != null && !isNaN(Number(props.price))) {
+      this.price = Number(props.price);
+      this.priceFormatted = this.formatCents(this.price);
+    }
+    this.allLinks = props.links;
+    this.userLinks = props.links.filter(x => !x.rel.startsWith('.'));
+    this.systemLinks = props.links.filter(x => x.rel.startsWith('.'));
+
+    this.fromTarget = props.links.filter(x => x.rel === '.from').map(x => x.target)[0];
+    this.toTarget = props.links.filter(x => x.rel === '.to').map(x => x.target)[0];
+    this.fromContactId = props.links.filter(x => x.rel === '.from-contact-id').map(x => x.target)[0];
+    this.toContactId = props.links.filter(x => x.rel === '.to-contact-id').map(x => x.target)[0];
+    this.media = props.links.filter(x => x.rel === '.media').map(x => x.target)[0];
+    if (this.fromTarget && this.toTarget && this.media) {
+      this.repliable = true;
+    } else {
+      this.repliable = false;
+    }
+  }
+
+  private formatCents(price: number) {
+    if (price == null || price === 0) {
+      return 'free';
+    }
+    let dollars = `${price / 100}`;
+    if (dollars.match(/\..$/)) {
+      dollars += '0';
+    }
+    return '$' + dollars;
+  }
+
+}

+ 5 - 0
src/content-link.ts

@@ -0,0 +1,5 @@
+export interface ContentLink {
+    rel: string;
+    target: string;
+    text?: string;
+}

+ 9 - 0
src/content-params.ts

@@ -0,0 +1,9 @@
+import { ContentLink } from "./content-link";
+
+export interface ContentParams {
+    id: string;
+    type: string;
+    links: ContentLink[];
+    myTags?: string[];
+    price?: number;
+}

+ 6 - 3
src/index.ts

@@ -9,6 +9,8 @@ import { UploadItemParameters } from './upload-item-parameters';
 import { encodeHex, mergeDeep, uuid } from './util';
 import WebClientNode from './webclient-node';
 import { reject, resolve } from 'bluebird';
+import { ContentParams } from './content-params';
+import { ContentItem } from './content-item';
 
 export class BankClient {
 
@@ -303,14 +305,15 @@ export class BankClient {
       return await this.getContactById(peerId, contactId);
     }
 
-    public async getContentItemByHash(hash: string): Promise<any> {
+    public async getContentItemByHash(hash: string): Promise<ContentItem> {
       if (!hash.startsWith('/ipfs/')) {
         hash = '/ipfs/' + hash;
       }
-      return this.webClient.requestJSON({
+      const contentParams = (await this.webClient.requestJSON({
         method: 'get',
         url: this.ipfsUrlBase + hash + '/content.json'
-      });
+      })) as ContentParams;
+      return new ContentItem(contentParams);
     }
 
     private async getItemsForCommaList(commaList: string): Promise<any[]> {