lilachat-frontend/js/chat.js

149 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-07-22 20:09:02 +00:00
// VARIABLES
let messageCount = 0;
let username = localStorage.getItem('username');
const form = document.querySelector('form');
// SEND A MESSAGE
// GRABS MESSAGE FROM FORM & SENDS
form.addEventListener("submit", async function (event) {
event.preventDefault();
const formData = new FormData(form);
2021-07-23 14:30:43 +00:00
formMessage = formData.get('message').toString();
2021-07-22 20:09:02 +00:00
//KINDA UNNECESSARY
//CHECKS TO SEE IF THE PERSON IS LOGGED IN IN ORDER TO SEND A MESSAGE.
2021-07-26 22:48:24 +00:00
const response = await fetch(`../api/token/${username}/`);
const matches = await response.json();
//YES THIS IS CONFUSING I KNOW.
if (matches.status === "ok") {
sendMessage()
} else {
const mismatch = 'Username and token mismatch. Try logging in again.'
printText(mismatch.bold())
logout()
localStorage.removeItem('username')
2021-07-25 19:09:19 +00:00
form.reset()
}
2021-07-26 22:02:20 +00:00
return formMessage;
2021-07-22 20:09:02 +00:00
})
//SEND MESSAGE FETCH FUNCTION
async function sendMessage() {
2021-07-23 19:41:55 +00:00
sendMessageInfo = { "name": username, "body": formMessage }
2021-07-26 22:48:24 +00:00
fetch('../api/message/send', {
2021-07-22 20:09:02 +00:00
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(sendMessageInfo),
})
form.reset()
2021-07-26 22:02:20 +00:00
modCommand()
2021-07-22 20:09:02 +00:00
}
// RECIEVE MESSAGES
let messageUpdate = window.setInterval(fetchMessages, 1000);
2021-07-22 20:09:02 +00:00
async function fetchMessages() {
const response = await fetch('/api/message/messages.json');
const recievedMessages = await response.json();
2021-07-24 15:17:41 +00:00
document.getElementById("chatbox").innerHTML = ""
2021-07-22 20:09:02 +00:00
for (const message of recievedMessages) {
2021-07-26 22:02:20 +00:00
let leftBracket = '('
let rightBracket = ')'
let space = ' '
if (message.pronouns === '' || message.pronouns === 'none' || message.pronouns === null) {
leftBracket = ''
rightBracket = ''
space = ''
}
printText(message.user.bold().toString() + space + leftBracket.small() + message.pronouns.small().toString() + rightBracket.small() + ": " + message.body.toString());
2021-07-22 20:09:02 +00:00
}
2021-07-24 14:10:41 +00:00
2021-07-22 20:09:02 +00:00
if (recievedMessages.length != messageCount) {
2021-07-24 15:17:41 +00:00
let scroll = document.getElementById("chatbox");
2021-07-22 20:09:02 +00:00
scroll.scrollTop = scroll.scrollHeight;
2021-07-24 14:10:41 +00:00
}
2021-07-22 20:09:02 +00:00
messageCount = recievedMessages.length;
}
// FUNCTION TO PRINT MESSAGES IN THE CHAT BOX
function printText(text) {
let p = document.createElement("p");
2021-07-24 15:17:41 +00:00
const div = document.getElementById("chatbox");
2021-07-22 20:09:02 +00:00
div.appendChild(p)
p.innerHTML = text
}
2021-07-23 06:51:26 +00:00
//LOGGED IN STUFF
//TODO ADD CHECK TO SEE IF USERNAME AND TOKEN MATCHES
2021-07-24 14:10:41 +00:00
function loggedIn() {
2021-07-24 19:58:53 +00:00
if (username === '' || username === null) {
2021-07-24 14:10:41 +00:00
document.querySelector("#loggeduser").innerHTML = 'You are not logged in'
} else {
2021-07-24 19:58:53 +00:00
document.querySelector("#loggeduser").innerHTML = `You are logged in as "${username}"`
2021-07-24 14:10:41 +00:00
}
}
2021-07-24 15:17:41 +00:00
loggedIn()
2021-07-26 22:02:20 +00:00
//MODERATION
async function modCommand() {
let action = ''
let target = ''
if (formMessage.startsWith('/ban')) {
action = "Ban"
target = formMessage.replace('/ban ', '')
sendCommand()
} else if (formMessage.startsWith('/kick')) {
action = "Kick"
target = formMessage.replace('/kick ', '')
sendCommand()
} else if (formMessage.startsWith('/promote')) {
action = "Promote"
target = formMessage.replace('/promote ', '')
sendCommand()
} else if (formMessage.startsWith('/demote')) {
action = "Demote"
target = formMessage.replace('/demote ', '')
sendCommand()
} else {
return;
}
async function sendCommand() {
let sendCommand = { "name": username, "action": action, "target": target }
const response = await fetch('/api/mod/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(sendCommand),
});
if (response.status === 'ok') {
return;
} else {
printText('Error Issuing Command. Are you an Admin or Mod?')
}
}
}
2021-07-24 15:17:41 +00:00