From a5e752d1a4e880ce08e25d0dfab893316db97dc3 Mon Sep 17 00:00:00 2001 From: Mario Nikhil Pereira <45009203+nmpereira@users.noreply.github.com> Date: Thu, 4 Jan 2024 20:35:07 -0500 Subject: [PATCH] messageLog refactor (#73) * move writeMessageToDb to sendMessageToDb * add ServerType to io --- helpers/sendMessageToDb.ts | 25 ++++++++ helpers/sendUserCount.ts | 15 +---- helpers/startTimer.ts | 10 +-- server.ts | 124 +++++++++++-------------------------- 4 files changed, 65 insertions(+), 109 deletions(-) create mode 100644 helpers/sendMessageToDb.ts diff --git a/helpers/sendMessageToDb.ts b/helpers/sendMessageToDb.ts new file mode 100644 index 0000000..be3fdb6 --- /dev/null +++ b/helpers/sendMessageToDb.ts @@ -0,0 +1,25 @@ +import { writeMessageToDb } from "../common/models/dbHelpers"; +import { ServerType } from "../common/types/socket/types"; + +export default async ({ + roomName, + message, + userName, + io, +}: { + roomName: string; + message: string; + userName: string; + io: ServerType; +}): Promise => { + await writeMessageToDb({ + roomName, + message, + userName, + }); + + io.to(roomName).emit("messageLog", { + messageLog: message, + date: new Date(), + }); +}; diff --git a/helpers/sendUserCount.ts b/helpers/sendUserCount.ts index a5c2599..bec1a2f 100644 --- a/helpers/sendUserCount.ts +++ b/helpers/sendUserCount.ts @@ -1,20 +1,9 @@ -import { Server } from "socket.io"; -import { - ClientToServerEvents, - InterServerEvents, - ServerToClientEvents, - SocketData, -} from "../common/types/socket/types"; +import { ServerType } from "../common/types/socket/types"; import { TimerStore } from "../common/types/types"; import { frontendRouteRooms } from "../common/common"; interface ISendUserCount { - io: Server< - ClientToServerEvents, - ServerToClientEvents, - InterServerEvents, - SocketData - >; + io: ServerType; roomName: string; timerStore: TimerStore; } diff --git a/helpers/startTimer.ts b/helpers/startTimer.ts index 0e1505a..d8d37db 100644 --- a/helpers/startTimer.ts +++ b/helpers/startTimer.ts @@ -1,8 +1,8 @@ import messageList from "../common/models/MessageList"; -import { writeMessageToDb } from "../common/models/dbHelpers"; import { ServerType } from "../common/types/socket/types"; import { TimerStore } from "../common/types/types"; import formatTimestamp from "./formatTimestamp"; +import sendMessageToDb from "./sendMessageToDb"; interface StartCountdownArgs { roomName: string; @@ -87,15 +87,11 @@ const startCountdown = async ({ altValue: formatTimestamp(durationInSeconds), }); - await writeMessageToDb({ + await sendMessageToDb({ roomName, message: currentMessage, userName: "Anonymous", - }); - - io.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + io, }); } } else { diff --git a/server.ts b/server.ts index 9f1d773..25d0586 100644 --- a/server.ts +++ b/server.ts @@ -10,20 +10,19 @@ import apiRoutes from "./routes/apiRoutes"; import storeMiddleware from "./middleware/storeMiddleware"; import { - ClientToServerEvents, - InterServerEvents, - ServerToClientEvents, - SocketData, EmitStartCountdownArgs, EmitWithRoomNameArgs, EmitWorkBreakTimerArgs, EmitJoinEventArgs, + ClientToServerEvents, + InterServerEvents, + ServerToClientEvents, + SocketData, } from "./common/types/socket/types"; import connectDB from "./common/models/connectDB"; import { readFromDb, readMessageFromDb, - writeMessageToDb, writeToDb, } from "./common/models/dbHelpers"; import messageList from "./common/models/MessageList"; @@ -31,6 +30,7 @@ import formatTimestamp from "./helpers/formatTimestamp"; import { frontendRouteRooms, statRooms } from "./common/common"; import timerStore from "./common/timerStore"; import sendUserCount from "./helpers/sendUserCount"; +import sendMessageToDb from "./helpers/sendMessageToDb"; const app = express(); const PORT = process.env.PORT || 4000; @@ -235,15 +235,11 @@ io.on("connection", async (socket) => { message: "created", }); - await writeMessageToDb({ + await sendMessageToDb({ roomName, message: currentMessage, userName, - }); - - socket.broadcast.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + io, }); } } @@ -266,15 +262,11 @@ io.on("connection", async (socket) => { message: "joined", }); - await writeMessageToDb({ + await sendMessageToDb({ roomName, message: currentMessage, - userName: socket.data.nickname, - }); - - socket.broadcast.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + userName, + io, }); // limit to last 50 messages @@ -312,14 +304,6 @@ io.on("connection", async (socket) => { } }); - // if (!frontendRouteRooms.includes(roomName)) { - // console.log( - // `User ${socket.data.nickname || socket.id} connected ${ - // roomName ? `to room ${roomName}` : "" - // }` - // ); - // } - socket.on("changeUsername", async ({ userName }: { userName: string }) => { if (timerStore[roomName]) { const oldUserName = socket.data.nickname; @@ -341,15 +325,12 @@ io.on("connection", async (socket) => { message: "changedUsername", value: userName, }); - await writeMessageToDb({ + + await sendMessageToDb({ roomName, message: currentMessage, userName, - }); - - io.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + io, }); } @@ -370,15 +351,12 @@ io.on("connection", async (socket) => { room: roomName, message: "left", }); - await writeMessageToDb({ + + await sendMessageToDb({ roomName, message: currentMessage, userName: socket.data.nickname, - }); - - io.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + io, }); // remove the user from the room. Remove only the first instance of the user @@ -453,15 +431,11 @@ io.on("connection", async (socket) => { ), }); - await writeMessageToDb({ + await sendMessageToDb({ roomName, message: currentMessage, userName: socket.data.nickname, - }); - - io.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + io, }); startCountdown({ @@ -506,15 +480,11 @@ io.on("connection", async (socket) => { altValue: formatTimestamp(timerStore[roomName].secondsRemaining), }); - await writeMessageToDb({ + await sendMessageToDb({ roomName, message: currentMessage, userName: socket.data.nickname, - }); - - io.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + io, }); startCountdown({ @@ -548,15 +518,12 @@ io.on("connection", async (socket) => { timerStore[roomName].secondsRemaining ), }); - await writeMessageToDb({ + + await sendMessageToDb({ roomName, message: currentMessage, userName: socket.data.nickname, - }); - - io.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + io, }); startCountdown({ @@ -591,16 +558,11 @@ io.on("connection", async (socket) => { ), }); - await writeMessageToDb({ + await sendMessageToDb({ roomName, message: currentMessage, - - userName: socket.data.nickname, - }); - - io.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + userName, + io, }); io.to(roomName).emit("updatedTitle", { @@ -639,15 +601,11 @@ io.on("connection", async (socket) => { ), }); - await writeMessageToDb({ + await sendMessageToDb({ roomName, message: currentMessage, - userName: socket.data.nickname, - }); - - io.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + userName, + io, }); io.to(roomName).emit("updatedTitle", { @@ -720,15 +678,11 @@ io.on("connection", async (socket) => { } if (currentMessage.length > 0) { - await writeMessageToDb({ + await sendMessageToDb({ roomName, message: currentMessage, userName: socket.data.nickname, - }); - - io.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + io, }); } } @@ -759,15 +713,11 @@ io.on("connection", async (socket) => { value: timerStore[roomName].isPublic ? "public" : "private", }); - await writeMessageToDb({ + await sendMessageToDb({ roomName, message: currentMessage, userName: socket.data.nickname, - }); - - io.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + io, }); console.log( @@ -814,15 +764,11 @@ io.on("connection", async (socket) => { altValue: timerStore[roomName].isBreak ? "break" : "work", }); - await writeMessageToDb({ + await sendMessageToDb({ roomName, message: currentMessage, userName: socket.data.nickname, - }); - - io.to(roomName).emit("messageLog", { - messageLog: currentMessage, - date: new Date(), + io, }); console.log(