updated original code, using createdAt from log

This commit is contained in:
Keith Campbell
2022-01-01 21:45:09 -05:00
parent f72b0e5522
commit ab73d05471
+19 -22
View File
@@ -2,9 +2,6 @@ const jwt = require('jsonwebtoken');
const { Op } = require('sequelize'); const { Op } = require('sequelize');
const { chatlog, mute, reports } = require('../database/models'); const { chatlog, mute, reports } = require('../database/models');
const timeElapsed = Date.now();
const today = new Date(timeElapsed);
const chat = io => { const chat = io => {
io.on('connection', socket => { io.on('connection', socket => {
//middleware //middleware
@@ -50,7 +47,7 @@ const chat = io => {
socket.join(socket.user.room); socket.join(socket.user.room);
//broadcast to this room //broadcast to this room
socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${today} ${socket.user.username} entered chat` }); socket.broadcast.to(socket.user.room).emit('message', {timestamp: log.createdAt, emphasis: true, text: `${socket.user.username} entered chat` });
//log //log
chatlog.create({ chatlog.create({
@@ -85,7 +82,7 @@ const chat = io => {
.then(() => { .then(() => {
//send a # to the user //send a # to the user
const count = io.sockets.size; const count = io.sockets.size;
socket.emit('message', { emphasis: true, text: count == 1 ? `${today} ${count} person in the chat` : `${today} ${count} people in the chat` }); socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: count == 1 ? `${count} person in the chat` : `${count} people in the chat` });
}) })
; ;
}); });
@@ -108,14 +105,14 @@ const chat = io => {
}); });
if (record) { if (record) {
socket.emit('message', { emphasis: true, text: `${today} You are currently muted` }); socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: 'You are currently muted' });
return; return;
} }
//log //log
const log = await chatlog.create({ const log = await chatlog.create({
username: socket.user.username, username: socket.user.username,
text: `${today} ${message.text}`, text: message.text,
room: socket.user.room room: socket.user.room
}); });
@@ -135,7 +132,7 @@ const chat = io => {
chatlog.create({ chatlog.create({
notification: true, notification: true,
username: socket.user.username, username: socket.user.username,
text: `${today} ${socket.user.username} left chat`, text: `${socket.user.username} left chat`,
room: socket.user.room, room: socket.user.room,
emphasis: true emphasis: true
}); });
@@ -168,13 +165,13 @@ const executeCommand = (io, socket, command) => {
} }
//broadcast to the old room //broadcast to the old room
socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${today} ${socket.user.username} left the room (going to ${room})` }); socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${socket.user.username} left the room (going to ${room})` });
//log //log
chatlog.create({ chatlog.create({
notification: true, notification: true,
username: socket.user.username, username: socket.user.username,
text: `${today} ${socket.user.username} left the room`, text: `${socket.user.username} left the room`,
room: socket.user.room, room: socket.user.room,
emphasis: true emphasis: true
}); });
@@ -185,25 +182,25 @@ const executeCommand = (io, socket, command) => {
socket.join(socket.user.room); socket.join(socket.user.room);
//broadcast to the new room //broadcast to the new room
socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${today} ${socket.user.username} entered the room` }); socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${socket.user.username} entered the room` });
//log //log
chatlog.create({ chatlog.create({
notification: true, notification: true,
username: socket.user.username, username: socket.user.username,
text: `${today} ${socket.user.username} entered the room`, text: `${socket.user.username} entered the room`,
room: socket.user.room, room: socket.user.room,
emphasis: true emphasis: true
}); });
//update the user //update the user
socket.emit('message', { emphasis: true, text: `${today} Entered room ${socket.user.room}` }); socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: `Entered room ${socket.user.room}` });
break; break;
} }
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.admin && !socket.user.mod) { if (!socket.user.admin && !socket.user.mod) {
socket.emit('message', { emphasis: true, text: `${today} /mute is only available to admins and mods` }); socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: '/mute is only available to admins and mods' });
break; break;
} }
@@ -215,7 +212,7 @@ const executeCommand = (io, socket, command) => {
//check valid command //check valid command
if (!username || !minutes || typeof minutes !== 'number' || minutes < 1) { if (!username || !minutes || typeof minutes !== 'number' || minutes < 1) {
socket.emit('message', { emphasis: true, text: `${today} format: /mute username minutes [reason]` }); socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: `format: /mute username minutes [reason]` });
break; break;
} }
@@ -229,13 +226,13 @@ const executeCommand = (io, socket, command) => {
}); });
//broadcast //broadcast
io.to(socket.user.room).emit('message', { strong: true, emphasis: true, text: `${today} ${username} has been muted for ${minutes} minute${minutes != 1 ? 's' : ''}${reason ? ': ' : ''}${reason}` }); 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 //log
chatlog.create({ chatlog.create({
notification: true, notification: true,
username: socket.user.username, username: socket.user.username,
text: `${today} ${username} has been muted for ${minutes} minute${minutes != 1 ? 's' : ''}: ${reason}`, text: `${username} has been muted for ${minutes} minute${minutes != 1 ? 's' : ''}: ${reason}`,
room: socket.user.room, room: socket.user.room,
strong: true, strong: true,
emphasis: true emphasis: true
@@ -246,7 +243,7 @@ const executeCommand = (io, socket, command) => {
case '/unmute': { case '/unmute': {
if (!socket.user.admin && !socket.user.mod) { if (!socket.user.admin && !socket.user.mod) {
socket.emit('message', { emphasis: true, text: `${today} /unmute is only available to admins and mods` }); socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: '/unmute is only available to admins and mods' });
break; break;
} }
@@ -266,18 +263,18 @@ const executeCommand = (io, socket, command) => {
}); });
if (rowCount == 0) { if (rowCount == 0) {
socket.emit('message', { emphasis: true, text: `${today} That user was not muted` }); socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: 'That user was not muted' });
break; break;
} }
//broadcast //broadcast
io.to(socket.user.room).emit('message', { emphasis: true, text: `${today} ${username} has been unmuted` }); io.to(socket.user.room).emit('message', { emphasis: true, text: `${username} has been unmuted` });
//log //log
chatlog.create({ chatlog.create({
notification: true, notification: true,
username: socket.user.username, username: socket.user.username,
text: `${today} ${username} has been unmuted`, text: `${username} has been unmuted`,
room: socket.user.room, room: socket.user.room,
emphasis: true emphasis: true
}); });
@@ -286,7 +283,7 @@ const executeCommand = (io, socket, command) => {
} }
default: { default: {
socket.emit('message', { emphasis: true, text: `${today} Unknown command` }); socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: 'Unknown command' });
} }
} }
}; };