Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ab0bad4f73 | |||
| f83ef938ab | |||
| 0b5cc49e6e |
@@ -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 '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 'message' -> Server broadcasts to all other users in your room
|
||||||
on 'disconnect' -> Server will no longer accept your messages
|
on 'disconnect' -> Server will no longer accept your messages
|
||||||
|
on 'report' -> Report the chatlog with the index 'id'
|
||||||
|
|
||||||
|
|
||||||
Chat Commands:
|
Chat Commands:
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -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;
|
||||||
@@ -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
@@ -1,13 +1,13 @@
|
|||||||
const jwt = require('jsonwebtoken');
|
const jwt = require('jsonwebtoken');
|
||||||
const { Op } = require('sequelize');
|
const { Op } = require('sequelize');
|
||||||
const { chatlog, mute } = require('../database/models');
|
const { chatlog, mute, reports } = require('../database/models');
|
||||||
|
|
||||||
const chat = io => {
|
const chat = io => {
|
||||||
io.on('connection', socket => {
|
io.on('connection', socket => {
|
||||||
//middleware
|
//middleware
|
||||||
socket.use((request, next) => {
|
socket.use((request, next) => {
|
||||||
//verify request format
|
//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(`Invalid request to the chat server ${request[0]}`);
|
||||||
}
|
}
|
||||||
return next();
|
return next();
|
||||||
@@ -109,15 +109,15 @@ const chat = io => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//broadcast to this room
|
|
||||||
socket.broadcast.to(socket.user.room).emit('message', { username: socket.user.username, text: message.text });
|
|
||||||
|
|
||||||
//log
|
//log
|
||||||
chatlog.create({
|
const log = await chatlog.create({
|
||||||
username: socket.user.username,
|
username: socket.user.username,
|
||||||
text: message.text,
|
text: message.text,
|
||||||
room: socket.user.room
|
room: socket.user.room
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//broadcast to this room (with the id)
|
||||||
|
socket.broadcast.to(socket.user.room).emit('message', log);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('disconnect', reason => {
|
socket.on('disconnect', reason => {
|
||||||
@@ -137,6 +137,19 @@ const chat = io => {
|
|||||||
emphasis: true
|
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
|
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' });
|
socket.emit('message', { emphasis: true, text: '/mute is only available to admins and mods' });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -229,7 +242,7 @@ const executeCommand = (io, socket, command) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case '/unmute': {
|
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' });
|
socket.emit('message', { emphasis: true, text: '/unmute is only available to admins and mods' });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
chatlog: require('./chatlog'),
|
chatlog: require('./chatlog'),
|
||||||
mute: require('./mute')
|
mute: require('./mute'),
|
||||||
|
reports: require('./reports')
|
||||||
};
|
};
|
||||||
@@ -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;
|
||||||
@@ -20,6 +20,9 @@ app.use(cors());
|
|||||||
//database connection
|
//database connection
|
||||||
const database = require('./database');
|
const database = require('./database');
|
||||||
|
|
||||||
|
//admin stuff
|
||||||
|
app.use('/admin', require('./admin'));
|
||||||
|
|
||||||
//access the chat
|
//access the chat
|
||||||
require('./chat')(io.of('/chat'));
|
require('./chat')(io.of('/chat'));
|
||||||
|
|
||||||
|
|||||||
@@ -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();
|
||||||
|
});
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user