-
Notifications
You must be signed in to change notification settings - Fork 3
/
chat_workingjs.js
339 lines (291 loc) · 11.3 KB
/
chat_workingjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
const socket = io();
const urlParams = new URLSearchParams(window.location.search);
const roomId = urlParams.get('roomId');
const roomType = urlParams.get('roomType');
const roomName = urlParams.get('roomName');
const username = urlParams.get('username');
const userLocation = urlParams.get('location');
const userId = urlParams.get('userId');
const userColorName = getCookie('userColor') || decodeURIComponent(urlParams.get('txtclr')) || 'white';
const userColor = getColorHex(userColorName);
let OFFENSIVE_WORDS = [];
let birthdayCelebrated = false;
// At the top of chat_room.js, add this color mapping function
function getColorHex(colorName) {
const colorMap = {
'white': '#FFFFFF',
'orange': '#FF9800',
'blue': '#00FFFF',
'green': '#00FF00',
'pink': '#FF00FF',
'yellow': '#FFFF00'
};
return colorMap[colorName] || '#FFFFFF'; // Default to white if color not found
}
// Fetch offensive words list from server
fetch('/offensive-words')
.then(response => response.json())
.then(words => {
OFFENSIVE_WORDS = words;
})
.catch(error => console.error('Error fetching offensive words:', error));
// Check if user is banned before connecting
if (getCookie('banned') === 'true') {
const banExpiration = getCookie('banExpiration');
if (banExpiration && Date.now() < parseInt(banExpiration)) {
window.location.href = 'removed.html';
} else {
deleteCookie('banned');
deleteCookie('banExpiration');
}
}
socket.emit('userConnected', { userId });
const chatRoom = document.getElementById('chatRoom');
const joinSound = document.getElementById('joinSound');
socket.emit('joinRoom', { roomId, username, location: userLocation, userId, color: userColorName });
socket.on('initializeUsers', (users) => {
chatRoom.innerHTML = '';
users.forEach((user, index) => {
const userElement = createUserElement(user, index);
chatRoom.appendChild(userElement);
});
});
socket.on('userJoined', (user) => {
const userElement = createUserElement(user);
chatRoom.appendChild(userElement);
joinSound.play(); // Play the sound when a user joins
});
socket.on('userLeft', (user) => {
const userElement = document.querySelector(`[data-user-id="${user.userId}"]`);
if (userElement) {
userElement.remove();
}
});
socket.on('typing', (data) => {
const userElement = document.querySelector(`[data-user-id="${data.userId}"]`);
if (userElement) {
const textareaElement = userElement.querySelector('textarea');
textareaElement.value = data.message;
}
});
socket.on('userBanned', (banExpiration) => {
const banDuration = Math.floor((banExpiration - Date.now()) / 1000);
setCookie('banned', 'true', banDuration / 86400);
setCookie('banExpiration', banExpiration.toString(), banDuration / 86400);
window.location.href = 'removed.html';
});
socket.on('duplicateUser', (data) => {
showLimitedToast('error', data.message);
setTimeout(() => {
window.location.href = data.redirectUrl;
}, 3000);
});
// Inactivity timeout
let inactivityTimeout;
const inactivityLimit = 120000; // 2 minutes
function resetInactivityTimeout() {
clearTimeout(inactivityTimeout);
inactivityTimeout = setTimeout(() => {
if (document.cookie.indexOf('banned=true') === -1) {
socket.emit('userDisconnected', { userId });
showLimitedToast('error', 'You were removed from the room for being inactive for 2 minutes.');
setTimeout(() => {
window.location.href = 'index.html';
}, 3000);
}
}, inactivityLimit);
}
document.addEventListener('keydown', resetInactivityTimeout);
document.addEventListener('mousemove', resetInactivityTimeout);
resetInactivityTimeout();
let activeToasts = [];
function showLimitedToast(type, message) {
// Remove any existing toasts with the same message
const existingToast = activeToasts.find(toast => toast && toast.options && toast.options.message === message);
if (existingToast) {
toastr.remove(existingToast);
activeToasts = activeToasts.filter(toast => toast !== existingToast);
}
// If we're at the max number of toasts, remove the oldest one
if (activeToasts.length >= toastr.options.maxOpened) {
const oldestToast = activeToasts.shift();
if (oldestToast) {
toastr.remove(oldestToast);
}
}
// Show the new toast
const newToast = toastr[type](message);
// Add the new toast to our array
activeToasts.push(newToast);
// Remove the toast from our array when it's hidden
newToast.on('hidden.bs.toast', function() {
const index = activeToasts.indexOf(newToast);
if (index > -1) {
activeToasts.splice(index, 1);
}
});
}
function escapeHtml(text) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, (m) => map[m]);
}
function containsOffensiveWord(text) {
return OFFENSIVE_WORDS.some(word => {
const regex = new RegExp(`\\b${word}\\b`, 'i');
return regex.test(text);
});
}
function isBirthdayMessage(text) {
const birthdayPhrases = [
"today is my birthday",
"it's my birthday",
"it is my birthday",
"today's my birthday",
"my birthday is today",
"i'm celebrating my birthday",
"im celebrating my birthday",
"today is my bday",
"its my bday",
"it's my bday",
"my bday is today",
"celebrating my bday",
"my birthday party is today",
"having my birthday party",
"born on this day"
];
return birthdayPhrases.some(phrase => text.toLowerCase().includes(phrase));
}
function createUserElement(user) {
const userElement = document.createElement('div');
userElement.classList.add('row');
if (chatRoom.classList.contains('vertical-layout')) {
userElement.classList.add('column');
}
userElement.dataset.userId = user.userId;
const userInfo = document.createElement('div');
userInfo.classList.add('sub-row');
userInfo.innerHTML = `<span>${escapeHtml(user.username)}</span><span>/</span><span>${escapeHtml(user.location)}</span>`;
userInfo.style.backgroundColor = '#333';
userInfo.style.color = 'white';
userInfo.style.padding = '5px';
userInfo.style.paddingLeft = '12px';
userInfo.style.marginBottom = '10px';
const userTyping = document.createElement('textarea');
userTyping.classList.add('sub-row');
userTyping.disabled = user.userId !== userId;
userTyping.maxLength = 1000;
userTyping.style.width = '100%';
userTyping.style.height = '100%';
userTyping.style.resize = 'none';
userTyping.style.backgroundColor = 'black';
userTyping.style.color = user.userId === userId ? userColor : (getColorHex(user.color) || '#FFA500');
userTyping.style.webkitTextFillColor = user.userId === userId ? userColor : (getColorHex(user.color) || '#FFA500');
userTyping.style.padding = '10px';
userTyping.style.fontSize = '16px';
userTyping.style.boxSizing = 'border-box';
userTyping.style.fontFamily = '"Courier New", Courier, monospace';
userTyping.style.opacity = '1';
if (user.userId === userId) {
userTyping.style.border = '1px solid white';
userTyping.addEventListener('input', () => {
if (userTyping.value.length >= userTyping.maxLength) {
showLimitedToast('error', `Message is too long! Maximum length is ${userTyping.maxLength} characters.`);
} else {
if (containsOffensiveWord(userTyping.value)) {
showLimitedToast('error', 'Message contains offensive words.');
socket.emit('message', { roomId, userId, message: userTyping.value, color: userColor });
userTyping.value = '';
} else {
socket.emit('typing', { roomId, userId, message: userTyping.value, color: userColor });
resetInactivityTimeout();
if (isBirthdayMessage(userTyping.value)) {
socket.emit('message', { roomId, userId, message: userTyping.value, color: userColor });
}
}
}
});
} else {
userTyping.style.border = 'none';
userTyping.style.userSelect = 'none';
}
userElement.appendChild(userInfo);
userElement.appendChild(userTyping);
return userElement;
}
socket.on('typing', (data) => {
const userElement = document.querySelector(`[data-user-id="${data.userId}"]`);
if (userElement) {
const textareaElement = userElement.querySelector('textarea');
textareaElement.value = data.message;
if (data.userId !== userId) {
const colorHex = getColorHex(data.color);
textareaElement.style.color = colorHex || '#FFA500';
textareaElement.style.webkitTextFillColor = colorHex || '#FFA500';
}
}
});
socket.on('message', (data) => {
const { userId: messageUserId, message, color } = data;
const userElement = document.querySelector(`[data-user-id="${messageUserId}"]`);
if (userElement) {
const textareaElement = userElement.querySelector('textarea');
textareaElement.value = message;
if (messageUserId !== userId) {
const colorHex = getColorHex(color);
textareaElement.style.color = colorHex || '#FFA500';
textareaElement.style.webkitTextFillColor = colorHex || '#FFA500';
}
}
});
function changeColor(color) {
socket.emit('changeColor', { userId, color });
const userElement = document.querySelector(`[data-user-id="${userId}"]`);
if (userElement) {
const textareaElement = userElement.querySelector('textarea');
textareaElement.style.color = color;
textareaElement.style.webkitTextFillColor = color;
}
setCookie('userColor', color, 30); // Store the selected color in a cookie
}
window.addEventListener('beforeunload', () => {
socket.emit('userDisconnected', { userId });
});
function updateUserColor(newColor) {
userColor = getColorHex(newColor);
setCookie('userColor', newColor, 30);
const userElement = document.querySelector(`[data-user-id="${userId}"]`);
if (userElement) {
const textareaElement = userElement.querySelector('textarea');
textareaElement.style.color = userColor;
textareaElement.style.webkitTextFillColor = userColor;
}
socket.emit('changeColor', { roomId, userId, color: newColor });
}
function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
let cookieArray = document.cookie.split(';');
for (let i = 0; i < cookieArray.length; i++) {
let cookiePair = cookieArray[i].split('=');
if (name == cookiePair[0].trim()) {
return decodeURIComponent(cookiePair[1]);
}
}
return null;
}
function deleteCookie(name) {
document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}