burgercat/static/js/chat.js

137 lines
4.5 KiB
JavaScript
Raw Normal View History

2023-07-11 20:41:57 +01:00
let channelDiv = document.getElementById("channelDiv")
let messageDiv = document.getElementById("messageDiv")
let messageBox = document.getElementById("messageBox")
let statusMessage = document.getElementById("statusMessage")
let channelID = 0
2023-07-14 01:29:58 +01:00
function addMessage(content, created, creator, roomid) {
const messageParagraph = document.createElement("p");
const timeParagraph = document.createElement("p");
2023-07-12 21:12:36 +01:00
2024-04-29 22:12:42 +01:00
const hideRegex = /(https?:\/\/(?:cdn\.discordapp\.com|media\.discordapp\.net|media\.tenor\.com|media1\.tenor\.com|i\.imgur\.com)\/.+?\.(?:png|apng|webp|svg|jpg|jpeg|gif))(?=$|\s)/gi;
2023-07-14 01:29:58 +01:00
let messageContent = content.replace(hideRegex, "");
messageParagraph.innerText = `${creator}: ${messageContent}`;
messageParagraph.classList.add("messageParagraph");
messageParagraph.id = "messageParagraph";
messageParagraph.appendChild(timeParagraph);
const time = new Intl.DateTimeFormat("en-GB", { hour: "numeric", minute: "numeric" }).format(Number(created.split(".")[0]) * 1000 + 20265);
messageParagraph.innerHTML = `<span style="color: #515051; font-size: 14px;">${time}</span> ${messageParagraph.innerHTML}`;
messageDiv.append(messageParagraph);
2023-07-12 21:12:36 +01:00
2023-07-14 01:29:58 +01:00
const imgLinks = content?.match(/(https?:\/\/(?:cdn\.discordapp\.com|media\.discordapp\.net|media\.tenor\.com|i\.imgur\.com|burger\.ctaposter\.xyz)\/.+?\.(?:png|apng|webp|svg|jpg|jpeg|gif))(?=$|\s)/gi) || [];
for (const link of imgLinks) {
const img = new Image();
img.src = link;
img.className = "messageImage";
img.onload = () => {
2023-07-12 21:12:36 +01:00
const maxWidth = 400;
const maxHeight = 400;
let { width, height } = img;
if (width > maxWidth || height > maxHeight) {
2023-07-14 01:29:58 +01:00
const ratio = Math.min(maxWidth / width, maxHeight / height);
width *= ratio;
height *= ratio;
2023-07-12 21:12:36 +01:00
}
img.width = width;
img.height = height;
messageParagraph.appendChild(img);
2023-07-14 01:29:58 +01:00
};
}
messageDiv.scrollTop = messageDiv.scrollHeight - messageDiv.clientHeight;
}
async function updateMessages(id) {
const response = await fetch(`/api/chat/getmessages/${id}`);
const messages = await response.json();
statusMessage.innerText = "";
document.querySelectorAll(".messageParagraph").forEach((el) => el.remove());
for (const message of messages.reverse()) {
const { creator, content, roomid, created } = message;
addMessage(content, created, creator["username"], roomid)
2023-07-11 20:41:57 +01:00
}
2023-07-12 21:12:36 +01:00
}
function selectChannel(id) {
channelID = id
let selectedButton = channelDiv.querySelector(".selected");
if (selectedButton) {
selectedButton.classList.remove("selected");
}
let channelButton = document.getElementById("channelButton" + id)
if (channelButton) {
channelButton.classList.add("selected")
}
else {
console.log("channelButton not found")
2023-07-11 20:41:57 +01:00
}
2023-07-12 21:12:36 +01:00
updateMessages(id)
2023-07-11 20:41:57 +01:00
}
async function updateRooms() {
let response = await fetch("/api/chat/listrooms");
let rooms = await response.json()
for (let i in rooms) {
let channelButton = document.createElement("button")
channelButton.appendChild(document.createTextNode(rooms[i]["name"]))
channelButton.id = "channelButton" + rooms[i]["id"]
channelButton.onclick = function () { selectChannel(rooms[i]["id"]) }
channelDiv.append(channelButton)
}
selectChannel(1)
}
async function sendMessage(content, id) {
fetch("/api/chat/send/" + String(id), {
method: "POST",
body: JSON.stringify({
content: content
}),
headers: {
"Content-Type": "application/json"
}
})
}
2024-04-29 21:36:42 +01:00
let updateInterval = 4100
2023-07-11 20:41:57 +01:00
messageBox.addEventListener("keyup", function onEvent(event) {
if (event.key === "Enter") {
2023-07-12 00:03:03 +01:00
if (!messageBox.value == "") {
2023-07-12 01:24:51 +01:00
if (messageBox.value.length < 140) {
sendMessage(messageBox.value, channelID)
messageBox.value = ""
2024-04-29 21:36:42 +01:00
updateInterval = 1300
2023-07-12 01:24:51 +01:00
}
2024-04-29 21:36:42 +01:00
updateMessages(channelID)
2023-07-12 00:03:03 +01:00
}
2023-07-11 20:41:57 +01:00
}
})
2024-04-29 21:36:42 +01:00
function messageTimer(){
2023-07-14 01:29:58 +01:00
updateMessages(channelID)
2024-04-29 21:36:42 +01:00
if (updateInterval < 6000) {
updateInterval = updateInterval + 100
}
2023-07-14 01:29:58 +01:00
2024-04-29 21:36:42 +01:00
console.log(updateInterval)
setTimeout(messageTimer, updateInterval);
}
2023-07-14 01:29:58 +01:00
2024-04-29 21:36:42 +01:00
messageTimer();
updateRooms()
updateMessages()
2023-07-14 01:29:58 +01:00