aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/util/formatters
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2020-04-04 01:15:01 +0300
committerGravatar Vitaly Takmazov2020-04-04 01:15:01 +0300
commita608baeed738894433aacfa041e2617f60ce959f (patch)
tree1e0de7056417ff0833ae3d4600de9fec6eb81631 /src/main/java/com/juick/util/formatters
parent7a2f89266c8f6337e4e81a2fd8488e0f80f4f9bd (diff)
Initialize all components from configuration
Diffstat (limited to 'src/main/java/com/juick/util/formatters')
-rw-r--r--src/main/java/com/juick/util/formatters/PlainTextFormatter.java105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/main/java/com/juick/util/formatters/PlainTextFormatter.java b/src/main/java/com/juick/util/formatters/PlainTextFormatter.java
new file mode 100644
index 00000000..b5d67030
--- /dev/null
+++ b/src/main/java/com/juick/util/formatters/PlainTextFormatter.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2008-2019, Juick
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.util.formatters;
+
+import com.juick.model.Message;
+import com.juick.util.MessageUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.ocpsoft.prettytime.PrettyTime;
+
+import java.util.Date;
+import java.util.Locale;
+
+/**
+ * Created by vitalyster on 12.10.2016.
+ */
+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, boolean markdown) {
+ 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")
+ .append(subtitle).append("\n");
+ if (markdown) {
+ sb.append(MessageUtils.formatMarkdownText(jmsg));
+ } else {
+ sb.append(StringUtils.defaultString(jmsg.getText()));
+ }
+ sb.append("\n");
+ if (!markdown && StringUtils.isNotEmpty(jmsg.getAttachmentType())) {
+ sb.append(MessageUtils.attachmentUrl(jmsg));
+ }
+ return sb.toString();
+ }
+
+ public static String formatPostSummary(Message m) {
+ int cropLength = 384;
+ String timeAgo = pt.format(Date.from(m.getCreated()));
+ String repliesCount = m.getReplies() == 1 ? "; 1 reply" : m.getReplies() == 0 ? ""
+ : String.format("; %d replies", m.getReplies());
+ StringBuilder sb = new StringBuilder();
+ String txt = StringUtils.defaultString(m.getText());
+ String attachmentUrl = MessageUtils.attachmentUrl(m);
+ if (StringUtils.isNotEmpty(attachmentUrl)) {
+ sb.append(attachmentUrl).append("\n");
+ }
+ if (txt.length() >= cropLength) {
+ sb.append(StringUtils.substring(txt, 0, cropLength)).append(" [...]");
+ } else {
+ sb.append(txt);
+ }
+ return String.format("@%s:%s\n%s\n#%s (%s%s) %s",
+ m.getUser().getName(), MessageUtils.getTagsString(m), sb.toString(), formatPostNumber(m), timeAgo, repliesCount, formatUrl(m));
+ }
+
+ public static String formatUrl(Message jmsg) {
+ if (MessageUtils.isReply(jmsg)) {
+ return String.format("https://juick.com/m/%d#%d", jmsg.getMid(), jmsg.getRid());
+ } else if (MessageUtils.isPM(jmsg)) {
+ return "https://juick.com/pm/inbox";
+ }
+ return "https://juick.com/m/" + jmsg.getMid();
+ }
+
+ public static String markdownUrl(String url, String description) {
+ if (StringUtils.isNotBlank(description)) {
+ return String.format("[%s](%s)", description, url);
+ } else {
+ return url;
+ }
+ }
+
+ public static String formatPostNumber(Message jmsg) {
+ if (jmsg.getRid() > 0) {
+ return String.format("%d/%d", jmsg.getMid(), jmsg.getRid());
+ }
+ return String.format("%d", jmsg.getMid());
+ }
+
+ public static String formatTwitterCard(Message jmsg) {
+ return MessageUtils.getMessageHashTags(jmsg) + StringUtils.defaultString(jmsg.getText());
+ }
+}