aboutsummaryrefslogtreecommitdiff
path: root/juick-common/src/main/java/com/juick
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-07-26 12:42:51 +0300
committerGravatar Vitaly Takmazov2018-07-26 12:44:12 +0300
commit264bd16c937c48b2af77473944217496fae0aa3d (patch)
treee84304d609de95bf0359c369a7643a63ad3376c1 /juick-common/src/main/java/com/juick
parent91bd51c87c715954e21e96948db11aa980dcbeb2 (diff)
Multiple trivial fixes
* https links in posts * escape reply numbers for telegram * catch when non safe url can not be stripped
Diffstat (limited to 'juick-common/src/main/java/com/juick')
-rw-r--r--juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java2
-rw-r--r--juick-common/src/main/java/com/juick/util/MessageUtils.java34
2 files changed, 23 insertions, 13 deletions
diff --git a/juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java b/juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java
index 91748684..378a523f 100644
--- a/juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java
+++ b/juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java
@@ -44,7 +44,7 @@ public class PlainTextFormatter {
sb.append(title).append(markdown ? MessageUtils.getMarkdownUser(jmsg.getUser()) : jmsg.getUser().getName()).append(":\n")
.append(subtitle).append("\n");
if (markdown) {
- sb.append(MessageUtils.escapeMarkdown(StringUtils.defaultString(jmsg.getText())));
+ sb.append(MessageUtils.formatMarkdownText(jmsg));
} else {
sb.append(StringUtils.defaultString(jmsg.getText()));
}
diff --git a/juick-common/src/main/java/com/juick/util/MessageUtils.java b/juick-common/src/main/java/com/juick/util/MessageUtils.java
index ae0c2a8b..bac24507 100644
--- a/juick-common/src/main/java/com/juick/util/MessageUtils.java
+++ b/juick-common/src/main/java/com/juick/util/MessageUtils.java
@@ -66,6 +66,8 @@ public class MessageUtils {
private final static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[\\{]|&lt;)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](?<!&gt;))*)([\\]\\}]|&gt;)");
+ private final static String replyNumberRegex = "((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))";
+
public static String formatMessageCode(String msg) {
msg = msg.replaceAll("&", "&amp;");
msg = msg.replaceAll("<", "&lt;");
@@ -109,11 +111,11 @@ public class MessageUtils {
// #12345
// <a href="http://juick.com/12345">#12345</a>
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1<a href=\"http://juick.com/m/$2\">#$2</a>$3");
+ msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1<a href=\"https://juick.com/m/$2\">#$2</a>$3");
// #12345/65
// <a href="http://juick.com/12345#65">#12345/65</a>
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"http://juick.com/m/$2#$3\">#$2/$3</a>$4");
+ msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"https://juick.com/m/$2#$3\">#$2/$3</a>$4");
// *bold*
// <b>bold</b>
@@ -129,15 +131,15 @@ public class MessageUtils {
// /12
// <a href="#12">/12</a>
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"#$2\">/$2</a>$3");
+ msg = msg.replaceAll(replyNumberRegex, "$1<a href=\"#$2\">/$2</a>$3");
// @username@jabber.org
// <a href="http://juick.com/username@jabber.org/">@username@jabber.org</a>
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"http://juick.com/$2/\">@$2</a>$3");
+ msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"https://juick.com/$2/\">@$2</a>$3");
// @username
// <a href="http://juick.com/username/">@username</a>
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"http://juick.com/$2/\">@$2</a>$3");
+ msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"https://juick.com/$2/\">@$2</a>$3");
// (http://juick.com/last?page=2)
// (<a href="http://juick.com/last?page=2" rel="nofollow">juick.com</a>)
@@ -211,6 +213,10 @@ public class MessageUtils {
}
return ret;
}
+ public static String formatMarkdownText(final Message msg) {
+ String s = StringUtils.defaultString(msg.getText()).replaceAll(replyNumberRegex, String.format("$1[/$2](https://juick.com/m/%d#$2)$3", msg.getMid()));
+ return escapeMarkdown(s);
+ }
public static String escapeMarkdown(final String s) {
return s.replace("_", "\\_").replace("*", "\\*")
.replace("`", "\\`");
@@ -280,14 +286,18 @@ public class MessageUtils {
public static String stripNonSafeUrls(String input) {
// strip login urls
- Matcher urlMatcher = Pattern.compile(MessageUtils.urlRegex).matcher(input);
- while (urlMatcher.find()) {
- URI uri = URI.create(urlMatcher.group(0));
- if (uri.getHost().equals("juick.com")) {
- UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUri(uri);
- uriComponentsBuilder.replaceQueryParam("hash");
- input = input.replace(urlMatcher.group(0), uriComponentsBuilder.build().toUriString());
+ try {
+ Matcher urlMatcher = Pattern.compile(MessageUtils.urlRegex).matcher(input);
+ while (urlMatcher.find()) {
+ URI uri = URI.create(urlMatcher.group(0));
+ if (uri.getHost().equals("juick.com")) {
+ UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUri(uri);
+ uriComponentsBuilder.replaceQueryParam("hash");
+ input = input.replace(urlMatcher.group(0), uriComponentsBuilder.build().toUriString());
+ }
}
+ } catch (IllegalArgumentException e) {
+ return input;
}
return input;
}