Merge pull request #65 from Maix0/nigel/functionBlockMessage

Nigel/function block message
This commit is contained in:
Maix0 2025-12-19 14:46:56 +01:00 committed by GitHub
commit 5ee7e7c0b7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 484 additions and 212 deletions

View file

@ -3,10 +3,10 @@
# ::: :::::::: # # ::: :::::::: #
# Docker.mk :+: :+: :+: # # Docker.mk :+: :+: :+: #
# +:+ +:+ +:+ # # +:+ +:+ +:+ #
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ # # By: nrobinso <nrobinso@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2025/06/11 18:10:26 by maiboyer #+# #+# # # Created: 2025/06/11 18:10:26 by maiboyer #+# #+# #
# Updated: 2025/12/12 14:56:10 by maiboyer ### ########.fr # # Updated: 2025/12/19 14:33:21 by maiboyer ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #

View file

@ -1,7 +1,7 @@
import { addMessage } from "./addMessage"; import { addMessage } from "./addMessage";
import { Socket } from 'socket.io-client'; import { Socket } from 'socket.io-client';
import { getUser } from "@app/auth"; import { getUser } from "@app/auth";
import type { ClientMessage } from "./types_front";
/** /**
* function sends socket.emit to the backend to active and broadcast a message to all sockets * function sends socket.emit to the backend to active and broadcast a message to all sockets
* echos the message with addMessage to the sender * echos the message with addMessage to the sender
@ -13,15 +13,21 @@ export function broadcastMsg (socket: Socket, msgCommand: string[]): void {
addMessage(msgText); addMessage(msgText);
const user = getUser(); const user = getUser();
if (user && socket?.connected) { if (user && socket?.connected) {
const message = { const message: ClientMessage = {
command: msgCommand, command: msgCommand[0],
destination: '', destination: '',
type: "chat", type: "chat",
user: user.name, user: user.name,
token: document.cookie, token: document.cookie,
text: msgText, text: msgText,
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id ?? "",
SenderUserName: user.name,
SenderUserID: user.id,
userID: '',
frontendUserName: '',
frontendUser: '',
Sendertext: '',
}; };
socket.emit('message', JSON.stringify(message)); socket.emit('message', JSON.stringify(message));
} }

View file

@ -23,6 +23,29 @@ export const color = {
reset: '', reset: '',
}; };
export type blockedUnBlocked =
{
userState: string,
userTarget: string,
by: string,
};
export type obj =
{
command: string,
destination: string,
type: string,
user: string,
frontendUserName: string,
frontendUser: string,
token: string,
text: string,
timestamp: number,
SenderWindowID: string,
Sendertext: string,
};
// get the name of the machine used to connect // get the name of the machine used to connect
const machineHostName = window.location.hostname; const machineHostName = window.location.hostname;
console.log('connect to login at %chttps://' + machineHostName + ':8888/app/login',color.yellow); console.log('connect to login at %chttps://' + machineHostName + ':8888/app/login',color.yellow);
@ -64,22 +87,6 @@ function actionBtnPopUpInvite(invite: ClientProfil, senderSocket: Socket) {
}, 0) }, 0)
}; };
// async function windowStateHidden() {
// const socketId = __socket || undefined;
// // let oldName = localStorage.getItem("oldName") ?? undefined;
// let oldName: string;
// if (socketId === undefined) return;
// let userName = await updateUser();
// oldName = userName?.name ?? "";
// if (oldName === "") return;
// localStorage.setItem('oldName', oldName);
// socketId.emit('client_left', {
// user: userName?.name,
// why: 'tab window hidden - socket not dead',
// });
// return;
// };
async function windowStateVisable() { async function windowStateVisable() {
const buddies = document.getElementById('div-buddies') as HTMLDivElement; const buddies = document.getElementById('div-buddies') as HTMLDivElement;
@ -141,36 +148,6 @@ function parseCmdMsg(msgText: string): string[] | undefined {
return command; return command;
} }
// async function listBuddies(socket: Socket, buddies: HTMLDivElement, listBuddies: string) {
// if (!buddies) return;
// const sendtextbox = document.getElementById('t-chat-window') as HTMLButtonElement;
// const buddiesElement = document.createElement("div-buddies-list");
// buddiesElement.textContent = listBuddies + '\n';
// const user = getUser()?.name ?? "";
// buddies.appendChild(buddiesElement);
// buddies.scrollTop = buddies.scrollHeight;
// console.log(`Added buddies: ${listBuddies}`);
// buddiesElement.style.cursor = "pointer";
// buddiesElement.addEventListener("click", () => {
// navigator.clipboard.writeText(listBuddies);
// if (listBuddies !== user && user !== "") {
// sendtextbox.value = `@${listBuddies}: `;
// console.log("Copied to clipboard:", listBuddies);
// sendtextbox.focus();
// }
// });
// buddiesElement.addEventListener("dblclick", () => {
// console.log("Open profile:", listBuddies);
// getProfil(socket, listBuddies);
// sendtextbox.value = "";
// });
// }
function waitSocketConnected(socket: Socket): Promise<void> { function waitSocketConnected(socket: Socket): Promise<void> {
return new Promise(resolve => { return new Promise(resolve => {
if (socket.connected) return resolve(); if (socket.connected) return resolve();
@ -199,11 +176,6 @@ function quitChat (socket: Socket) {
}; };
// const bconnected = document.getElementById('b-help') as HTMLButtonElement;
// if (bconnected) {
// bconnected.click();
// }
function logout(socket: Socket) { function logout(socket: Socket) {
socket.emit("logout"); // notify server socket.emit("logout"); // notify server
socket.disconnect(); // actually close the socket socket.disconnect(); // actually close the socket
@ -216,6 +188,7 @@ function logout(socket: Socket) {
async function connected(socket: Socket): Promise<void> { async function connected(socket: Socket): Promise<void> {
setTimeout(async () => {
try { try {
const buddies = document.getElementById('div-buddies') as HTMLDivElement; const buddies = document.getElementById('div-buddies') as HTMLDivElement;
const loggedIn = isLoggedIn(); const loggedIn = isLoggedIn();
@ -224,23 +197,22 @@ async function connected(socket: Socket): Promise<void> {
let oldUser = localStorage.getItem("oldName") ?? ""; let oldUser = localStorage.getItem("oldName") ?? "";
console.log('%coldUser:',color.yellow, oldUser); console.log('%coldUser:',color.yellow, oldUser);
if (loggedIn?.name === undefined) {console.log('');return ;} if (loggedIn?.name === undefined) {console.log('');return ;}
setTimeout(() => {
oldUser = loggedIn.name ?? ""; oldUser = loggedIn.name ?? "";
}, 0); // const res = await client.guestLogin();
// const res = await client.guestLogin(); let user = await updateUser();
let user = await updateUser(); console.log('%cUser?name:',color.yellow, user?.name);
console.log('%cUser?name:',color.yellow, user?.name); localStorage.setItem("oldName", oldUser);
localStorage.setItem("oldName", oldUser); buddies.textContent = "";
buddies.textContent = ""; socket.emit('list', {
socket.emit('list', { oldUser: oldUser,
oldUser: oldUser, user: user?.name,
user: user?.name, });
}); } catch (e) {
} catch (e) { console.error("Login error:", e);
console.error("Login error:", e); showError('Failed to login: Unknown error');
showError('Failed to login: Unknown error'); }
} }, 16);
}; };
async function whoami(socket: Socket) { async function whoami(socket: Socket) {
try { try {
@ -270,30 +242,6 @@ async function whoami(socket: Socket) {
} }
}; };
// async function openProfilePopup(profil: ClientProfil) {
// const modalname = document.getElementById("modal-name") ?? null;
// if (modalname)
// modalname.innerHTML = `
// <div class="profile-info">
// <div-profil-name id="profilName"> Profil of ${profil.user} </div>
// <div-login-name id="loginName"> Login Name: '${profil.loginName ?? 'Guest'}' </div>
// </br>
// <div-login-name id="loginName"> Login ID: '${profil.userID ?? ''}' </div>
// </br>
// <button id="popup-b-clear" class="btn-style popup-b-clear">Clear Text</button>
// <button id="popup-b-invite" class="btn-style popup-b-invite">U Game ?</button>
// <button id="popup-b-block" class="btn-style popup-b-block">Block User</button>
// <div id="profile-about">About: '${profil.text}' </div>
// </div>
// `;
// const profilList = document.getElementById("profile-modal") ?? null;
// if (profilList)
// profilList.classList.remove("hidden");
// // The popup now exists → attach the event
// }
let count = 0; let count = 0;
function incrementCounter(): number { function incrementCounter(): number {
count += 1; count += 1;
@ -304,12 +252,12 @@ async function openMessagePopup(message: string) {
const modalmessage = document.getElementById("modal-message") ?? null; const modalmessage = document.getElementById("modal-message") ?? null;
if(!message) return if(!message) return
const obj:any = JSON.parse(message); const obj:string = JSON.parse(message);
if (modalmessage) { if (modalmessage) {
const messageElement = document.createElement("div"); const messageElement = document.createElement("div");
messageElement.innerHTML = ` messageElement.innerHTML = `
<div class="profile-info"> <div class="profile-info"
<div id="profile-about">Next Game Message ${incrementCounter()}: ${obj.link}</div> <div id="profile-about">Next Game Message ${incrementCounter()}: ${obj}</div>
</div>`; </div>`;
modalmessage.appendChild(messageElement); modalmessage.appendChild(messageElement);
modalmessage.scrollTop = modalmessage.scrollHeight; modalmessage.scrollTop = modalmessage.scrollHeight;
@ -328,6 +276,8 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
let socket = getSocket(); let socket = getSocket();
let blockMessage: boolean;
setTimeout(async () => {
// Listen for the 'connect' event // Listen for the 'connect' event
socket.on("connect", async () => { socket.on("connect", async () => {
@ -336,6 +286,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
await waitSocketConnected(socket); await waitSocketConnected(socket);
console.log("I AM Connected to the server:", socket.id); console.log("I AM Connected to the server:", socket.id);
const user = getUser()?.name; const user = getUser()?.name;
const userID = getUser()?.id;
// Ensure we have a user AND socket is connected // Ensure we have a user AND socket is connected
if (!user || !socket.connected) return; if (!user || !socket.connected) return;
const message = { const message = {
@ -347,6 +298,8 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
text: " has Just ARRIVED in the chat", text: " has Just ARRIVED in the chat",
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
SenderID: userID,
}; };
socket.emit('message', JSON.stringify(message)); socket.emit('message', JSON.stringify(message));
const messageElement = document.createElement("div"); const messageElement = document.createElement("div");
@ -354,6 +307,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
systemWindow.appendChild(messageElement); systemWindow.appendChild(messageElement);
systemWindow.scrollTop = systemWindow.scrollHeight; systemWindow.scrollTop = systemWindow.scrollHeight;
}); });
}, 0);
// Listen for messages from the server "MsgObjectServer" // Listen for messages from the server "MsgObjectServer"
socket.on("MsgObjectServer", (data: { message: ClientMessage}) => { socket.on("MsgObjectServer", (data: { message: ClientMessage}) => {
@ -362,11 +316,16 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
const chatWindow = document.getElementById("t-chatbox") as HTMLDivElement; const chatWindow = document.getElementById("t-chatbox") as HTMLDivElement;
const bconnected = document.getElementById('b-help') as HTMLButtonElement; const bconnected = document.getElementById('b-help') as HTMLButtonElement;
console.log('UserSender:', data.message.SenderUserID);
console.log('User:', getUser()?.id);
if (bconnected) { if (bconnected) {
connected(socket); connected(socket);
} }
console.log('stahe eeee :', blockMessage);
if (chatWindow && data.message.destination === "") { if (chatWindow && data.message.destination === "" && !blockMessage) {
const messageElement = document.createElement("div"); const messageElement = document.createElement("div");
messageElement.textContent = `${data.message.user}: ${data.message.text}`; messageElement.textContent = `${data.message.user}: ${data.message.text}`;
chatWindow.appendChild(messageElement); chatWindow.appendChild(messageElement);
@ -397,7 +356,13 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
}); });
socket.on('profilMessage', (profil: ClientProfil) => { socket.on('profilMessage', (profil: ClientProfil) => {
profil.SenderID = getUser()?.id ?? "";
profil.SenderName = getUser()?.name ?? "";
openProfilePopup(profil); openProfilePopup(profil);
socket.emit('isBlockdBtn', profil);
console.log(`DEBUG LOG: userId:${profil.userID}: senderID${profil.SenderID}' senderID:${getUser()?.id}`);
console.log(`DEBUG LOG: user:${profil.user}: sender:${profil.SenderName}' senderID:${getUser()?.name}`);
socket.emit('check_Block_button', profil);
actionBtnPopUpClear(profil, socket); actionBtnPopUpClear(profil, socket);
actionBtnPopUpInvite(profil, socket); actionBtnPopUpInvite(profil, socket);
actionBtnPopUpBlock(profil, socket); actionBtnPopUpBlock(profil, socket);
@ -414,15 +379,28 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
socket.on('blockUser', (blocked: ClientProfil) => { socket.on('blockUser', (blocked: ClientProfil) => {
let icon = '⛔'; let icon = '⛔';
const chatWindow = document.getElementById("t-chatbox") as HTMLDivElement; const chatWindow = document.getElementById("t-chatbox") as HTMLDivElement;
const messageElement = document.createElement("div"); const messageElement = document.createElement("div");
if (`${blocked.text}` === '\'I have un-blocked you\'' ) { icon = '💚'}; if (`${blocked.text}` === 'I have un-blocked you' ) {icon = '💚'};
messageElement.innerText =`${icon}${blocked.SenderName}: ${blocked.text}`; messageElement.innerText =`${icon}${blocked.SenderName}: ${blocked.text}`;
chatWindow.appendChild(messageElement); chatWindow.appendChild(messageElement);
chatWindow.scrollTop = chatWindow.scrollHeight; chatWindow.scrollTop = chatWindow.scrollHeight;
}); });
socket.on('blockBtn', (data: blockedUnBlocked) => {
const blockUserBtn = document.querySelector("#popup-b-block");
if (blockUserBtn) {
console.log(' =================== >>> User State:', data.userState);
console.log(' =================== >>> UserTarget:', data.userTarget);
console.log(' =================== >>> By:', data.by);
let message = "";
if (data.userState === "block") {message = "un-block", blockMessage = true} else{message = "block", blockMessage = false}
blockUserBtn.textContent = message;
}
});
socket.on('logout', () => { socket.on('logout', () => {
@ -441,11 +419,10 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
let toggle = false let toggle = false
window.addEventListener("focus", async () => { window.addEventListener("focus", async () => {
//nst bwhoami = document.getElementById('b-whoami') as HTMLButtonElement;
setTimeout(() => { setTimeout(() => {
connected(socket); connected(socket);
}, 0); }, 16);
if (window.location.pathname === '/app/chat') { if (window.location.pathname === '/app/chat') {
console.log('%cWindow is focused on /chat:' + socket.id, color.green); console.log('%cWindow is focused on /chat:' + socket.id, color.green);
if (socket.id) { if (socket.id) {
@ -462,10 +439,6 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
toggle = false; toggle = false;
}); });
// setInterval(async () => {
// //connected(socket);
// },10000); // every 10 sec
socket.on('listBud', async (myBuddies: string[]) => { socket.on('listBud', async (myBuddies: string[]) => {
const buddies = document.getElementById('div-buddies') as HTMLDivElement; const buddies = document.getElementById('div-buddies') as HTMLDivElement;
console.log('%cList buddies connected ',color.yellow, myBuddies); console.log('%cList buddies connected ',color.yellow, myBuddies);
@ -553,6 +526,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
break; break;
default: default:
const user = getUser()?.name; const user = getUser()?.name;
const userID = getUser()?.id;
// Ensure we have a user AND socket is connected // Ensure we have a user AND socket is connected
if (!user || !socket.connected) return; if (!user || !socket.connected) return;
const message = { const message = {
@ -564,6 +538,8 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
text: msgCommand[1], text: msgCommand[1],
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
SenderID: userID,
}; };
//socket.emit('MsgObjectServer', message); //socket.emit('MsgObjectServer', message);
socket.emit('privMessage', JSON.stringify(message)); socket.emit('privMessage', JSON.stringify(message));
@ -609,5 +585,6 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
}); });
} }
} }
}; };
addRoute('/chat', handleChat, { bypass_auth: true }); addRoute('/chat', handleChat);

View file

@ -1,9 +1,18 @@
export type ClientMessage = { export type ClientMessage = {
command: string command: string
destination: string; destination: string;
type: string,
user: string; user: string;
userID: string,
token: string
frontendUserName: string,
frontendUser: string,
text: string; text: string;
SenderWindowID: string; SenderWindowID: string,
SenderUserName: string,
SenderUserID: string,
timestamp: number,
Sendertext: string,
}; };
@ -18,6 +27,23 @@ export type ClientProfil = {
timestamp: number, timestamp: number,
SenderWindowID:string, SenderWindowID:string,
SenderName: string, SenderName: string,
SenderID: string,
Sendertext: string, Sendertext: string,
innerHtml?: string, innerHtml?: string,
}; };
export type obj =
{
command: string,
destination: string,
type: string,
user: string,
frontendUserName: string,
frontendUser: string,
token: string,
text: string,
timestamp: number,
SenderWindowID: string,
Sendertext: string,
};

View file

@ -9,7 +9,7 @@ import { Server, Socket } from 'socket.io';
import type { User } from '@shared/database/mixin/user'; import type { User } from '@shared/database/mixin/user';
import type { BlockedData } from '@shared/database/mixin/blocked'; import type { BlockedData } from '@shared/database/mixin/blocked';
import { broadcast } from './broadcast'; import { broadcast } from './broadcast';
import type { ClientProfil, ClientMessage } from './chat_types'; import type { ClientProfil, ClientMessage, obj } from './chat_types';
import { sendPrivMessage } from './sendPrivMessage'; import { sendPrivMessage } from './sendPrivMessage';
import { sendBlocked } from './sendBlocked'; import { sendBlocked } from './sendBlocked';
import { sendInvite } from './sendInvite'; import { sendInvite } from './sendInvite';
@ -36,22 +36,18 @@ declare const __SERVICE_NAME: string;
// key = socket, value = clientname // key = socket, value = clientname
interface ClientInfo { interface ClientInfo {
user: string; user: string;
socket: string
lastSeen: number; lastSeen: number;
} }
// function setAboutPlayer(about: string): string {
// if (!about) {
// about = 'Player is good Shape - This is a default description';
// }
// return about;
// };
// function setGameLink(link: string): string { export type blockedUnBlocked =
// if (!link) { {
// link = '<a href=\'https://google.com\' style=\'color: blue; text-decoration: underline; cursor: pointer;\'>Click me</a>'; userState: string,
// } userTarget: string,
// return link; by: string,
// }; };
export const clientChat = new Map<string, ClientInfo>(); export const clientChat = new Map<string, ClientInfo>();
@ -107,13 +103,47 @@ declare module 'fastify' {
client_left: (userName: string, why: string) => void; client_left: (userName: string, why: string) => void;
list: (oldUser: string, user: string) => void; list: (oldUser: string, user: string) => void;
updateClientName: (oldUser: string, user: string) => void; updateClientName: (oldUser: string, user: string) => void;
blockBtn: (data: blockedUnBlocked) => void;
isBlockdBtn: (data: blockedUnBlocked) => void;
check_Block_button: (data: blockedUnBlocked) => void;
}>; }>;
} }
} }
/**
* function get the object user in an array of users[] by name
* @param users
* @param name
* @returns
*/
function getUserById(users: User[], id: string) {
return users.find(user => user.id === id) || null;
};
function isUser_BlockedBy_me(fastify: FastifyInstance, blockedBy_Id : string, isBlocked_Id: string): string {
const users: User[] = fastify.db.getAllUsers() ?? [];
if (!users) return '';
const UserToBlock: User | null = getUserById(users, `${isBlocked_Id}`);
const UserAskingToBlock: User | null = getUserById(users, `${blockedBy_Id}`);
if (!UserToBlock) {
console.log(color.blue, `'User: ${UserAskingToBlock?.id} has not blocked' ${isBlocked_Id}`);
return '';
};
if (!UserAskingToBlock) {
console.log(color.blue, `'User: ${UserToBlock?.id} has not blocked by' ${blockedBy_Id}`);
return '';
};
const usersBlocked: BlockedData[] = fastify.db.getAllBlockedUsers() ?? [];
const userAreBlocked: boolean = isBlocked(UserAskingToBlock, UserToBlock, usersBlocked);
if (userAreBlocked) {
console.log(color.yellow, `'User :${UserAskingToBlock.name}) Hhas UN blocked ${UserToBlock.name}`);
return UserAskingToBlock.name;
}
console.log(color.blue, `'User :${UserAskingToBlock.name}) has BBBblocked ${UserToBlock.name}`);
return '';
};
async function onReady(fastify: FastifyInstance) { async function onReady(fastify: FastifyInstance) {
// shows address for connection au server transcendance // shows address for connection au server transcendance
const session = process.env.SESSION_MANAGER ?? ''; const session = process.env.SESSION_MANAGER ?? '';
if (session) { if (session) {
@ -122,45 +152,36 @@ async function onReady(fastify: FastifyInstance) {
console.log(color.yellow, 'Connect at : https://' + machineName + ':8888/app/login'); console.log(color.yellow, 'Connect at : https://' + machineName + ':8888/app/login');
} }
fastify.io.on('connection', (socket: Socket) => { fastify.io.on('connection', (socket: Socket) => {
socket.on('message', (message: string) => { socket.on('message', (message: string) => {
// console.info(color.blue, 'DEBUG LOG: Socket connected!', color.reset, socket.id); // console.info(color.blue, 'DEBUG LOG: Socket connected!', color.reset, socket.id);
// console.log( color.blue, 'DEBUG LOG: Received message from client', color.reset, message); // console.log( color.blue, 'DEBUG LOG: Received message from client', color.reset, message);
const obj: ClientMessage = JSON.parse(message) as ClientMessage; const obj: ClientMessage = JSON.parse(message) as ClientMessage;
clientChat.set(socket.id, { user: obj.user, lastSeen: Date.now() }); clientChat.set(socket.id, { user: obj.user, socket: socket.id, lastSeen: Date.now() });
// console.log(color.green, 'DEBUG LOG: Message from client', color.reset, `Sender: login name: ${obj.user} - windowID ${obj.SenderWindowID} - text message: ${obj.text}`); // console.log(color.green, 'DEBUG LOG: Message from client', color.reset, `Sender: login name: ${obj.user} - windowID ${obj.SenderWindowID} - text message: ${obj.text}`);
socket.emit('welcome', { msg: 'Welcome to the chat! : ' }); socket.emit('welcome', { msg: 'Welcome to the chat! : ' });
// Send object directly — DO NOT wrap it in a string // Send object directly — DO NOT wrap it in a string
broadcast(fastify, obj, obj.SenderWindowID); broadcast(fastify, obj, obj.SenderWindowID);
// const users: User[] = fastify.db.getAllUsers() ?? [];
// console.log('DEBUG: senderWindow :', getUserById(users, obj.SenderUserID)?.name);
// fastify.io.fetchSockets().then((sockets) => {
// for (const socket of sockets) {
// const clientInfo = clientChat.get(socket.id);
// if (!clientInfo?.user) {
// console.log(color.yellow, `Skipping socket ${socket.id} (no user found)`);
// continue;
// }
// console.log('DEBUG: UserIDWindow :', getUserByName(users, clientInfo.user)?.id);
// const IDUser = getUserByName(users, clientInfo.user)?.id;
// console.log(filter_Blocked_user(fastify, obj, IDUser?? ""));
// }
// });
// console.log(color.red, 'DEBUG LOG: connected in the Chat :', connectedUser(fastify.io), color.reset); // console.log(color.red, 'DEBUG LOG: connected in the Chat :', connectedUser(fastify.io), color.reset);
}); });
nextGame_SocketListener(fastify, socket); nextGame_SocketListener(fastify, socket);
list_SocketListener(fastify, socket); list_SocketListener(fastify, socket);
// socket.on('list', (object) => {
// const userFromFrontend = object || null;
// const client = clientChat.get(socket.id) || null;
// //console.log(color.red, 'DEBUG LOG: list activated', userFromFrontend, color.reset, socket.id);
// if (userFromFrontend.oldUser !== userFromFrontend.user) {
// //console.log(color.red, 'DEBUG LOG: list activated', userFromFrontend.oldUser, color.reset);
// // if (client?.user === null) {
// // console.log('ERROR: clientName is NULL');
// // return;
// // };
// if (client) {
// client.user = userFromFrontend.user;
// }
// }
// connectedUser(fastify.io, socket.id);
// });
socket.on('updateClientName', (object) => { socket.on('updateClientName', (object) => {
const userFromFrontend = object || null; const userFromFrontend = object || null;
const client = clientChat.get(socket.id) || null; const client = clientChat.get(socket.id) || null;
@ -183,16 +204,22 @@ async function onReady(fastify: FastifyInstance) {
const clientName = clientInfo?.user; const clientName = clientInfo?.user;
if (!clientName) return; if (!clientName) return;
console.log(color.green, `Client logging out: ${clientName} (${socket.id})`); // console.log(color.green, `Client logging out: ${clientName} (${socket.id})`);
const obj = { const obj: ClientMessage = {
command: '', command: '',
destination: 'system-info', destination: 'system-info',
type: 'chat' as const, type: 'chat' as const,
user: clientName, user: clientName,
token: '', token: '',
text: 'LEFT the chat', text: 'LEFT the chat',
timestamp: Date.now(), frontendUserName: '',
SenderWindowID: socket.id, frontendUser: '',
timestamp: Date.now(),
SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID: '',
}; };
broadcast(fastify, obj, socket.id); broadcast(fastify, obj, socket.id);
// Optional: remove from map // Optional: remove from map
@ -203,23 +230,25 @@ async function onReady(fastify: FastifyInstance) {
socket.on('disconnecting', (reason) => { socket.on('disconnecting', (reason) => {
const clientName = clientChat.get(socket.id)?.user || null; const clientName = clientChat.get(socket.id)?.user || null;
console.log( // console.log(color.green, `Client disconnecting: ${clientName} (${socket.id}) reason:`, reason,);
color.green,
`Client disconnecting: ${clientName} (${socket.id}) reason:`,
reason,
);
if (reason === 'transport error') return; if (reason === 'transport error') return;
if (clientName !== null) { if (clientName !== null) {
const obj = { const obj: ClientMessage = {
command: '', command: '',
destination: 'system-info', destination: 'system-info',
type: 'chat', type: 'chat',
user: clientName, user: clientName,
token: '', token: '',
text: 'LEFT the chat', text: 'LEFT the chat',
frontendUserName: '',
frontendUser: '',
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID: '',
}; };
broadcast(fastify, obj, obj.SenderWindowID); broadcast(fastify, obj, obj.SenderWindowID);
@ -227,24 +256,25 @@ async function onReady(fastify: FastifyInstance) {
}); });
socket.on('client_left', (data) => { socket.on('client_left', (data) => {
void data;
const clientName = clientChat.get(socket.id)?.user || null; const clientName = clientChat.get(socket.id)?.user || null;
const leftChat = data || null; // const leftChat = data || null; console.log(color.green, `Left the Chat User: ${clientName} id Socket: ${socket.id} reason:`, leftChat.why,);
console.log(
color.green,
`Left the Chat User: ${clientName} id Socket: ${socket.id} reason:`,
leftChat.why,
);
if (clientName !== null) { if (clientName !== null) {
const obj = { const obj: ClientMessage = {
command: '', command: '',
destination: 'system-info', destination: 'system-info',
type: 'chat', type: 'chat',
user: clientName, user: clientName,
token: '', token: '',
text: 'LEFT the chat but the window is still open', text: 'LEFT the chat but the window is still open',
frontendUserName: '',
frontendUser: '',
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID: '',
}; };
// console.log(color.blue, 'DEBUG LOG: BROADCASTS OUT :', obj.SenderWindowID); // console.log(color.blue, 'DEBUG LOG: BROADCASTS OUT :', obj.SenderWindowID);
@ -257,42 +287,43 @@ async function onReady(fastify: FastifyInstance) {
socket.on('privMessage', (data) => { socket.on('privMessage', (data) => {
const clientName: string = clientChat.get(socket.id)?.user || ''; const clientName: string = clientChat.get(socket.id)?.user || '';
const prvMessage: ClientMessage = JSON.parse(data) || ''; const prvMessage: ClientMessage = JSON.parse(data) || '';
console.log( // console.log(color.blue, `DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target Name:`, prvMessage.command,);
color.blue,
`DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target Name:`,
prvMessage.command,
);
if (clientName !== null) { if (clientName !== null) {
const obj = { const obj: ClientMessage = {
command: prvMessage.command, command: prvMessage.command,
destination: 'privateMsg', destination: 'privateMsg',
type: 'chat', type: 'chat',
user: clientName, user: clientName,
token: '', token: '',
text: prvMessage.text, text: prvMessage.text,
frontendUserName: '',
frontendUser: '',
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID:'',
}; };
// console.log(color.blue, 'DEBUG LOG: PRIV MESSAGE OUT :', obj.SenderWindowID); // console.log(color.blue, 'DEBUG LOG: PRIV MESSAGE OUT :', obj.SenderWindowID);
sendPrivMessage(fastify, obj, obj.SenderWindowID); sendPrivMessage(fastify, obj, obj.SenderWindowID);
// clientChat.delete(obj.user); // clientChat.delete(obj.user);
} }
}); });
socket.on('profilMessage', async (data: string) => { socket.on('profilMessage', async (data: string) => {
const clientName: string = clientChat.get(socket.id)?.user || ''; const clientName: string = clientChat.get(socket.id)?.user || '';
const profilMessage: ClientMessage = JSON.parse(data) || ''; const profilMessage: ClientMessage = JSON.parse(data) || '';
const users: User[] = fastify.db.getAllUsers() ?? []; // const users: User[] = fastify.db.getAllUsers() ?? [];
// console.log(color.yellow, 'DEBUG LOG: ALL USERS EVER CONNECTED:', users); // console.log(color.yellow, 'DEBUG LOG: ALL USERS EVER CONNECTED:', users);
// console.log(color.blue, `DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target profil:`, profilMessage.user); // console.log(color.blue, `DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target profil:`, profilMessage.user);
const profile: ClientProfil = await makeProfil(fastify, profilMessage.user, socket); const profile: ClientProfil = await makeProfil(fastify, profilMessage.user, socket);
if (clientName !== null) { if (clientName !== null) {
const testuser: User | null = getUserByName(users, profilMessage.user); // const testuser: User | null = getUserByName(users, profilMessage.user);
console.log(color.yellow, 'user:', testuser?.name ?? 'Guest'); // console.log(color.yellow, 'user:', testuser?.name ?? 'Guest');
console.log(color.blue, 'DEBUG - profil message MESSAGE OUT :', profile.SenderWindowID); // console.log(color.blue, 'DEBUG - profil message MESSAGE OUT :', profile.SenderWindowID);
sendProfil(fastify, profile, profile.SenderWindowID); sendProfil(fastify, profile, profile.SenderWindowID);
// clientChat.delete(obj.user); // clientChat.delete(obj.user);
} }
}); });
@ -309,6 +340,39 @@ async function onReady(fastify: FastifyInstance) {
} }
}); });
socket.on('isBlockdBtn', async (data: ClientProfil) => {
const profilBlock: ClientProfil = data || '';
const users: User[] = fastify.db.getAllUsers() ?? [];
const UserToBlock: User | null = getUserByName(users, `${profilBlock.user}`);
const UserAskingToBlock: User | null = getUserByName(users, `${profilBlock.SenderName}`);
// console.log(color.yellow, `user to block: ${profilBlock.user}`);
// console.log(color.yellow, UserToBlock);
// console.log(color.yellow, `user Asking to block: ${profilBlock.SenderName}`);
// console.log(color.yellow, UserAskingToBlock);
// console.log(color.red, clientName);
if (!UserAskingToBlock || !UserToBlock) return;
if (isUser_BlockedBy_me(fastify, UserAskingToBlock!.id, UserToBlock!.id)) {
const message: blockedUnBlocked = {
userState: 'block',
userTarget: UserToBlock.name,
by: UserAskingToBlock.name,
};
socket.emit('blockBtn', message);
}
else {
const message: blockedUnBlocked = {
userState: 'un-block',
userTarget: UserToBlock.name,
by: UserAskingToBlock.name,
};
socket.emit('blockBtn', message);
}
});
socket.on('blockUser', async (data: string) => { socket.on('blockUser', async (data: string) => {
const clientName: string = clientChat.get(socket.id)?.user || ''; const clientName: string = clientChat.get(socket.id)?.user || '';
const profilBlock: ClientProfil = JSON.parse(data) || ''; const profilBlock: ClientProfil = JSON.parse(data) || '';
@ -316,34 +380,52 @@ async function onReady(fastify: FastifyInstance) {
const UserToBlock: User | null = getUserByName(users, `${profilBlock.user}`); const UserToBlock: User | null = getUserByName(users, `${profilBlock.user}`);
const UserAskingToBlock: User | null = getUserByName(users, `${profilBlock.SenderName}`); const UserAskingToBlock: User | null = getUserByName(users, `${profilBlock.SenderName}`);
console.log(color.yellow, `user to block: ${profilBlock.user}`); // console.log(color.yellow, `user to block: ${profilBlock.user}`);
console.log(color.yellow, UserToBlock); // console.log(color.yellow, UserToBlock);
console.log(color.yellow, `user Asking to block: ${profilBlock.SenderName}`); // console.log(color.yellow, `user Asking to block: ${profilBlock.SenderName}`);
console.log(color.yellow, UserAskingToBlock); // console.log(color.yellow, UserAskingToBlock);
// console.log(color.red, clientName);
const usersBlocked: BlockedData[] = fastify.db.getAllBlockedUsers() ?? []; const usersBlocked: BlockedData[] = fastify.db.getAllBlockedUsers() ?? [];
if (!UserAskingToBlock || !UserToBlock || !usersBlocked) return; if (!UserAskingToBlock || !UserToBlock || !usersBlocked) return;
const userAreBlocked: boolean = isBlocked(UserAskingToBlock, UserToBlock, usersBlocked); const userAreBlocked: boolean = isBlocked(UserAskingToBlock, UserToBlock, usersBlocked);
if (isUser_BlockedBy_me(fastify, UserAskingToBlock!.id, UserToBlock!.id)) {
const message: blockedUnBlocked = {
userState: 'un-block',
userTarget: '',
by: '',
};
socket.emit('blockBtn', message);
}
else {
const message: blockedUnBlocked = {
userState: 'block',
userTarget: UserToBlock.name,
by: UserAskingToBlock.name,
};
socket.emit('blockBtn', message);
}
if (userAreBlocked) { if (userAreBlocked) {
console.log(color.green, 'Both users are blocked as requested'); // console.log(color.green, 'Both users are blocked as requested');
// return true; // or any other action you need to take // return true; // or any other action you need to take
// console.log(color.red, 'ALL BLOCKED USERS:', usersBlocked);
console.log(color.red, 'ALL BLOCKED USERS:', usersBlocked);
fastify.db.removeBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id); fastify.db.removeBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id);
const usersBlocked2 = fastify.db.getAllBlockedUsers(); // const usersBlocked2 = fastify.db.getAllBlockedUsers();
console.log(color.green, 'remove ALL BLOCKED USERS:', usersBlocked2); // console.log(color.green, 'remove A BLOCKED USER:', usersBlocked2);
if (clientName !== null) { if (clientName !== null) {
const blockedMessage = 'I have un-blocked you'; const blockedMessage = 'I have un-blocked you';
if (clientName !== null) { if (clientName !== null) {
const obj = { const obj: obj = {
command: 'message', command: 'message',
destination: 'privateMsg', destination: 'privateMsg',
type: 'chat', type: 'chat',
user: clientName, user: clientName,
token: '', token: '',
text: '', text: '',
frontendUserName: '',
frontendUser: '',
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: 'You have un-blocked', Sendertext: 'You have un-blocked',
@ -354,20 +436,28 @@ async function onReady(fastify: FastifyInstance) {
} }
// profilBlock.Sendertext = `'You have un-blocked '`; // profilBlock.Sendertext = `'You have un-blocked '`;
sendBlocked(fastify, blockedMessage, profilBlock); sendBlocked(fastify, blockedMessage, profilBlock);
// const message: blockedUnBlocked =
// {
// userState: "un-block",
// userTarget: "",
// by: "",
// };
// socket.emit('blockBtn', message);
} }
} }
else { else {
console.log(color.red, 'The users are not blocked in this way'); // console.log(color.red, 'The users are not blocked in this way');
console.log(color.red, 'ALL BLOCKED USERS:', usersBlocked); // console.log(color.red, 'ALL BLOCKED USERS:', usersBlocked);
fastify.db.addBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id); fastify.db.addBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id);
const usersBlocked2 = fastify.db.getAllBlockedUsers(); // const usersBlocked2 = fastify.db.getAllBlockedUsers();
console.log(color.green, 'ALL BLOCKED USERS:', usersBlocked2); // console.log(color.green, 'ALL BLOCKED USERS:', usersBlocked2);
if (clientName !== null) { if (clientName !== null) {
const blockedMessage = 'I have blocked you'; const blockedMessage = 'I have blocked you';
profilBlock.Sendertext = 'You have blocked '; profilBlock.Sendertext = 'You have blocked ';
if (clientName !== null) { if (clientName !== null) {
const obj = { const obj: obj = {
command: 'message', command: 'message',
destination: 'privateMsg', destination: 'privateMsg',
type: 'chat', type: 'chat',
@ -375,6 +465,8 @@ async function onReady(fastify: FastifyInstance) {
token: '', token: '',
text: '', text: '',
timestamp: Date.now(), timestamp: Date.now(),
frontendUserName: '',
frontendUser: '',
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: 'You have blocked', Sendertext: 'You have blocked',
}; };
@ -383,6 +475,14 @@ async function onReady(fastify: FastifyInstance) {
// clientChat.delete(obj.user); // clientChat.delete(obj.user);
} }
sendBlocked(fastify, blockedMessage, profilBlock); sendBlocked(fastify, blockedMessage, profilBlock);
// const message: blockedUnBlocked =
// {
// userState: "block",
// userTarget: UserToBlock.name,
// by: UserAskingToBlock.name,
// };
// socket.emit('blockBtn', message);
} }
} }
}); });
@ -411,12 +511,12 @@ async function onReady(fastify: FastifyInstance) {
// client.user = clientName; // client.user = clientName;
// } // }
} }
console.log( // console.log(
color.green, // color.green,
`Client entered the Chat: ${clientName} (${socket.id})`, // `Client entered the Chat: ${clientName} (${socket.id})`,
); // );
if (clientName !== null) { if (clientName !== null) {
const obj = { const obj: ClientMessage = {
command: '', command: '',
destination: 'system-info', destination: 'system-info',
type: 'chat', type: 'chat',
@ -427,6 +527,10 @@ async function onReady(fastify: FastifyInstance) {
text: text, text: text,
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID: '',
}; };
broadcast(fastify, obj, obj.SenderWindowID); broadcast(fastify, obj, obj.SenderWindowID);
} }

View file

@ -1,8 +1,37 @@
import type { ClientMessage } from './chat_types'; import type { ClientMessage } from './chat_types';
import { clientChat, color } from './app'; import { clientChat } from './app';
import { FastifyInstance } from 'fastify'; import { FastifyInstance } from 'fastify';
import { getUserByName } from './getUserByName';
import type { User } from '@shared/database/mixin/user';
type BlockRelation = {
blocked: string;
blocker: string;
};
function whoBlockedMe(fastify: FastifyInstance, myID: string): BlockRelation [] {
const usersBlocked =
fastify.db.getAllBlockedUsers() ?? [];
return usersBlocked
.filter(entry => entry.blocked === myID)
.map(entry => ({
blocked: entry.user,
blocker: entry.blocked,
}));
}
export function broadcast(fastify: FastifyInstance, data: ClientMessage, sender?: string) { export function broadcast(fastify: FastifyInstance, data: ClientMessage, sender?: string) {
const AllusersBlocked: User[] = fastify.db.getAllUsers() ?? [];
// console.log(color.yellow, 'me:', getUserByName(AllusersBlocked, data.user)?.id)
const UserID = getUserByName(AllusersBlocked, data.user)?.id ?? '';
const list:BlockRelation[] = whoBlockedMe(fastify, UserID);
const blockers = list.map(read => read.blocker);
const blocked = list.map(read => read.blocked);
console.log('All blockers:', blockers);
console.log('All blocked:', blocked);
// console.log(color.yellow, 'list:', list)
fastify.io.fetchSockets().then((sockets) => { fastify.io.fetchSockets().then((sockets) => {
for (const socket of sockets) { for (const socket of sockets) {
// Skip sender's own socket // Skip sender's own socket
@ -10,10 +39,24 @@ export function broadcast(fastify: FastifyInstance, data: ClientMessage, sender?
// Get client name from map // Get client name from map
const clientInfo = clientChat.get(socket.id); const clientInfo = clientChat.get(socket.id);
if (!clientInfo?.user) { if (!clientInfo?.user) {
console.log(color.yellow, `Skipping socket ${socket.id} (no user found)`); // console.log(color.yellow, `Skipping socket ${socket.id} (no user found)`);
continue; continue;
} }
// console.log('BLOCKED MAYBE', getUserById(sender));
// console.log('TARGET',socket.id );
// Emit structured JSON object // Emit structured JSON object
// const UserID = getUserByName(AllusersBlocked, data.user)?.name ?? "";
// const UserByID = getUserByName(AllusersBlocked, data.user)?.id ?? "";
// console.log(color.blue, 'Asking:', UserID);
// console.log(color.blue, 'Asking ID:', UserByID);
console.log('Blocked list:', list);
// console.log('Sender ID:', UserByID);
// if (!list.includes(UserByID)) {
// console.log('TRUE → sender NOT blocked');
// } else {
// console.log('FALSE → sender IS blocked');
// }
// if (list.filter(entry => entry.blocker === UserByID)) continue;
socket.emit('MsgObjectServer', { message: data }); socket.emit('MsgObjectServer', { message: data });
// Debug logs // Debug logs
// console.log(color.green, `'DEBUG LOG: Broadcast to:', ${data.command} message: ${data.text}`); // console.log(color.green, `'DEBUG LOG: Broadcast to:', ${data.command} message: ${data.text}`);

View file

@ -1,11 +1,21 @@
export type ClientMessage = { export type ClientMessage = {
command: string command: string
destination: string; destination: string;
type: string,
user: string; user: string;
userID: string,
token: string
frontendUserName: string,
frontendUser: string,
text: string; text: string;
SenderWindowID: string; SenderWindowID: string,
SenderUserName: string,
SenderUserID: string,
timestamp: number,
Sendertext: string,
}; };
export type ClientProfil = { export type ClientProfil = {
command: string, command: string,
destination: string, destination: string,
@ -17,7 +27,24 @@ export type ClientProfil = {
timestamp: number, timestamp: number,
SenderWindowID:string, SenderWindowID:string,
SenderName: string, SenderName: string,
SenderID: string,
Sendertext: string, Sendertext: string,
innerHtml?: string, innerHtml?: string,
}; };
export type obj =
{
command: string,
destination: string,
type: string,
user: string,
frontendUserName: string,
frontendUser: string,
token: string,
text: string,
timestamp: number,
SenderWindowID: string,
Sendertext: string,
};

8
src/chat/src/color.ts Normal file
View file

@ -0,0 +1,8 @@
// colors for console.log
export const color = {
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
reset: '\x1b[0m',
};

View file

@ -0,0 +1,33 @@
import type { ClientMessage } from './chat_types';
import { FastifyInstance } from 'fastify';
import type { User } from '@shared/database/mixin/user';
import { getUserById } from './getUserById';
import { isUser_BlockedBy_me } from './isUser_BlockedBy_me';
/**
* function to check if blocked or not - checks with ID
* @param fastify
* @param data
* @returns true or false - true if blocked user by a user
*/
export function filter_Blocked_user(fastify: FastifyInstance, data: ClientMessage, id: string): boolean {
const users: User[] = fastify.db.getAllUsers() ?? [];
const UserToBlock: string = id;
const UserAskingToBlock: User | null = getUserById(users, `${data.SenderUserID}`);
if (!UserAskingToBlock) {
// console.log('SOMETHING NULL', data);
// console.log('UsetToBlock', UserToBlock?.id);
// console.log('UsetToBlock', UserToBlock?.name);
// console.log('UsetAskingToBlock', UserAskingToBlock?.id);
// console.log('UsetAskingToBlock', UserAskingToBlock?.name);
return false;
}
if (isUser_BlockedBy_me(fastify, UserAskingToBlock!.id, UserToBlock)) {
return true;
}
else {
return false;
}
}

View file

@ -0,0 +1,10 @@
import type { User } from '@shared/database/mixin/user';
/**
* function get the object user in an array of users[] by name
* @param users
* @param name
* @returns
*/
export function getUserById(users: User[], id: string) {
return users.find(user => user.id === id) || null;
};

View file

@ -0,0 +1,38 @@
import { FastifyInstance } from 'fastify';
import type { User } from '@shared/database/mixin/user';
import type { BlockedData } from '@shared/database/mixin/blocked';
import { isBlocked } from './isBlocked';
import { getUserById } from './getUserById';
import { color } from './color';
/**
* checks the Db for the two matching Ids
* @param fastify
* @param blockedBy_Id
* @param isBlocked_Id
* @returns Null if not blocked
*/
export function isUser_BlockedBy_me(fastify: FastifyInstance, blockedBy_Id : string, isBlocked_Id: string): string {
const users: User[] = fastify.db.getAllUsers() ?? [];
if (!users) return '';
const UserToBlock: User | null = getUserById(users, `${isBlocked_Id}`);
const UserAskingToBlock: User | null = getUserById(users, `${blockedBy_Id}`);
if (!UserToBlock) {
console.log(color.blue, `'User: ${UserAskingToBlock?.id} has not blocked' ${isBlocked_Id}`);
return '';
}
if (!UserAskingToBlock) {
console.log(color.blue, `'User: ${UserToBlock?.id} has not blocked by' ${blockedBy_Id}`);
return '';
}
const usersBlocked: BlockedData[] = fastify.db.getAllBlockedUsers() ?? [];
const userAreBlocked: boolean = isBlocked(UserAskingToBlock, UserToBlock, usersBlocked);
if (userAreBlocked) {
console.log(color.yellow, `'User :${UserAskingToBlock.name}) Hhas UN blocked ${UserToBlock.name}`);
return UserAskingToBlock.name;
}
console.log(color.blue, `'User :${UserAskingToBlock.name}) has BBBblocked ${UserToBlock.name}`);
return '';
};

View file

@ -35,6 +35,7 @@ export async function makeProfil(fastify: FastifyInstance, user: string, socket:
SenderWindowID: socket.id, SenderWindowID: socket.id,
SenderName: '', SenderName: '',
Sendertext: '', Sendertext: '',
SenderID: '',
innerHtml: '', innerHtml: '',
}; };
} }

View file

@ -1,6 +1,5 @@
import { FastifyPluginAsync } from 'fastify'; import { FastifyPluginAsync } from 'fastify';
import { Static, Type } from 'typebox'; import { Static, Type } from 'typebox';
import { broadcast } from '../broadcast';
export const ChatReq = Type.Object({ export const ChatReq = Type.Object({
message: Type.String(), message: Type.String(),
@ -19,7 +18,7 @@ const route: FastifyPluginAsync = async (fastify): Promise<void> => {
config: { requireAuth: false }, config: { requireAuth: false },
}, },
async function(req, res) { async function(req, res) {
broadcast(this, { command: '', destination: '', user: 'CMwaLeSever!!', text: req.body.message, SenderWindowID: 'server' }); // broadcast(this, { command: '', destination: '', user: 'CMwaLeSever!!', text: req.body.message, SenderWindowID: 'server' });
void res; void res;
}, },
); );