hugo-mastodon-api-comments/mastodon-api-comments.js

74 lines
2.1 KiB
JavaScript

/* globals MastodonAPI */
function fetch_fediverse_comments(instance, status_id)
{
const root = document.getElementById("fediverse-comments");
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)
{
write_comments(data);
});
}
function comments_intro(url)
{
const p = document.createElement("p");
const a = document.createElement("a");
p.appendChild(document.createTextNode("You can "));
p.setAttribute("class", "fediverse-comment-intro");
a.setAttribute("href", url);
a.appendChild(
document.createTextNode("comment on this post in the Fediverse"));
p.appendChild(a);
p.appendChild(document.createTextNode("."));
return p;
}
function write_comments(data)
{
const root = document.getElementById("fediverse-comments");
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 + '">');
}
const p = document.createElement("p");
p.setAttribute("class", "fediverse-comment");
p.appendChild(author_html(status));
p.innerHTML += content;
root.appendChild(p);
}
}
function author_html(status)
{
const p = document.createElement("p");
p.setAttribute("class", "fediverse-comment-author");
const strong = document.createElement("strong");
strong.appendChild(document.createTextNode(status.display_name));
p.appendChild(strong);
p.appendChild(document.createTextNode(" (" + status.acct + ") wrote:"));
return p;
}