Added Endpoints and AJAX Helper Functions for emote management, as well as imporvements to link embedding.

This commit is contained in:
rainbow napkin 2024-12-17 21:51:34 -05:00
parent b9283607d6
commit 255e6e0d7f
7 changed files with 63 additions and 27 deletions

View file

@ -1,17 +1,17 @@
{
"default": {
"name": "[bill]",
"name": "bill",
"link": "https://upload.wikimedia.org/wikipedia/en/thumb/9/9b/Bill_Dauterive.png/150px-Bill_Dauterive.png",
"type": "image"
},
"array": [
{
"name": "[crabrave]",
"name": "crabrave",
"link": "https://media.tenor.com/PqFN1orijJ4AAAAC/crab-sneeth.gif",
"type": "image"
},
{
"name": "[homerbushes]",
"name": "homerbushes",
"link": "https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExb3gydGNrcnF4OWthbDg1c2RxczU4cTUzaGJsb3Bmazdsa3F5NWwxOSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9dg/O4B7pH57BbdVZVcNgS/giphy.mp4",
"type": "video"
}

View file

@ -48,21 +48,28 @@ module.exports.post = async function(req, res){
//if we have one
if(emoteDB != null){
//Throw a shit fit
return errorHandler(res, `Emote '[${name}]' already exists!`);
return errorHandler(res, `Emote [${name}] already exists!`);
}
const linkObj = linkUtils.markLink(link);
//Initialize the emote object from linkUtils
var emoteObj = await linkUtils.markLink(link);
console.log(linkObj);
//If we didn't get a valid image/video file
if(emoteObj.type != 'image' && emoteObj.type != 'video'){
//AAAAAAAAAAAAAAAAAA
return errorHandler(res, 'URL either does not lead to a valid image/video file, or leads to one that is larger than 4MB');
}
/*
//Add the toke
await tokeCommandModel.create({command});
//Add the name to the emoteObj
emoteObj.name = name;
//Create the emote document
await emoteModel.create(emoteObj);
//Return the updated command list
res.status(200);
return res.send(await tokeCommandModel.getCommandStrings());
*/
return res.send(await emoteModel.getEmotes());
}else{
//otherwise scream
res.status(400);
@ -80,20 +87,22 @@ module.exports.delete = async function(req, res){
//if they're empty
if(validResult.isEmpty()){
/*
const {command} = matchedData(req);
const tokeDB = await tokeCommandModel.findOne({command});
//Pull name from sanatized/validated input
const {name} = matchedData(req);
//query for existing emote
const emoteDB = await emoteModel.findOne({name});
if(tokeDB == null){
return errorHandler(res, `Cannot delete non-existant toke command '!${command}'!`);
//if we don't have one
if(emoteDB == null){
//Throw a shit fit
return errorHandler(res, `Cannot delete non-existant emote: [${name}]!`);
}
await tokeDB.deleteOne();
await emoteDB.deleteOne();
//Return the updated command list
res.status(200);
return res.send(await tokeCommandModel.getCommandStrings());
*/
return res.send(await emoteModel.getEmotes());
}else{
//otherwise scream
res.status(400);

View file

@ -58,5 +58,6 @@ router.delete('/tokeCommands', permissionSchema.reqPermCheck("editTokeCommands")
//emote
router.get('/emote', permissionSchema.reqPermCheck('adminPanel'), emoteController.get);
router.post('/emote', permissionSchema.reqPermCheck('editEmotes'), emoteValidator.name(), emoteValidator.link(), emoteController.post);
router.delete('/emote', permissionSchema.reqPermCheck('editEmotes'), emoteValidator.name(), emoteController.delete);
module.exports = router;

View file

@ -56,13 +56,13 @@ emoteSchema.statics.loadDefaults = async function(){
//if the emote doesn't exist
if(!foundEmote){
const emoteDB = await _this.create(emote);
console.log(`Loading default emote '${emote.name}' into DB from defaultEmote.json`);
console.log(`Loading default emote [${emote.name}] into DB from defaultEmote.json`);
}
}catch(err){
if(emote != null){
console.log(err);
console.log(`Error loading emote '${emote.name}':`);
console.log(`Error loading emote [${emote.name}]:`);
}else{
console.log("Error, null emote:");
}

View file

@ -20,11 +20,14 @@ const validator = require('validator');//No express here, so regular validator i
module.exports.markLink = async function(link){
//Set max file size to 4MB
const maxSize = 4000000;
//Set badLink type
var type = 'deadLink';
//Assume links are guilty until proven innocent
var type = "malformedLink"
//Make sure we have an actual, factual URL
if(validator.isURL(link)){
//The URL is valid, so this is at least a dead link
type = 'deadLink';
//Don't try this at home, we're what you call "Experts"
//TODO: Handle this shit simultaneously and send the chat before its done, then send updated types for each link as they're pulled individually
try{

View file

@ -244,6 +244,23 @@ class canopyAdminUtils{
utils.ux.displayResponseError(await response.json());
}
}
async deleteEmote(name){
var response = await fetch(`/api/admin/emote`,{
method: "DELETE",
headers: {
"Content-Type": "application/json"
},
//Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
body: JSON.stringify({name})
});
if(response.status == 200){
return await response.json();
}else{
utils.ux.displayResponseError(await response.json());
}
}
}
class adminUserList{

View file

@ -54,10 +54,6 @@ class chatPostprocessor{
this.bodyArray.forEach((wordObj) => {
if(wordObj.type == 'word'){
//Inject current wordObj string into the chat body
//this doesnt work right with escaped strings atm
//we should make an array that contains all the strings split by nodes
//so text can be added in word chunks, allowing escaped strings and
//node injections w/o adding them as html instead of an appended node
injectString(wordObj.string);
}else if(wordObj.type == 'link'){
//Create a link node from our link
@ -79,6 +75,16 @@ class chatPostprocessor{
//Append node to chatBody
injectNode(wordObj, badLink);
}else if(wordObj.type == 'malformedLink'){
//Create a text span node from our link
const malformedLink = document.createElement('span');
malformedLink.classList.add('chat-malformed-link');
//Use textContent to be safe since links can't be escaped (this is why we don't just add it using injectString)
//arguably we could sanatize malformed links serverside since they're never actually used as links
malformedLink.textContent = wordObj.link;
//Append node to chatBody
injectNode(wordObj, malformedLink);
}else if(wordObj.type == 'image'){
//Create an img node from our link
const img = document.createElement('img');