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 --- .../main/java/com/juick/api/TelegramBotHook.java | 55 ++-------------------- 1 file changed, 4 insertions(+), 51 deletions(-) (limited to 'juick-api/src/main/java') 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; - } } -- cgit v1.2.3