From 6a549a3ca68dd0392e7c31e093d75428f99242d6 Mon Sep 17 00:00:00 2001 From: Ehsan Date: Thu, 21 Jan 2021 10:08:11 +1100 Subject: [PATCH] Get the full text if it is truncated --- package.json | 2 +- src/__tests__/entities/tweet.test.ts | 40 ++++++++++++++++++++++++---- src/entities/Tweet.ts | 14 ++++++++-- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 4c568dd..c68c5d0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "retweeter", - "version": "1.0.11", + "version": "1.0.12", "description": "Simple bot that retweets", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/__tests__/entities/tweet.test.ts b/src/__tests__/entities/tweet.test.ts index 3f4837b..ab92f30 100644 --- a/src/__tests__/entities/tweet.test.ts +++ b/src/__tests__/entities/tweet.test.ts @@ -101,13 +101,17 @@ describe('Tweet', () => { }); describe('hasTooManyHashtags', () => { - it('should return true if tweet does not have too many hashtags', () => { + it('should return true if tweet has too many hashtags', () => { const tweet = new Tweet(getRawTweet({ entities: { hashtags: [ { indices: [1, 1], text: 'blah blah' + }, + { + indices: [1, 1], + text: 'blah2 blah2' } ], media: [], @@ -117,13 +121,13 @@ describe('Tweet', () => { polls: [] } }), getTweetDefaultConfigs({ - hashtagsLimit: 0 + hashtagsLimit: 1 })); expect(tweet.hasTooManyHashtags()).toBe(true); }); - it('should return true if tweet has too many hashtags', () => { + it('should return false if tweet does not have too many hashtags', () => { const tweet = new Tweet(getRawTweet(), getTweetDefaultConfigs()); expect(tweet.hasTooManyHashtags()).toBe(false); }); @@ -149,10 +153,10 @@ describe('Tweet', () => { it('should return true if word block list is in tweet', () => { const tweet = new Tweet( getRawTweet({ - text: 'this tweet has a bad word in it' + text: 'this tweet has a bad word in it به گا ' }), getTweetDefaultConfigs({ - wordBlocklist: ['bad', 'worst'] + wordBlocklist: [' به گا '] }) ); @@ -368,4 +372,30 @@ describe('Tweet', () => { expect(tweet.retweetError).toBe('User is not public'); }); }); + + describe('getText', () => { + it('should return full text if it is truncated', () => { + const tweet = new Tweet( + getRawTweet({ + truncated: true, + full_text: 'this is the full text' + }), + getTweetDefaultConfigs() + ); + + expect(tweet.getText()).toBe('this is the full text'); + }); + + it('should return text if it is not truncated', () => { + const tweet = new Tweet( + getRawTweet({ + truncated: false, + text: 'this is the text' + }), + getTweetDefaultConfigs() + ); + + expect(tweet.getText()).toBe('this is the text'); + }); + }); }); \ No newline at end of file diff --git a/src/entities/Tweet.ts b/src/entities/Tweet.ts index 6181c48..c76e9d5 100644 --- a/src/entities/Tweet.ts +++ b/src/entities/Tweet.ts @@ -57,15 +57,25 @@ export default class Tweet extends ReTweetableAbstract { hasBlockListedWord(): boolean { if (!Helper.objectExists(this.config.wordBlocklist) - || !Helper.objectExists(this.rawTweet.text)) { + || this.getText().length === 0) { return false; } - const tweetText = this.rawTweet.text.toLowerCase(); + const tweetText = this.getText().toLowerCase(); return this.config.wordBlocklist.some(blockListedWord => tweetText.includes(blockListedWord.toLowerCase())); } + getText(): string { + const text = this.rawTweet.truncated ? this.rawTweet.full_text : this.rawTweet.text; + + if (text) { + return text; + } + + return ''; + } + getRetweetValidations(): Validation[] { return [ {