aboutsummaryrefslogtreecommitdiff
path: root/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java
blob: bd4cfedbce55171c02eb57a84e57cdaefbbf2e85 (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
/*
 * 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) {
        StringBuilder sb = new StringBuilder();
        boolean isReply = jmsg.getRid() > 0;
        String title = isReply ? "Reply by @"  : "@";
        String subtitle = isReply ? jmsg.getReplyQuote() : jmsg.getTagsString();
        sb.append(title).append(jmsg.getUser().getName()).append(":\n")
                .append(subtitle).append("\n").append(jmsg.getText()).append("\n");
        if (StringUtils.isNotEmpty(jmsg.getAttachmentType())) {
            // FIXME: attachment does not serialized to xml
            if (jmsg.getAttachment() == null) {
                if (jmsg.getRid() > 0) {
                    sb.append(String.format("http://i.juick.com/photos-1024/%d-%d.%s", jmsg.getMid(),
                            jmsg.getRid(), jmsg.getAttachmentType()));
                } else {
                    sb.append(String.format("http://i.juick.com/photos-1024/%d.%s", jmsg.getMid(),
                            jmsg.getAttachmentType()));
                }
            } else {
                sb.append(jmsg.getAttachment().getMedium().getUrl());
            }
        }
        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());
        String txt = m.getText().length() >= cropLength ?
                StringUtils.substring(m.getText(), 0, cropLength) + " [...]" : m.getText();
        return String.format("@%s:%s\n%s\n#%d (%s%s) http://juick.com/%d",
                m.getUser().getName(), m.getTagsString(), txt, m.getMid(), timeAgo, repliesCount, m.getMid());
    }

    public static String formatUrl(com.juick.Message jmsg) {
        if (jmsg.getRid() > 0) {
            return String.format("https://juick.com/%d#%d", jmsg.getMid(), jmsg.getRid());
        }
        return  "https://juick.com/" + jmsg.getMid();
    }

    public static String formatTwitterCard(Message jmsg) {
        return MessageUtils.getMessageHashTags(jmsg) + jmsg.getText();
    }
}