Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Commit

Permalink
refactor, bugfix user can have same name as bot
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricouellet committed Jan 23, 2022
1 parent bd9928d commit 084d33e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
35 changes: 13 additions & 22 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,64 @@ const http = require("http");
const socketIoServer = require("socket.io");
const express = require("express");

const { events } = require("./helpers/constants");
const sanitizer = require("./helpers/sanitizer");
const messaging = require("./helpers/messaging");
const users = require("./helpers/users");

const PORT = parseInt(process.env.PORT) || 3000;

// Builtin Socket.IO
const EV_CONNECT = "connection";
const EV_DISCONNECT = "disconnect";

// Custom events
const EV_JOIN = "join";
const EV_MESSAGE = "message";
const EV_CHAT_MESSAGE = "chat_message";
const EV_USERS = "users";

const app = express();
const server = http.createServer(app);
const io = socketIoServer(server);

app.use(helmet());
app.use(express.static(path.join(__dirname, "public")));

io.on(EV_CONNECT, (socket) => {
io.on(events.CONNECT, (socket) => {
console.log(`client socket ${socket.id} connected`);

socket.on(EV_JOIN, (username) => {
socket.on(events.JOIN, (username) => {
const joined = users.add(socket.id, username);

socket.emit(events.JOIN, joined.username);

socket.emit(
EV_MESSAGE,
events.MESSAGE,
messaging.generateServerMessage("Welcome to NoChat :-)")
);

socket.emit(EV_JOIN, joined.username);

socket.broadcast.emit(
EV_MESSAGE,
events.MESSAGE,
messaging.generateServerMessage(`${joined.username} has joined the chat.`)
);

io.emit(EV_USERS, users.getUsernames());
io.emit(events.USERS, users.getUsernames());
});

socket.on(EV_DISCONNECT, () => {
socket.on(events.DISCONNECT, () => {
const left = users.getById(socket.id);

if (left) {
users.remove(left.id);

io.emit(
EV_MESSAGE,
events.MESSAGE,
messaging.generateServerMessage(`${left.username} has left the chat.`)
);
}

io.emit(EV_USERS, users.getUsernames());
io.emit(events.USERS, users.getUsernames());
});

socket.on(EV_CHAT_MESSAGE, (message) => {
socket.on(events.CHAT_MESSAGE, (message) => {
const messageObject = {
sender: users.getById(socket.id).username,
text: sanitizer.sanitize(message, "*", 2),
time: messaging.generateFormattedDate(),
};

io.emit(EV_MESSAGE, messageObject);
io.emit(events.MESSAGE, messageObject);
});
});

Expand Down
10 changes: 10 additions & 0 deletions helpers/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports.BOT_NAME = "NoChat Bot";

module.exports.events = {
CONNECT: "connection",
DISCONNECT: "disconnect",
JOIN: "join",
MESSAGE: "message",
CHAT_MESSAGE: "chat_message",
USERS: "users",
};
4 changes: 3 additions & 1 deletion helpers/messaging.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const {BOT_NAME} = require('./constants');

/**
* Generate a message coming from the server.
* @param {string} message The message to send.
* @return {{sender: string, text: string, time: string}} The message object containing the sender, text and formatted time.
*/
function generateServerMessage(message) {
return {
sender: "NoChat Server",
sender: BOT_NAME,
text: message,
time: generateFormattedDate(),
};
Expand Down
2 changes: 1 addition & 1 deletion helpers/sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ const profanityFilter = require("leo-profanity");
* @return {string} The filtered string.
*/
module.exports.sanitize = (str) => {
return profanityFilter.clean(str, "*", 2);
return profanityFilter.clean(str.trim(), "🤫", 2);
};
11 changes: 8 additions & 3 deletions helpers/users.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const { BOT_NAME } = require("./constants");
const sanitizer = require('./sanitizer');

let users = [];

/**
Expand All @@ -7,13 +10,15 @@ let users = [];
* @return {{id: string, username: string}} The user that was added.
*/
function add(id, username) {
id = id.trim();
username = sanitizer.sanitize(username);
let existing = users.find((user) => user.username === username);

if (existing !== undefined) {
let i = 0;
if (existing !== undefined || username === BOT_NAME) {
let i = 1;
while (existing !== undefined) {
i++;
existing = users.find((user) => user.username === `${username}(${i})`);
i++;
}
username = `${username}(${i})`;
}
Expand Down

0 comments on commit 084d33e

Please sign in to comment.