From c2f4735877781313ef0d0960ac3c0403944a495c Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 2 Mar 2017 17:24:39 +0300 Subject: juick-server: protocol refactoring --- .../com/juick/server/protocol/JuickProtocol.java | 32 ++++++++++------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'juick-server/src/main/java/com/juick/server') diff --git a/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java b/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java index 2597ddbc2..6ad177a85 100644 --- a/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java +++ b/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java @@ -15,10 +15,7 @@ import org.apache.commons.lang3.reflect.MethodUtils; import javax.inject.Inject; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -68,10 +65,10 @@ public class JuickProtocol { .findFirst(); if (!cmd.isPresent()) { // default command - post as new message - return postMessage(user, userInput); + return postMessage(user, userInput.trim()); } else { Matcher matcher = Pattern.compile(cmd.get().getAnnotation(UserCommand.class).pattern(), - cmd.get().getAnnotation(UserCommand.class).patternFlags()).matcher(userInput); + cmd.get().getAnnotation(UserCommand.class).patternFlags()).matcher(userInput.trim()); List groups = new ArrayList<>(); while (matcher.find()) { for (int i = 1; i <= matcher.groupCount(); i++) { @@ -92,17 +89,16 @@ public class JuickProtocol { return new ProtocolReply("New message posted.\n#" + mid + " " + baseUri + mid); } - @UserCommand(pattern = "^#(\\++)$", help = "#+ - Show last Juick messages (#++ - second page, ...)") + @UserCommand(pattern = "^#\\+$", help = "#+ - Show last Juick messages") public ProtocolReply commandLast(User user, String... arguments) { - // number of + is the page count - int page = arguments[0].length() - 1; - List mids = messagesService.getAll(user.getUid(), page); + List mids = messagesService.getAll(user.getUid(), 0); List messages = messagesService.getMessages(mids); - return new ProtocolReply("Last messages: \n" + String.join("\n", messages.stream().map(PlainTextFormatter::formatPost) - .collect(Collectors.toList()))); + return new ProtocolReply("Last messages: \n" + + messages.stream().sorted(Collections.reverseOrder()).map(PlainTextFormatter::formatPostSummary) + .collect(Collectors.joining("\n\n"))); } - @UserCommand(pattern = "^\\s*bl\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, + @UserCommand(pattern = "^bl$", patternFlags = Pattern.CASE_INSENSITIVE, help = "BL - Show your blacklist") public ProtocolReply commandBL(User user_from, String... arguments) { List blusers; @@ -238,7 +234,7 @@ public class JuickProtocol { return new ProtocolReply("User not found"); } - @UserCommand(pattern = "^\\s*d\\s*\\#([0-9]+)\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, + @UserCommand(pattern = "^d\\s*\\#([0-9]+)$", patternFlags = Pattern.CASE_INSENSITIVE, help = "D #12345 - delete the message") public ProtocolReply commandDel(User user, String... args) { int mid = NumberUtils.toInt(args[0], 0); @@ -248,7 +244,7 @@ public class JuickProtocol { return new ProtocolReply("Error"); } - @UserCommand(pattern = "^\\s*login\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, + @UserCommand(pattern = "^login$", patternFlags = Pattern.CASE_INSENSITIVE, help = "LOGIN - log in to Juick website") public ProtocolReply commandLogin(User user, String... arguments) { return new ProtocolReply(baseUri + "?" + userService.getHashByUID(user.getUid())); @@ -265,7 +261,7 @@ public class JuickProtocol { messages.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList()))); } - @UserCommand(pattern = "^\\s*(on|off)\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, + @UserCommand(pattern = "^(on|off)$", patternFlags = Pattern.CASE_INSENSITIVE, help = "ON/OFF - Enable/disable subscriptions delivery") public ProtocolReply commandOnOff(User user, String[] input) { UserService.ActiveStatus newStatus; @@ -285,7 +281,7 @@ public class JuickProtocol { } } - @UserCommand(pattern = "^\\s*ping\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, + @UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE, help = "PING - returns you a PONG") public ProtocolReply commandPing(User user, String[] input) { return new ProtocolReply("PONG"); @@ -409,7 +405,7 @@ public class JuickProtocol { return new ProtocolReply("Error"); } - @UserCommand(pattern = "^\\s*help\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, + @UserCommand(pattern = "^help$", patternFlags = Pattern.CASE_INSENSITIVE, help = "HELP - returns this help message") public ProtocolReply commandHelp(User user, String[] input) { List commandsHelp = Arrays.stream(getClass().getDeclaredMethods()) -- cgit v1.2.3