\n");
return xs_str_cat(os, s);
}
xs_str *html_entry(snac *user, xs_str *os, const xs_dict *msg, int local,
int level, const char *md5, int hide_children)
{
char *id = xs_dict_get(msg, "id");
char *type = xs_dict_get(msg, "type");
char *actor;
int sensitive = 0;
char *v;
xs *boosts = NULL;
/* do not show non-public messages in the public timeline */
if ((local || !user) && !is_msg_public(msg))
return os;
/* hidden? do nothing more for this conversation */
if (user && is_hidden(user, id))
return os;
/* avoid too deep nesting, as it may be a loop */
if (level >= 256)
return os;
xs *s = xs_str_new("
\n");
{
xs *s1 = xs_fmt("\n", md5);
s = xs_str_cat(s, s1);
}
if (strcmp(type, "Follow") == 0) {
s = xs_str_cat(s, "
\n
\n");
xs *s1 = xs_fmt("
%s
\n", L("follows you"));
s = xs_str_cat(s, s1);
s = html_msg_icon(s, msg);
s = xs_str_cat(s, "
\n
\n");
return xs_str_cat(os, s);
}
else
if (!xs_match(type, "Note|Question|Page|Article")) {
/* skip oddities */
return os;
}
/* ignore notes with "name", as they are votes to Questions */
if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
return os;
/* bring the main actor */
if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
return os;
/* ignore muted morons immediately */
if (user && is_muted(user, actor))
return os;
if ((user == NULL || strcmp(actor, user->actor) != 0)
&& !valid_status(actor_get(actor, NULL)))
return os;
if (level == 0)
s = xs_str_cat(s, "
\n"); /** **/
else
s = xs_str_cat(s, "
\n"); /** **/
s = xs_str_cat(s, "
\n
"); /** **/
if (user && is_pinned(user, id)) {
/* add a pin emoji */
xs *f = xs_fmt(" 📌 ", L("Pinned"));
s = xs_str_cat(s, f);
}
if (strcmp(type, "Question") == 0) {
/* add the ballot box emoji */
xs *f = xs_fmt(" 🗳 ", L("Poll"));
s = xs_str_cat(s, f);
if (user && was_question_voted(user, id)) {
/* add a check to show this poll was voted */
xs *f2 = xs_fmt(" ✓ ", L("Voted"));
s = xs_str_cat(s, f2);
}
}
/* if it's a user from this same instance, add the score */
if (xs_startswith(id, srv_baseurl)) {
int n_likes = object_likes_len(id);
int n_boosts = object_announces_len(id);
/* alternate emojis: %d 👍 %d 🔁 */
xs *s1 = xs_fmt("%d ★ %d ↺\n", n_likes, n_boosts);
s = xs_str_cat(s, s1);
}
s = xs_str_cat(s, "
\n");
if (boosts == NULL)
boosts = object_announces(id);
if (xs_list_len(boosts)) {
/* if somebody boosted this, show as origin */
char *p = xs_list_get(boosts, -1);
xs *actor_r = NULL;
if (user && xs_list_in(boosts, user->md5) != -1) {
/* we boosted this */
xs *es1 = encode_html(xs_dict_get(user->config, "name"));
xs *s1 = xs_fmt(
"
\n", es1);
s = xs_str_cat(s, s1);
}
/* is it sensitive? */
if (user && xs_type(xs_dict_get(msg, "sensitive")) == XSTYPE_TRUE) {
if (xs_is_null(v = xs_dict_get(msg, "summary")) || *v == '\0')
v = "...";
/* only show it when not in the public timeline and the config setting is "open" */
char *cw = xs_dict_get(user->config, "cw");
if (xs_is_null(cw) || local)
cw = "";
xs *es1 = encode_html(v);
xs *s1 = xs_fmt("%s [%s]\n", cw, es1, L("SENSITIVE CONTENT"));
s = xs_str_cat(s, s1);
sensitive = 1;
}
#if 0
{
xs *md5 = xs_md5_hex(id, strlen(id));
xs *s1 = xs_fmt("
%s
\n", md5);
s = xs_str_cat(s, s1);
}
#endif
{
const char *content = xs_dict_get(msg, "content");
xs *c = sanitize(xs_is_null(content) ? "" : content);
char *p, *v;
/* do some tweaks to the content */
c = xs_replace_i(c, "\r", "");
while (xs_endswith(c, "
"))
c = xs_crop_i(c, 0, -4);
c = xs_replace_i(c, "
", "
");
if (!xs_startswith(c, "
")) {
xs *s1 = c;
c = xs_fmt("
%s
", s1);
}
/* replace the :shortnames: */
if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
xs *tag = NULL;
if (xs_type(p) == XSTYPE_DICT) {
/* not a list */
tag = xs_list_new();
tag = xs_list_append(tag, p);
}
else
if (xs_type(p) == XSTYPE_LIST)
tag = xs_dup(p);
else
tag = xs_list_new();
xs_list *tags = tag;
/* iterate the tags */
while (xs_list_iter(&tags, &v)) {
char *t = xs_dict_get(v, "type");
if (t && strcmp(t, "Emoji") == 0) {
char *n = xs_dict_get(v, "name");
char *i = xs_dict_get(v, "icon");
if (n && i) {
char *u = xs_dict_get(i, "url");
xs *img = xs_fmt("", u, n);
c = xs_replace_i(c, n, img);
}
}
}
}
if (strcmp(type, "Question") == 0) { /** question content **/
xs_list *oo = xs_dict_get(msg, "oneOf");
xs_list *ao = xs_dict_get(msg, "anyOf");
xs_list *p;
xs_dict *v;
int closed = 0;
if (xs_dict_get(msg, "closed"))
closed = 2;
else
if (user && xs_startswith(id, user->actor))
closed = 1; /* we questioned; closed for us */
else
if (user && was_question_voted(user, id))
closed = 1; /* we already voted; closed for us */
/* get the appropriate list of options */
p = oo != NULL ? oo : ao;
if (closed || user == NULL) {
/* closed poll */
c = xs_str_cat(c, "
\n");
while (xs_list_iter(&p, &v)) {
const char *name = xs_dict_get(v, "name");
const xs_dict *replies = xs_dict_get(v, "replies");
if (name && replies) {
int nr = xs_number_get(xs_dict_get(replies, "totalItems"));
xs *es1 = encode_html(name);
xs *l = xs_fmt("
%s:
%d
\n", es1, nr);
c = xs_str_cat(c, l);
}
}
c = xs_str_cat(c, "
\n");
}
else {
/* poll still active */
xs *s1 = xs_fmt("
\n"
"\n
\n\n", L("Vote"));
s1 = xs_str_cat(s1, s2);
c = xs_str_cat(c, s1);
}
/* if it's *really* closed, say it */
if (closed == 2) {
xs *s1 = xs_fmt("
%s
\n", L("Closed"));
c = xs_str_cat(c, s1);
}
else {
/* show when the poll closes */
const char *end_time = xs_dict_get(msg, "endTime");
if (!xs_is_null(end_time)) {
time_t t0 = time(NULL);
time_t t1 = xs_parse_iso_date(end_time, 0);
if (t1 > 0 && t1 > t0) {
time_t diff_time = t1 - t0;
xs *tf = xs_str_time_diff(diff_time);
char *p = tf;
/* skip leading zeros */
for (; *p == '0' || *p == ':'; p++);
xs *es1 = encode_html(p);
xs *s1 = xs_fmt("
%s %s
", L("Closes in"), es1);
c = xs_str_cat(c, s1);
}
}
}
}
s = xs_str_cat(s, c);
}
s = xs_str_cat(s, "\n");
/* add the attachments */
v = xs_dict_get(msg, "attachment");
if (!xs_is_null(v)) { /** attachments **/
xs *attach = NULL;
/* ensure it's a list */
if (xs_type(v) == XSTYPE_DICT) {
attach = xs_list_new();
attach = xs_list_append(attach, v);
}
else
if (xs_type(v) == XSTYPE_LIST)
attach = xs_dup(v);
else
attach = xs_list_new();
/* does the message have an image? */
if (xs_type(v = xs_dict_get(msg, "image")) == XSTYPE_DICT) {
/* add it to the attachment list */
attach = xs_list_append(attach, v);
}
/* make custom css for attachments easier */
s = xs_str_cat(s, "
\n");
xs_list *p = attach;
while (xs_list_iter(&p, &v)) {
const char *t = xs_dict_get(v, "mediaType");
if (xs_is_null(t))
t = xs_dict_get(v, "type");
if (xs_is_null(t))
continue;
const char *url = xs_dict_get(v, "url");
if (xs_is_null(url))
url = xs_dict_get(v, "href");
if (xs_is_null(url))
continue;
/* infer MIME type from non-specific attachments */
if (xs_list_len(attach) < 2 && xs_match(t, "Link|Document")) {
const char *mt = xs_mime_by_ext(url);
if (xs_match(mt, "image/*|audio/*|video/*")) /* */
t = mt;
}
const char *name = xs_dict_get(v, "name");
if (xs_is_null(name))
name = xs_dict_get(msg, "name");
if (xs_is_null(name))
name = L("No description");
xs *es1 = encode_html(name);
xs *s1 = NULL;
if (xs_startswith(t, "image/") || strcmp(t, "Image") == 0) {
s1 = xs_fmt(
""
"\n",
url, url, es1, es1);
}
else
if (xs_startswith(t, "video/")) {
s1 = xs_fmt("\n", url, url, es1);
}
else
if (xs_startswith(t, "audio/")) {
s1 = xs_fmt("\n", url, url, es1);
}
else
if (strcmp(t, "Link") == 0) {
xs *es2 = encode_html(url);
s1 = xs_fmt("
\n", url, es1);
}
if (!xs_is_null(s1))
s = xs_str_cat(s, s1);
}
s = xs_str_cat(s, "
\n");
}
/* has this message an audience (i.e., comes from a channel or community)? */
const char *audience = xs_dict_get(msg, "audience");
if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
xs *es1 = encode_html(audience);
xs *s1 = xs_fmt("
\n",
audience, L("Source channel or community"), es1);
s = xs_str_cat(s, s1);
}
if (sensitive)
s = xs_str_cat(s, "
\n");
s = xs_str_cat(s, "
\n");
/** controls **/
if (!local && user)
s = html_entry_controls(user, s, msg, md5);
/** children **/
if (!hide_children) {
xs *children = object_children(id);
int left = xs_list_len(children);
if (left) {
char *p, *cmd5;
int older_open = 0;
xs *ss = xs_str_new(NULL);
int n_children = 0;
ss = xs_str_cat(ss, "...
\n");
if (level < 4)
ss = xs_str_cat(ss, "
\n");
else
ss = xs_str_cat(ss, "
\n");
if (left > 3) {
xs *s1 = xs_fmt("%s\n", L("Older..."));
ss = xs_str_cat(ss, s1);
older_open = 1;
}
p = children;
while (xs_list_iter(&p, &cmd5)) {
xs *chd = NULL;
if (user)
timeline_get_by_md5(user, cmd5, &chd);
else
object_get_by_md5(cmd5, &chd);
if (older_open && left <= 3) {
ss = xs_str_cat(ss, "\n");
older_open = 0;
}
if (chd != NULL && xs_is_null(xs_dict_get(chd, "name"))) {
ss = html_entry(user, ss, chd, local, level + 1, cmd5, hide_children);
n_children++;
}
else
srv_debug(2, xs_fmt("cannot read child %s", cmd5));
left--;
}
if (older_open)
ss = xs_str_cat(ss, "\n");
ss = xs_str_cat(ss, "
\n");
ss = xs_str_cat(ss, "\n");
if (n_children)
s = xs_str_cat(s, ss);
}
}
s = xs_str_cat(s, "
\n
\n");
return xs_str_cat(os, s);
}
xs_str *html_footer(xs_str *s)
{
xs *s1 = xs_fmt(
"\n",
srv_baseurl,
L("about this site"),
WHAT_IS_SNAC_URL
);
return xs_str_cat(s, s1);
}
xs_str *html_timeline(snac *user, const xs_list *list, int local,
int skip, int show, int show_more, char *tag)
/* returns the HTML for the timeline */
{
xs_str *s = xs_str_new(NULL);
xs_list *p = (xs_list *)list;
char *v;
double t = ftime();
if (user)
s = html_user_header(user, s, local);
else
s = html_instance_header(s, tag);
if (user && !local)
s = html_top_controls(user, s);
s = xs_str_cat(s, "\n");
s = xs_str_cat(s, "
\n");
while (xs_list_iter(&p, &v)) {
xs *msg = NULL;
int status;
if (user && !is_pinned_by_md5(user, v))
status = timeline_get_by_md5(user, v, &msg);
else
status = object_get_by_md5(v, &msg);
if (!valid_status(status))
continue;
/* if it's an instance page, discard private users */
if (user == NULL && xs_startswith(xs_dict_get(msg, "id"), srv_baseurl)) {
const char *atto = xs_dict_get(msg, "attributedTo");
xs *l = xs_split(atto, "/");
const char *uid = xs_list_get(l, -1);
snac user;
int skip = 1;
if (uid && user_open(&user, uid)) {
if (xs_type(xs_dict_get(user.config, "private")) != XSTYPE_TRUE)
skip = 0;
user_free(&user);
}
if (skip)
continue;
}
s = html_entry(user, s, msg, local, 0, v, user ? 0 : 1);
}
s = xs_str_cat(s, "
\n");
if (list && user && local) {
if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE) {
s = xs_str_cat(s, "\n");
}
else {
xs *s1 = xs_fmt(
"