aboutsummaryrefslogtreecommitdiff
path: root/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java
blob: 37b0c6d385171110f1ce99de3e148ebdba743a9d (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
package com.juick.formatters;

import com.juick.Message;

/**
 * Created by vitalyster on 12.10.2016.
 */
public class PlainTextFormatter {
    public static String formatPost(com.juick.Message jmsg) {
        StringBuilder sb = new StringBuilder();
        boolean isReply = jmsg.getRID() > 0;
        String title = isReply ? "Reply by @"  : "@";
        String subtitle = isReply ? getReplyQuote(jmsg) : jmsg.getTagsString();
        sb.append(title).append(jmsg.getUser().getUName()).append(":\n")
                .append(subtitle).append("\n").append(jmsg.getText()).append("\n");
        if (jmsg.Photo != null) {
            sb.append(jmsg.Photo);
        }
        return sb.toString();
    }

    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();
    }

    private static String getReplyQuote(Message msg) {
        String quote = msg.getText();
        if (quote.length() > 50) {
            quote = ">" + quote.substring(0, 47).replace('\n', ' ') + "...\n";
        } else if (quote.length() > 0) {
            quote = ">" + quote.replace('\n', ' ') + "\n";
        }
        return quote;
    }
}