Merge pull request #37 from 67P/refactor/get-contributor-by-id
Refactor getContributorById to be move generic
This commit was merged in pull request #37.
This commit is contained in:
+32
-31
@@ -118,29 +118,24 @@ export default Service.extend({
|
|||||||
getContributorById(id) {
|
getContributorById(id) {
|
||||||
return this.get('contributorsContract')
|
return this.get('contributorsContract')
|
||||||
.then((contract) => contract.getContributorById(id))
|
.then((contract) => contract.getContributorById(id))
|
||||||
|
.then(this.reassembleIpfsHash)
|
||||||
// Set basic data
|
// Set basic data
|
||||||
.then(({
|
.then(({ account: address, balance, ipfsHash, isCore }) => {
|
||||||
account: address,
|
|
||||||
hashFunction,
|
|
||||||
hashSize,
|
|
||||||
isCore,
|
|
||||||
profileHash: digest,
|
|
||||||
balance
|
|
||||||
}) => {
|
|
||||||
let isCurrentUser = this.get('currentUserAccounts').includes(address);
|
let isCurrentUser = this.get('currentUserAccounts').includes(address);
|
||||||
let profileHash = fromBytes32({ digest, hashFunction, hashSize });
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
address,
|
address,
|
||||||
|
balance: balance.toNumber(),
|
||||||
|
ipfsHash,
|
||||||
isCore,
|
isCore,
|
||||||
isCurrentUser,
|
isCurrentUser,
|
||||||
profileHash,
|
|
||||||
balance: balance.toNumber()
|
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
// Fetch IPFS data if available
|
// Fetch IPFS data if available
|
||||||
.then(this.loadContributorProfile.bind(this))
|
.then((data) => {
|
||||||
|
return this.fetchAndMergeIpfsData(data, ContributorSerializer);
|
||||||
|
})
|
||||||
.then((attributes) => {
|
.then((attributes) => {
|
||||||
return this.buildContributor(attributes);
|
return this.buildContributor(attributes);
|
||||||
});
|
});
|
||||||
@@ -149,11 +144,12 @@ export default Service.extend({
|
|||||||
getContributors() {
|
getContributors() {
|
||||||
return this.get('contributorsContract')
|
return this.get('contributorsContract')
|
||||||
.then((contract) => contract.contributorsCount())
|
.then((contract) => contract.contributorsCount())
|
||||||
.then(contributorsCount => {
|
.then((count) => {
|
||||||
debug('[kredits] contributorsCount:', contributorsCount.toNumber());
|
count = count.toNumber();
|
||||||
|
debug('[kredits] contributors count:', count);
|
||||||
let contributors = [];
|
let contributors = [];
|
||||||
|
|
||||||
for(var id = 1; id <= contributorsCount.toNumber(); id++) {
|
for(var id = 1; id <= count; id++) {
|
||||||
contributors.push(this.getContributorById(id));
|
contributors.push(this.getContributorById(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,6 +157,14 @@ export default Service.extend({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
reassembleIpfsHash(data) {
|
||||||
|
let { ipfsHash: digest, hashFunction, hashSize } = data;
|
||||||
|
data.ipfsHash = fromBytes32({ digest, hashFunction, hashSize });
|
||||||
|
delete data.hashFunction;
|
||||||
|
delete data.hashSize;
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the contributor's profile data from IPFS and returns the attributes
|
* Loads the contributor's profile data from IPFS and returns the attributes
|
||||||
@@ -168,26 +172,22 @@ export default Service.extend({
|
|||||||
* @method
|
* @method
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
loadContributorProfile(data) {
|
fetchAndMergeIpfsData(data, Serializer) {
|
||||||
let profileHash = data.profileHash;
|
let ipfsHash = data.ipfsHash;
|
||||||
|
|
||||||
if (!profileHash) {
|
if (!ipfsHash) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.get('ipfs')
|
return this.get('ipfs')
|
||||||
.getFile(profileHash)
|
.getFile(ipfsHash)
|
||||||
.then(ContributorSerializer.deserialize)
|
.then(Serializer.deserialize)
|
||||||
.then((attributes) => {
|
.then((attributes) => {
|
||||||
debug('[kredits] loaded contributor profile', attributes);
|
debug('[kredits] fetched ipfs data:', attributes);
|
||||||
return Object.assign({}, data, attributes);
|
return Object.assign({}, data, attributes);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
error(
|
error('[kredits] error trying to fetch', ipfsHash, err);
|
||||||
'[kredits] error trying to load contributor profile',
|
|
||||||
profileHash,
|
|
||||||
err
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -251,19 +251,20 @@ export default Service.extend({
|
|||||||
debug('[kredits] add contributor', attributes);
|
debug('[kredits] add contributor', attributes);
|
||||||
|
|
||||||
let json = ContributorSerializer.serialize(attributes);
|
let json = ContributorSerializer.serialize(attributes);
|
||||||
|
// TODO: validate against schema
|
||||||
|
|
||||||
return this.get('ipfs')
|
return this.get('ipfs')
|
||||||
.storeFile(json)
|
.storeFile(json)
|
||||||
// Set profileHash
|
// Set ipfsHash
|
||||||
.then((profileHash) => {
|
.then((ipfsHash) => {
|
||||||
attributes.profileHash = profileHash;
|
attributes.ipfsHash = ipfsHash;
|
||||||
return attributes;
|
return attributes;
|
||||||
})
|
})
|
||||||
.then((attributes) => {
|
.then((attributes) => {
|
||||||
return this.get('kreditsContract')
|
return this.get('kreditsContract')
|
||||||
.then((contract) => {
|
.then((contract) => {
|
||||||
let { address, isCore, profileHash } = attributes;
|
let { address, isCore, ipfsHash } = attributes;
|
||||||
let { digest, hashFunction, hashSize } = toBytes32(profileHash);
|
let { digest, hashFunction, hashSize } = toBytes32(ipfsHash);
|
||||||
|
|
||||||
let contributor = [
|
let contributor = [
|
||||||
address,
|
address,
|
||||||
|
|||||||
Reference in New Issue
Block a user