diff options
5 files changed, 35 insertions, 29 deletions
diff --git a/juick-core/build.gradle b/juick-core/build.gradle index 58d8eec3..772818e4 100644 --- a/juick-core/build.gradle +++ b/juick-core/build.gradle @@ -8,6 +8,7 @@ dependencies { compile 'org.apache.commons:commons-lang3:3.5' compile 'org.apache.commons:commons-collections4:4.1' compile 'commons-io:commons-io:2.5' + compile 'org.ocpsoft.prettytime:prettytime:3.2.7.Final' testCompile "junit:junit:${rootProject.junitVersion}" testCompile "org.hamcrest:hamcrest-all:${rootProject.hamcrestVersion}" diff --git a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java index c6e9022a..d75365e0 100644 --- a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java +++ b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java @@ -1,9 +1,16 @@ package com.juick.formatters; +import org.apache.commons.lang3.StringUtils; +import org.ocpsoft.prettytime.PrettyTime; + +import java.util.Locale; + /** * Created by vitalyster on 12.10.2016. */ public class PlainTextFormatter { + static PrettyTime pt = new PrettyTime(new Locale("ru")); + public static String formatPost(com.juick.Message jmsg) { StringBuilder sb = new StringBuilder(); boolean isReply = jmsg.getRid() > 0; @@ -17,6 +24,17 @@ public class PlainTextFormatter { return sb.toString(); } + public static String formatPostSummary(com.juick.Message m) { + int cropLength = 384; + String timeAgo = pt.format(m.getDate()); + String repliesCount = m.getReplies() == 1 ? "; 1 reply" : m.getReplies() == 0 ? "" + : String.format("; %d replies", m.getReplies()); + String txt = m.getText().length() >= cropLength ? + StringUtils.substring(m.getText(), 0, cropLength) + " [...]" : m.getText(); + return String.format("@%s:%s\n%s\n#%d (%s%s) http://juick.com/%d", + m.getUser().getName(), m.getTagsString(), txt, m.getMid(), timeAgo, repliesCount, m.getMid()); + } + public static String formatUrl(com.juick.Message jmsg) { if (jmsg.getRid() > 0) { return String.format("https://juick.com/%d#%d", jmsg.getMid(), jmsg.getRid()); 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 2597ddbc..6ad177a8 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<String> 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<Integer> mids = messagesService.getAll(user.getUid(), page); + List<Integer> mids = messagesService.getAll(user.getUid(), 0); List<Message> 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<User> 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<String> commandsHelp = Arrays.stream(getClass().getDeclaredMethods()) diff --git a/juick-xmpp/build.gradle b/juick-xmpp/build.gradle index 3cfbe86f..ceff6064 100644 --- a/juick-xmpp/build.gradle +++ b/juick-xmpp/build.gradle @@ -14,7 +14,6 @@ dependencies { compile 'javax.inject:javax.inject:1' compile 'org.apache.httpcomponents:httpclient:4.5.3' compile 'org.apache.commons:commons-dbcp2:2.1.1' - compile 'org.ocpsoft.prettytime:prettytime:3.2.7.Final' providedRuntime 'mysql:mysql-connector-java:5.1.40' } diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java b/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java index e6efd500..bab65141 100644 --- a/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java +++ b/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java @@ -2,6 +2,7 @@ package com.juick.components.s2s; import com.juick.User; import com.juick.components.XMPPServer; +import com.juick.formatters.PlainTextFormatter; import org.apache.commons.lang3.StringUtils; import org.ocpsoft.prettytime.PrettyTime; import org.slf4j.Logger; @@ -481,18 +482,9 @@ public class JuickBot implements StanzaListener, AutoCloseable { } String printMessages(List<Integer> mids, boolean crop) { - int cropLength = 384; return xmpp.messagesService.getMessages(mids).stream() .sorted(Collections.reverseOrder()) - .map(m -> { - String timeAgo = pt.format(m.getDate()); - String repliesCount = m.getReplies() == 1 ? "; 1 reply" : m.getReplies() == 0 ? "" - : String.format("; %d replies", m.getReplies()); - String txt = crop ? m.getText().length() >= cropLength ? - StringUtils.substring(m.getText(), 0, cropLength) + " [...]" : m.getText() : m.getText(); - return String.format("@%s:%s\n%s\n#%d (%s%s) http://juick.com/%d", - m.getUser().getName(), m.getTagsString(), txt, m.getMid(), timeAgo, repliesCount, m.getMid()); - }).collect(Collectors.joining("\n\n")); + .map(PlainTextFormatter::formatPostSummary).collect(Collectors.joining("\n\n")); } void broadcastPresence(Presence.Type type) { |