aboutsummaryrefslogtreecommitdiff
path: root/juick-commands/src/main/java/com/juick/command/PostMessage.java
blob: 9dba3396f27134e403282b1fd66e8054a7170836 (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
package com.juick.command;

import com.juick.Tag;
import com.juick.User;
import com.juick.server.util.TagUtils;
import com.juick.service.MessagesService;
import com.juick.service.SubscriptionService;
import com.juick.service.TagService;
import lombok.Getter;
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 PostMessage implements Command {

    private final TagService tagService;
    private final MessagesService messagesService;
    private final SubscriptionService subscriptionService;

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

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

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

    @Override
    public String help() {
        return "";
    }

    @Override
    public String execute(User sender, MessageListener protocolListener, String command) {
        List<Tag> tags = getTagService().fromString(command, false);
        String body = command.substring(TagUtils.toString(tags).length());
        int mid = getMessagesService().createMessage(sender.getUid(), body, null, tags);
        getSubscriptionService().subscribeMessage(mid, sender.getUid());
        protocolListener.messagePosted(getMessagesService().getMessage(mid));
        return String.format("New message posted.\n#%d %s%d", mid, getBaseUri(), mid);
    }
}