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 | |
parent | ce0ed9ac9e82b19763b7dfd4814d327e0b49ddb7 (diff) |
Added a few commands.
10 files changed, 384 insertions, 3 deletions
diff --git a/juick-commands/src/main/java/com/juick/command/Command.java b/juick-commands/src/main/java/com/juick/command/Command.java index 3d974bba..9a8775e5 100644 --- a/juick-commands/src/main/java/com/juick/command/Command.java +++ b/juick-commands/src/main/java/com/juick/command/Command.java @@ -12,5 +12,7 @@ public interface Command { Pattern pattern(); + String help(); + String execute(User sender, ProtocolListener protocolListener, String command); } diff --git a/juick-commands/src/main/java/com/juick/command/DeleteMessage.java b/juick-commands/src/main/java/com/juick/command/DeleteMessage.java new file mode 100644 index 00000000..ab46a28a --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/DeleteMessage.java @@ -0,0 +1,47 @@ +package com.juick.command; + +import com.juick.User; +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.regex.Pattern; + +/** + * @author ma1uta + */ +@Getter +@Component +public class DeleteMessage extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^d\\s*\\#([0-9]+)$", Pattern.CASE_INSENSITIVE); + + private final MessagesService messagesService; + + @Autowired + public DeleteMessage(MessagesService messagesService) { + this.messagesService = messagesService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "D #12345 - delete the message"; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + int mid = NumberUtils.toInt(arguments[0], 0); + if (getMessagesService().deleteMessage(sender.getUid(), mid)) { + return String.format("Message %s deleted", mid); + } + return "Error"; + } +} diff --git a/juick-commands/src/main/java/com/juick/command/EditOrReplyMessage.java b/juick-commands/src/main/java/com/juick/command/EditOrReplyMessage.java new file mode 100644 index 00000000..00ec4be9 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/EditOrReplyMessage.java @@ -0,0 +1,65 @@ +package com.juick.command; + +import com.juick.Tag; +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.MessagesService; +import com.juick.service.TagService; +import lombok.Getter; +import org.apache.commons.lang3.math.NumberUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class EditOrReplyMessage extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^([#.])(\\d+)(([.-/])(\\d+))?\\s([\\s\\S]+)", Pattern.CASE_INSENSITIVE); + + private final TagService tagService; + private final MessagesService messagesService; + @Value("${baseuri}") + private String baseUri = "http://juick.com"; + + @Autowired + public EditOrReplyMessage(TagService tagService, MessagesService messagesService) { + this.tagService = tagService; + this.messagesService = messagesService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message"; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + int mid = NumberUtils.toInt(arguments[1]); + int rid = NumberUtils.toInt(arguments[4], 0); + String txt = arguments[5]; + List<Tag> messageTags = getTagService().fromString(txt, true); + if (messageTags.size() > 0) { + if (sender.getUid() != getMessagesService().getMessageAuthor(mid).getUid()) { + return "It is not your message"; + } + getTagService().updateTags(mid, messageTags); + return "Tags are updated"; + } else { + int newrid = getMessagesService().createReply(mid, rid, sender.getUid(), txt, null); + protocolListener.messagePosted(getMessagesService().getReply(mid, newrid)); + return String.format("Reply posted.\n#%d/%d %s%d#%d", mid, newrid, getBaseUri(), mid, newrid); + } + } +} 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<Integer> mids = getMessagesService().getMyFeed(sender.getUid(), page, false); + List<Message> 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())); + } +} diff --git a/juick-commands/src/main/java/com/juick/command/Ping.java b/juick-commands/src/main/java/com/juick/command/Ping.java index 7d081b41..7684af7a 100644 --- a/juick-commands/src/main/java/com/juick/command/Ping.java +++ b/juick-commands/src/main/java/com/juick/command/Ping.java @@ -20,6 +20,11 @@ public class Ping implements Command { } @Override + public String help() { + return "PING - returns you a PONG"; + } + + @Override public String execute(User sender, ProtocolListener protocolListener, String command) { return "PONG"; } diff --git a/juick-commands/src/main/java/com/juick/command/PostMessage.java b/juick-commands/src/main/java/com/juick/command/PostMessage.java new file mode 100644 index 00000000..2ef5ce22 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/PostMessage.java @@ -0,0 +1,58 @@ +package com.juick.command; + +import com.juick.Tag; +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.server.util.TagUtils; +import com.juick.service.MessagesService; +import com.juick.service.SubscriptionService; +import com.juick.service.TagService; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class PostMessage implements Command { + + private final TagService tagService; + private final MessagesService messagesService; + private final SubscriptionService subscriptionService; + + @Value("${baseuri}") + private String baseUri = "http://juick.com"; + + @Autowired + public PostMessage(TagService tagService, MessagesService messagesService, SubscriptionService subscriptionService) { + this.tagService = tagService; + this.messagesService = messagesService; + this.subscriptionService = subscriptionService; + } + + @Override + public Pattern pattern() { + return null; + } + + @Override + public String help() { + return ""; + } + + @Override + public String execute(User sender, ProtocolListener protocolListener, String command) { + List<Tag> tags = getTagService().fromString(command, false); + String body = command.substring(TagUtils.toString(tags).length()); + int mid = getMessagesService().createMessage(sender.getUid(), body, null, tags); + getSubscriptionService().subscribeMessage(mid, sender.getUid()); + protocolListener.messagePosted(getMessagesService().getMessage(mid)); + return String.format("New message posted.\n#%d %s%d", mid, getBaseUri(), mid); + } +} diff --git a/juick-commands/src/main/java/com/juick/command/PrivateMessage.java b/juick-commands/src/main/java/com/juick/command/PrivateMessage.java index 1e02f1da..e41b0037 100644 --- a/juick-commands/src/main/java/com/juick/command/PrivateMessage.java +++ b/juick-commands/src/main/java/com/juick/command/PrivateMessage.java @@ -34,6 +34,11 @@ public class PrivateMessage extends MultiArgsCommand { } @Override + public String help() { + return "@username message - send PM to username"; + } + + @Override protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { String addressee = arguments[0]; String body = arguments[1]; diff --git a/juick-commands/src/main/java/com/juick/command/Processor.java b/juick-commands/src/main/java/com/juick/command/Processor.java index bf38a1b3..51ce2830 100644 --- a/juick-commands/src/main/java/com/juick/command/Processor.java +++ b/juick-commands/src/main/java/com/juick/command/Processor.java @@ -10,6 +10,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Set; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import javax.annotation.PostConstruct; /** * @author ma1uta @@ -23,26 +26,48 @@ public class Processor { private final Set<Command> commands; @Setter private ProtocolListener protocolListener; + private String help; @Autowired public Processor(Set<Command> commands) { this.commands = commands; } + @PostConstruct + public void init() { + this.help = + "HELP - returns this help message.\n" + getCommands().stream().map(Command::help).sorted().collect(Collectors.joining("\n")); + } + public String execute(User sender, String command) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Execute command: {}", command); } + command = command != null ? command.trim() : ""; + if ("help".equalsIgnoreCase(command)) { + LOGGER.debug("Show help"); + return getHelp(); + } + Command defaultCommand = null; for (Command commandItem : getCommands()) { - if (commandItem.pattern().matcher(command).matches()) { + Pattern pattern = commandItem.pattern(); + if (pattern != null && pattern.matcher(command).matches()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Found command processor: {}", commandItem.getClass().getName()); } - return commandItem.execute(sender, command); + return commandItem.execute(sender, getProtocolListener(), command); + } else { + defaultCommand = commandItem; } } + if (defaultCommand != null) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Found default command processor: {}", defaultCommand.getClass().getName()); + } + return defaultCommand.execute(sender, getProtocolListener(), command); + } LOGGER.debug("Command processor not found, return help"); - return ""; + return getHelp(); } } 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"; + } +} diff --git a/juick-commands/src/main/java/com/juick/command/SubscribeUser.java b/juick-commands/src/main/java/com/juick/command/SubscribeUser.java new file mode 100644 index 00000000..7589a954 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/SubscribeUser.java @@ -0,0 +1,61 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.SubscriptionService; +import com.juick.service.UserService; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class SubscribeUser extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^([su])\\s+@(\\S+)$", Pattern.CASE_INSENSITIVE); + + private final UserService userService; + private final SubscriptionService subscriptionService; + + @Autowired + public SubscribeUser(UserService userService, SubscriptionService subscriptionService) { + this.userService = userService; + this.subscriptionService = subscriptionService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "S @user - subscribe to user's posts"; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + boolean subscribe = arguments[0].equalsIgnoreCase("s"); + User toUser = getUserService().getUserByName(arguments[1]); + if (toUser.getUid() > 0) { + if (subscribe) { + if (getSubscriptionService().subscribeUser(sender, toUser)) { + protocolListener.userSubscribed(sender, toUser); + return "Subscribed"; + // TODO: already subscribed case + } + } else { + if (subscriptionService.unSubscribeUser(sender, toUser)) { + return "Unsubscribed from @" + toUser.getName(); + } + return "You was not subscribed to @" + toUser.getName(); + } + } + return "Error"; + } +} |