Use profile data from IPFS for contributor models

This commit is contained in:
2017-06-06 19:05:42 +02:00
parent 372a8a9b2d
commit 4cb3f5d8ba
4 changed files with 69 additions and 6 deletions
+1 -1
View File
@@ -3,7 +3,7 @@
<tr class="{{if contributor.isCurrentUser 'current-user'}}" {{action "toggleContributorInfo" contributor}}> <tr class="{{if contributor.isCurrentUser 'current-user'}}" {{action "toggleContributorInfo" contributor}}>
<td class="person"> <td class="person">
<img class="avatar" src={{contributor.avatarURL}}> <img class="avatar" src={{contributor.avatarURL}}>
{{contributor.github_username}} {{contributor.name}}
</td> </td>
<td class="kredits"> <td class="kredits">
<span class="amount">{{contributor.kredits}}</span> <span class="amount">{{contributor.kredits}}</span>
+60
View File
@@ -1,5 +1,9 @@
import Ember from 'ember'; import Ember from 'ember';
const {
isPresent,
} = Ember;
export default Ember.Object.extend({ export default Ember.Object.extend({
address: null, address: null,
@@ -18,6 +22,56 @@ export default Ember.Object.extend({
return `https\:\/\/avatars2.githubusercontent.com/u/${this.get('github_uid')}?v=3&s=128`; return `https\:\/\/avatars2.githubusercontent.com/u/${this.get('github_uid')}?v=3&s=128`;
}.property('github_uid'), }.property('github_uid'),
/**
* Loads the contributor's profile data from IPFS and sets local instance
* properties from it
*
* @method
* @public
*/
loadProfile(ipfs) {
let promise = new Ember.RSVP.Promise((resolve, reject) => {
ipfs.getFile(this.get('ipfsHash')).then(content => {
let profileJSON = JSON.parse(content);
let profile = Ember.Object.create(profileJSON);
this.set('name', profile.get('name'));
let accounts = profile.get('accounts');
let github = accounts.findBy('site', 'github.com');
let wiki = accounts.findBy('site', 'wiki.kosmos.org');
if (isPresent(github)) {
this.setProperties({
github_username: github.username,
github_uid: github.uid,
});
}
if (isPresent(wiki)) {
this.setProperties({
wiki_username: wiki.username
});
}
Ember.Logger.debug('[contributor] loaded contributor profile', profile);
resolve();
}).catch((err) => {
Ember.Logger.error('[contributor] error trying to load contributor profile', this.get('ipfsHash'), err);
reject(err);
});
});
return promise;
},
/**
* Creates a JSON-LD object of the contributor, according to
* https://github.com/67P/kosmos-schemas/blob/master/schemas/contributor.json
*
* @method
* @public
*/
toJSON() { toJSON() {
let contributor = { let contributor = {
"@context": "https://schema.kosmos.org", "@context": "https://schema.kosmos.org",
@@ -49,6 +103,12 @@ export default Ember.Object.extend({
return contributor; return contributor;
}, },
/**
* Returns the JSON-LD representation of the model as a string
*
* @method
* @public
*/
serialize() { serialize() {
return JSON.stringify(this.toJSON()); return JSON.stringify(this.toJSON());
} }
+3
View File
@@ -26,6 +26,9 @@ export default Ember.Service.extend({
getFile(hash) { getFile(hash) {
return this.get('ipfs').cat(hash, { buffer: true }).then(res => { return this.get('ipfs').cat(hash, { buffer: true }).then(res => {
return res.toString(); return res.toString();
}, err => {
Ember.Logger.error('[ipfs] error trying to fetch file', hash, err);
return err;
}); });
} }
+5 -5
View File
@@ -101,17 +101,17 @@ export default Service.extend({
this.getValueFromContract('kreditsContract', 'contributorAddresses', i).then(address => { this.getValueFromContract('kreditsContract', 'contributorAddresses', i).then(address => {
this.getValueFromContract('kreditsContract', 'contributors', address).then(person => { this.getValueFromContract('kreditsContract', 'contributors', address).then(person => {
this.getValueFromContract('tokenContract', 'balanceOf', address).then(balance => { this.getValueFromContract('tokenContract', 'balanceOf', address).then(balance => {
Ember.Logger.debug('person', person);
let contributor = Contributor.create({ let contributor = Contributor.create({
address: address, address: address,
github_username: person[1],
github_uid: person[0],
ipfsHash: person[2], ipfsHash: person[2],
kredits: balance.toNumber(), kredits: balance.toNumber(),
isCurrentUser: this.get('currentUserAccounts').includes(address) isCurrentUser: this.get('currentUserAccounts').includes(address)
}); });
Ember.Logger.debug('[kredits] contributor', contributor);
resolve(contributor); contributor.loadProfile(this.get('ipfs')).then(
() => resolve(contributor),
err => reject(err)
);
}); });
}); });
}).catch(err => reject(err)); }).catch(err => reject(err));