aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/util/formatters/PlainTextFormatter.java
blob: 9a885bca015529fbdeff8235d8c827bcf07c130a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (C) 2008-2020, 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, String domain) {
        return formatPost(jmsg, false, domain);
    }

    public static String formatPost(Message jmsg, boolean compatibleWithDurov, String domain) {
        StringBuilder sb = new StringBuilder();
        String title = MessageUtils.isReply(jmsg) ? "Reply by @"  : MessageUtils.isPM(jmsg) ? "Private message from @" : "@";
        String subtitle = MessageUtils.isReply(jmsg) ? jmsg.getReplyQuote()
                : compatibleWithDurov ? MessageUtils.getMessageHashTags(jmsg) : MessageUtils.getTagsString(jmsg);
        sb.append(title).append(compatibleWithDurov ? MessageUtils.getUserHtmlLink(jmsg.getUser(), domain)  : jmsg.getUser().getName()).append(":\n")
                .append(subtitle).append("\n");
        sb.append(compatibleWithDurov? MessageUtils.formatMessage(jmsg.getText(), true) : StringUtils.defaultString(jmsg.getText()));
        sb.append("\n");
        if (!compatibleWithDurov && 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, 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());
    }
}