aboutsummaryrefslogtreecommitdiff
path: root/juick-commands/src/main/java/com/juick/command/ShowUserInfo.java
diff options
context:
space:
mode:
authorGravatar Anatoliy Sablin2017-11-06 16:27:11 +0300
committerGravatar Anatoliy Sablin2017-11-06 16:27:11 +0300
commitd77ff16de5543b5820ee3b185660a052c5f57a15 (patch)
treeba701d56a441dc7e9caea89bad96929bc3a70d95 /juick-commands/src/main/java/com/juick/command/ShowUserInfo.java
parent41b885d84958d07ade536850829b3ba394a4f8f9 (diff)
Added rest commands.
Diffstat (limited to 'juick-commands/src/main/java/com/juick/command/ShowUserInfo.java')
-rw-r--r--juick-commands/src/main/java/com/juick/command/ShowUserInfo.java55
1 files changed, 55 insertions, 0 deletions
diff --git a/juick-commands/src/main/java/com/juick/command/ShowUserInfo.java b/juick-commands/src/main/java/com/juick/command/ShowUserInfo.java
new file mode 100644
index 00000000..336b2e0d
--- /dev/null
+++ b/juick-commands/src/main/java/com/juick/command/ShowUserInfo.java
@@ -0,0 +1,55 @@
+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 com.juick.service.UserService;
+import lombok.Getter;
+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 ShowUserInfo extends MultiArgsCommand {
+
+ private static final Pattern PATTERN = Pattern.compile("^@([^\\s\\n+]+)(\\+?)$", Pattern.CASE_INSENSITIVE);
+
+ private final UserService userService;
+ private final MessagesService messagesService;
+
+ public ShowUserInfo(UserService userService, MessagesService messagesService) {
+ this.userService = userService;
+ this.messagesService = messagesService;
+ }
+
+ @Override
+ public Pattern pattern() {
+ return PATTERN;
+ }
+
+ @Override
+ public String help() {
+ return "@username+ - Show user's info and last 10 messages (@username++ - second page, ..)";
+ }
+
+ @Override
+ protected String execute(User sender, ProtocolListener protocolListener, String... arguments) {
+ User blogUser = getUserService().getUserByName(arguments[0]);
+ int page = arguments[1].length();
+ if (blogUser.getUid() > 0) {
+ List<Integer> mids = getMessagesService().getUserBlog(blogUser.getUid(), 0, page);
+ List<com.juick.Message> messages = getMessagesService().getMessages(mids);
+ return String.format("Last messages from @%s:\n%s", arguments[0],
+ String.join("\n", messages.stream()
+ .map(PlainTextFormatter::formatPost).collect(Collectors.toList())));
+ }
+ return "User not found";
+ }
+}