Skip to content

Commit

Permalink
feat: get tweet count by accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
WatchDG authored and AVVS committed Aug 23, 2022
1 parent aeb28f6 commit 7e99a4f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ lib
docs
.idea
.vscode
/v8-compile-cache-0
18 changes: 18 additions & 0 deletions schemas/tweet.count.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$id": "tweet.count",
"type": "array",
"items": {
"type": "object",
"required": [
"account"
],
"properties": {
"account": {
"type": "string"
},
"cursor": {
"$ref": "common.json#/definitions/tweetId"
}
}
}
}
19 changes: 19 additions & 0 deletions src/actions/tweet.count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { ActionTransport } = require('@microfleet/plugin-router');

/**
* @api {http} <prefix>.tweet.count Count tweets by accounts
* @apiVersion 1.0.0
* @apiName tweet.count
* @apiGroup Feed
* @apiSchema {jsonschema=../../schemas/tweet.count.json} apiParam
*/
async function TweetCountAction({ params }) {
const data = await this.service('feed')
.countByAccounts(params);
return { data };
}

TweetCountAction.schema = 'tweet.count';
TweetCountAction.transports = [ActionTransport.amqp];

module.exports = TweetCountAction;
6 changes: 6 additions & 0 deletions src/services/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ class Feed {
.twitterStatuses()
.byId(data.tweetId);
}

async countByAccounts(data) {
return this.service('storage')
.twitterStatuses()
.countByAccounts(data);
}
}

module.exports = Feed;
25 changes: 25 additions & 0 deletions src/services/storage/twitter-statuses.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ class TwitterStatuses {
return query;
}

countByAccount(data) {
const {
account,
cursor,
} = data;

const query = this.knex
.select('account')
.countDistinct('id')
.from(this.table)
.where('account', account)
.groupBy('account');

if (cursor) {
query.where('id', '>', cursor);
}

return query;
}

countByAccounts(data) {
const subqueries = data.map(({ account, cursor }) => this.countByAccount({ account, cursor }));
return this.knex.unionAll(subqueries);
}

remove(data) {
return this.knex(this.table).where('account', data.account).del();
}
Expand Down
4 changes: 4 additions & 0 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ services:
NCONF_FILE_PATH: '["/src/configs/ms-users/config.js"]'
MS_USERS__INIT_ADMIN_ACCOUNTS_DELAY: '1'

postgres:
ports:
- "65432:5432"

tester:
depends_on:
- postgres
Expand Down
5 changes: 5 additions & 0 deletions test/suites/01.twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ describe('twitter', function testSuite() {
assert(meta.favorite_count);
});

it('get count by accounts', async () => {
const reply = await service.amqp.publishAndWait('social.tweet.count', [{ account: 'EvgenyPoyarkov' }, { account: 'v_aminev' }]);
assert.notEqual(reply.data.length, 0);
});

it('rejects with error on get tweet validation', async () => {
await assert.rejects(service.amqp.publishAndWait(uri.getOne, payload.invalidTweet), {
name: 'HttpStatusError',
Expand Down

0 comments on commit 7e99a4f

Please sign in to comment.