aboutsummaryrefslogtreecommitdiff
path: root/juick-commands/src/main/java/com/juick/command/PrivateMessage.java
blob: e41b0037eba041a871b99ba376b1f1e0055800be (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
54
55
56
57
58
package com.juick.command;

import com.juick.User;
import com.juick.server.protocol.ProtocolListener;
import com.juick.service.PMQueriesService;
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 PrivateMessage extends MultiArgsCommand {

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

    private final UserService userService;
    private final PMQueriesService pmQueriesService;

    @Autowired
    public PrivateMessage(UserService userService, PMQueriesService pmQueriesService) {
        this.userService = userService;
        this.pmQueriesService = pmQueriesService;
    }

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

    @Override
    public String help() {
        return "@username message - send PM to username";
    }

    @Override
    protected String execute(User sender, ProtocolListener protocolListener, String... arguments) {
        String addressee = arguments[0];
        String body = arguments[1];

        User toUser = getUserService().getUserByName(addressee);

        if (toUser.getUid() > 0) {
            if (!getUserService().isInBLAny(toUser.getUid(), sender.getUid())) {
                if (getPmQueriesService().createPM(sender.getUid(), toUser.getUid(), body)) {
                    protocolListener.privateMessage(sender, toUser, body);
                    return "Private message sent";
                }
            }
        }
        return "Error";
    }
}