Skip to content

Commit 1f0ce9c

Browse files
committed
Add block list
1 parent 66ca315 commit 1f0ce9c

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

src/__tests__/__fixtures__/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export const getTweetDefaultConfigs = (overrides?: Partial<TweetConfig>, userCon
1818
minFavs: 3,
1919
minFavsToFollowers: 0.02,
2020
hashtagsLimit: 5,
21-
userConfig: getUserDefaultConfigs(userConfigOverrides)
21+
userConfig: getUserDefaultConfigs(userConfigOverrides),
22+
wordBlockList: []
2223
};
2324

2425
return {...defaultConfigs, ...overrides};

src/__tests__/entities/tweet.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,56 @@ describe('Tweet', () => {
145145
});
146146
});
147147

148+
describe('hasBlockListedWord', () => {
149+
it('should return true if word block list is in tweet', () => {
150+
const tweet = new Tweet(
151+
getRawTweet({
152+
text: 'this tweet has a bad word in it'
153+
}),
154+
getTweetDefaultConfigs({
155+
wordBlockList: ['bad', 'worst']
156+
})
157+
);
158+
159+
expect(tweet.hasBlockListedWord()).toBe(true);
160+
});
161+
162+
it('should return false if word block list is empty', () => {
163+
const tweet = new Tweet(
164+
getRawTweet(),
165+
getTweetDefaultConfigs()
166+
);
167+
168+
expect(tweet.hasBlockListedWord()).toBe(false);
169+
});
170+
171+
it('should return false if tweet text is empty', () => {
172+
const tweet = new Tweet(
173+
getRawTweet({
174+
text: ''
175+
}),
176+
getTweetDefaultConfigs({
177+
wordBlockList: ['bad', 'worst']
178+
})
179+
);
180+
181+
expect(tweet.hasBlockListedWord()).toBe(false);
182+
});
183+
184+
it('should return false if word block list is NOT in tweet', () => {
185+
const tweet = new Tweet(
186+
getRawTweet({
187+
text: 'this is a good tweet'
188+
}),
189+
getTweetDefaultConfigs({
190+
wordBlockList: ['bad', 'worst']
191+
})
192+
);
193+
194+
expect(tweet.hasBlockListedWord()).toBe(false);
195+
});
196+
});
197+
148198
describe('isReTweetable', () => {
149199
it('should return true if tweet is eligible to be retweeted', () => {
150200
const tweet = new Tweet(getRawTweet({
@@ -288,6 +338,22 @@ describe('Tweet', () => {
288338
expect(tweet.retweetError).toBe('Tweet is withheld');
289339
});
290340

341+
it('should return false if tweet has a block listed word', () => {
342+
const tweet = new Tweet(getRawTweet({
343+
text: 'this tweet is the worst.',
344+
favorite_count: 1000,
345+
user: getRawUser({
346+
followers_count: 1000,
347+
statuses_count: 1000,
348+
})
349+
}), getTweetDefaultConfigs({
350+
wordBlockList: ['worst']
351+
}));
352+
353+
expect(tweet.isReTweetable()).toBe(false);
354+
expect(tweet.retweetError).toBe('Tweet has block listed word in it');
355+
});
356+
291357
it('should return false if user is not eligible', () => {
292358
const tweet = new Tweet(getRawTweet({
293359
favorite_count: 1000,

src/entities/Tweet.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export type TweetConfig = {
77
minFavs: number,
88
minFavsToFollowers: number,
99
hashtagsLimit: number,
10-
userConfig: TweetUserConfig
10+
userConfig: TweetUserConfig,
11+
wordBlockList?: string[]
1112
};
1213

1314
export default class Tweet extends ReTweetableAbstract {
@@ -54,6 +55,17 @@ export default class Tweet extends ReTweetableAbstract {
5455
return Helper.objectExists(this.rawTweet.withheld_copyright) && this.rawTweet.withheld_copyright;
5556
}
5657

58+
hasBlockListedWord(): boolean {
59+
if (!Helper.objectExists(this.config.wordBlockList)
60+
|| !Helper.objectExists(this.rawTweet.text)) {
61+
return false;
62+
}
63+
64+
const tweetText = this.rawTweet.text.toLowerCase();
65+
66+
return this.config.wordBlockList.some(blockListedWord => tweetText.includes(blockListedWord.toLowerCase()));
67+
}
68+
5769
getRetweetValidations(): Validation[] {
5870
return [
5971
{
@@ -84,6 +96,10 @@ export default class Tweet extends ReTweetableAbstract {
8496
validate: () => this.isWithheld(),
8597
message: () => 'Tweet is withheld'
8698
},
99+
{
100+
validate: () => this.hasBlockListedWord(),
101+
message: () => 'Tweet has block listed word in it'
102+
},
87103
{
88104
validate: () => !this.tweetUser.isReTweetable(),
89105
message: () => this.tweetUser.retweetError

0 commit comments

Comments
 (0)