v6/site/static/js/comments.js

62 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2025-02-20 10:13:26 -05:00
let hasLoadedComments = false;
2025-02-22 17:10:05 -05:00
const commentsContainer = document.getElementById("comments-container");
commentsContainer.addEventListener("toggle", (_) => {
2025-02-22 17:53:14 -05:00
if (commentsContainer.open && !hasLoadedComments) {
2025-02-22 17:10:05 -05:00
hasLoadedComments = true;
fetchComments();
}
2022-12-10 13:15:32 -05:00
});
async function fetchComments() {
2025-02-22 17:10:05 -05:00
if (typeof commentsPostID !== "undefined") {
const res = await fetch(
`https://social.shadowfacts.net/api/v1/statuses/${commentsPostID}/context`,
);
const json = await res.json();
const comments = json.descendants.map(makeCommentHTML).join("");
const list = document.getElementById("comments-list");
list.innerHTML = comments;
list.querySelectorAll(".comment-body a").forEach((a) => {
a.target = "_blank";
a.rel = "nofollow";
});
}
2022-12-10 13:15:32 -05:00
}
2025-02-20 10:13:26 -05:00
function makeCommentHTML(status) {
2025-02-22 17:10:05 -05:00
const date = new Date(status.created_at);
const month = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
][date.getMonth()];
return `
2025-02-20 10:13:26 -05:00
<div class="comment">
<div class="comment-user">
<img src="${status.account.avatar}" loading="lazy">
<div class="comment-user-name">
<h3>${status.account.display_name}</h3>
<a href="${status.account.url}" target="_blank">@${status.account.acct}</a>
</div>
<a href="${status.url}" target="_blank">
<time datetime="${status.created_at}">
${month} ${date.getDate()}, ${date.getFullYear()}
</time>
</a>
</div>
<div class="comment-body">
${status.content}
</div>
</div>
`;
2022-12-10 13:15:32 -05:00
}