2019-10-31 05:34:19 +01:00
|
|
|
/* globals MastodonAPI */
|
|
|
|
|
2019-10-31 06:20:30 +01:00
|
|
|
function fetch_mastodon_api_comments(instance, status_id)
|
2019-10-31 05:34:19 +01:00
|
|
|
{
|
2019-10-31 08:14:03 +01:00
|
|
|
const root = document.getElementById("mastodon-api-comments_" + status_id);
|
2019-10-31 05:34:19 +01:00
|
|
|
|
|
|
|
let api = new MastodonAPI(
|
|
|
|
{
|
|
|
|
instance: "https://" + instance,
|
|
|
|
api_user_token: ""
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get the URL of the status and write the intro.
|
|
|
|
api.get("statuses/" + status_id, function(data)
|
|
|
|
{
|
|
|
|
root.appendChild(comments_intro(data.url));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get all the replies to the status and write them to the page.
|
|
|
|
api.get("statuses/" + status_id + "/context", function(data)
|
|
|
|
{
|
2019-10-31 08:14:03 +01:00
|
|
|
write_comments(root, data);
|
2019-10-31 05:34:19 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function comments_intro(url)
|
|
|
|
{
|
|
|
|
const p = document.createElement("p");
|
|
|
|
const a = document.createElement("a");
|
|
|
|
p.appendChild(document.createTextNode("You can "));
|
2019-10-31 06:20:30 +01:00
|
|
|
p.setAttribute("class", "mastodon-api-comment-intro");
|
2019-10-31 05:34:19 +01:00
|
|
|
a.setAttribute("href", url);
|
|
|
|
a.appendChild(
|
|
|
|
document.createTextNode("comment on this post in the Fediverse"));
|
|
|
|
p.appendChild(a);
|
|
|
|
p.appendChild(document.createTextNode("."));
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-31 08:14:03 +01:00
|
|
|
function write_comments(root, data)
|
2019-10-31 05:34:19 +01:00
|
|
|
{
|
|
|
|
for (const status of data.descendants)
|
|
|
|
{
|
|
|
|
let content = status.content;
|
|
|
|
for (const emoji of status.emojis)
|
|
|
|
{
|
|
|
|
content = content.replace(
|
|
|
|
':' + emoji.shortcode + ':',
|
|
|
|
'<img style="height: 1em;" src="' + emoji.url + '">');
|
|
|
|
}
|
|
|
|
|
2019-11-02 06:54:59 +01:00
|
|
|
const div = document.createElement("div");
|
|
|
|
div.setAttribute("class", "mastodon-api-comment");
|
|
|
|
div.appendChild(author_html(status));
|
2019-10-31 10:34:42 +01:00
|
|
|
if (status.spoiler_text.length > 0)
|
|
|
|
{
|
2019-11-02 07:44:32 +01:00
|
|
|
div.appendChild(subject_html(status));
|
2019-10-31 10:34:42 +01:00
|
|
|
}
|
|
|
|
const content_p = document.createElement("p");
|
|
|
|
content_p.setAttribute("class", "mastodon-api-comment-content");
|
2019-11-01 06:38:53 +01:00
|
|
|
content_p.innerHTML = content;
|
2019-11-02 06:54:59 +01:00
|
|
|
div.appendChild(content_p);
|
|
|
|
root.appendChild(div);
|
2019-10-31 05:34:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:34:42 +01:00
|
|
|
function author_html(status)
|
2019-10-31 05:34:19 +01:00
|
|
|
{
|
|
|
|
const p = document.createElement("p");
|
2019-10-31 06:20:30 +01:00
|
|
|
p.setAttribute("class", "mastodon-api-comment-author");
|
2019-10-31 05:34:19 +01:00
|
|
|
const strong = document.createElement("strong");
|
2019-10-31 10:34:42 +01:00
|
|
|
strong.appendChild(document.createTextNode(status.account.display_name));
|
2019-10-31 05:34:19 +01:00
|
|
|
p.appendChild(strong);
|
2019-10-31 10:34:42 +01:00
|
|
|
p.appendChild(
|
|
|
|
document.createTextNode(" (" + status.account.acct + ") wrote on "
|
|
|
|
+ get_status_time(status) + ":"));
|
2019-10-31 05:34:19 +01:00
|
|
|
return p;
|
|
|
|
}
|
2019-10-31 10:34:42 +01:00
|
|
|
|
2019-11-02 07:44:32 +01:00
|
|
|
function subject_html(status)
|
|
|
|
{
|
|
|
|
const p = document.createElement("p");
|
|
|
|
p.setAttribute("class", "mastodon-api-comment-subject");
|
|
|
|
const subject = document.createElement("strong");
|
|
|
|
subject.appendChild(document.createTextNode(status.spoiler_text));
|
|
|
|
p.appendChild(subject);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:34:42 +01:00
|
|
|
// Human readable time, YYYY-MM-DD HH:MM.
|
|
|
|
function get_status_time(status)
|
|
|
|
{
|
|
|
|
return status.created_at.substr(0, 16).replace('T', ' ');
|
|
|
|
}
|