aboutsummaryrefslogtreecommitdiffstats
path: root/src/activitypub/appost.cpp
blob: c1f78d4b11a813245f660a9c3922269c32e199f3 (plain)
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
#include "appost.h"
#include "src/settings_interface.h"

APPost::APPost(APObjectFields fields) {
    for (QString actor_url : fields.to_actors)
        to_actors.push_back(APActor(actor_url));
    for (QString actor_url : fields.cc_actors)
        cc_actors.push_back(APActor(actor_url));
    by_actor = fields.by_actor;

    object_url = fields.object_url;
    web_url = fields.web_url;
    reply_to_url = fields.reply_to_url;
    published = QDateTime::fromString(fields.published, Qt::ISODate);

    for (APAttachmentFields attachment : fields.attachments)
        attachments.push_back(APAttachment(attachment));

    languages = fields.languages;
    content = fields.content;

    if (not fields.summary.isEmpty()) {
        is_sensitive = true;
        summary = fields.summary;
    }

    visibility = fields.visibility;
}

QString APPost::get_html_status_languages() {
    QString text;

    for (auto lang : languages)
        text.append(lang + ", ");

    if (text.isEmpty())
        text = "<em>(unknown)</em>";
    else
        text.chop(2); // remove leftover ", "

    return text;
}

QString APPost::get_html_render(HtmlRenderDetails render_info) {
    QString html(get_html_template(QStringLiteral("appost")));

    if (published.isValid()) {
        // Using QLocale::toString() is forward compatible with Qt 6 as QDateTime::toString() will not return anymore a string in the system locale.
        html.replace("{{published}}", render_info.locale->toString(
            SettingsInterface::quick_read_setting<AppSettingsTypes::Timezone>("ui/timezone") ? published.toUTC() : published.toLocalTime()
        ));
    }

    html.replace("{{url-id}}", object_url);

    html.replace("{{to}}", to_actors.get_html_render(render_info));
    html.replace("{{cc}}", cc_actors.get_html_render(render_info));
    html.replace("{{by}}", by_actor.get_html_render(render_info));

    QString summary_text = summary;
    if (summary_text.isEmpty())
        summary_text = "<em>(empty)</em>";
    html.replace("{{summary}}", summary_text);

    html.replace("{{attachment-div}}", attachments.get_html_render(render_info));

    html.replace("{{url-status}}", web_url);

    html.replace("{{content}}", content);
    html.replace("{{lang}}", get_html_status_languages());

    return html;
}