Added muting by mods or admins

This commit is contained in:
2021-03-18 04:26:40 +11:00
parent 66c1c99d5d
commit e60535c525
4 changed files with 142 additions and 16 deletions
+113 -14
View File
@@ -1,6 +1,6 @@
const jwt = require('jsonwebtoken'); const jwt = require('jsonwebtoken');
const { chatlog } = require('../database/models');
const { Op } = require('sequelize'); const { Op } = require('sequelize');
const { chatlog, mute } = require('../database/models');
const chat = io => { const chat = io => {
io.on('connection', socket => { io.on('connection', socket => {
@@ -51,6 +51,7 @@ const chat = io => {
//log //log
chatlog.create({ chatlog.create({
notification: true,
username: socket.user.username, username: socket.user.username,
text: `${socket.user.username} entered chat`, text: `${socket.user.username} entered chat`,
room: socket.user.room, room: socket.user.room,
@@ -61,19 +62,22 @@ const chat = io => {
chatlog.findAll({ chatlog.findAll({
where: { where: {
room: { room: {
[Op.eq]: socket.user.room [Op.eq]: socket.user.room,
},
strong: {
[Op.ne]: true
},
emphasis: {
[Op.ne]: true
} }
}, },
order: [ order: [
['id', 'ASC'] ['id', 'DESC']
], ],
limit: 50 limit: 50
}) })
.then(rows => rows.map(row => row.dataValues)) .then(rows => rows.map(row => row.dataValues))
.then(rows => rows.filter(row => { .then(rows => rows.reverse())
//emphasis and strong don't use usernames
return !(row.emphasis || row.strong);
}))
.then(rows => socket.emit('backlog', rows)) .then(rows => socket.emit('backlog', rows))
.then(() => { .then(() => {
//send a # to the user //send a # to the user
@@ -83,10 +87,26 @@ const chat = io => {
; ;
}); });
socket.on('message', message => { socket.on('message', async message => {
//server commands begin with a '/' //server commands begin with a '/'
if (message.text.startsWith('/')) { if (message.text.startsWith('/')) {
return executeCommand(socket, message.text); return executeCommand(io, socket, message.text);
}
const record = await mute.findOne({
where: {
username: {
[Op.eq]: socket.user.username
},
until: {
[Op.gt]: new Date(Date.now())
}
}
});
if (record) {
socket.emit('message', { emphasis: true, text: 'You are currently muted' });
return;
} }
//broadcast to this room //broadcast to this room
@@ -110,6 +130,7 @@ const chat = io => {
//log //log
chatlog.create({ chatlog.create({
notification: true,
username: socket.user.username, username: socket.user.username,
text: `${socket.user.username} left chat`, text: `${socket.user.username} left chat`,
room: socket.user.room, room: socket.user.room,
@@ -120,9 +141,9 @@ const chat = io => {
}; };
//handle commands //handle commands
const executeCommand = (socket, command) => { const executeCommand = (io, socket, command) => {
switch(command.split(' ')[0]) { switch(command.split(' ')[0]) {
case '/room': case '/room': {
const room = command.split(' ')[1]; const room = command.split(' ')[1];
if (!room) { if (!room) {
@@ -135,6 +156,7 @@ const executeCommand = (socket, command) => {
//log //log
chatlog.create({ chatlog.create({
notification: true,
username: socket.user.username, username: socket.user.username,
text: `${socket.user.username} left the room`, text: `${socket.user.username} left the room`,
room: socket.user.room, room: socket.user.room,
@@ -151,6 +173,7 @@ const executeCommand = (socket, command) => {
//log //log
chatlog.create({ chatlog.create({
notification: true,
username: socket.user.username, username: socket.user.username,
text: `${socket.user.username} entered the room`, text: `${socket.user.username} entered the room`,
room: socket.user.room, room: socket.user.room,
@@ -160,12 +183,88 @@ const executeCommand = (socket, command) => {
//update the user //update the user
socket.emit('message', { emphasis: true, text: `Entered room ${socket.user.room}` }); socket.emit('message', { emphasis: true, text: `Entered room ${socket.user.room}` });
break; break;
}
default: case '/mute': {//NOTE: mutes globally, broadcasts only to admin's room
if (socket.user.privilege != 'administrator' && socket.user.privilege != 'moderator') {
socket.emit('message', { emphasis: true, text: '/mute is only available to admins and mods' });
break;
}
const arr = command.split(' ');
arr.shift(); // /mute
const username = arr.shift();
const minutes = parseInt(arr.shift());
const reason = arr.join(' ');
//check valid command
if (!username || !minutes || typeof minutes !== 'number' || minutes < 1) {
socket.emit('message', { emphasis: true, text: `format: /mute username minutes [reason]` });
break;
}
//upsert
const interval = new Date((new Date()).setMinutes((new Date()).getMinutes() + minutes)); //wow
mute.upsert({
username: username,
until: interval,
reason: reason
});
//broadcast
io.to(socket.user.room).emit('message', { strong: true, emphasis: true, text: `${username} has been muted for ${minutes} minute${minutes != 1 ? 's' : ''}${reason ? ': ' : ''}${reason}` });
//log
chatlog.create({
notification: true,
username: socket.user.username,
text: `${username} has been muted for ${minutes} minute${minutes != 1 ? 's' : ''}: ${reason}`,
room: socket.user.room,
strong: true,
emphasis: true
});
break;
}
case '/unmute': {
const arr = command.split(' ');
arr.shift(); // /mute
const username = arr.shift();
const rowCount = mute.destroy({
where: {
username: {
[Op.eq]: username
}
}
});
if (rowCount == 0) {
socket.emit('message', { emphasis: true, text: 'That user was not muted' });
break;
}
//broadcast
io.to(socket.user.room).emit('message', { emphasis: true, text: `${username} has been unmuted` });
//log
chatlog.create({
notification: true,
username: socket.user.username,
text: `${username} has been unmuted`,
room: socket.user.room,
emphasis: true
});
break;
}
default: {
socket.emit('message', { emphasis: true, text: 'Unknown command' }); socket.emit('message', { emphasis: true, text: 'Unknown command' });
}
} }
}; };
module.exports = chat; module.exports = chat;
//TODO: add banning and muting controls
+7 -1
View File
@@ -10,6 +10,12 @@ module.exports = sequelize.define('chatlog', {
unique: true unique: true
}, },
notification: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
},
username: { username: {
type: 'varchar(320)', type: 'varchar(320)',
allowNull: false allowNull: false
@@ -35,5 +41,5 @@ module.exports = sequelize.define('chatlog', {
type: Sequelize.BOOLEAN, type: Sequelize.BOOLEAN,
allowNull: false, allowNull: false,
defaultValue: false defaultValue: false
}, }
}); });
+2 -1
View File
@@ -1,3 +1,4 @@
module.exports = { module.exports = {
chatlog: require('./chatlog') chatlog: require('./chatlog'),
mute: require('./mute')
}; };
+20
View File
@@ -0,0 +1,20 @@
const Sequelize = require('sequelize');
const sequelize = require('..');
module.exports = sequelize.define('mute', {
username: {
type: 'varchar(320)',
allowNull: false,
unique: true
},
until: {
type: 'DATETIME',
allowNull: false
},
reason: {
type: Sequelize.TEXT,
allowNull: true,
}
});