Refactor getContributorById to be move generic #37

Merged
fsmanuel merged 2 commits from refactor/get-contributor-by-id into master 2018-04-07 23:32:46 +00:00
+32 -31
View File
@@ -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() {
bumi commented 2018-04-07 23:05:38 +00:00 (Migrated from github.com)
Review

was wondering about the naming here... as it is more like building the ipfs hash - but then it is also replacing the "ipfsHash" attribute.

was wondering about the naming here... as it is more like building the ipfs hash - but then it is also replacing the "ipfsHash" attribute.
bumi commented 2018-04-07 23:06:31 +00:00 (Migrated from github.com)
Review

as ipfsHash gets replaced here, should the hashFunction and hashSize be removed from the data, those only work with the hex value that we replace

as ipfsHash gets replaced here, should the hashFunction and hashSize be removed from the data, those only work with the hex value that we replace
fsmanuel commented 2018-04-07 23:19:08 +00:00 (Migrated from github.com)
Review

Good point. I was just ignoring them here:
https://github.com/67P/kredits-web/pull/37/files#diff-13940afb5bb6bef4e0cd9dfe4dcecb7cR123
Not sure what's the beste. But as you pointed out they are useless after that step. I'll remove them.

Good point. I was just ignoring them here: https://github.com/67P/kredits-web/pull/37/files#diff-13940afb5bb6bef4e0cd9dfe4dcecb7cR123 Not sure what's the beste. But as you pointed out they are useless after that step. I'll remove them.
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,