From 0f8d7d84caaa2235a014aa0339d6d840384c98e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Fri, 11 Nov 2022 16:28:59 +0100 Subject: [PATCH 1/4] Don't cache contribution details in browser storage --- app/utils/process-contribution-data.js | 8 ++++++-- .../utils/process-contribution-data-test.js | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/utils/process-contribution-data.js b/app/utils/process-contribution-data.js index a12395e..0fc167c 100644 --- a/app/utils/process-contribution-data.js +++ b/app/utils/process-contribution-data.js @@ -1,4 +1,4 @@ -export default function processContributionData(data) { +export default function processContributionData(data, options={}) { const processed = {} if (data.confirmedAtBlock && (typeof data.confirmedAtBlock.toNumber === 'function')) { @@ -9,12 +9,16 @@ export default function processContributionData(data) { const otherProperties = [ 'id', 'contributorId', 'amount', 'vetoed', 'ipfsHash', 'kind', - 'description', 'details', 'url', 'date', 'time', 'pendingTx' + 'description', 'url', 'date', 'time', 'pendingTx' ]; otherProperties.forEach(prop => { processed[prop] = data[prop]; }); + if (options.includeDetails) { + processed.details = data.details; + } + return processed; } diff --git a/tests/unit/utils/process-contribution-data-test.js b/tests/unit/utils/process-contribution-data-test.js index 9c93c02..8355133 100644 --- a/tests/unit/utils/process-contribution-data-test.js +++ b/tests/unit/utils/process-contribution-data-test.js @@ -4,16 +4,18 @@ import testData from '../../fixtures/contribution-data'; module('Unit | Utility | process-contribution-data', function() { - let result = processContributionData(testData); - test('formats the data correctly', function(assert) { + const result = processContributionData(testData); + assert.ok(typeof result.confirmedAt === 'number'); }); test('copies other properties', function(assert) { + const result = processContributionData(testData); + [ 'id', 'contributorId', 'amount', 'vetoed', - 'ipfsHash', 'kind', 'description', 'details', + 'ipfsHash', 'kind', 'description', 'url', 'date', 'time', 'pendingTx' ].forEach(p => { assert.ok(Object.prototype.hasOwnProperty.call(result, p), `copies property ${p}`); @@ -21,8 +23,17 @@ module('Unit | Utility | process-contribution-data', function() { }); test('does not copy unnecessary properties', function(assert) { + const result = processContributionData(testData); + ['exists', '5'].forEach(p => { assert.notOk(Object.prototype.hasOwnProperty.call(result, p)); }) }); + + test('includeDetails option', function(assert) { + const result = processContributionData(testData, { includeDetails: true }); + + assert.ok(Object.prototype.hasOwnProperty.call(result, 'details'), 'includes the details property/value'); + }); + }); From 11f19c8344e994409db09ee50cc3d97d6774dfbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Fri, 11 Nov 2022 20:28:26 +0100 Subject: [PATCH 2/4] Refactor adding new contributions from events Fixes contributions not being added when not already in memory (from creation). Simplifies the entire function. --- app/services/kredits.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/services/kredits.js b/app/services/kredits.js index a069a7d..3a4366a 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -305,11 +305,11 @@ export default Service.extend({ }), addContribution (attributes) { - console.debug('[kredits] add contribution', attributes); + console.debug('[kredits] Adding contribution', attributes); - return this.kredits.Contribution.addContribution(attributes, { gasLimit: 300000 }) + return this.kredits.Contribution.add(attributes, { gasLimit: 300000 }) .then(data => { - console.debug('[kredits] add contribution response', data); + console.debug('[kredits] Contribution.add response', data); attributes.contributor = this.contributors.findBy('id', attributes.contributorId); const contribution = Contribution.create(attributes); contribution.set('pendingTx', data); @@ -667,12 +667,12 @@ export default Service.extend({ }); if (pendingContribution) { - const attributes = await this.kredits.Contribution.getById(id); - attributes.contributor = this.contributors.findBy('id', attributes.contributorId); - const newContribution = Contribution.create(attributes); - this.contributions.addObject(newContribution); this.contributions.removeObject(pendingContribution); } + + const data = await this.kredits.Contribution.getById(id); + const c = this.loadContributionFromData(data); + await this.browserCache.contributions.setItem(c.id.toString(), c.serialize()); }, handleContributionVetoed (contributionId) { From 92056517a6f8bb8c99668a65cddf833c44c4c19d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Fri, 11 Nov 2022 20:29:54 +0100 Subject: [PATCH 3/4] Cache contribution vetos Fixes potentially not marking contributions as vetoed if there's no incoming event after loading the app. Also improves performance either way. --- app/services/kredits.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/services/kredits.js b/app/services/kredits.js index 3a4366a..f47954d 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -675,14 +675,15 @@ export default Service.extend({ await this.browserCache.contributions.setItem(c.id.toString(), c.serialize()); }, - handleContributionVetoed (contributionId) { + async handleContributionVetoed (contributionId) { console.debug('[kredits] ContributionVetoed event received for ', contributionId); - const contribution = this.contributions.findBy('id', contributionId); - console.debug('[kredits] contribution', contribution); + const c = this.contributions.findBy('id', contributionId); - if (contribution) { - contribution.set('vetoed', true); - contribution.set('pendingTx', null); + if (c) { + console.debug('[kredits] Updating contribution', c); + c.set('vetoed', true); + c.set('pendingTx', null); + await this.browserCache.contributions.setItem(c.id.toString(), c.serialize()); } }, From ff3fe49dd0769f79dd1f281083fb34447dc7b83e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Fri, 11 Nov 2022 21:10:21 +0100 Subject: [PATCH 4/4] Wording --- app/controllers/dashboard.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/dashboard.js b/app/controllers/dashboard.js index 433a0cb..a8bccb9 100644 --- a/app/controllers/dashboard.js +++ b/app/controllers/dashboard.js @@ -41,7 +41,7 @@ export default Controller.extend({ vetoContribution (contributionId) { this.kredits.veto(contributionId).then(transaction => { - console.debug('[controllers:index] Veto submitted to Ethereum blockhain: '+transaction.hash); + console.debug('[controllers:index] Veto submitted to chain: '+transaction.hash); }); },