Skip to content

Commit

Permalink
fix: fixes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
bacher committed Sep 23, 2019
1 parent 95531c1 commit 19d3ae8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ const core = require('gls-core-service');
const BasicMain = core.services.BasicMain;
const env = require('./data/env');
const Connector = require('./services/Connector');
const Cleaner = require('./services/Cleaner');
const MongoDB = core.services.MongoDB;

class Main extends BasicMain {
constructor() {
super(env);
this.addNested(new Connector(), new MongoDB());
this.addNested(new MongoDB(), new Connector(), new Cleaner());
}
}

Expand Down
26 changes: 18 additions & 8 deletions src/controllers/BandwidthProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ class BandwidthProvider extends BasicController {
userId: auth.actor,
permission: auth.permission,
},
expirationTime: new Date(Date.now() + 3600000),
action: JSON.stringify(action),
expirationTime: new Date(trx.expiration + 'Z'),
action,
serializedTransaction: transaction.serializedTransaction,
signatures: finalTrx.signatures,
});
Expand Down Expand Up @@ -277,10 +277,12 @@ class BandwidthProvider extends BasicController {
};
}

async getProposals({ auth: { user } }) {
async getProposals({ auth: { user }, params: { contract, method } }) {
const items = await ProposalModel.find(
{
'waitingFor.userId': user,
'action.account': contract,
'action.name': method,
expirationTime: {
$gt: new Date(),
},
Expand All @@ -297,14 +299,22 @@ class BandwidthProvider extends BasicController {
}
);

const { usernames } = await this.callService('prism', 'getUsernames', {
userIds: items.map(({ initiatorId }) => initiatorId),
});
let usernames = {};

try {
const results = await this.callService('prism', 'getUsernames', {
userIds: items.map(({ initiatorId }) => initiatorId),
});

usernames = results.usernames;
} catch (err) {
Logger.warn('getUsernames failed:', err.message);
}

for (const item of items) {
item.id = item._id;
item.proposalId = item._id;
item._id = undefined;
item.initiatorUsername = usernames[item.initiatorId];
item.initiatorUsername = usernames[item.initiatorId] || null;
}

return {
Expand Down
4 changes: 3 additions & 1 deletion src/model/Proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = MongoDB.makeModel(
},
},
action: {
type: String,
type: Object,
required: true,
},
serializedTransaction: {
Expand All @@ -43,6 +43,8 @@ module.exports = MongoDB.makeModel(
{
fields: {
'waitingFor.userId': 1,
'action.account': 1,
'action.name': 1,
},
},
{
Expand Down
31 changes: 31 additions & 0 deletions src/services/Cleaner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const core = require('gls-core-service');
const BasicService = core.services.Basic;
const Logger = core.utils.Logger;

const ProposalModel = require('../model/Proposal');

const CLEAR_EVERY = 120000;

class Cleaner extends BasicService {
async start() {
this.startLoop(CLEAR_EVERY, CLEAR_EVERY);
}

async iteration() {
try {
await this._clear();
} catch (err) {
Logger.warn('Clearing failed:', err);
}
}

async _clear() {
await ProposalModel.deleteMany({
expirationTime: {
$lte: new Date(),
},
});
}
}

module.exports = Cleaner;
2 changes: 1 addition & 1 deletion src/services/StorageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Storage extends BasicService {
try {
this._cleanup();
} catch (error) {
Logger.error(`Cleanup error - ${error.stack}`);
Logger.error('Cleanup error:', error);
}
}

Expand Down

0 comments on commit 19d3ae8

Please sign in to comment.