aboutsummaryrefslogtreecommitdiff
path: root/juick-commands/src/main/java/com/juick/command/BlacklistUser.java
blob: b138e816b67cee9cb1e54834ae6e53acbd118156 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.juick.command;

import com.juick.User;
import com.juick.service.PrivacyQueriesService;
import com.juick.service.UserService;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

/**
 * @author ma1uta
 */
@Component
@Getter
public class BlacklistUser extends MultiArgsCommand {

    private static final Pattern PATTERN = Pattern.compile("^bl\\s+@([^\\s\\n+]+)", Pattern.CASE_INSENSITIVE);

    private final UserService userService;
    private final PrivacyQueriesService privacyQueriesService;

    @Autowired
    public BlacklistUser(UserService userService, PrivacyQueriesService privacyQueriesService) {
        this.userService = userService;
        this.privacyQueriesService = privacyQueriesService;
    }

    @Override
    public Pattern pattern() {
        return PATTERN;
    }

    @Override
    public String help() {
        return "BL @username - add @username to your blacklist";
    }

    @Override
    protected String execute(User sender, MessageListener protocolListener, String... arguments) {
        User blUser = getUserService().getUserByName(arguments[0]);
        if (blUser != null) {
            PrivacyQueriesService.PrivacyResult result = getPrivacyQueriesService().blacklistUser(sender, blUser);
            if (result == PrivacyQueriesService.PrivacyResult.Added) {
                return "User added to your blacklist";
            } else {
                return "User removed from your blacklist";
            }
        }
        return "User not found";
    }
}