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

Commit

Permalink
Merge pull request #15 from cedricouellet/develop
Browse files Browse the repository at this point in the history
censorship removed, browser notifs fixed, refactor, zalgo usernames
  • Loading branch information
Bobelbo authored Jan 27, 2022
2 parents de1d5e4 + 894846c commit da09739
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 58 deletions.
18 changes: 11 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const http = require("http");
const socketIO = 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 { events } = require("./lib/constants");
const messaging = require("./lib/messaging");
const users = require("./lib/users");
const isZalgo = require("./helpers/isZalgo");

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

Expand Down Expand Up @@ -59,17 +59,17 @@ function receiveChatMessage(socket, message) {
return;
}

if (messaging.getZalgoScore(message)) {
if (isZalgo(message)) {
socket.emit(events.MESSAGE,
messaging.generateServerMessage("Zalgo is not supported...\n\nFuck off!")
messaging.generateServerMessage("Nice try, but Zalgo is not allowed...")
);

return;
}

const messageObject = {
sender: sender,
text: sanitizer.sanitize(message || "hi"),
text: message.trim(),
};

io.emit(events.MESSAGE, messageObject);
Expand All @@ -88,6 +88,10 @@ function receiveChatMessage(socket, message) {

function joinUser(socket, username) {
try {
if (isZalgo(username.trim())) {
username = "I think I'm funny because I use Zalgo";
}

const joined = users.add(socket.id, username);

socket.emit(events.JOIN, joined.username);
Expand Down
20 changes: 20 additions & 0 deletions helpers/isZalgo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Detects if a string contains Zalgo text.
* @param {string} str The string to test.
* @return {boolean} If the string contains Zalgo or not.
*/
function isZalgo(str) {
if (str.length === 0) return false;

const reg = new RegExp(/([aeiouy]\u0308)|[\u0300-\u036f\u0489]/, "gi");
const charBites = str.match(/[\s\S]{1,3}/g).length;
let rawScore = 0;

str.split("").forEach(char => {
if (reg.test(char)) rawScore++;
});

return rawScore / charBites >= 1;
}

module.exports = isZalgo;
37 changes: 0 additions & 37 deletions helpers/messaging.js

This file was deleted.

10 changes: 0 additions & 10 deletions helpers/sanitizer.js

This file was deleted.

File renamed without changes.
17 changes: 17 additions & 0 deletions lib/messaging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const {BOT_NAME} = require('./constants');

/**
* Generate a message coming from the server.
* @param {string} message The message to send.
* @return {{sender: string, text: string}} The message object containing the sender and text.
*/
function generateServerMessage(message) {
return {
sender: BOT_NAME,
text: message,
};
}

module.exports = {
generateServerMessage,
};
3 changes: 1 addition & 2 deletions helpers/users.js → lib/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { BOT_NAME } = require("./constants");
const sanitizer = require('./sanitizer');

let users = [];

Expand All @@ -11,7 +10,7 @@ let users = [];
*/
function add(id, username) {
id = id.trim();
username = sanitizer.sanitize(username);
username = username.trim();
let existing = users.find((user) => user.username === username);

if (existing !== undefined || username === BOT_NAME) {
Expand Down
18 changes: 16 additions & 2 deletions public/js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const EV_USERS = "users";
const EV_SUBMIT = "submit";
const EV_MOUSEOVER = "mouseover";
const EV_CHANGE = "change";
const EV_FOCUS = "focus";
const EV_BLUR = "blur";

// DOM elements
const faviconHolder = document.getElementById("favicon");
Expand All @@ -28,6 +30,9 @@ let username = sessionStorage.getItem(KEY_USERNAME) || "Guest";
let notificationsEnabled = localStorage.getItem(KEY_NOTIFICATIONS) === 'true' || false;
notifyCheckbox.checked = notificationsEnabled;

// Utility variables
let isFocused = true;

// Notification senders
const notifier = new BrowserNotifier(window);
const notificationDisplayer = new NotificationDisplayer(faviconHolder, NOTIFICATION_FAVICON);
Expand All @@ -43,7 +48,16 @@ socket.on(EV_USERS, onUsersReceived);

// DOM event listeners
chatForm.addEventListener(EV_SUBMIT, onSubmitClicked);
window.addEventListener(EV_MOUSEOVER, () => notificationDisplayer.clearNotifications());

window.addEventListener(EV_FOCUS, () => {
isFocused = true;
notificationDisplayer.clearNotifications();
});

window.addEventListener(EV_BLUR, () => {
isFocused = false;
});

notifyCheckbox.addEventListener(EV_CHANGE, (e) => {
notificationsEnabled = e.target.checked;
localStorage.setItem(KEY_NOTIFICATIONS, notificationsEnabled);
Expand All @@ -66,7 +80,7 @@ function onMessageReceived(messageObject) {
if (messageObject?.sender !== username) {
notificationDisplayer.addNotification();

if (notificationsEnabled === true) {
if (notificationsEnabled === true && isFocused === false) {
notifier.notify(`${messageObject?.sender}: ${messageObject?.text}`);
}
}
Expand Down

0 comments on commit da09739

Please sign in to comment.