diff options
Diffstat (limited to 'juick-common/src')
3 files changed, 35 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))([\\[\\{]|<)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](?<!>))*)([\\]\\}]|>)"); + private final static String replyNumberRegex = "((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))"; + public static String formatMessageCode(String msg) { msg = msg.replaceAll("&", "&"); msg = msg.replaceAll("<", "<"); @@ -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; } diff --git a/juick-common/src/test/java/com/juick/MessageTest.java b/juick-common/src/test/java/com/juick/MessageTest.java index 7d11503d..aaa66af2 100644 --- a/juick-common/src/test/java/com/juick/MessageTest.java +++ b/juick-common/src/test/java/com/juick/MessageTest.java @@ -156,4 +156,16 @@ public class MessageTest { assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*test"), equalTo(1)); assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*ab"), equalTo(1)); } + @Test + public void markdownContentShouldNotHaveUnescapedReplyNumbersBecauseOfTelegram() { + Message msg = new Message(); + msg.setMid(1); + msg.setText("See /303 again"); + assertThat(MessageUtils.formatMarkdownText(msg), is("See [/303](https://juick.com/m/1#303) again")); + } + @Test + public void shouldNotThrowIfUrlContainsIllegalCharacters() { + String msg = "[te](http://juick.com/)[st](http://juick.com/)"; + assertThat(MessageUtils.stripNonSafeUrls(msg), is(msg)); + } } |