diff options
Diffstat (limited to 'juick-server/src')
-rw-r--r-- | juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java | 38 |
1 files changed, 20 insertions, 18 deletions
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 eb579712..6bd8e11f 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 @@ -1,10 +1,11 @@ package com.juick.server.protocol; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.Message; import com.juick.Tag; import com.juick.User; import com.juick.formatters.PlainTextFormatter; -import com.juick.json.MessageSerializer; import com.juick.server.*; import com.juick.server.protocol.annotation.UserCommand; import com.juick.util.TagUtils; @@ -22,7 +23,7 @@ import java.util.stream.Collectors; */ public class JuickProtocol { - MessageSerializer json = new MessageSerializer(); + ObjectMapper json = new ObjectMapper(); JdbcTemplate sql; String baseUri; @@ -41,7 +42,7 @@ public class JuickProtocol { * @throws NoSuchMethodException */ public ProtocolReply getReply(User user, String userInput) throws InvocationTargetException, - IllegalAccessException, NoSuchMethodException { + IllegalAccessException, NoSuchMethodException, JsonProcessingException { Optional<Method> cmd = Arrays.stream(getClass().getDeclaredMethods()) .filter(m -> m.isAnnotationPresent(UserCommand.class)) .filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(), @@ -64,24 +65,24 @@ public class JuickProtocol { } } - public ProtocolReply postMessage(User user, String input) { + public ProtocolReply postMessage(User user, String input) throws JsonProcessingException { List<Tag> tags = TagQueries.fromString(sql, input, false); String body = input.substring(TagUtils.toString(tags).length()); int mid = MessagesQueries.createMessage(sql, user.getUid(), body, null, tags); SubscriptionsQueries.subscribeMessage(sql, mid, user.getUid()); //app.events().publishEvent(new JuickMessageEvent(app.messages().getMessage(mid))); return new ProtocolReply("New message posted.\n#" + mid + " " + baseUri + mid, - Optional.of(json.serializeList(Collections.singletonList(MessagesQueries.getMessage(sql, mid))))); + Optional.of(json.writeValueAsString(Collections.singletonList(MessagesQueries.getMessage(sql, mid))))); } @UserCommand(pattern = "^#(\\++)$", help = "#+ - Show last Juick messages (#++ - second page, ...)") - public ProtocolReply commandLast(User user, String... arguments) { + public ProtocolReply commandLast(User user, String... arguments) throws JsonProcessingException { // number of + is the page count int page = arguments[0].length() - 1; List<Integer> mids = MessagesQueries.getAll(sql, user.getUid(), page); List<Message> messages = MessagesQueries.getMessages(sql, mids); return new ProtocolReply("Last messages: \n" + String.join("\n", messages.stream().map(PlainTextFormatter::formatPost) - .collect(Collectors.toList())), Optional.of(json.serializeList(messages))); + .collect(Collectors.toList())), Optional.of(json.writeValueAsString(messages))); } @UserCommand(pattern = "^\\s*bl\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, @@ -182,19 +183,19 @@ public class JuickProtocol { } @UserCommand(pattern = "!", help = "! - Show your favorite messages") - public ProtocolReply commandFavorites(User currentUser, String... args) { + public ProtocolReply commandFavorites(User currentUser, String... args) throws JsonProcessingException { List<Integer> mids = MessagesQueries.getUserRecommendations(sql, currentUser.getUid(), 0); if (mids.size() > 0) { List<Message> messages = MessagesQueries.getMessages(sql, mids); return new ProtocolReply("Favorite messages: \n" + String.join("\n", messages.stream().map(PlainTextFormatter::formatPost) - .collect(Collectors.toList())), Optional.of(json.serializeList(messages))); + .collect(Collectors.toList())), Optional.of(json.writeValueAsString(messages))); } return new ProtocolReply("No favorite messages, try to \"like\" something ;)", Optional.empty()); } @UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$", help = "@username+ - Show user's info and last 10 messages (@username++ - second page, ..)") - public ProtocolReply commandUser(User user, String... arguments) { + public ProtocolReply commandUser(User user, String... arguments) throws JsonProcessingException { User blogUser = UserQueries.getUserByName(sql, arguments[0]); int page = arguments[1].length(); if (blogUser != null) { @@ -203,7 +204,7 @@ public class JuickProtocol { return new ProtocolReply(String.format("Last messages from @%s:\n%s", arguments[0], String.join("\n", messages.stream() .map(Object::toString).collect(Collectors.toList()))), - Optional.of(json.serializeList(messages))); + Optional.of(json.writeValueAsString(messages))); } return new ProtocolReply("User not found", Optional.empty()); } @@ -230,7 +231,7 @@ public class JuickProtocol { } @UserCommand(pattern = "^(#+)$", help = "# - Show last messages from your feed (## - second page, ...)") - public ProtocolReply commandMyFeed(User user, String... arguments) { + public ProtocolReply commandMyFeed(User user, String... arguments) throws JsonProcessingException { // number of # is the page count int page = arguments[0].length() - 1; List<Integer> mids = MessagesQueries.getMyFeed(sql, user.getUid(), page); @@ -238,7 +239,7 @@ public class JuickProtocol { // TODO: add instructions for empty feed return new ProtocolReply("Your feed: \n" + String.join("\n", messages.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())), - Optional.of(json.serializeList(messages))); + Optional.of(json.writeValueAsString(messages))); } @UserCommand(pattern = "^\\s*(on|off)\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, @@ -333,7 +334,7 @@ public class JuickProtocol { } @UserCommand(pattern = "^#(\\d+)(\\+?)$", help = "#1234 - Show message (#1234+ - message with replies)") - public ProtocolReply commandShow(User user, String... arguments) { + public ProtocolReply commandShow(User user, String... arguments) throws JsonProcessingException { boolean showReplies = arguments[1].length() > 0; int mid; try { @@ -348,15 +349,16 @@ public class JuickProtocol { replies.add(0, msg); return new ProtocolReply(String.join("\n", replies.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())), - Optional.of(json.serializeList(replies))); + Optional.of(json.writeValueAsString(replies))); } - return new ProtocolReply(PlainTextFormatter.formatPost(msg), Optional.of(json.serializeList(Collections.singletonList(msg)))); + return new ProtocolReply(PlainTextFormatter.formatPost(msg), + Optional.of(json.writeValueAsString(Collections.singletonList(msg)))); } return new ProtocolReply("Message not found", Optional.empty()); } @UserCommand(pattern = "^(#|\\.)(\\d+)((\\.|\\-|\\/)(\\d+))?\\s([\\s\\S]+)", help = "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message") - public ProtocolReply EditOrReply(User user, String... args) { + public ProtocolReply EditOrReply(User user, String... args) throws JsonProcessingException { int mid; try { mid = Integer.parseInt(args[1]); @@ -381,7 +383,7 @@ public class JuickProtocol { int newrid = MessagesQueries.createReply(sql, mid, rid, user.getUid(), txt, null); return new ProtocolReply("Reply posted.\n#" + mid + "/" + newrid + " " + baseUri + mid + "/" + newrid, - Optional.of(json.serializeList(Collections.singletonList(MessagesQueries.getReply(sql, mid, newrid))))); + Optional.of(json.writeValueAsString(Collections.singletonList(MessagesQueries.getReply(sql, mid, newrid))))); } } |