added timestamp to messages and logs, need to test
This commit is contained in:
+22
-19
@@ -2,6 +2,9 @@ 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
|
||||||
@@ -47,7 +50,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: `${socket.user.username} entered chat` });
|
socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${today} ${socket.user.username} entered chat` });
|
||||||
|
|
||||||
//log
|
//log
|
||||||
chatlog.create({
|
chatlog.create({
|
||||||
@@ -82,7 +85,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 ? `${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) {
|
if (record) {
|
||||||
socket.emit('message', { emphasis: true, text: 'You are currently muted' });
|
socket.emit('message', { emphasis: true, text: `${today} 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: message.text,
|
text: `${today} ${message.text}`,
|
||||||
room: socket.user.room
|
room: socket.user.room
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -132,7 +135,7 @@ const chat = io => {
|
|||||||
chatlog.create({
|
chatlog.create({
|
||||||
notification: true,
|
notification: true,
|
||||||
username: socket.user.username,
|
username: socket.user.username,
|
||||||
text: `${socket.user.username} left chat`,
|
text: `${today} ${socket.user.username} left chat`,
|
||||||
room: socket.user.room,
|
room: socket.user.room,
|
||||||
emphasis: true
|
emphasis: true
|
||||||
});
|
});
|
||||||
@@ -165,13 +168,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: `${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
|
//log
|
||||||
chatlog.create({
|
chatlog.create({
|
||||||
notification: true,
|
notification: true,
|
||||||
username: socket.user.username,
|
username: socket.user.username,
|
||||||
text: `${socket.user.username} left the room`,
|
text: `${today} ${socket.user.username} left the room`,
|
||||||
room: socket.user.room,
|
room: socket.user.room,
|
||||||
emphasis: true
|
emphasis: true
|
||||||
});
|
});
|
||||||
@@ -182,25 +185,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: `${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
|
//log
|
||||||
chatlog.create({
|
chatlog.create({
|
||||||
notification: true,
|
notification: true,
|
||||||
username: socket.user.username,
|
username: socket.user.username,
|
||||||
text: `${socket.user.username} entered the room`,
|
text: `${today} ${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: `Entered room ${socket.user.room}` });
|
socket.emit('message', { emphasis: true, text: `${today} 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: '/mute is only available to admins and mods' });
|
socket.emit('message', { emphasis: true, text: `${today} /mute is only available to admins and mods` });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,7 +215,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: `format: /mute username minutes [reason]` });
|
socket.emit('message', { emphasis: true, text: `${today} format: /mute username minutes [reason]` });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,13 +229,13 @@ const executeCommand = (io, socket, command) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
//broadcast
|
//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
|
//log
|
||||||
chatlog.create({
|
chatlog.create({
|
||||||
notification: true,
|
notification: true,
|
||||||
username: socket.user.username,
|
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,
|
room: socket.user.room,
|
||||||
strong: true,
|
strong: true,
|
||||||
emphasis: true
|
emphasis: true
|
||||||
@@ -243,7 +246,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: '/unmute is only available to admins and mods' });
|
socket.emit('message', { emphasis: true, text: `${today} /unmute is only available to admins and mods` });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,18 +266,18 @@ const executeCommand = (io, socket, command) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (rowCount == 0) {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//broadcast
|
//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
|
//log
|
||||||
chatlog.create({
|
chatlog.create({
|
||||||
notification: true,
|
notification: true,
|
||||||
username: socket.user.username,
|
username: socket.user.username,
|
||||||
text: `${username} has been unmuted`,
|
text: `${today} ${username} has been unmuted`,
|
||||||
room: socket.user.room,
|
room: socket.user.room,
|
||||||
emphasis: true
|
emphasis: true
|
||||||
});
|
});
|
||||||
@@ -283,7 +286,7 @@ const executeCommand = (io, socket, command) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
socket.emit('message', { emphasis: true, text: 'Unknown command' });
|
socket.emit('message', { emphasis: true, text: `${today} Unknown command` });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user