From f72b0e5522ea7dec499baa73421158d4aaecaf64 Mon Sep 17 00:00:00 2001 From: Keith Campbell Date: Sat, 1 Jan 2022 17:11:11 -0500 Subject: [PATCH 1/5] added timestamp to messages and logs, need to test --- server/chat/index.js | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/server/chat/index.js b/server/chat/index.js index 09a5edd..77bfb8e 100644 --- a/server/chat/index.js +++ b/server/chat/index.js @@ -2,6 +2,9 @@ const jwt = require('jsonwebtoken'); const { Op } = require('sequelize'); const { chatlog, mute, reports } = require('../database/models'); +const timeElapsed = Date.now(); +const today = new Date(timeElapsed); + const chat = io => { io.on('connection', socket => { //middleware @@ -47,7 +50,7 @@ const chat = io => { socket.join(socket.user.room); //broadcast to this room - socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${socket.user.username} entered chat` }); + socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${today} ${socket.user.username} entered chat` }); //log chatlog.create({ @@ -82,7 +85,7 @@ const chat = io => { .then(() => { //send a # to the user const count = io.sockets.size; - socket.emit('message', { emphasis: true, text: count == 1 ? `${count} person in the chat` : `${count} people in the chat` }); + socket.emit('message', { emphasis: true, text: count == 1 ? `${today} ${count} person in the chat` : `${today} ${count} people in the chat` }); }) ; }); @@ -105,14 +108,14 @@ const chat = io => { }); if (record) { - socket.emit('message', { emphasis: true, text: 'You are currently muted' }); + socket.emit('message', { emphasis: true, text: `${today} You are currently muted` }); return; } //log const log = await chatlog.create({ username: socket.user.username, - text: message.text, + text: `${today} ${message.text}`, room: socket.user.room }); @@ -132,7 +135,7 @@ const chat = io => { chatlog.create({ notification: true, username: socket.user.username, - text: `${socket.user.username} left chat`, + text: `${today} ${socket.user.username} left chat`, room: socket.user.room, emphasis: true }); @@ -165,13 +168,13 @@ const executeCommand = (io, socket, command) => { } //broadcast to the old room - socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${socket.user.username} left the room (going to ${room})` }); + socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${today} ${socket.user.username} left the room (going to ${room})` }); //log chatlog.create({ notification: true, username: socket.user.username, - text: `${socket.user.username} left the room`, + text: `${today} ${socket.user.username} left the room`, room: socket.user.room, emphasis: true }); @@ -182,25 +185,25 @@ const executeCommand = (io, socket, command) => { socket.join(socket.user.room); //broadcast to the new room - socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${socket.user.username} entered the room` }); + socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${today} ${socket.user.username} entered the room` }); //log chatlog.create({ notification: true, username: socket.user.username, - text: `${socket.user.username} entered the room`, + text: `${today} ${socket.user.username} entered the room`, room: socket.user.room, emphasis: true }); //update the user - socket.emit('message', { emphasis: true, text: `Entered room ${socket.user.room}` }); + socket.emit('message', { emphasis: true, text: `${today} Entered room ${socket.user.room}` }); break; } case '/mute': {//NOTE: mutes globally, broadcasts only to admin's room 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: `${today} /mute is only available to admins and mods` }); break; } @@ -212,7 +215,7 @@ const executeCommand = (io, socket, command) => { //check valid command if (!username || !minutes || typeof minutes !== 'number' || minutes < 1) { - socket.emit('message', { emphasis: true, text: `format: /mute username minutes [reason]` }); + socket.emit('message', { emphasis: true, text: `${today} format: /mute username minutes [reason]` }); break; } @@ -226,13 +229,13 @@ const executeCommand = (io, socket, command) => { }); //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}` }); + 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}` }); //log chatlog.create({ notification: true, username: socket.user.username, - text: `${username} has been muted for ${minutes} minute${minutes != 1 ? 's' : ''}: ${reason}`, + text: `${today} ${username} has been muted for ${minutes} minute${minutes != 1 ? 's' : ''}: ${reason}`, room: socket.user.room, strong: true, emphasis: true @@ -243,7 +246,7 @@ const executeCommand = (io, socket, command) => { case '/unmute': { 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: `${today} /unmute is only available to admins and mods` }); break; } @@ -263,18 +266,18 @@ const executeCommand = (io, socket, command) => { }); if (rowCount == 0) { - socket.emit('message', { emphasis: true, text: 'That user was not muted' }); + socket.emit('message', { emphasis: true, text: `${today} That user was not muted` }); break; } //broadcast - io.to(socket.user.room).emit('message', { emphasis: true, text: `${username} has been unmuted` }); + io.to(socket.user.room).emit('message', { emphasis: true, text: `${today} ${username} has been unmuted` }); //log chatlog.create({ notification: true, username: socket.user.username, - text: `${username} has been unmuted`, + text: `${today} ${username} has been unmuted`, room: socket.user.room, emphasis: true }); @@ -283,7 +286,7 @@ const executeCommand = (io, socket, command) => { } default: { - socket.emit('message', { emphasis: true, text: 'Unknown command' }); + socket.emit('message', { emphasis: true, text: `${today} Unknown command` }); } } }; From ab73d054715edc549f1bce7e4987eaecc80e4461 Mon Sep 17 00:00:00 2001 From: Keith Campbell Date: Sat, 1 Jan 2022 21:45:09 -0500 Subject: [PATCH 2/5] updated original code, using createdAt from log --- server/chat/index.js | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/server/chat/index.js b/server/chat/index.js index 77bfb8e..94d840c 100644 --- a/server/chat/index.js +++ b/server/chat/index.js @@ -2,9 +2,6 @@ const jwt = require('jsonwebtoken'); const { Op } = require('sequelize'); const { chatlog, mute, reports } = require('../database/models'); -const timeElapsed = Date.now(); -const today = new Date(timeElapsed); - const chat = io => { io.on('connection', socket => { //middleware @@ -50,7 +47,7 @@ const chat = io => { socket.join(socket.user.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 chatlog.create({ @@ -85,7 +82,7 @@ const chat = io => { .then(() => { //send a # to the user 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) { - 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; } //log const log = await chatlog.create({ username: socket.user.username, - text: `${today} ${message.text}`, + text: message.text, room: socket.user.room }); @@ -135,7 +132,7 @@ const chat = io => { chatlog.create({ notification: true, username: socket.user.username, - text: `${today} ${socket.user.username} left chat`, + text: `${socket.user.username} left chat`, room: socket.user.room, emphasis: true }); @@ -168,13 +165,13 @@ const executeCommand = (io, socket, command) => { } //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 chatlog.create({ notification: true, username: socket.user.username, - text: `${today} ${socket.user.username} left the room`, + text: `${socket.user.username} left the room`, room: socket.user.room, emphasis: true }); @@ -185,25 +182,25 @@ const executeCommand = (io, socket, command) => { socket.join(socket.user.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 chatlog.create({ notification: true, username: socket.user.username, - text: `${today} ${socket.user.username} entered the room`, + text: `${socket.user.username} entered the room`, room: socket.user.room, emphasis: true }); //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; } case '/mute': {//NOTE: mutes globally, broadcasts only to admin's room 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; } @@ -215,7 +212,7 @@ const executeCommand = (io, socket, command) => { //check valid command 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; } @@ -229,13 +226,13 @@ const executeCommand = (io, socket, command) => { }); //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 chatlog.create({ notification: true, 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, strong: true, emphasis: true @@ -246,7 +243,7 @@ const executeCommand = (io, socket, command) => { case '/unmute': { 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; } @@ -266,18 +263,18 @@ const executeCommand = (io, socket, command) => { }); 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; } //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 chatlog.create({ notification: true, username: socket.user.username, - text: `${today} ${username} has been unmuted`, + text: `${username} has been unmuted`, room: socket.user.room, emphasis: true }); @@ -286,7 +283,7 @@ const executeCommand = (io, socket, command) => { } default: { - socket.emit('message', { emphasis: true, text: `${today} Unknown command` }); + socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: 'Unknown command' }); } } }; From 2aedb6e938e412326836c5a2bb699df818f092f4 Mon Sep 17 00:00:00 2001 From: Keith Campbell Date: Sun, 2 Jan 2022 18:00:16 -0500 Subject: [PATCH 3/5] ready for PR/review --- server/chat/index.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/server/chat/index.js b/server/chat/index.js index 94d840c..5b63a4d 100644 --- a/server/chat/index.js +++ b/server/chat/index.js @@ -46,11 +46,8 @@ const chat = io => { socket.join(socket.user.room); - //broadcast to this room - socket.broadcast.to(socket.user.room).emit('message', {timestamp: log.createdAt, emphasis: true, text: `${socket.user.username} entered chat` }); - //log - chatlog.create({ + const log = await chatlog.create({ notification: true, username: socket.user.username, text: `${socket.user.username} entered chat`, @@ -58,6 +55,9 @@ const chat = io => { emphasis: true }); + //broadcast to this room + socket.broadcast.to(socket.user.room).emit('message', {timestamp: log.createdAt, emphasis: true, text: `${socket.user.username} entered chat` }); + //send backlog to the user chatlog.findAll({ where: { @@ -129,7 +129,7 @@ const chat = io => { socket.broadcast.to(socket.user.room || '.error').emit('message', { emphasis: true, text: `${socket.user.username} left chat` }); //log - chatlog.create({ + const log = await chatlog.create({ notification: true, username: socket.user.username, text: `${socket.user.username} left chat`, @@ -164,11 +164,8 @@ const executeCommand = (io, socket, command) => { break; } - //broadcast to the old room - socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${socket.user.username} left the room (going to ${room})` }); - //log - chatlog.create({ + const log = await chatlog.create({ notification: true, username: socket.user.username, text: `${socket.user.username} left the room`, @@ -176,16 +173,16 @@ const executeCommand = (io, socket, command) => { emphasis: true }); + //broadcast to the old room + socket.broadcast.to(socket.user.room).emit('message', { timestamp: log.createdAt, emphasis: true, text: `${socket.user.username} left the room (going to ${room})` }); + //move socket.leave(socket.user.room); socket.user.room = room; socket.join(socket.user.room); - //broadcast to the new room - socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${socket.user.username} entered the room` }); - //log - chatlog.create({ + const log = await chatlog.create({ notification: true, username: socket.user.username, text: `${socket.user.username} entered the room`, @@ -193,6 +190,9 @@ const executeCommand = (io, socket, command) => { emphasis: true }); + //broadcast to the new room + socket.broadcast.to(socket.user.room).emit('message', {timestamp: log.createdAt, emphasis: true, text: `${socket.user.username} entered the room` }); + //update the user socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: `Entered room ${socket.user.room}` }); break; @@ -224,12 +224,9 @@ const executeCommand = (io, socket, command) => { 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({ + const log = await chatlog.create({ notification: true, username: socket.user.username, text: `${username} has been muted for ${minutes} minute${minutes != 1 ? 's' : ''}: ${reason}`, @@ -238,6 +235,9 @@ const executeCommand = (io, socket, command) => { emphasis: true }); + //broadcast + io.to(socket.user.room).emit('message', { timestamp: log.createdAt, strong: true, emphasis: true, text: `${username} has been muted for ${minutes} minute${minutes != 1 ? 's' : ''}${reason ? ': ' : ''}${reason}` }); + break; } @@ -267,11 +267,8 @@ const executeCommand = (io, socket, command) => { break; } - //broadcast - io.to(socket.user.room).emit('message', { emphasis: true, text: `${username} has been unmuted` }); - //log - chatlog.create({ + const log = await chatlog.create({ notification: true, username: socket.user.username, text: `${username} has been unmuted`, @@ -279,6 +276,9 @@ const executeCommand = (io, socket, command) => { emphasis: true }); + //broadcast + io.to(socket.user.room).emit('message', {timestamp: log.createdAt, emphasis: true, text: `${username} has been unmuted` }); + break; } From 05eecf8bddb6209ddc5fefd27a2f88278fd4a6fd Mon Sep 17 00:00:00 2001 From: Keith Campbell Date: Sun, 2 Jan 2022 18:35:50 -0500 Subject: [PATCH 4/5] removed everything but lines 50-60 --- server/chat/index.js | 52 ++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/server/chat/index.js b/server/chat/index.js index 5b63a4d..8ab71cc 100644 --- a/server/chat/index.js +++ b/server/chat/index.js @@ -82,7 +82,7 @@ const chat = io => { .then(() => { //send a # to the user const count = io.sockets.size; - socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: count == 1 ? `${count} person in the chat` : `${count} people in the chat` }); + socket.emit('message', { emphasis: true, text: count == 1 ? `${count} person in the chat` : `${count} people in the chat` }); }) ; }); @@ -105,7 +105,7 @@ const chat = io => { }); if (record) { - socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: 'You are currently muted' }); + socket.emit('message', { emphasis: true, text: 'You are currently muted' }); return; } @@ -129,7 +129,7 @@ const chat = io => { socket.broadcast.to(socket.user.room || '.error').emit('message', { emphasis: true, text: `${socket.user.username} left chat` }); //log - const log = await chatlog.create({ + chatlog.create({ notification: true, username: socket.user.username, text: `${socket.user.username} left chat`, @@ -164,8 +164,11 @@ const executeCommand = (io, socket, command) => { break; } + //broadcast to the old room + socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${socket.user.username} left the room (going to ${room})` }); + //log - const log = await chatlog.create({ + chatlog.create({ notification: true, username: socket.user.username, text: `${socket.user.username} left the room`, @@ -173,16 +176,16 @@ const executeCommand = (io, socket, command) => { emphasis: true }); - //broadcast to the old room - socket.broadcast.to(socket.user.room).emit('message', { timestamp: log.createdAt, emphasis: true, text: `${socket.user.username} left the room (going to ${room})` }); - //move socket.leave(socket.user.room); socket.user.room = room; socket.join(socket.user.room); + //broadcast to the new room + socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${socket.user.username} entered the room` }); + //log - const log = await chatlog.create({ + chatlog.create({ notification: true, username: socket.user.username, text: `${socket.user.username} entered the room`, @@ -190,17 +193,14 @@ const executeCommand = (io, socket, command) => { emphasis: true }); - //broadcast to the new room - socket.broadcast.to(socket.user.room).emit('message', {timestamp: log.createdAt, emphasis: true, text: `${socket.user.username} entered the room` }); - //update the user - socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: `Entered room ${socket.user.room}` }); + socket.emit('message', { emphasis: true, text: `Entered room ${socket.user.room}` }); break; } case '/mute': {//NOTE: mutes globally, broadcasts only to admin's room if (!socket.user.admin && !socket.user.mod) { - socket.emit('message', {timestamp: log.createdAt, 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; } @@ -212,7 +212,7 @@ const executeCommand = (io, socket, command) => { //check valid command if (!username || !minutes || typeof minutes !== 'number' || minutes < 1) { - socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: `format: /mute username minutes [reason]` }); + socket.emit('message', { emphasis: true, text: `format: /mute username minutes [reason]` }); break; } @@ -224,9 +224,12 @@ const executeCommand = (io, socket, command) => { 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 - const log = await chatlog.create({ + chatlog.create({ notification: true, username: socket.user.username, text: `${username} has been muted for ${minutes} minute${minutes != 1 ? 's' : ''}: ${reason}`, @@ -235,15 +238,12 @@ const executeCommand = (io, socket, command) => { emphasis: true }); - //broadcast - io.to(socket.user.room).emit('message', { timestamp: log.createdAt, strong: true, emphasis: true, text: `${username} has been muted for ${minutes} minute${minutes != 1 ? 's' : ''}${reason ? ': ' : ''}${reason}` }); - break; } case '/unmute': { if (!socket.user.admin && !socket.user.mod) { - socket.emit('message', {timestamp: log.createdAt, 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; } @@ -263,12 +263,15 @@ const executeCommand = (io, socket, command) => { }); if (rowCount == 0) { - socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: 'That user was not muted' }); + 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 - const log = await chatlog.create({ + chatlog.create({ notification: true, username: socket.user.username, text: `${username} has been unmuted`, @@ -276,14 +279,11 @@ const executeCommand = (io, socket, command) => { emphasis: true }); - //broadcast - io.to(socket.user.room).emit('message', {timestamp: log.createdAt, emphasis: true, text: `${username} has been unmuted` }); - break; } default: { - socket.emit('message', {timestamp: log.createdAt, emphasis: true, text: 'Unknown command' }); + socket.emit('message', { emphasis: true, text: 'Unknown command' }); } } }; From bd5b6e8233548556736f136ff3043ddee2410a8b Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 3 Jan 2022 08:30:03 +0000 Subject: [PATCH 5/5] Added async keyword --- server/chat/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/chat/index.js b/server/chat/index.js index 8ab71cc..e6b2ee1 100644 --- a/server/chat/index.js +++ b/server/chat/index.js @@ -35,7 +35,7 @@ const chat = io => { }); //from here, handles all normal messages - socket.on('open chat', message => { + socket.on('open chat', async message => { //handle rooms - only in a room if you've opened chat const newlyOpened = !socket.user.room; socket.user.room = socket.user.room || 'general'; //default to general @@ -120,7 +120,7 @@ const chat = io => { socket.broadcast.to(socket.user.room).emit('message', log); }); - socket.on('disconnect', reason => { + socket.on('disconnect', async reason => { //broadcast to this room if (!socket.user) { return;