Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add French badwords & implement broadcastMessage #28

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/@types/french-badwords-list.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "french-badwords-list" {
const array: string[];
export { array };
}
110 changes: 79 additions & 31 deletions backend/controllers/Chat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import type SocketIO from "socket.io";
import type { Socket } from "socket.io";
import ChatMessagePayload from "../../common/requests/ChatMessagePayload";
import { PrismaClient } from "@prisma/client";

import leoProfanity from "leo-profanity";
import frenchBadwordsList from "french-badwords-list";
import WSS from "../server/Websocket";

leoProfanity.clearList();
leoProfanity.add(frenchBadwordsList.array);

const prisma = new PrismaClient();

class ChatController {
/**
Expand All @@ -10,35 +18,75 @@ class ChatController {
* @param socket The client socket
* @param data The payload
*/
public static async broadcastMessage(socket: SocketIO.Socket, ...data: unknown[]) {
// TODO: Broadcast the message to all clients
/**
* VALIDATION
* * Validate the message data
* * Check if the user is logged in
* * Check if the user is not muted
* * Check if the user has sent more that 3 messages in the last 5 seconds
* * - Mute the user for 3 secondes (1st time)
* * - Mute the user for 10 secondes (2nd time)
* * - Mute the user for 1 minute (3rd time)
* * - Mute the user for 5 minutes (4th time)
* * - Mute the user for 30 minutes (5th time)
* * - Mute the user for 1 hour (6th time)
* * - Mute the user for 2 hours (7th time)
* * - Mute the user for 12 hours (8th time)
* * - Mute the user for 24 hours (9th time)
* * - Mute the user definitively (10th time)
* * Check if the message is not longer than 200 characters
* * Check if the message is not empty
* * Check if the message does not contain any bad words
* * Check if the message does not contain any links
*
* PROCESS
* * Log the message
*
* RESPONSE
* * Broadcast the message to all clients
*/
public static async broadcastMessage(socket: SocketIO.Socket, [message, callback]: [string, (success: boolean) => void]) {
if (!message || message.length < 1 || message.length > 200) {
callback(false);
return;
}

// Check if the message contains bad words
const cleanMessage = leoProfanity.clean(message);

if (!socket.data.email) {
callback(false);
return;
}

const user = await prisma.account.findFirst({
where: {
devinciEmail: socket.data.email,
},
});

if (!user) {
callback(false);
return;
}

if (user.isMuted) {
callback(false);
return;
}

const now = new Date();

const lastTimestamps = ((user.lastSentMessageTimes as number[]) ?? []).filter((timestamp) => timestamp > now.getTime() - 5000);
if (lastTimestamps.length > 3) {
user.isMuted = true;
// Save the user
await prisma.account.update({
where: { id: user.id },
data: { isMuted: true },
});

callback(false);
return;
}

user.lastSentMessageTimes = [...lastTimestamps, now.getTime()];

// Save the user
await prisma.account.update({
where: { id: user.id },
data: { lastSentMessageTimes: user.lastSentMessageTimes },
});

prisma.logEntry.create({
data: {
devinciEmail: user.devinciEmail,
time: new Date().getTime(),
ip: socket.handshake.address,
action: {
type: "message",
originalContent: message,
content: cleanMessage,
},
},
});

WSS.broadcastMessage(user.devinciEmail, cleanMessage);

callback(true);
}
}

Expand Down
32 changes: 32 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"devDependencies": {
"@types/express": "^4.17.21",
"@types/jsonwebtoken": "^9.0.5",
"@types/leo-profanity": "^1.5.3",
"@types/nodemailer": "^6.4.14",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
Expand All @@ -29,7 +30,9 @@
"common": "file:../common",
"dotenv": "^16.4.1",
"express": "^4.18.2",
"french-badwords-list": "^1.0.7",
"jsonwebtoken": "^9.0.2",
"leo-profanity": "^1.7.0",
"mysql2": "^3.9.1",
"nodemailer": "^6.9.9",
"redis": "^4.6.13",
Expand Down
5 changes: 5 additions & 0 deletions backend/server/Websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class WSS {
if(!socket) this.io.emit("classementUpdate", classement);
else socket.emit("classementUpdate", classement);
}

static async broadcastMessage(senderEmail: string, message: string) {
this.io.emit("message", senderEmail, message);
}

}

export default WSS;
7 changes: 4 additions & 3 deletions common/docs/socket-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Place a pixel on the canvas

Send a message to all players

| index | type | description |
| :---: | :----- | :---------- |
| 0 | string | The message |
| index | type | description |
| :---: | :---------------- | :----------- |
| 0 | string | The message |
| 1 | callback(boolean) | The callback |
Loading