diff options
3 files changed, 48 insertions, 57 deletions
diff --git a/juick-api/src/main/java/com/juick/api/TelegramBotHook.java b/juick-api/src/main/java/com/juick/api/TelegramBotHook.java index d0a5b177..038166cc 100644 --- a/juick-api/src/main/java/com/juick/api/TelegramBotHook.java +++ b/juick-api/src/main/java/com/juick/api/TelegramBotHook.java @@ -2,10 +2,8 @@ package com.juick.api; import com.juick.User; import com.juick.json.MessageSerializer; -import com.juick.server.MessagesQueries; import com.juick.server.UserQueries; import com.juick.server.protocol.JuickProtocol; -import com.juick.server.protocol.ProtocolReply; import com.neovisionaries.ws.client.*; import com.pengrad.telegrambot.BotUtils; import com.pengrad.telegrambot.Callback; @@ -31,6 +29,9 @@ import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; +import static com.juick.formatters.PlainTextFormatter.formatPost; +import static com.juick.formatters.PlainTextFormatter.formatUrl; + /** * Created by vt on 12/05/16. */ @@ -81,7 +82,7 @@ public class TelegramBotHook { // anonymous chats.stream().filter(u -> getUser(u) == 0).forEach(c -> telegramNotify(c, msg, msgUrl)); } else { - String msg = formatReply(jmsg); + String msg = formatPost(jmsg); getSubscribersToComments(jmsg.getMID(), jmsg.getUser().getUID()).stream() .forEach(c -> telegramNotify(c, msg, msgUrl)); } @@ -93,33 +94,6 @@ public class TelegramBotHook { } } - private String formatPost(com.juick.Message jmsg) { - StringBuilder sb = new StringBuilder(); - sb.append("@").append(jmsg.getUser().getUName()).append(":\n") - .append(jmsg.getTagsString()).append("\n").append(jmsg.getText()).append("\n"); - if (jmsg.Photo != null) { - sb.append(jmsg.Photo); - } - return sb.toString(); - } - private String formatReply(com.juick.Message jmsg) { - StringBuilder sb = new StringBuilder(); - sb.append("Reply by @").append(jmsg.getUser().getUName()).append(":\n") - .append(getReplyQuote(jmsg.getMID(), jmsg.ReplyTo)).append("\n") - .append(jmsg.getText()).append("\n"); - if (jmsg.Photo != null) { - sb.append(jmsg.Photo); - } - return sb.toString(); - } - - private 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 void telegramNotify(Long c, String msg, String msgUrl) { SendMessage telegramMessage = new SendMessage(c, msg); if (msgUrl != null) { @@ -241,25 +215,4 @@ public class TelegramBotHook { return jdbc.queryForList("SELECT tg_id FROM telegram INNER JOIN subscr_messages " + "ON (telegram.user_id=subscr_messages.suser_id) WHERE message_id=? AND suser_id!=?", Long.class, mid, ignore_uid); } - - private String getReplyQuote(int MID, int ReplyTo) { - String quote = ""; - if (ReplyTo > 0) { - com.juick.Message q = MessagesQueries.getReply(jdbc, MID, ReplyTo); - if (q != null) { - quote = q.getText(); - } - } else { - com.juick.Message q = MessagesQueries.getMessage(jdbc, MID); - if (q != null) { - quote = q.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/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<Method> cmd = Arrays.asList(getClass().getDeclaredMethods()).stream() + Optional<Method> 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<Integer> mids = MessagesQueries.getAll(sql, user.getUID(), page); List<Message> 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<Message> 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<Message> 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)))); |