Refactor contributor

This commit is contained in:
2018-04-02 19:08:29 +02:00
parent 6a31b0d579
commit 73d7ed52dd
8 changed files with 1251 additions and 703 deletions
@@ -18,6 +18,9 @@
<li><a href="https://ipfs.io/ipfs/{{contributor.profileHash}}">Inspect IPFS profile</a></li>
{{/if}}
</ul>
{{#if contributor.showMetadata}}
<pre>{{contributor.ipfsData}}</pre>
{{/if}}
</td>
</tr>
{{/each}}
+10
View File
@@ -0,0 +1,10 @@
import Contributor from 'kredits-web/models/contributor';
import Proposal from 'kredits-web/models/proposal';
export default {
name: 'register-models',
initialize: function(app) {
app.register('model:contributor', Contributor, { singleton: false });
app.register('model:proposal', Proposal, { singleton: false });
}
};
+23 -19
View File
@@ -1,11 +1,15 @@
import Ember from 'ember';
import computed from 'ember-computed';
import injectService from 'ember-service/inject';
const {
isPresent,
} = Ember;
export default Ember.Object.extend({
ipfs: injectService(),
id: null,
address: null,
name: null,
kind: null,
@@ -18,10 +22,12 @@ export default Ember.Object.extend({
isCore: false,
isCurrentUser: false,
avatarURL: function() {
return `https\:\/\/avatars2.githubusercontent.com/u/${this.get('github_uid')}?v=3&s=128`;
}.property('github_uid'),
avatarURL: computed('github_uid', function() {
let github_uid = this.get('github_uid');
if (github_uid) {
return `https://avatars2.githubusercontent.com/u/${github_uid}?v=3&s=128`;
}
}),
/**
* Loads the contributor's profile data from IPFS and sets local instance
@@ -30,16 +36,19 @@ export default Ember.Object.extend({
* @method
* @public
*/
loadProfile(ipfs) {
return new Ember.RSVP.Promise((resolve, reject) => {
if (!this.get('profileHash')) {
resolve(this);
return;
}
loadProfile() {
let profileHash = this.get('profileHash');
if (!profileHash) {
return;
}
ipfs.getFile(this.get('profileHash')).then(content => {
let profileJSON = JSON.parse(content);
let profile = Ember.Object.create(profileJSON);
return this.get('ipfs')
.getFile(profileHash)
.then((content) => {
let profile = Ember.Object.create(JSON.parse(content));
this.set('ipfsData', JSON.stringify(profile, null, 2));
Ember.Logger.debug('[contributor] loaded contributor profile', profile);
this.setProperties({
name: profile.get('name'),
@@ -61,14 +70,9 @@ export default Ember.Object.extend({
wiki_username: wiki.username
});
}
Ember.Logger.debug('[contributor] loaded contributor profile', profile);
resolve(this);
}).catch((err) => {
Ember.Logger.error('[contributor] error trying to load contributor profile', this.get('profileHash'), err);
reject(err);
Ember.Logger.error('[contributor] error trying to load contributor profile', profileHash, err);
});
});
},
/**
+4 -2
View File
@@ -1,5 +1,4 @@
import Ember from 'ember';
import Contributor from 'kredits-web/models/contributor';
export default Ember.Route.extend({
@@ -20,6 +19,9 @@ export default Ember.Route.extend({
},
model() {
let newContributor = Ember.getOwner(this).lookup('model:contributor');
newContributor.set('kind', 'person');
let kredits = this.get('kredits');
let totalSupply = kredits.get('tokenContract')
.then((contract) => contract.invoke('totalSupply'));
@@ -28,7 +30,7 @@ export default Ember.Route.extend({
contributors: kredits.getContributors(),
proposals: kredits.getProposals(),
totalSupply,
newContributor: Contributor.create({ kind: 'person' })
newContributor: newContributor,
});
}
+15 -14
View File
@@ -9,13 +9,13 @@ import injectService from 'ember-service/inject';
import computed from 'ember-computed';
import config from 'kredits-web/config/environment';
import Contributor from 'kredits-web/models/contributor';
import Proposal from 'kredits-web/models/proposal';
import abis from 'contracts/abis';
import addresses from 'contracts/addresses';
const {
getOwner,
Logger: {
debug,
warn
@@ -52,7 +52,7 @@ export default Service.extend({
if (typeof window.web3 !== 'undefined') {
debug('[kredits] Using user-provided instance, e.g. from Mist browser or Metamask');
web3Instance = window.web3;
web3Instance = new Web3(window.web3.currentProvider);
this.set('web3Provided', true);
} else {
debug('[kredits] Creating new instance from npm module class');
@@ -110,33 +110,34 @@ export default Service.extend({
getContributorData(id) {
return this.get('contributorsContract')
.then((contract) => contract.invoke('contributors', id))
.then(contributorData => {
debug('[kredits] contributor', contributorData);
let [ address, digest, hashFunction, size, isCore ] = contributorData;
.then((data) => {
debug('[kredits] contributor', data);
let [ address, digest, hashFunction, size, isCore ] = data;
let isCurrentUser = this.get('currentUserAccounts').includes(address);
let profileHash = this.getMultihashFromBytes32({
digest,
hashFunction: hashFunction.toNumber(),
size: size.toNumber()
});
let isCurrentUser = this.get('currentUserAccounts').includes(address);
return this.get('tokenContract')
.then((contract) => contract.invoke('balanceOf', address))
.then(balance => {
let contributor = Contributor.create({
.then((balance) => {
balance = balance.toNumber();
let contributor = getOwner(this).lookup('model:contributor');
contributor.setProperties({
id,
address,
profileHash,
isCore,
isCurrentUser,
kredits: balance.toNumber()
balance
});
// TODO: move ipfs into model
return contributor.loadProfile(this.get('ipfs'));
// Load data from IPFS
contributor.loadProfile();
return contributor;
});
});
},
@@ -77,6 +77,11 @@ table.contributor-list {
padding-left: 0.2rem;
}
}
pre {
line-height: 1rem;
padding-bottom: 1rem;
}
}
}
}