Test Failed
Push — main ( 402e53...4169bc )
by Ehsan
03:02
created

Tweet.isReTweetable   C

Complexity

Conditions 9

Size

Total Lines 43
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

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