Compare commits

...

3 Commits

Author SHA1 Message Date
Kayne Ruse ab0bad4f73 Chat report table working 2021-03-28 07:57:56 +11:00
Kayne Ruse f83ef938ab Updated admin and mod flag system 2021-03-24 08:23:02 +11:00
Kayne Ruse 0b5cc49e6e Added reporting feature 2021-03-24 03:20:29 +11:00
9 changed files with 139 additions and 9 deletions
+1
View File
@@ -19,6 +19,7 @@ on 'error' -> Server emits and logs an error
on 'open chat' -> Preps the server for your messages, places you in the room 'general'
on 'message' -> Server broadcasts to all other users in your room
on 'disconnect' -> Server will no longer accept your messages
on 'report' -> Report the chatlog with the index 'id'
Chat Commands:
+21
View File
@@ -0,0 +1,21 @@
const express = require('express');
const router = express.Router();
//middleware
const tokenAuth = require('../utilities/token-auth');
router.use(tokenAuth);
router.use((req, res, next) => {
//check the user's admin status
if (!req.user.mod) {
return res.status(401).send('Mods only');
}
next();
});
//basic route management
router.get('/reports', require('./reports'));
router.delete('/reports', require('./reports-delete'));
module.exports = router;
+15
View File
@@ -0,0 +1,15 @@
const { chatlog, reports } = require('../database/models');
//admin/reports
const route = async (req, res) => {
const reps = await reports.destroy({
where: {
chatlogId: req.body.chatlogId
}
});
//respond
res.status(200).end();
};
module.exports = route;
+31
View File
@@ -0,0 +1,31 @@
const { chatlog, reports } = require('../database/models');
//admin/reports
const route = async (req, res) => {
const reps = await reports.findAll({
include: [{
model: chatlog,
required: true
}],
order: ['chatlogId']
});
//collate
const response = [];
for(let i = 0; i < reps.length; i++) {
//new chatlog
if (response.length == 0 || response[response.length - 1].chatlogId != reps[i].chatlogId) {
response.push(reps[i]);
response[response.length - 1].reporter = [response[response.length - 1].reporter]; //reporters in an array
continue;
}
//multiple people reported this, add to the existing array
response[response.length - 1].reporter.push(reps[i].reporter);
}
//respond
res.status(200).json(response);
};
module.exports = route;
+21 -8
View File
@@ -1,13 +1,13 @@
const jwt = require('jsonwebtoken');
const { Op } = require('sequelize');
const { chatlog, mute } = require('../database/models');
const { chatlog, mute, reports } = require('../database/models');
const chat = io => {
io.on('connection', socket => {
//middleware
socket.use((request, next) => {
//verify request format
if (!['open chat', 'message'].includes(request[0])) {
if (!['open chat', 'message', 'report'].includes(request[0])) {
return next(`Invalid request to the chat server ${request[0]}`);
}
return next();
@@ -109,15 +109,15 @@ const chat = io => {
return;
}
//broadcast to this room
socket.broadcast.to(socket.user.room).emit('message', { username: socket.user.username, text: message.text });
//log
chatlog.create({
const log = await chatlog.create({
username: socket.user.username,
text: message.text,
room: socket.user.room
});
//broadcast to this room (with the id)
socket.broadcast.to(socket.user.room).emit('message', log);
});
socket.on('disconnect', reason => {
@@ -137,6 +137,19 @@ const chat = io => {
emphasis: true
});
});
socket.on('report', info => {
//handle reports of malicious content
if (!info.id) {
return;
}
//report
reports.create({
reporter: socket.user.username,
chatlogId: info.id
});
});
});
};
@@ -186,7 +199,7 @@ const executeCommand = (io, socket, command) => {
}
case '/mute': {//NOTE: mutes globally, broadcasts only to admin's room
if (socket.user.privilege != 'administrator' && socket.user.privilege != 'moderator') {
if (!socket.user.admin && !socket.user.mod) {
socket.emit('message', { emphasis: true, text: '/mute is only available to admins and mods' });
break;
}
@@ -229,7 +242,7 @@ const executeCommand = (io, socket, command) => {
}
case '/unmute': {
if (socket.user.privilege != 'administrator' && socket.user.privilege != 'moderator') {
if (!socket.user.admin && !socket.user.mod) {
socket.emit('message', { emphasis: true, text: '/unmute is only available to admins and mods' });
break;
}
+2 -1
View File
@@ -1,4 +1,5 @@
module.exports = {
chatlog: require('./chatlog'),
mute: require('./mute')
mute: require('./mute'),
reports: require('./reports')
};
+24
View File
@@ -0,0 +1,24 @@
const Sequelize = require('sequelize');
const sequelize = require('..');
const chatlog = require('./chatlog');
const reports = sequelize.define('reports', {
id: {
type: Sequelize.INTEGER(11),
allowNull: false,
autoIncrement: true,
primaryKey: true,
unique: true
},
reporter: {
type: 'varchar(320)',
allowNull: false
},
});
chatlog.hasMany(reports, { foreignKey: 'chatlogId', foreignKeyConstraint: true });
reports.belongsTo(chatlog, { foreignKey: 'chatlogId' });
module.exports = reports;
+3
View File
@@ -20,6 +20,9 @@ app.use(cors());
//database connection
const database = require('./database');
//admin stuff
app.use('/admin', require('./admin'));
//access the chat
require('./chat')(io.of('/chat'));
+21
View File
@@ -0,0 +1,21 @@
const jwt = require('jsonwebtoken');
//middleware to authenticate the JWT token
module.exports = (req, res, next) => {
const authHeader = req.headers['authorization'];
const token = authHeader?.split (' ')[1]; //'Bearer token'
if (!token) {
return res.status(401).send('No token found');
}
return jwt.verify(token, process.env.SECRET_ACCESS, (err, user) => {
if (err) {
return res.status(403).send(err);
}
req.user = user;
return next();
});
};