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

import com.juick.User;
import com.juick.formatters.PlainTextFormatter;
import com.juick.service.MessagesService;
import lombok.Getter;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author ma1uta
 */
@Component
@Getter
public class LastMessages implements Command {

    private static final Pattern PATTERN = Pattern.compile("^#\\+$", Pattern.CASE_INSENSITIVE);

    private final MessagesService messagesService;

    public LastMessages(MessagesService messagesService) {
        this.messagesService = messagesService;
    }

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

    @Override
    public String help() {
        return "#+ - Show last Juick messages";
    }

    @Override
    public String execute(User sender, MessageListener protocolListener, String command) {
        return "Last messages:\n"
            + getMessagesService().getMessages(getMessagesService().getAll(sender.getUid(), 0)).stream()
            .sorted(Collections.reverseOrder())
            .map(PlainTextFormatter::formatPostSummary).collect(Collectors.joining("\n\n"));
    }
}