From e377357b00cdb151aa20d96a0ce7a5f1a8bbe9c9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 12 Jan 2023 11:46:40 +0300 Subject: Telegram: parse_mode=html --- src/main/java/com/juick/CommandsManager.java | 11 ++++-- src/main/java/com/juick/EmailManager.java | 4 +- src/main/java/com/juick/TelegramBotManager.java | 16 ++++---- src/main/java/com/juick/model/CommandResult.java | 8 ++-- src/main/java/com/juick/util/MessageUtils.java | 43 ++++++++++++++-------- .../juick/util/formatters/PlainTextFormatter.java | 21 ++++------- 6 files changed, 57 insertions(+), 46 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/juick/CommandsManager.java b/src/main/java/com/juick/CommandsManager.java index 6b33cf16..e01e5020 100644 --- a/src/main/java/com/juick/CommandsManager.java +++ b/src/main/java/com/juick/CommandsManager.java @@ -65,6 +65,7 @@ import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.text.StringEscapeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationEventPublisher; /** @@ -93,6 +94,8 @@ public class CommandsManager { private ApplicationEventPublisher applicationEventPublisher; @Inject private WebApp webApp; + @Value("${web_domain:localhost}") + private String webDomain; @Inject private ActivityPubManager activityPubManager; @@ -160,7 +163,7 @@ public class CommandsManager { SystemActivity.message(user, msg, subscriptionService.getSubscribedUsers(msg.getUser().getUid(), msg)))); return CommandResult.build(msg, "New message posted.\n#" + msg.getMid() + " https://juick.com/m/" + msg.getMid(), - String.format("[New message](%s) posted", PlainTextFormatter.formatUrl(msg))); + String.format("New message posted", PlainTextFormatter.formatUrl(msg))); } @UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE, @@ -455,7 +458,7 @@ public class CommandsManager { return CommandResult.fromString(replies.stream() .map(PlainTextFormatter::formatPostSummary).collect(Collectors.joining("\n"))); } - return CommandResult.fromString(PlainTextFormatter.formatPost(msg.get())); + return CommandResult.fromString(PlainTextFormatter.formatPost(msg.get(), webDomain)); } return CommandResult.fromString("Message not found"); } @@ -465,7 +468,7 @@ public class CommandsManager { int rid = NumberUtils.toInt(arguments[1], 0); Message reply = messagesService.getReply(mid, rid); if (reply != null) { - return CommandResult.fromString(PlainTextFormatter.formatPost(reply)); + return CommandResult.fromString(PlainTextFormatter.formatPost(reply, webDomain)); } return CommandResult.fromString("Reply not found"); } @@ -596,7 +599,7 @@ public class CommandsManager { subscriptionService.getUsersSubscribedToComments(original, reply)))); return CommandResult.build(reply, "Reply posted.\n#" + mid + "/" + newrid + " " + "https://juick.com/m/" + mid + "#" + newrid, - String.format("[Reply](%s) posted", PlainTextFormatter.formatUrl(reply))); + String.format("Reply posted", PlainTextFormatter.formatUrl(reply))); } else { return CommandResult.fromString("Message is read-only"); } diff --git a/src/main/java/com/juick/EmailManager.java b/src/main/java/com/juick/EmailManager.java index a62d31f3..62584273 100644 --- a/src/main/java/com/juick/EmailManager.java +++ b/src/main/java/com/juick/EmailManager.java @@ -68,6 +68,8 @@ public class EmailManager implements NotificationListener { private ObjectMapper jsonMapper; @Inject private WebApp webApp; + @Value("${web_domain:localhost}") + private String webDomain; @Value("${service_email:}") private String serviceEmail; @@ -145,7 +147,7 @@ public class EmailManager implements NotificationListener { headers.put("In-Reply-To", String.format("<%d.%d@juick.com>", original.getMid(), original.getRid())); } } - String plainText = webApp.renderPlaintext(formatPost(msg), formatUrl(msg)).orElseThrow(IllegalStateException::new); + String plainText = webApp.renderPlaintext(formatPost(msg, webDomain), formatUrl(msg)).orElseThrow(IllegalStateException::new); String hash = userService.getHashByUID(userService.getUserByEmail(email).getUid()); String htmlText = webApp.renderHtml(MessageUtils.formatHtml(msg), formatUrl(msg), msg, hash).orElseThrow(IllegalStateException::new); sendEmail(StringUtils.EMPTY, email, subject, plainText, htmlText, headers); diff --git a/src/main/java/com/juick/TelegramBotManager.java b/src/main/java/com/juick/TelegramBotManager.java index 9d0b5862..6a150bef 100644 --- a/src/main/java/com/juick/TelegramBotManager.java +++ b/src/main/java/com/juick/TelegramBotManager.java @@ -285,7 +285,7 @@ public class TelegramBotManager implements NotificationListener { messagesService.setMessageProperty(newMessage.getMid(), newMessage.getRid(), "durovId", String.valueOf(messageId)); } - String messageTxt = StringUtils.isNotEmpty(result.getMarkdown()) ? result.getMarkdown() + String messageTxt = StringUtils.isNotEmpty(result.getText()) ? result.getHtml() : "Unknown error or unsupported command"; telegramNotify(userId, messageTxt, new com.juick.model.Message()); } catch (Exception e) { @@ -309,7 +309,7 @@ public class TelegramBotManager implements NotificationListener { if (replyTo > 0) { telegramPhoto.replyToMessageId(replyTo); } - telegramPhoto.parseMode(ParseMode.Markdown); + telegramPhoto.parseMode(ParseMode.HTML); if(msg.length() < 1024) { telegramPhoto.caption(msg); isSendTxt = false; @@ -335,7 +335,7 @@ public class TelegramBotManager implements NotificationListener { if (replyTo > 0) { telegramMessage.replyToMessageId(replyTo); } - telegramMessage.parseMode(ParseMode.Markdown).disableWebPagePreview(true); + telegramMessage.parseMode(ParseMode.HTML).disableWebPagePreview(true); bot.execute(telegramMessage, new Callback<>() { @Override public void onResponse(SendMessage request, SendResponse response) { @@ -425,7 +425,7 @@ public class TelegramBotManager implements NotificationListener { } } private String formatTelegramMessage(com.juick.model.Message jmsg) { - return String.format("[%s](%s) %s", MSG_LINK, formatUrl(jmsg), formatPost(jmsg, true)); + return String.format("%s %s", formatUrl(jmsg), MSG_LINK, formatPost(jmsg, true, webDomain)); } private void processMessage(com.juick.model.Message jmsg, List subscribedUsers) { if (jmsg.isService()) { @@ -434,7 +434,7 @@ public class TelegramBotManager implements NotificationListener { String msgUrl = formatUrl(jmsg); if (MessageUtils.isPM(jmsg)) { telegramService.getTelegramIdentifiers(Collections.singletonList(jmsg.getTo())) - .forEach(c -> telegramNotify(c, formatPost(jmsg, true), jmsg)); + .forEach(c -> telegramNotify(c, formatPost(jmsg, true, webDomain), jmsg)); } else if (MessageUtils.isReply(jmsg)) { String fmsg = formatTelegramMessage(jmsg); telegramService.getTelegramIdentifiers( @@ -458,11 +458,11 @@ public class TelegramBotManager implements NotificationListener { if (!userService.isInBLAny(message.getUser().getUid(), liker.getUid())) { telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser())) .forEach(c -> telegramNotify(c, String.format("%s recommends your [post](%s)", - MessageUtils.getMarkdownUser(liker), formatUrl(message)), new com.juick.model.Message())); + MessageUtils.getUserHtmlLink(liker, webDomain), formatUrl(message)), new com.juick.model.Message())); } telegramService.getTelegramIdentifiers(subscribers) .forEach(c -> telegramNotify(c, String.format("%s recommends you someone's [post](%s)", - MessageUtils.getMarkdownUser(liker), formatUrl(message)), new com.juick.model.Message())); + MessageUtils.getUserHtmlLink(liker, webDomain), formatUrl(message)), new com.juick.model.Message())); } @Override @@ -480,6 +480,6 @@ public class TelegramBotManager implements NotificationListener { private void processFollow(User subscriber, List target) { telegramService.getTelegramIdentifiers(target) .forEach(c -> telegramNotify(c, String.format("%s subscribed to your blog", - MessageUtils.getMarkdownUser(subscriber)), new com.juick.model.Message())); + MessageUtils.getUserHtmlLink(subscriber, webDomain)), new com.juick.model.Message())); } } diff --git a/src/main/java/com/juick/model/CommandResult.java b/src/main/java/com/juick/model/CommandResult.java index cb96b80d..79e1af03 100644 --- a/src/main/java/com/juick/model/CommandResult.java +++ b/src/main/java/com/juick/model/CommandResult.java @@ -21,14 +21,14 @@ import java.util.Optional; public class CommandResult { private String text; - private String markdown; + private String html; private Message newMessage; public String getText() { return text; } - public String getMarkdown() { - return markdown; + public String getHtml() { + return html; } public Optional getNewMessage() { @@ -38,7 +38,7 @@ public class CommandResult { CommandResult result = new CommandResult(); result.newMessage = newMessage; result.text = text; - result.markdown = markdown; + result.html = markdown; return result; } public static CommandResult fromString(String text) { diff --git a/src/main/java/com/juick/util/MessageUtils.java b/src/main/java/com/juick/util/MessageUtils.java index 0ece7249..3651540e 100644 --- a/src/main/java/com/juick/util/MessageUtils.java +++ b/src/main/java/com/juick/util/MessageUtils.java @@ -149,6 +149,9 @@ public class MessageUtils { } public static String formatMessage(String msg) { + return formatMessage(msg, false); + } + public static String formatMessage(String msg, boolean compatibleWithDurov) { msg = msg.replaceAll("&", "&"); msg = msg.replaceAll("<", "<"); @@ -156,7 +159,9 @@ public class MessageUtils { // -- // — - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\-\\-?((?=\\s)|(?=\\Z))", "$1—$2"); + if (!compatibleWithDurov) { + msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\-\\-?((?=\\s)|(?=\\Z))", "$1—$2"); + } // http://juick.com/last?page=2 // juick.com @@ -186,20 +191,28 @@ public class MessageUtils { // _underline_ // underline - msg = msg.replaceAll(underlineRegex, "$1$2$3"); + msg = msg.replaceAll(underlineRegex, "$1$2$3"); // /12 // /12 msg = msg.replaceAll(replyNumberRegex, "$1/$2$3"); + // @username@mastodon.social // @username@mastodon.social - msg = msg.replaceAll(jidRegex, "$1@$2$3"); + if (compatibleWithDurov) { + msg = msg.replaceAll(jidRegex, "$1@$2$3"); + } else { + msg = msg.replaceAll(jidRegex, "$1@$2$3"); + } // @username // @username - msg = msg.replaceAll(usernameRegex, "$1@$2$3"); - + if (compatibleWithDurov) { + msg = msg.replaceAll(usernameRegex, "$1@$2$3"); + } else { + msg = msg.replaceAll(usernameRegex, "$1@$2$3"); + } // (http://juick.com/last?page=2) // (juick.com) Matcher m = regexLinks2.matcher(msg); @@ -211,11 +224,13 @@ public class MessageUtils { m.appendTail(sb); msg = sb.toString(); - // > citate - msg = msg.replaceAll(citateRegex, "$1"); - msg = msg.replaceAll("", "\n"); + if (!compatibleWithDurov) { + // > citate + msg = msg.replaceAll(citateRegex, "$1"); + msg = msg.replaceAll("", "\n"); - msg = msg.replaceAll("\n", "
\n"); + msg = msg.replaceAll("\n", "
\n"); + } return msg; } @@ -259,11 +274,11 @@ public class MessageUtils { .collect(Collectors.joining(", ")); } - public static String getMarkdownUser(final User user) { + public static String getUserHtmlLink(final User user, final String webDomain) { if (user.getUri().toASCIIString().length() > 0) { - return String.format("[%s](%s)", user.getName(), user.getUri()); + return String.format("%s", user.getUri(), user.getName()); } else { - return String.format("[%s](https://juick.com/%s/)", user.getName(), user.getName()); + return String.format("%s", webDomain, user.getName(), user.getName()); } } @@ -276,10 +291,6 @@ public class MessageUtils { return StringUtils.defaultString(msg.getText()) .replaceAll(replyNumberRegex, String.format("$1[/$2](https://juick.com/m/%d#$2)$3", msg.getMid())); } - public static String escapeMarkdown(final String s) { - return s.replace("_", "\\_").replace("*", "\\*") - .replace("`", "\\`"); - } public static String attachmentUrl(final Message jmsg) { if (StringUtils.isEmpty(jmsg.getAttachmentType())) { return StringUtils.EMPTY; diff --git a/src/main/java/com/juick/util/formatters/PlainTextFormatter.java b/src/main/java/com/juick/util/formatters/PlainTextFormatter.java index 83feb8a6..b1b1a44a 100644 --- a/src/main/java/com/juick/util/formatters/PlainTextFormatter.java +++ b/src/main/java/com/juick/util/formatters/PlainTextFormatter.java @@ -31,25 +31,20 @@ import java.util.Locale; public class PlainTextFormatter { static PrettyTime pt = new PrettyTime(new Locale("en")); - public static String formatPost(Message jmsg) { - return formatPost(jmsg, false); + public static String formatPost(Message jmsg, String domain) { + return formatPost(jmsg, false, domain); } - public static String formatPost(Message jmsg, boolean markdown) { + public static String formatPost(Message jmsg, boolean compatibleWithDurov, String domain) { StringBuilder sb = new StringBuilder(); String title = MessageUtils.isReply(jmsg) ? "Reply by @" : MessageUtils.isPM(jmsg) ? "Private message from @" : "@"; - String subtitle = MessageUtils.isReply(jmsg) ? markdown ? MessageUtils.escapeMarkdown(StringUtils.defaultString(jmsg.getReplyQuote())) - : jmsg.getReplyQuote() - : markdown ? MessageUtils.getMessageHashTags(jmsg) : MessageUtils.getTagsString(jmsg); - sb.append(title).append(markdown ? MessageUtils.getMarkdownUser(jmsg.getUser()) : jmsg.getUser().getName()).append(":\n") + String subtitle = MessageUtils.isReply(jmsg) ? jmsg.getReplyQuote() + : compatibleWithDurov ? MessageUtils.getMessageHashTags(jmsg) : MessageUtils.getTagsString(jmsg); + sb.append(title).append(compatibleWithDurov ? MessageUtils.getUserHtmlLink(jmsg.getUser(), domain) : jmsg.getUser().getName()).append(":\n") .append(subtitle).append("\n"); - if (markdown) { - sb.append(MessageUtils.formatMarkdownText(jmsg)); - } else { - sb.append(StringUtils.defaultString(jmsg.getText())); - } + sb.append(compatibleWithDurov? MessageUtils.formatMessage(jmsg.getText(), true) : StringUtils.defaultString(jmsg.getText())); sb.append("\n"); - if (!markdown && StringUtils.isNotEmpty(jmsg.getAttachmentType())) { + if (!compatibleWithDurov && StringUtils.isNotEmpty(jmsg.getAttachmentType())) { sb.append(MessageUtils.attachmentUrl(jmsg)); } return sb.toString(); -- cgit v1.2.3