diff options
Diffstat (limited to 'juick-commands/src/main/java/com/juick/command/LastMessages.java')
-rw-r--r-- | juick-commands/src/main/java/com/juick/command/LastMessages.java | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/juick-commands/src/main/java/com/juick/command/LastMessages.java b/juick-commands/src/main/java/com/juick/command/LastMessages.java new file mode 100644 index 00000000..77ba8a81 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/LastMessages.java @@ -0,0 +1,46 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.formatters.PlainTextFormatter; +import com.juick.server.protocol.ProtocolListener; +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, ProtocolListener 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")); + } +} |