aboutsummaryrefslogtreecommitdiff
path: root/juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java
blob: 0e7569ab043c0f2a0d3d88d5a7cd75ac45ff291d (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
/*
 * Copyright (C) 2008-2017, 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.formatters;

import com.juick.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("ru"));

    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.getMarkdownTags(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.escapeMarkdown(StringUtils.defaultString(jmsg.getText())));
        } 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.getTimestamp()));
        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(com.juick.Message jmsg) {
        if (MessageUtils.isReply(jmsg)) {
            return String.format("https://juick.com/%d#%d", jmsg.getMid(), jmsg.getRid());
        } else if (MessageUtils.isPM(jmsg)) {
            return "https://juick.com/pm/inbox";
        }
        return  "https://juick.com/" + jmsg.getMid();
    }

    public static String formatPostNumber(com.juick.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());
    }
}