Skip to content

Commit

Permalink
messageLog refactor (#73)
Browse files Browse the repository at this point in the history
* move writeMessageToDb to sendMessageToDb

* add ServerType to io
  • Loading branch information
nmpereira authored Jan 5, 2024
1 parent f2c5527 commit a5e752d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 109 deletions.
25 changes: 25 additions & 0 deletions helpers/sendMessageToDb.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
await writeMessageToDb({
roomName,
message,
userName,
});

io.to(roomName).emit("messageLog", {
messageLog: message,
date: new Date(),
});
};
15 changes: 2 additions & 13 deletions helpers/sendUserCount.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
10 changes: 3 additions & 7 deletions helpers/startTimer.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
124 changes: 35 additions & 89 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ 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";
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;
Expand Down Expand Up @@ -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,
});
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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,
});
}

Expand All @@ -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
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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", {
Expand Down Expand Up @@ -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", {
Expand Down Expand Up @@ -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,
});
}
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit a5e752d

Please sign in to comment.