Skip to content

Commit

Permalink
feat(twitter): skip filtering for the valid accounts (#80)
Browse files Browse the repository at this point in the history
* feat(twitter): skip filtering for the valid accounts

* fix: use map for valid accountIds

* fix: avoid from lint camelcase warnings

* fix: rename to skipValidAccounts
  • Loading branch information
sky3d authored Apr 15, 2021
1 parent 63547c5 commit 9c755d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions schemas/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@
"retweets": {
"type": "boolean",
"default": false
},
"skipValidAccounts": {
"description": "skip filtering for valid accounts",
"type": "boolean",
"default": false
}
}
},
Expand Down
1 change: 1 addition & 0 deletions src/configs/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
stream_filters: {
replies: false,
retweets: false,
skipValidAccounts: false,
},
},
};
22 changes: 20 additions & 2 deletions src/services/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
const Notifier = require('./notifier');
const { transform, TYPE_TWEET } = require('../utils/response');


function extractAccount(accum, value) {
const accountId = value.meta.account_id;

Expand Down Expand Up @@ -68,8 +69,9 @@ class Twitter {
if (isNil(retweet)) {
return false;
}
const tweetOwnerId = get(retweet, 'user.id');
// Keep the tweets which are retweeted by the user
return get(retweet, 'user.id') !== data.user.id;
return tweetOwnerId !== data.user.id;
}

static isReply = (data) => {
Expand Down Expand Up @@ -197,6 +199,8 @@ class Twitter {
this.logger = logger.child({ namespace: '@social/twitter' });
this._destroyed = false;
this.following = [];
this.accountIds = {};

this.fetchTweets = Twitter.tweetFetcherFactory(this.client, this.logger, twitterApiConfig(config));

// cheaper than bind
Expand Down Expand Up @@ -257,6 +261,14 @@ class Twitter {
: [];
}

fillAccountIds(accounts = []) {
this.accountIds = accounts.reduce(
(map, it) => ({ ...map, [it.account_id]: true }),
{}
);
Object.setPrototypeOf(this.accountIds, null);
}

listen(accounts) {
const params = {};
if (accounts.length > 0) {
Expand All @@ -265,6 +277,7 @@ class Twitter {
.join(',');

this.setFollowing(accounts);
this.fillAccountIds(accounts);
}

if (!params.follow) {
Expand Down Expand Up @@ -366,7 +379,12 @@ class Twitter {
}

shouldFilterTweet(data) {
const { replies, retweets } = this.filterOptions;
const { replies, retweets, skipValidAccounts } = this.filterOptions;

// Don't filter retweets posted by the valid users
if (skipValidAccounts && this.accountIds[data.user.id] !== undefined) {
return false;
}
if (replies && Twitter.isReply(data)) {
return true;
}
Expand Down

0 comments on commit 9c755d0

Please sign in to comment.