import request from "./request.js";
const message_div = document.getElementById("messages");
const messages_raw = await fetch(`/api/threads/${message_div.getAttribute("value")}/messages/`).then(res => res.json());
for (const message of messages_raw)
renderMessage(message);
function renderMessage(message) {
const messageElement = document.createElement("div");
messageElement.classList.add("message");
messageElement.setAttribute("id", "message-" + message.id);
messageElement.innerHTML = `
${new Date(message.time).toLocaleString()}
${message.content.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\n", "
")}
${/* if */!message.deleted ?
`
DELETE
EDIT
` /* else */ :
`
This message has been deleted
UNDELETE
`
}
`;
message_div.appendChild(messageElement);
message_div.innerHTML += "
";
};
window.scrollTo(0, document.body.scrollHeight);
/**
* Message Sender
*/
document.getElementById("send").addEventListener("submit", async e => {
e.preventDefault();
const form = e.target;
const data = new FormData(form);
request("/api/messages", "POST", { threadID: data.get("threadID"), content: data.get("content") })
.then(res => {
if (!res) return;
form.reset();
res.reactCount = 0;
renderMessage(res);
});
});
/**
* OTHER FUNCTIONS
*/
async function delete_thread(id) {
const response = await request("/api/threads/" + id + "/delete");
if (response.deleted) {
alert("Thread deleted");
location.reload();
}
}
async function undelete_thread(id) {
const response = await request("/api/threads/" + id + "/undelete");
if (!response.deleted) {
alert("Thread undeleted");
location.reload();
}
}
async function undelete_message(id) {
const response = await request(`/api/messages/${id}/undelete`);
if (!response.deleted)
document.getElementById("message-delete-" + id).innerHTML = `DELETE`;
}
async function delete_message(id) {
const response = await request(`/api/messages/${id}/delete`);
if (response.deleted) {
alert("Message deleted");
document.getElementById("message-delete-" + id).innerHTML = `
This message has been deleted
UNDELETE`;// ADMIN PERM FIX
}
}
async function react(id, type) {
const res = await request(`/api/messages/${id}/react/${type}`)
document.getElementById(`count${id}`).innerHTML = res.reactCount;
}
window.delete_message = delete_message;
window.undelete_message = undelete_message;
window.react = react;
window.delete_thread = delete_thread;
window.undelete_thread = undelete_thread;