aboutsummaryrefslogtreecommitdiff
path: root/juick-commands/src/main/java/com/juick/command/EditOrReplyMessage.java
blob: e7fbf764c6e6a27a90a85adf9c045d5f32a2c5f8 (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
59
60
61
62
63
64
package com.juick.command;

import com.juick.Tag;
import com.juick.User;
import com.juick.service.MessagesService;
import com.juick.service.TagService;
import lombok.Getter;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.regex.Pattern;

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

    private static final Pattern PATTERN = Pattern.compile("^([#.])(\\d+)(([.-/])(\\d+))?\\s([\\s\\S]+)", Pattern.CASE_INSENSITIVE);

    private final TagService tagService;
    private final MessagesService messagesService;
    @Value("${baseuri}")
    private String baseUri = "http://juick.com";

    @Autowired
    public EditOrReplyMessage(TagService tagService, MessagesService messagesService) {
        this.tagService = tagService;
        this.messagesService = messagesService;
    }

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

    @Override
    public String help() {
        return "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message";
    }

    @Override
    protected String execute(User sender, MessageListener protocolListener, String... arguments) {
        int mid = NumberUtils.toInt(arguments[1]);
        int rid = NumberUtils.toInt(arguments[4], 0);
        String txt = arguments[5];
        List<Tag> messageTags = getTagService().fromString(txt, true);
        if (messageTags.size() > 0) {
            if (sender.getUid() != getMessagesService().getMessageAuthor(mid).getUid()) {
                return "It is not your message";
            }
            getTagService().updateTags(mid, messageTags);
            return "Tags are updated";
        } else {
            int newrid = getMessagesService().createReply(mid, rid, sender.getUid(), txt, null);
            protocolListener.messagePosted(getMessagesService().getReply(mid, newrid));
            return String.format("Reply posted.\n#%d/%d %s%d#%d", mid, newrid, getBaseUri(), mid, newrid);
        }
    }
}