From ad92ffa447d9381876689d60da26ba8ccaf6cd1b Mon Sep 17 00:00:00 2001 From: Manuel Wiedenmann Date: Sun, 8 Apr 2018 01:58:34 +0200 Subject: [PATCH] Extract serializer --- app/lib/kredits/index.js | 2 + app/lib/kredits/serializers/contribution.js | 64 ++++++++++++ app/models/proposal.js | 106 +++----------------- 3 files changed, 81 insertions(+), 91 deletions(-) create mode 100644 app/lib/kredits/serializers/contribution.js diff --git a/app/lib/kredits/index.js b/app/lib/kredits/index.js index 327bed5..7042bcf 100644 --- a/app/lib/kredits/index.js +++ b/app/lib/kredits/index.js @@ -1,7 +1,9 @@ +import ContributionSerializer from './serializers/contribution'; import ContributorSerializer from './serializers/contributor'; import { fromBytes32, toBytes32 } from './utils/multihash'; export { + ContributionSerializer, ContributorSerializer, fromBytes32, toBytes32 diff --git a/app/lib/kredits/serializers/contribution.js b/app/lib/kredits/serializers/contribution.js new file mode 100644 index 0000000..0802eea --- /dev/null +++ b/app/lib/kredits/serializers/contribution.js @@ -0,0 +1,64 @@ +/** + * Handle serialization for JSON-LD object of the contribution, according to + * https://github.com/67P/kosmos-schemas/blob/master/schemas/contribution.json + * + * @class + * @public + */ +export default class Contributor { + /** + * Deserialize JSON to object + * + * @method + * @public + */ + static deserialize(serialized) { + let { + kind, + description, + details, + url, + } = JSON.parse(serialized); + + return { + kind, + description, + details, + url, + ipfsData: serialized, + }; + } + + /** + * Serialize object to JSON + * + * @method + * @public + */ + static serialize(deserialized) { + let { + contributorIpfsHash, + kind, + description, + url, + } = deserialized; + + let data = { + "@context": "https://schema.kosmos.org", + "@type": "Contribution", + "contributor": { + "ipfs": contributorIpfsHash + }, + kind, + description, + "details": {} + }; + + if (url) { + data["url"] = url; + } + + // Write it pretty to ipfs + return JSON.stringify(data, null, 2); + } +} diff --git a/app/models/proposal.js b/app/models/proposal.js index fa9fc71..4b49981 100644 --- a/app/models/proposal.js +++ b/app/models/proposal.js @@ -1,103 +1,27 @@ -import Ember from 'ember'; - -const { - isPresent, - isEmpty, -} = Ember; - -export default Ember.Object.extend({ +import EmberObject from 'ember-object'; +export default EmberObject.extend({ + // Contract id: null, creatorAddress: null, - recipientAddress: null, recipientId: null, - recipientName: null, - recipientProfile: null, + amount: null, votesCount: null, votesNeeded: null, - amount: null, executed: null, - contribution: null, - kind: null, - description: null, - url: null, - details: null, ipfsHash: null, - /** - * Loads the contribution details from IPFS and sets local instance - * properties from it - * - * @method - * @public - */ - loadContribution(ipfs) { - let promise = new Ember.RSVP.Promise((resolve, reject) => { - ipfs.getFile(this.get('ipfsHash')).then(content => { - let contributionJSON = JSON.parse(content); - let contribution = Ember.Object.create(contributionJSON); + // IPFS + kind: null, + description: null, + details: {}, + url: null, + ipfsData: '', - this.setProperties({ - kind: contribution.get('kind'), - description: contribution.get('description'), - url: contribution.get('url') - }); - - // TODO load details - // let details = profile.get('accounts'); - - Ember.Logger.debug('[proposal] loaded contribution details', contributionJSON); - resolve(); - }).catch((err) => { - Ember.Logger.error('[proposal] error trying to load contribution details', this.get('ipfsHash'), err); - reject(err); - }); - }); - - return promise; - }, - - /** - * Creates a JSON-LD object of the contribution, according to - * https://github.com/67P/kosmos-schemas/blob/master/schemas/contribution.json - * - * @method - * @public - */ - contributionToJSON() { - if (isEmpty(this.get('recipientProfile'))) { - throw new Error('IPFS hash for recipient profile missing from proposal object'); - } - if (isEmpty(this.get('kind')) || isEmpty(this.get('description'))) { - throw new Error('Missing one or more required properties: kind, description'); - } - - let contribution = { - "@context": "https://schema.kosmos.org", - "@type": "Contribution", - "contributor": { - "ipfs": this.get('recipientProfile') - }, - "kind": this.get('kind'), - "description": this.get('description'), - "details": {} - }; - - if (isPresent(this.get('url'))) { - contribution["url"] = this.get('url'); - } - - return contribution; - }, - - /** - * Returns the JSON-LD representation of the contribution as a string - * - * @method - * @public - */ - serializeContribution() { - return JSON.stringify(this.contributionToJSON()); - } + // Deprecated + recipientAddress: null, + recipientName: null, + recipientProfile: null, + // TODO: add contributor relation });