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

TweetUser.isReTweetable   B

Complexity

Conditions 6

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 23
dl 0
loc 28
rs 8.3946
c 0
b 0
f 0
1
import moment from 'moment';
2
import {Twitter} from 'twit';
3
import Constant from '../Constant';
4
import Helper from '../Helper';
5
import {ReTweetableAbstract} from '../ReTweetable';
6
7
export type TweetUserConfig = {
8
    minCreationDiff: number,
9
    minFollowers: number,
10
    minTweets: number,
11
    userBlocklist?: string[]
12
};
13
14
export default class TweetUser extends ReTweetableAbstract {
15
    rawUser: Twitter.User;
16
    config: TweetUserConfig;
17
18
    constructor(rawUser: Twitter.User, config: TweetUserConfig) {
19
        super();
20
        this.rawUser = rawUser;
21
        this.config = config;
22
    }
23
24
    isPublic(): boolean {
25
        return !this.rawUser.protected;
26
    }
27
28
    isCreatedRecently(): boolean {
29
        const createdAt = moment(this.rawUser.created_at, Constant.TWITTER_DATETIME_FORMAT, Constant.LANG_EN);
30
        const now = moment();
31
32
        return now.diff(createdAt, Constant.MOMENT_DAYS) < this.config.minCreationDiff;
33
    }
34
35
    hasEnoughFollowers(): boolean {
36
        return this.rawUser.followers_count >= this.config.minFollowers;
37
    }
38
39
    hasEnoughTweets(): boolean {
40
        return this.rawUser.statuses_count >= this.config.minTweets;
41
    }
42
43
    isBlockListed(): boolean {
44
        return Helper.objectExists(this.config.userBlocklist) && this.config.userBlocklist.includes(this.rawUser.id_str);
45
    }
46
47
    isReTweetable(): boolean {
48
        if (this.isBlockListed()) {
49
            this.retweetError = 'User is in blocklist';
50
            return false;
51
        }
52
53
        if (!this.isPublic()) {
54
            this.retweetError = 'User is not public';
55
            return false;
56
        }
57
58
        if (this.isCreatedRecently()) {
59
            this.retweetError = 'User is created recently';
60
            return false;
61
        }
62
63
        if (!this.hasEnoughFollowers()) {
64
            this.retweetError = 'User does not have enough followers';
65
            return false;
66
        }
67
68
        if (!this.hasEnoughTweets()) {
69
            this.retweetError = 'User does not have enough tweets';
70
            return false;
71
        }
72
73
        return true;
74
    }
75
}