From 41b885d84958d07ade536850829b3ba394a4f8f9 Mon Sep 17 00:00:00 2001 From: Anatoliy Sablin Date: Mon, 6 Nov 2017 15:45:55 +0300 Subject: Added a few commands. --- .../src/main/java/com/juick/command/MyFeeds.java | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 juick-commands/src/main/java/com/juick/command/MyFeeds.java (limited to 'juick-commands/src/main/java/com/juick/command/MyFeeds.java') diff --git a/juick-commands/src/main/java/com/juick/command/MyFeeds.java b/juick-commands/src/main/java/com/juick/command/MyFeeds.java new file mode 100644 index 00000000..5156e3c5 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/MyFeeds.java @@ -0,0 +1,52 @@ +package com.juick.command; + +import com.juick.Message; +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.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * @author ma1uta + */ +@Component +@Getter +public class MyFeeds extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^(#+)$", Pattern.CASE_INSENSITIVE); + + private final MessagesService messagesService; + + @Autowired + public MyFeeds(MessagesService messagesService) { + this.messagesService = messagesService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "# - Show last messages from your feed (## - second page, ...)"; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + // number of # is the page count + int page = arguments[0].length() - 1; + List mids = getMessagesService().getMyFeed(sender.getUid(), page, false); + List messages = getMessagesService().getMessages(mids); + // TODO: add instructions for empty feed + return "Your feed: \n" + String.join("\n", + messages.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())); + } +} -- cgit v1.2.3