Skip to content

Commit

Permalink
Get the full text if it is truncated
Browse files Browse the repository at this point in the history
  • Loading branch information
iranianpep committed Jan 20, 2021
1 parent 5231e5d commit 6a549a3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
40 changes: 35 additions & 5 deletions src/__tests__/entities/tweet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand All @@ -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);
});
Expand All @@ -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: [' به گا ']
})
);

Expand Down Expand Up @@ -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');
});
});
});
14 changes: 12 additions & 2 deletions src/entities/Tweet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
{
Expand Down

0 comments on commit 6a549a3

Please sign in to comment.