From 4cb3f5d8ba5251aec225634b512c8cce8fa7e8a5 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Tue, 6 Jun 2017 19:05:42 +0200 Subject: [PATCH] Use profile data from IPFS for contributor models --- app/components/contributor-list/template.hbs | 2 +- app/models/contributor.js | 60 ++++++++++++++++++++ app/services/ipfs.js | 3 + app/services/kredits.js | 10 ++-- 4 files changed, 69 insertions(+), 6 deletions(-) diff --git a/app/components/contributor-list/template.hbs b/app/components/contributor-list/template.hbs index dae31e5..6d6f80f 100644 --- a/app/components/contributor-list/template.hbs +++ b/app/components/contributor-list/template.hbs @@ -3,7 +3,7 @@ - {{contributor.github_username}} + {{contributor.name}} {{contributor.kredits}} diff --git a/app/models/contributor.js b/app/models/contributor.js index fcb859d..c591edc 100644 --- a/app/models/contributor.js +++ b/app/models/contributor.js @@ -1,5 +1,9 @@ import Ember from 'ember'; +const { + isPresent, +} = Ember; + export default Ember.Object.extend({ 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`; }.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() { let contributor = { "@context": "https://schema.kosmos.org", @@ -49,6 +103,12 @@ export default Ember.Object.extend({ return contributor; }, + /** + * Returns the JSON-LD representation of the model as a string + * + * @method + * @public + */ serialize() { return JSON.stringify(this.toJSON()); } diff --git a/app/services/ipfs.js b/app/services/ipfs.js index 453a005..6ef5a33 100644 --- a/app/services/ipfs.js +++ b/app/services/ipfs.js @@ -26,6 +26,9 @@ export default Ember.Service.extend({ getFile(hash) { return this.get('ipfs').cat(hash, { buffer: true }).then(res => { return res.toString(); + }, err => { + Ember.Logger.error('[ipfs] error trying to fetch file', hash, err); + return err; }); } diff --git a/app/services/kredits.js b/app/services/kredits.js index aca0665..f5d9396 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -101,17 +101,17 @@ 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('person', person); let contributor = Contributor.create({ address: address, - github_username: person[1], - github_uid: person[0], ipfsHash: person[2], kredits: balance.toNumber(), 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));