Passed
Push — main ( 309c7d...4a8caa )
by Ehsan
03:04
created

Tweet.getRetweetValidations   A

Complexity

Conditions 1

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 27
dl 0
loc 34
ccs 17
cts 17
cp 1
crap 1
rs 9.232
c 0
b 0
f 0
1
import {Twitter} from 'twit';
2 4
import Helper from '../Helper';
3 4
import {ReTweetableAbstract, Validation} from '../ReTweetable';
4 4
import TweetUser, {TweetUserConfig} from './TweetUser';
5
6
export type TweetConfig = {
7
    minFavs: number,
8
    minFavsToFollowers: number,
9
    hashtagsLimit: number,
10
    userConfig: TweetUserConfig
11
};
12
13 4
export default class Tweet extends ReTweetableAbstract {
14
    rawTweet: Twitter.Status;
15
    tweetUser: TweetUser;
16
    config: TweetConfig;
17
18
    constructor(rawTweet: Twitter.Status, config: TweetConfig) {
19 35
        super();
20 35
        this.rawTweet = rawTweet;
21 35
        this.tweetUser = new TweetUser(this.rawTweet.user, config.userConfig);
22 35
        this.config = config;
23
    }
24
25
    isRetweet(): boolean {
26 19
        return this.rawTweet.retweeted || Helper.objectExists(this.rawTweet.retweeted_status);
27
    }
28
29
    isReply(): boolean {
30 14
        return Helper.objectExists(this.rawTweet.in_reply_to_screen_name)
31
        || Helper.objectExists(this.rawTweet.in_reply_to_status_id)
32
        || Helper.objectExists(this.rawTweet.in_reply_to_status_id_str)
33
        || Helper.objectExists(this.rawTweet.in_reply_to_user_id)
34
        || Helper.objectExists(this.rawTweet.in_reply_to_user_id_str);
35
    }
36
37
    isSensitive(): boolean {
38 13
        return Helper.objectExists(this.rawTweet.possibly_sensitive) && this.rawTweet.possibly_sensitive;
39
    }
40
41
    hasMinFavs(): boolean {
42 11
        return Helper.objectExists(this.rawTweet.favorite_count) && this.rawTweet.favorite_count >= this.config.minFavs;
43
    }
44
45
    hasMinFavsToFollowersRatio(): boolean {
46 10
        return Helper.objectExists(this.rawTweet.favorite_count) && this.rawTweet.favorite_count / this.rawTweet.user.followers_count >= this.config.minFavsToFollowers;
47
    }
48
49
    hasTooManyHashtags(): boolean {
50 9
        return this.rawTweet.entities.hashtags.length > this.config.hashtagsLimit;
51
    }
52
53
    isWithheld(): boolean {
54 8
        return Helper.objectExists(this.rawTweet.withheld_copyright) && this.rawTweet.withheld_copyright;
55
    }
56
57
    getRetweetValidations(): Validation[] {
58 14
        return [
59
            {
60 14
                validate: () => this.isRetweet(),
61 2
                message: () => 'Tweet is retweet'
62
            },
63
            {
64 12
                validate: () => this.isReply(),
65 1
                message: () => 'Tweet is reply'
66
            },
67
            {
68 11
                validate: () => this.isSensitive(),
69 2
                message: () => 'Tweet is sensitive'
70
            },
71
            {
72 9
                validate: () => !this.hasMinFavs(),
73 1
                message: () => 'Tweet does not have min favs'
74
            },
75
            {
76 8
                validate: () => !this.hasMinFavsToFollowersRatio(),
77 1
                message: () => 'Tweet does not have min favs to followers'
78
            },
79
            {
80 7
                validate: () => this.hasTooManyHashtags(),
81 1
                message: () => 'Tweet has too many hashtags'
82
            },
83
            {
84 6
                validate: () => this.isWithheld(),
85 1
                message: () => 'Tweet is withheld'
86
            },
87
            {
88 5
                validate: () => !this.tweetUser.isReTweetable(),
89 1
                message: () => this.tweetUser.retweetError
90
            },
91
        ];
92
    }
93
}