Create contribution IPFS objects, attach to proposals

This commit is contained in:
2017-06-06 21:27:05 +02:00
parent 0afbeea01b
commit 7b91e64af4
6 changed files with 149 additions and 37 deletions
+26 -20
View File
@@ -2,6 +2,7 @@ import Ember from 'ember';
const {
Component,
isPresent,
inject: {
service
},
@@ -13,6 +14,7 @@ export default Component.extend({
kredits: service(),
proposal: null,
contributors: null,
inProgress: false,
isValidRecipient: computed('proposal.recipientAddress', function() {
@@ -24,35 +26,39 @@ export default Component.extend({
}),
isValidUrl: computed('proposal.url', function() {
// TODO
return true;
return isPresent(this.get('proposal.url'));
}),
isValidIpfsHash: computed('proposal.ipfsHash', function() {
// TODO
return true;
isValidDescription: computed('proposal.description', function() {
return isPresent(this.get('proposal.description'));
}),
isValid: computed.and('isValidRecipient', 'isValidAmount', 'isValidUrl',
'isValidIpfsHash'),
isValid: computed.and('isValidRecipient',
'isValidAmount',
'isValidDescription'),
actions: {
save() {
if (this.get('isValid')) {
this.set('inProgress', true);
this.get('kredits').addProposal(this.get('proposal'))
.then(() => {
this.attrs.onSave();
}).catch((error) => {
Ember.Logger.error('[add-proposal] error creating the proposal', error);
alert('Something went wrong.');
}).finally(() => {
this.set('inProgress', false);
});
} else {
if (! this.get('isValid')) {
alert('Invalid data. Please review and try again.');
}
this.set('inProgress', true);
let proposal = this.get('proposal');
// Set the recipient's IPFS profile hash so it can be used in the
// contribution object (which is to be stored in IPFS as well)
let contributor = this.get('contributors').findBy('address', proposal.get('recipientAddress'));
proposal.set('recipientProfile', contributor.get('ipfsHash'));
this.get('kredits').addProposal(proposal)
.then(() => {
this.attrs.onSave();
}).catch((error) => {
Ember.Logger.error('[add-proposal] error creating the proposal', error);
alert('Something went wrong.');
}).finally(() => {
this.set('inProgress', false);
});
}
}
+15 -6
View File
@@ -7,6 +7,15 @@
{{/each}}
</select>
</p>
<p>
<select required onchange={{action (mut proposal.kind) value="target.value"}}>
<option value="community" selected={{eq proposal.kind "community"}}>Community</option>
<option value="design" selected={{eq proposal.kind "design"}}>Design</option>
<option value="dev" selected={{eq proposal.kind "dev"}}>Development</option>
<option value="docs" selected={{eq proposal.kind "docs"}}>Documentation</option>
<option value="ops" selected={{eq proposal.kind "ops"}}>IT Operations</option>
</select>
</p>
<p>
{{input type="text"
placeholder="100"
@@ -15,15 +24,15 @@
</p>
<p>
{{input type="text"
placeholder="URL"
value=proposal.url
class=(if isValidUrl 'valid' '')}}
placeholder="Description"
value=proposal.description
class=(if isValidDescription 'valid' '')}}
</p>
<p>
{{input type="text"
placeholder="IPFS Hash"
value=proposal.ipfsHash
class=(if isValidIpfsHash 'valid' '')}}
placeholder="URL (optional)"
value=proposal.url
class=(if isValidUrl 'valid' '')}}
</p>
<p class="actions">
{{input type="submit" value=(if inProgress 'Processing' 'Save') disabled=inProgress}}
+54 -1
View File
@@ -1,16 +1,69 @@
import Ember from 'ember';
const {
isPresent,
isEmpty,
} = Ember;
export default Ember.Object.extend({
id: null,
creatorAddress: null,
recipientAddress: null,
recipientName: null,
recipientProfile: null,
votesCount: null,
votesNeeded: null,
amount: null,
executed: null,
contribution: null,
kind: null,
description: null,
url: null,
ipfsHash: null
details: null,
ipfsHash: null,
/**
* 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());
}
});
+1
View File
@@ -18,6 +18,7 @@ export default Route.extend({
recipientAddress: params.recipient,
amount: params.amount,
url: params.url,
kind: params.kind || 'dev',
ipfsHash: params.ipfsHash
});
+10 -7
View File
@@ -102,6 +102,8 @@ export default Service.extend({
this.getValueFromContract('kreditsContract', 'contributorAddresses', i).then(address => {
this.getValueFromContract('kreditsContract', 'contributors', address).then(person => {
this.getValueFromContract('tokenContract', 'balanceOf', address).then(balance => {
Ember.Logger.debug('[kredits] person', address, person);
let contributor = Contributor.create({
address: address,
ipfsHash: person[2],
@@ -203,14 +205,15 @@ export default Service.extend({
const {
recipientAddress,
amount,
url,
ipfsHash
} = proposal.getProperties('recipientAddress', 'amount', 'url', 'ipfsHash');
url
} = proposal.getProperties('recipientAddress', 'amount', 'url');
this.get('kreditsContract').addProposal(recipientAddress, amount, url, ipfsHash, (err, data) => {
if (err) { reject(err); return; }
Ember.Logger.debug('[kredits] add proposal response', data);
resolve();
this.get('ipfs').storeFile(proposal.serializeContribution()).then(ipfsHash => {
this.get('kreditsContract').addProposal(recipientAddress, amount, url, ipfsHash, (err, data) => {
if (err) { reject(err); return; }
Ember.Logger.debug('[kredits] add proposal response', data);
resolve();
});
});
});
},