From 3e4807157e3c244820dd4d5149997970530e4fcb Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 23 Aug 2017 00:47:37 +0300 Subject: www: Twitter Cards --- .../com/juick/formatters/PlainTextFormatter.java | 10 ++++++++-- .../src/main/java/com/juick/util/MessageUtils.java | 23 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'juick-core/src/main/java/com') diff --git a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java index b86ebb73..8f9378a8 100644 --- a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java +++ b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java @@ -17,6 +17,8 @@ package com.juick.formatters; +import com.juick.Message; +import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringUtils; import org.ocpsoft.prettytime.PrettyTime; @@ -28,7 +30,7 @@ import java.util.Locale; public class PlainTextFormatter { static PrettyTime pt = new PrettyTime(new Locale("ru")); - public static String formatPost(com.juick.Message jmsg) { + public static String formatPost(Message jmsg) { StringBuilder sb = new StringBuilder(); boolean isReply = jmsg.getRid() > 0; String title = isReply ? "Reply by @" : "@"; @@ -41,7 +43,7 @@ public class PlainTextFormatter { return sb.toString(); } - public static String formatPostSummary(com.juick.Message m) { + public static String formatPostSummary(Message m) { int cropLength = 384; String timeAgo = pt.format(m.getDate()); String repliesCount = m.getReplies() == 1 ? "; 1 reply" : m.getReplies() == 0 ? "" @@ -58,4 +60,8 @@ public class PlainTextFormatter { } return "https://juick.com/" + jmsg.getMid(); } + + public static String formatTwitterCard(Message jmsg) { + return MessageUtils.getMessageHashTags(jmsg) + jmsg.getText(); + } } diff --git a/juick-core/src/main/java/com/juick/util/MessageUtils.java b/juick-core/src/main/java/com/juick/util/MessageUtils.java index a901fac1..03b677d4 100644 --- a/juick-core/src/main/java/com/juick/util/MessageUtils.java +++ b/juick-core/src/main/java/com/juick/util/MessageUtils.java @@ -17,8 +17,13 @@ package com.juick.util; +import com.juick.Message; +import com.juick.Tag; +import org.apache.commons.codec.CharEncoding; import org.apache.commons.lang3.StringUtils; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -137,4 +142,22 @@ public class MessageUtils { msg = msg.replaceAll("\n", "
\n"); return msg; } + + public static String getMessageHashTags(final Message jmsg) { + StringBuilder hashtags = new StringBuilder(); + for (Tag tag : jmsg.getTags()) { + hashtags.append("#").append(tag).append(" "); + } + return hashtags.toString(); + } + + // TODO: check if it is really needed + public static String percentEncode(final String s) { + String ret = StringUtils.EMPTY; + try { + ret = URLEncoder.encode(s, CharEncoding.UTF_8).replace("+", "%20").replace("*", "%2A").replace("%7E", "~"); + } catch (UnsupportedEncodingException e) { + } + return ret; + } } -- cgit v1.2.3