Test Failed
Push — main ( 310e6d...a95597 )
by Ehsan
06:01 queued 02:56
created

Tweet.hasMinFavsToFollowersRatio   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
crap 1
1
import {Twitter} from 'twit';
2 2
import Helper from './Helper';
3 2
import {ReTweetableAbstract} from './ReTweetable';
4 2
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 2
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 56
        super();
20 56
        this.rawTweet = rawTweet;
21 56
        this.tweetUser = new TweetUser(this.rawTweet.user, config.userConfig);
22 56
        this.config = config;
23
    }
24
25
    isRetweet(): boolean {
26 31
        return this.rawTweet.retweeted || Helper.objectExists(this.rawTweet.retweeted_status);
27
    }
28
29
    isReply(): boolean {
30 26
        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 24
        return Helper.objectExists(this.rawTweet.possibly_sensitive) && this.rawTweet.possibly_sensitive;
39
    }
40
41
    hasMinFavs(): boolean {
42 20
        return Helper.objectExists(this.rawTweet.favorite_count) && this.rawTweet.favorite_count >= this.config.minFavs;
43
    }
44
45
    hasMinFavsToFollowersRatio(): boolean {
46 18
        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 16
        return this.rawTweet.entities.hashtags.length > this.config.hashtagsLimit;
51
    }
52
53
    isWithheld(): boolean {
54 14
        return Helper.objectExists(this.rawTweet.withheld_copyright) && this.rawTweet.withheld_copyright;
55
    }
56
57
    isReTweetable(): boolean {
58 25
        if (this.isRetweet()) {
59 3
            this.retweetError = 'Tweet is retweet';
60 3
            return false;
61
        }
62
63 22
        if (this.isReply()) {
64 2
            this.retweetError = 'Tweet is reply';
65 2
            return false;
66
        }
67
68 20
        if (this.isSensitive()) {
69 4
            this.retweetError = 'Tweet is sensitive';
70 4
            return false;
71
        }
72
73 16
        if (!this.hasMinFavs()) {
74 2
            this.retweetError = 'Tweet does not have min favs';
75 2
            return false;
76
        }
77
78 14
        if (!this.hasMinFavsToFollowersRatio()) {
79 2
            this.retweetError = 'Tweet does not have min favs to followers';
80 2
            return false;
81
        }
82
83 12
        if (this.hasTooManyHashtags()) {
84 2
            this.retweetError = 'Tweet has too many hashtags';
85 2
            return false;
86
        }
87
88 10
        if (this.isWithheld()) {
89 2
            this.retweetError = 'Tweet is withheld';
90 2
            return false;
91
        }
92
93 8
        if (!this.tweetUser.isReTweetable()) {
94 2
            this.retweetError = this.tweetUser.retweetError;
95 2
            return false;
96
        }
97
98 6
        return true;
99
    }
100
}