From 688eefb6db3ddeb9da583d5e1109e4639cf4e642 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 12 Oct 2016 15:15:24 +0300 Subject: refactor message formatting --- .../com/juick/formatters/PlainTextFormatter.java | 38 ++++++++++++++++++++++ .../com/juick/server/protocol/JuickProtocol.java | 12 +++---- 2 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java (limited to 'juick-core') diff --git a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java new file mode 100644 index 00000000..37b0c6d3 --- /dev/null +++ b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java @@ -0,0 +1,38 @@ +package com.juick.formatters; + +import com.juick.Message; + +/** + * Created by vitalyster on 12.10.2016. + */ +public class PlainTextFormatter { + public static String formatPost(com.juick.Message jmsg) { + StringBuilder sb = new StringBuilder(); + boolean isReply = jmsg.getRID() > 0; + String title = isReply ? "Reply by @" : "@"; + String subtitle = isReply ? getReplyQuote(jmsg) : jmsg.getTagsString(); + sb.append(title).append(jmsg.getUser().getUName()).append(":\n") + .append(subtitle).append("\n").append(jmsg.getText()).append("\n"); + if (jmsg.Photo != null) { + sb.append(jmsg.Photo); + } + return sb.toString(); + } + + public static String formatUrl(com.juick.Message jmsg) { + if (jmsg.getRID() > 0) { + return String.format("https://juick.com/%d#%d", jmsg.getMID(), jmsg.getRID()); + } + return "https://juick.com/" + jmsg.getMID(); + } + + private static String getReplyQuote(Message msg) { + String quote = msg.getText(); + if (quote.length() > 50) { + quote = ">" + quote.substring(0, 47).replace('\n', ' ') + "...\n"; + } else if (quote.length() > 0) { + quote = ">" + quote.replace('\n', ' ') + "\n"; + } + return quote; + } +} diff --git a/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java b/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java index f7056cd4..024179a7 100644 --- a/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java +++ b/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java @@ -1,6 +1,7 @@ package com.juick.server.protocol; import com.juick.*; +import com.juick.formatters.PlainTextFormatter; import com.juick.json.MessageSerializer; import com.juick.server.*; import com.juick.server.protocol.annotation.UserCommand; @@ -39,7 +40,7 @@ public class JuickProtocol { */ public ProtocolReply getReply(User user, String userInput) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { - Optional cmd = Arrays.asList(getClass().getDeclaredMethods()).stream() + Optional cmd = Arrays.stream(getClass().getDeclaredMethods()) .filter(m -> m.isAnnotationPresent(UserCommand.class)) .filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(), m.getAnnotation(UserCommand.class).patternFlags()).matcher(userInput).matches()) @@ -74,11 +75,10 @@ public class JuickProtocol { @UserCommand(pattern = "^#(\\++)$", help = "#+ - Show last Juick messages (#++ - second page, ...)") public ProtocolReply commandLast(User user, String... arguments) { // number of + is the page count - int page = arguments[0].length(); + int page = arguments[0].length() - 1; List mids = MessagesQueries.getAll(sql, user.getUID(), page); List messages = MessagesQueries.getMessages(sql, mids); - // TODO: message toString - return new ProtocolReply("Last messages: \n" + String.join("\n", messages.stream().map(Object::toString) + return new ProtocolReply("Last messages: \n" + String.join("\n", messages.stream().map(PlainTextFormatter::formatPost) .collect(Collectors.toList())), Optional.of(json.serializeList(messages))); } @@ -158,7 +158,7 @@ public class JuickProtocol { List messages = MessagesQueries.getMessages(sql, mids); // TODO: add instructions for empty feed return new ProtocolReply("Your feed: \n" + String.join("\n", - messages.stream().map(Object::toString).collect(Collectors.toList())), + messages.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())), Optional.of(json.serializeList(messages))); } @@ -267,7 +267,7 @@ public class JuickProtocol { List replies = MessagesQueries.getReplies(sql, mid); replies.add(0, msg); return new ProtocolReply(String.join("\n", - replies.stream().map(Object::toString).collect(Collectors.toList())), + replies.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())), Optional.of(json.serializeList(replies))); } return new ProtocolReply(msg.toString(), Optional.of(json.serializeList(Collections.singletonList(msg)))); -- cgit v1.2.3