From b11a395c46aa92179a4a6019d2ebef15877b3946 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 16 Nov 2016 12:10:32 +0300 Subject: formatMessage moved to core, remove copy-paste from juick-rss --- .../src/main/java/com/juick/util/MessageUtils.java | 119 +++++++++++++++++++++ .../java/com/juick/components/Notifications.java | 3 - juick-rss/src/main/java/com/juick/rss/Main.java | 80 ++------------ juick-www/src/main/java/com/juick/www/PM.java | 3 +- .../src/main/java/com/juick/www/PageTemplates.java | 5 +- juick-www/src/main/java/com/juick/www/RSS.java | 3 +- .../src/main/java/com/juick/www/UserThread.java | 9 +- juick-www/src/main/java/com/juick/www/Utils.java | 92 ---------------- .../src/main/java/com/juick/util/MessageUtils.java | 24 ----- 9 files changed, 137 insertions(+), 201 deletions(-) create mode 100644 juick-core/src/main/java/com/juick/util/MessageUtils.java delete mode 100644 server-core/src/main/java/com/juick/util/MessageUtils.java diff --git a/juick-core/src/main/java/com/juick/util/MessageUtils.java b/juick-core/src/main/java/com/juick/util/MessageUtils.java new file mode 100644 index 00000000..12ae71c1 --- /dev/null +++ b/juick-core/src/main/java/com/juick/util/MessageUtils.java @@ -0,0 +1,119 @@ +package com.juick.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Created by aalexeev on 11/13/16. + */ +public class MessageUtils { + private MessageUtils() { + throw new IllegalStateException(); + } + + public static String formatQuote(final String quote) { + String result = quote; + + if (quote != null) { + if (quote.length() > 50) { + result = ">" + quote.substring(0, 47).replace('\n', ' ') + "...\n"; + } else if (!quote.isEmpty()) { + result = ">" + quote.replace('\n', ' ') + "\n"; + } + } + + return result; + } + private static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[\\{]|<)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](?", ">"); + + // http://juick.com/last?page=2 + // http://juick.com/last?page=2 + msg = msg.replaceAll("((?<=\\s)|(?<=\\A))((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\n\\\"]+)/?[^\\s\\n\\\"]*)", "$1$2"); + + // (http://juick.com/last?page=2) + // (http://juick.com/last?page=2) + Matcher m = regexLinks2.matcher(msg); + StringBuffer sb = new StringBuffer(); + while (m.find()) { + String url = m.group(3).replace(" ", "%20").replaceAll("\\s+", ""); + m.appendReplacement(sb, "$1$2" + url + "$5"); + } + m.appendTail(sb); + msg = sb.toString(); + + return "
" + msg + "
"; + } + public static String formatMessage(String msg) { + msg = msg.replaceAll("&", "&"); + msg = msg.replaceAll("<", "<"); + msg = msg.replaceAll(">", ">"); + + // -- + // — + msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\-\\-?((?=\\s)|(?=\\Z))", "$1—$2"); + + // http://juick.com/last?page=2 + // juick.com + msg = msg.replaceAll("((?<=\\s)|(?<=\\A))((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\n\\\"]+)/?[^\\s\\n\\\"]*)", "$1$3"); + + // [link text][http://juick.com/last?page=2] + // link text + msg = msg.replaceAll("\\[([^\\]]+)\\]\\[((?:ht|f)tps?://[^\\]]+)\\]", "$1"); + msg = msg.replaceAll("\\[([^\\]]+)\\]\\(((?:ht|f)tps?://[^\\)]+)\\)", "$1"); + + // #12345 + // #12345 + msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1#$2$3"); + + // #12345/65 + // #12345/65 + msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1#$2/$3$4"); + + // *bold* + // bold + msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))\\*([^\\*\\n<>]+)\\*((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); + + // /italic/ + // italic + msg = msg.replaceAll("((?<=\\s)|(?<=\\A))/([^\\/\\n<>]+)/((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); + + // _underline_ + // underline + msg = msg.replaceAll("((?<=\\s)|(?<=\\A))_([^\\_\\n<>]+)_((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); + + // /12 + // /12 + msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1/$2$3"); + + // @username@jabber.org + // @username@jabber.org + msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3"); + + // @username + // @username + msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3"); + + // (http://juick.com/last?page=2) + // (juick.com) + Matcher m = regexLinks2.matcher(msg); + StringBuffer sb = new StringBuffer(); + while (m.find()) { + String url = m.group(3).replace(" ", "%20").replaceAll("\\s+", ""); + m.appendReplacement(sb, "$1$2$4$5"); + } + m.appendTail(sb); + msg = sb.toString(); + + // > citate + msg = msg.replaceAll("(?:(?<=\\n)|(?<=\\A))> *(.*)?(\\n|(?=\\Z))", "$1"); + msg = msg.replaceAll("", "\n"); + + msg = msg.replaceAll("\n", "
\n"); + return msg; + } +} diff --git a/juick-notifications/src/main/java/com/juick/components/Notifications.java b/juick-notifications/src/main/java/com/juick/components/Notifications.java index 5b51c65d..21e85c37 100644 --- a/juick-notifications/src/main/java/com/juick/components/Notifications.java +++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java @@ -18,9 +18,7 @@ package com.juick.components; import com.google.android.gcm.server.*; -import com.google.android.gcm.server.Message; import com.juick.json.MessageSerializer; -import com.juick.util.ThreadHelper; import com.notnoop.apns.APNS; import com.notnoop.apns.ApnsService; import org.apache.commons.lang3.StringEscapeUtils; @@ -40,7 +38,6 @@ import org.apache.http.util.TextUtils; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.env.Environment; diff --git a/juick-rss/src/main/java/com/juick/rss/Main.java b/juick-rss/src/main/java/com/juick/rss/Main.java index bf8348fb..029057e3 100644 --- a/juick-rss/src/main/java/com/juick/rss/Main.java +++ b/juick-rss/src/main/java/com/juick/rss/Main.java @@ -20,7 +20,9 @@ package com.juick.rss; import com.juick.Message; import com.juick.server.MessagesQueries; import com.juick.server.UserQueries; +import com.juick.util.MessageUtils; import org.apache.commons.dbcp2.BasicDataSource; +import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.jdbc.core.JdbcTemplate; @@ -35,8 +37,6 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Properties; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * @author ugnich @@ -136,14 +136,14 @@ public class Main extends HttpServlet { } } out.println("]]>"); - out.println(""); + out.println(""); out.println("" + sdfRSS.format(msg.getDate()) + ""); out.println("http://juick.com/" + msg.getUser().getName() + "/" + msg.getMid() + ""); if (!msg.getTags().isEmpty()) { for (int n = 0; n < msg.getTags().size(); n++) { - out.println("" + escapeHtml(msg.getTags().get(n).getName()) + ""); + out.println("" + StringEscapeUtils.escapeHtml4(msg.getTags().get(n).getName()) + ""); } } if (msg.AttachmentType != null) { @@ -201,14 +201,14 @@ public class Main extends HttpServlet { reply.pubDate = rs.getTimestamp(6); reply.attachmentType = rs.getString(7); return reply; - }, -hours).stream().forEach(r -> { + }, -hours).forEach(r -> { out.println(""); out.println("http://juick.com/" + r.muname + "/" + r.mid + ""); out.println("http://juick.com/" + r.muname + "/" + r.mid + "#" + r.rid + ""); out.println("http://juick.com/" + r.muname + "/" + r.mid + "#" + r.rid + ""); out.println("http://juick.com/" + r.uname + "/"); out.println("@" + r.uname + ":"); - out.println(""); + out.println(""); out.println("" + sdfRSS.format(r.pubDate) + ""); String attachment = r.attachmentType; if (attachment != null && !attachment.isEmpty()) { @@ -230,72 +230,4 @@ public class Main extends HttpServlet { return sql.queryForList("SELECT message_id FROM messages WHERE messages.ts>TIMESTAMPADD(HOUR,?,NOW())", Integer.class, -hours); } - - private static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[\\{]|<)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](?", ">"); - } - - public static String formatMessage(String msg) { - msg = escapeHtml(msg); - - // -- - // — - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\-\\-?((?=\\s)|(?=\\Z))", "$1—$2"); - - // http://juick.com/last?page=2 - // juick.com - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\n\\\"]+)/?[^\\s\\n\\\"]*)", "$1$3"); - - // #12345 - // #12345 - msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1#$2$3"); - - // #12345/65 - // #12345/65 - msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1#$2/$3$4"); - - // *bold* - // bold - msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))\\*([^\\*\\n<>]+)\\*((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); - - // /italic/ - // italic - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))/([^\\/\\n<>]+)/((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); - - // _underline_ - // underline - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))_([^\\_\\n<>]+)_((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); - - // /12 - // /12 - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1/$2$3"); - - // @username@jabber.org - // @username@jabber.org - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3"); - - // @username - // @username - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3"); - - // (http://juick.com/last?page=2) - // (juick.com) - Matcher m = regexLinks2.matcher(msg); - StringBuffer sb = new StringBuffer(); - while (m.find()) { - String url = m.group(3).replace(" ", "%20").replaceAll("\\s+", ""); - m.appendReplacement(sb, "$1$2$4$5"); - } - m.appendTail(sb); - msg = sb.toString(); - - // > citate - msg = msg.replaceAll("(?:(?<=\\n)|(?<=\\A))> *(.*)?(\\n|(?=\\Z))", "
$1
"); - msg = msg.replaceAll("
", "\n"); - - msg = msg.replaceAll("\n", "
\n"); - return msg; - } } diff --git a/juick-www/src/main/java/com/juick/www/PM.java b/juick-www/src/main/java/com/juick/www/PM.java index 95e2623a..79128e3d 100644 --- a/juick-www/src/main/java/com/juick/www/PM.java +++ b/juick-www/src/main/java/com/juick/www/PM.java @@ -20,6 +20,7 @@ package com.juick.www; import com.juick.server.PMQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; +import com.juick.util.MessageUtils; import com.juick.util.UserUtils; import com.mitchellbosecke.pebble.error.PebbleException; import com.mitchellbosecke.pebble.template.PebbleTemplate; @@ -50,7 +51,7 @@ public class PM { String title = "PM: Inbox"; List msgs = PMQueries.getLastPMInbox(sql, visitor.getUid()); - msgs.forEach(m -> m.setText(Utils.formatMessage(m.getText()))); + msgs.forEach(m -> m.setText(MessageUtils.formatMessage(m.getText()))); response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { PebbleTemplate template = Utils.getEngine().getTemplate("views/pm_inbox.html"); diff --git a/juick-www/src/main/java/com/juick/www/PageTemplates.java b/juick-www/src/main/java/com/juick/www/PageTemplates.java index 4f7e17b7..f0974e69 100644 --- a/juick-www/src/main/java/com/juick/www/PageTemplates.java +++ b/juick-www/src/main/java/com/juick/www/PageTemplates.java @@ -22,6 +22,7 @@ import com.juick.Tag; import com.juick.server.MessagesQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; +import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -315,9 +316,9 @@ public class PageTemplates { String txt; if (msg.getTags().stream().anyMatch(t -> t.getName().equals("code"))) { - txt = Utils.formatMessageCode(msg.getText()); + txt = MessageUtils.formatMessageCode(msg.getText()); } else { - txt = Utils.formatMessage(msg.getText()); + txt = MessageUtils.formatMessage(msg.getText()); } out.println("
"); diff --git a/juick-www/src/main/java/com/juick/www/RSS.java b/juick-www/src/main/java/com/juick/www/RSS.java index f27ea539..5218494d 100644 --- a/juick-www/src/main/java/com/juick/www/RSS.java +++ b/juick-www/src/main/java/com/juick/www/RSS.java @@ -19,6 +19,7 @@ package com.juick.www; import com.juick.Message; import com.juick.server.MessagesQueries; +import com.juick.util.MessageUtils; import com.mitchellbosecke.pebble.error.PebbleException; import com.mitchellbosecke.pebble.template.PebbleTemplate; import org.springframework.jdbc.core.JdbcTemplate; @@ -49,7 +50,7 @@ public class RSS { List msgs = MessagesQueries.getMessages(sql, mids); - msgs.forEach(m -> Utils.formatMessage(m.getText())); + msgs.forEach(m -> MessageUtils.formatMessage(m.getText())); response.setContentType("application/rss+xml; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { diff --git a/juick-www/src/main/java/com/juick/www/UserThread.java b/juick-www/src/main/java/com/juick/www/UserThread.java index f12e3c64..394d6712 100644 --- a/juick-www/src/main/java/com/juick/www/UserThread.java +++ b/juick-www/src/main/java/com/juick/www/UserThread.java @@ -21,6 +21,7 @@ import com.juick.Message; import com.juick.Tag; import com.juick.server.MessagesQueries; import com.juick.server.UserQueries; +import com.juick.util.MessageUtils; import org.springframework.jdbc.core.JdbcTemplate; import java.io.IOException; @@ -106,9 +107,9 @@ public class UserThread { String txt; if (msg.getTags().stream().anyMatch(t -> t.getName().equals("code"))) { - txt = Utils.formatMessageCode(msg.getText()); + txt = MessageUtils.formatMessageCode(msg.getText()); } else { - txt = Utils.formatMessage(msg.getText()); + txt = MessageUtils.formatMessage(msg.getText()); } if (!tags.isEmpty()) { @@ -290,7 +291,7 @@ public class UserThread { out.println("
"); out.println(" "); out.println(" "); - out.println("
" + Utils.formatMessage(msg.getText()) + "
"); + out.println("
" + MessageUtils.formatMessage(msg.getText()) + "
"); if (msg.AttachmentType != null) { out.println("
\"\"/
"); } @@ -336,7 +337,7 @@ public class UserThread { out.println("
"); out.println(" "); out.println(" "); - out.println("
" + Utils.formatMessage(msg.getText()) + "
"); + out.println("
" + MessageUtils.formatMessage(msg.getText()) + "
"); if (msg.AttachmentType != null) { out.println("
\"\"/
"); } diff --git a/juick-www/src/main/java/com/juick/www/Utils.java b/juick-www/src/main/java/com/juick/www/Utils.java index 988ecb17..611563c7 100644 --- a/juick-www/src/main/java/com/juick/www/Utils.java +++ b/juick-www/src/main/java/com/juick/www/Utils.java @@ -208,98 +208,6 @@ public class Utils { } } - private static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[\\{]|<)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](?", ">"); - - // http://juick.com/last?page=2 - // http://juick.com/last?page=2 - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\n\\\"]+)/?[^\\s\\n\\\"]*)", "$1$2"); - - // (http://juick.com/last?page=2) - // (http://juick.com/last?page=2) - Matcher m = regexLinks2.matcher(msg); - StringBuffer sb = new StringBuffer(); - while (m.find()) { - String url = m.group(3).replace(" ", "%20").replaceAll("\\s+", ""); - m.appendReplacement(sb, "$1$2" + url + "$5"); - } - m.appendTail(sb); - msg = sb.toString(); - - return "
" + msg + "
"; - } - public static String formatMessage(String msg) { - msg = msg.replaceAll("&", "&"); - msg = msg.replaceAll("<", "<"); - msg = msg.replaceAll(">", ">"); - - // -- - // — - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\-\\-?((?=\\s)|(?=\\Z))", "$1—$2"); - - // http://juick.com/last?page=2 - // juick.com - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\n\\\"]+)/?[^\\s\\n\\\"]*)", "$1$3"); - - // [link text][http://juick.com/last?page=2] - // link text - msg = msg.replaceAll("\\[([^\\]]+)\\]\\[((?:ht|f)tps?://[^\\]]+)\\]", "$1"); - msg = msg.replaceAll("\\[([^\\]]+)\\]\\(((?:ht|f)tps?://[^\\)]+)\\)", "$1"); - - // #12345 - // #12345 - msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1#$2$3"); - - // #12345/65 - // #12345/65 - msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1#$2/$3$4"); - - // *bold* - // bold - msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))\\*([^\\*\\n<>]+)\\*((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); - - // /italic/ - // italic - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))/([^\\/\\n<>]+)/((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); - - // _underline_ - // underline - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))_([^\\_\\n<>]+)_((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); - - // /12 - // /12 - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1/$2$3"); - - // @username@jabber.org - // @username@jabber.org - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3"); - - // @username - // @username - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3"); - - // (http://juick.com/last?page=2) - // (juick.com) - Matcher m = regexLinks2.matcher(msg); - StringBuffer sb = new StringBuffer(); - while (m.find()) { - String url = m.group(3).replace(" ", "%20").replaceAll("\\s+", ""); - m.appendReplacement(sb, "$1$2$4$5"); - } - m.appendTail(sb); - msg = sb.toString(); - - // > citate - msg = msg.replaceAll("(?:(?<=\\n)|(?<=\\A))> *(.*)?(\\n|(?=\\Z))", "$1"); - msg = msg.replaceAll("", "\n"); - - msg = msg.replaceAll("\n", "
\n"); - return msg; - } public static PebbleEngine getEngine() { return engine; } diff --git a/server-core/src/main/java/com/juick/util/MessageUtils.java b/server-core/src/main/java/com/juick/util/MessageUtils.java deleted file mode 100644 index 1e8165c3..00000000 --- a/server-core/src/main/java/com/juick/util/MessageUtils.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.juick.util; - -/** - * Created by aalexeev on 11/13/16. - */ -public class MessageUtils { - private MessageUtils() { - throw new IllegalStateException(); - } - - public static String formatQuote(final String quote) { - String result = quote; - - if (quote != null) { - if (quote.length() > 50) { - result = ">" + quote.substring(0, 47).replace('\n', ' ') + "...\n"; - } else if (!quote.isEmpty()) { - result = ">" + quote.replace('\n', ' ') + "\n"; - } - } - - return result; - } -} -- cgit v1.2.3