Hello Kredits world
This commit is contained in:
44
lib/ipfs-pinner.js
Normal file
44
lib/ipfs-pinner.js
Normal file
@@ -0,0 +1,44 @@
|
||||
class IpfsPinner {
|
||||
constructor (kredits, ipfsApi) {
|
||||
this.kredits = kredits;
|
||||
this.ipfsApi = ipfsApi || this.kredits.ipfs;
|
||||
}
|
||||
|
||||
pinAll () {
|
||||
return Promise.all([
|
||||
this._pinAllFromContract(this.kredits.Contributor),
|
||||
this._pinAllFromContract(this.kredits.Contribution)
|
||||
]);
|
||||
}
|
||||
|
||||
monitor (callback) {
|
||||
this.kredits.Contribution.on('ContributionAdded', (id) => {
|
||||
this.kredits.Contribution.getData(id)
|
||||
.then(data => { return this.ipfsApi.pin(data); })
|
||||
.then(callback);
|
||||
});
|
||||
this.kredits.Contributor.on('ContributorAdded', (id) => {
|
||||
this.kredits.Contribution.getData(id)
|
||||
.then(data => { return this.ipfsApi.pin(data); })
|
||||
.then(callback);
|
||||
});
|
||||
this.kredits.Contributor.on('ContributorProfileUpdated', (id) => {
|
||||
this.kredits.Contributor.getData(id)
|
||||
.then(data => { return this.ipfsApi.pin(data); })
|
||||
.then(callback);
|
||||
});
|
||||
}
|
||||
|
||||
_pinAllFromContract (contract) {
|
||||
return contract.count.then(count => {
|
||||
let promises = [...Array(count).keys()].map(i => {
|
||||
let id = i + 1; // 0 => 1 - ids start with 1 and not with 0
|
||||
return contract.getData(id).then(data => {
|
||||
return this.ipfsApi.pin(data);
|
||||
});
|
||||
});
|
||||
return Promise.all(promises);
|
||||
});
|
||||
}
|
||||
}
|
||||
module.exports = IpfsPinner;
|
||||
Reference in New Issue
Block a user