diff options
author | Anatoliy Sablin | 2017-11-06 15:45:55 +0300 |
---|---|---|
committer | Anatoliy Sablin | 2017-11-06 15:45:55 +0300 |
commit | 41b885d84958d07ade536850829b3ba394a4f8f9 (patch) | |
tree | 93661b9f558c4234afd4e5a84b0442fdb27454a2 /juick-commands/src/main/java/com/juick/command/ShowMessage.java | |
parent | ce0ed9ac9e82b19763b7dfd4814d327e0b49ddb7 (diff) |
Added a few commands.
Diffstat (limited to 'juick-commands/src/main/java/com/juick/command/ShowMessage.java')
-rw-r--r-- | juick-commands/src/main/java/com/juick/command/ShowMessage.java | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/juick-commands/src/main/java/com/juick/command/ShowMessage.java b/juick-commands/src/main/java/com/juick/command/ShowMessage.java new file mode 100644 index 00000000..f50dfba9 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/ShowMessage.java @@ -0,0 +1,61 @@ +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.apache.commons.lang3.math.NumberUtils; +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 ShowMessage extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^#(\\d+)(\\+?)$", Pattern.CASE_INSENSITIVE); + + private final MessagesService messagesService; + + @Autowired + public ShowMessage(MessagesService messagesService) { + this.messagesService = messagesService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "#1234 - Show message (#1234+ - message with replies)"; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + boolean showReplies = arguments[1].length() > 0; + int mid = NumberUtils.toInt(arguments[0], 0); + if (mid == 0) { + return "Error"; + } + Message msg = getMessagesService().getMessage(mid); + if (msg != null) { + if (showReplies) { + List<Message> replies = getMessagesService().getReplies(mid); + replies.add(0, msg); + return String.join("\n", replies.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())); + } + return PlainTextFormatter.formatPost(msg); + } + return "Message not found"; + } +} |