Compare commits

...

1 Commits

Author SHA1 Message Date
Kayne Ruse ab0bad4f73 Chat report table working 2021-03-28 07:57:56 +11:00
6 changed files with 92 additions and 0 deletions
+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;
+1
View File
@@ -19,5 +19,6 @@ const reports = sequelize.define('reports', {
}); });
chatlog.hasMany(reports, { foreignKey: 'chatlogId', foreignKeyConstraint: true }); chatlog.hasMany(reports, { foreignKey: 'chatlogId', foreignKeyConstraint: true });
reports.belongsTo(chatlog, { foreignKey: 'chatlogId' });
module.exports = reports; module.exports = reports;
+3
View File
@@ -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'));
+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();
});
};