Extract serializer
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
|
import ContributionSerializer from './serializers/contribution';
|
||||||
import ContributorSerializer from './serializers/contributor';
|
import ContributorSerializer from './serializers/contributor';
|
||||||
import { fromBytes32, toBytes32 } from './utils/multihash';
|
import { fromBytes32, toBytes32 } from './utils/multihash';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
ContributionSerializer,
|
||||||
ContributorSerializer,
|
ContributorSerializer,
|
||||||
fromBytes32,
|
fromBytes32,
|
||||||
toBytes32
|
toBytes32
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
-91
@@ -1,103 +1,27 @@
|
|||||||
import Ember from 'ember';
|
import EmberObject from 'ember-object';
|
||||||
|
|
||||||
const {
|
|
||||||
isPresent,
|
|
||||||
isEmpty,
|
|
||||||
} = Ember;
|
|
||||||
|
|
||||||
export default Ember.Object.extend({
|
|
||||||
|
|
||||||
|
export default EmberObject.extend({
|
||||||
|
// Contract
|
||||||
id: null,
|
id: null,
|
||||||
creatorAddress: null,
|
creatorAddress: null,
|
||||||
recipientAddress: null,
|
|
||||||
recipientId: null,
|
recipientId: null,
|
||||||
recipientName: null,
|
amount: null,
|
||||||
recipientProfile: null,
|
|
||||||
votesCount: null,
|
votesCount: null,
|
||||||
votesNeeded: null,
|
votesNeeded: null,
|
||||||
amount: null,
|
|
||||||
executed: null,
|
executed: null,
|
||||||
contribution: null,
|
|
||||||
kind: null,
|
|
||||||
description: null,
|
|
||||||
url: null,
|
|
||||||
details: null,
|
|
||||||
ipfsHash: null,
|
ipfsHash: null,
|
||||||
|
|
||||||
/**
|
// IPFS
|
||||||
* Loads the contribution details from IPFS and sets local instance
|
kind: null,
|
||||||
* properties from it
|
description: null,
|
||||||
*
|
details: {},
|
||||||
* @method
|
url: null,
|
||||||
* @public
|
ipfsData: '',
|
||||||
*/
|
|
||||||
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);
|
|
||||||
|
|
||||||
this.setProperties({
|
// Deprecated
|
||||||
kind: contribution.get('kind'),
|
recipientAddress: null,
|
||||||
description: contribution.get('description'),
|
recipientName: null,
|
||||||
url: contribution.get('url')
|
recipientProfile: null,
|
||||||
});
|
|
||||||
|
|
||||||
// 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());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// TODO: add contributor relation
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user