Several improvements to chat pre/post processing

This commit is contained in:
rainbow napkin 2024-12-17 20:44:14 -05:00
parent 12922658b9
commit b9283607d6
10 changed files with 217 additions and 71 deletions

View file

@ -19,6 +19,7 @@ const validator = require('validator');//No express here, so regular validator i
//Local Imports
const tokebot = require('./tokebot');
const linkUtils = require('../../utils/linkUtils');
const permissionModel = require('../../schemas/permissionSchema');
const channelModel = require('../../schemas/channel/channelSchema');
@ -39,6 +40,7 @@ module.exports = class commandPreprocessor{
//If we don't pass sanatization/validation turn this car around
if(!this.sanatizeCommand()){
console.log('test');
return;
}
@ -87,56 +89,14 @@ module.exports = class commandPreprocessor{
}
async markLinks(){
const maxSize = 4000000;
//Empty out the links array
this.links = [];
//For each link sent from the client
//this.rawData.links.forEach((link) => {
for (const link of this.rawData.links){
//Set standard link type
var type = 'link';
//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{
//Pull content type
var response = await fetch(link,{
method: "HEAD",
});
//Get file type from header
const fileType = response.headers.get('content-type');
const fileSize = response.headers.get('content-length');
//If they're reporting file types
if(fileType != null){
//If we have an image
if(fileType.match('image/')){
//If the file size is unreported OR it's smaller than 4MB (not all servers report this and images that big are pretty rare)
if(fileSize == null || fileSize <= maxSize){
//Mark link as an image
type = 'image';
}
//If it's a video
}else if(fileType.match('video/mp4' || 'video/webm')){
//If the server is reporting file-size and it's reporting under 4MB (Reject unreported sizes to be on the safe side is video is huge)
if(fileSize != null && fileSize <= maxSize){
//mark link as a video
type = 'video';
}
}
}
//Probably bad form but if something happens in here I'm blaming whoever hosted the link
//maybe don't host a fucked up server and I wouldn't handle with an empty catch
}catch{};
//Add a marked link object to our links array
this.links.push({
link,
type
});
this.links.push(await linkUtils.markLink(link));
}
}

View file

@ -20,6 +20,7 @@ const {validationResult, matchedData} = require('express-validator');
//local imports
const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils');
const emoteModel = require('../../../schemas/emoteSchema');
const linkUtils = require('../../../utils/linkUtils');
module.exports.get = async function(req, res){
try{
@ -39,14 +40,22 @@ module.exports.post = async function(req, res){
//if they're empty
if(validResult.isEmpty()){
/*
const {command} = matchedData(req);
const tokeDB = await tokeCommandModel.findOne({command});
//get matched data
const {name, link} = matchedData(req);
//query for existing emote
const emoteDB = await emoteModel.findOne({name});
if(tokeDB != null){
return errorHandler(res, `Toke command '!${command}' already exists!`);
//if we have one
if(emoteDB != null){
//Throw a shit fit
return errorHandler(res, `Emote '[${name}]' already exists!`);
}
const linkObj = linkUtils.markLink(link);
console.log(linkObj);
/*
//Add the toke
await tokeCommandModel.create({command});

View file

@ -23,6 +23,7 @@ const { Router } = require('express');
const accountValidator = require("../../validators/accountValidator");
const {permissionsValidator, channelPermissionValidator} = require("../../validators/permissionsValidator");
const tokebotValidator = require("../../validators/tokebotValidator");
const emoteValidator = require("../../validators/emoteValidator");
const permissionSchema = require("../../schemas/permissionSchema");
const listUsersController = require("../../controllers/api/admin/listUsersController");
const listChannelsController = require("../../controllers/api/admin/listChannelsController");
@ -56,5 +57,6 @@ router.post('/tokeCommands', permissionSchema.reqPermCheck("editTokeCommands"),
router.delete('/tokeCommands', permissionSchema.reqPermCheck("editTokeCommands"), tokebotValidator.command(), tokeCommandController.delete);
//emote
router.get('/emote', permissionSchema.reqPermCheck('adminPanel'), emoteController.get);
router.post('/emote', permissionSchema.reqPermCheck('editEmotes'), emoteValidator.name(), emoteValidator.link(), emoteController.post);
module.exports = router;

View file

@ -81,6 +81,12 @@ const permissionSchema = new mongoose.Schema({
default: "admin",
required: true
},
editEmotes: {
type: mongoose.SchemaTypes.String,
enum: rankEnum,
default: "admin",
required: true
},
channelOverrides: {
type: channelPermissionSchema,
default: () => ({})

70
src/utils/linkUtils.js Normal file
View file

@ -0,0 +1,70 @@
/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024 Rainbownapkin and the TTN Community
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//NPM Imports
const validator = require('validator');//No express here, so regular validator it is!
module.exports.markLink = async function(link){
//Set max file size to 4MB
const maxSize = 4000000;
//Set badLink type
var type = 'deadLink';
//Make sure we have an actual, factual URL
if(validator.isURL(link)){
//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{
//Pull content type
var response = await fetch(link,{
method: "HEAD",
});
//If we made it this far then the link is, at the very least, not dead.
type = 'link'
//Get file type from header
const fileType = response.headers.get('content-type');
const fileSize = response.headers.get('content-length');
//If they're reporting file types
if(fileType != null){
//If we have an image
if(fileType.match('image/')){
//If the file size is unreported OR it's smaller than 4MB (not all servers report this and images that big are pretty rare)
if(fileSize == null || fileSize <= maxSize){
//Mark link as an image
type = 'image';
}
//If it's a video
}else if(fileType.match('video/mp4' || 'video/webm')){
//If the server is reporting file-size and it's reporting under 4MB (Reject unreported sizes to be on the safe side is video is huge)
if(fileSize != null && fileSize <= maxSize){
//mark link as a video
type = 'video';
}
}
}
//Probably bad form but if something happens in here I'm blaming whoever hosted the link
//maybe don't host a fucked up server and I wouldn't handle with an empty catch
}catch{};
}
return {
link,
type
}
}

View file

@ -0,0 +1,23 @@
/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024 Rainbownapkin and the TTN Community
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//NPM Imports
const { check } = require('express-validator');
module.exports = {
name: (field = 'name') => check(field).escape().trim().isAlphanumeric().isLength({min: 1, max: 30}),
link: (field = 'link') => check(field).trim().isURL()
}