From f49505cc74e8ad0d3f2f6637f6aba2dbb2b612b6 Mon Sep 17 00:00:00 2001 From: NigeParis Date: Sat, 20 Dec 2025 16:29:05 +0100 Subject: [PATCH] Added and Fixed bug of Blocage a User in the Chat --- frontend/src/pages/chat/chat.ts | 2 +- src/chat/src/app.ts | 4 +- src/chat/src/broadcast.ts | 73 +++++++++++++++++---------------- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/frontend/src/pages/chat/chat.ts b/frontend/src/pages/chat/chat.ts index 868d149..32e3224 100644 --- a/frontend/src/pages/chat/chat.ts +++ b/frontend/src/pages/chat/chat.ts @@ -325,7 +325,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn connected(socket); } console.log('stahe eeee :', blockMessage); - if (chatWindow && data.message.destination === "" && !blockMessage) { + if (chatWindow && data.message.destination === "") { const messageElement = document.createElement("div"); messageElement.textContent = `${data.message.user}: ${data.message.text}`; chatWindow.appendChild(messageElement); diff --git a/src/chat/src/app.ts b/src/chat/src/app.ts index 3a392d1..f1dfc8f 100644 --- a/src/chat/src/app.ts +++ b/src/chat/src/app.ts @@ -392,8 +392,8 @@ async function onReady(fastify: FastifyInstance) { if (isUser_BlockedBy_me(fastify, UserAskingToBlock!.id, UserToBlock!.id)) { const message: blockedUnBlocked = { userState: 'un-block', - userTarget: '', - by: '', + userTarget: UserToBlock.name, + by: UserAskingToBlock.name, }; socket.emit('blockBtn', message); } diff --git a/src/chat/src/broadcast.ts b/src/chat/src/broadcast.ts index 58e2485..17e8e15 100644 --- a/src/chat/src/broadcast.ts +++ b/src/chat/src/broadcast.ts @@ -3,12 +3,28 @@ import { clientChat } from './app'; import { FastifyInstance } from 'fastify'; import { getUserByName } from './getUserByName'; import type { User } from '@shared/database/mixin/user'; +import { color } from './app'; type BlockRelation = { blocked: string; blocker: string; }; +function checkNamePair(list: BlockRelation[], name1: string, name2: string): (boolean) { + const matches: BlockRelation[] = []; + let exists: boolean = false; + for (const item of list) { + if (item.blocker === name1) { + matches.push(item); + if (item.blocked === name2) { + exists = true; + return true;; + } + } + } + return exists; +} + function whoBlockedMe(fastify: FastifyInstance, myID: string): BlockRelation [] { const usersBlocked = fastify.db.getAllBlockedUsers() ?? []; @@ -21,45 +37,30 @@ function whoBlockedMe(fastify: FastifyInstance, myID: string): BlockRelation [] })); } -export function broadcast(fastify: FastifyInstance, data: ClientMessage, sender?: string) { +export async 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) => { - for (const socket of sockets) { - // Skip sender's own socket - if (socket.id === sender) continue; - // Get client name from map - const clientInfo = clientChat.get(socket.id); - if (!clientInfo?.user) { - // console.log(color.yellow, `Skipping socket ${socket.id} (no user found)`); - continue; - } - // console.log('BLOCKED MAYBE', getUserById(sender)); - // console.log('TARGET',socket.id ); - // 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 }); - // Debug logs - // console.log(color.green, `'DEBUG LOG: Broadcast to:', ${data.command} message: ${data.text}`); + const sockets = await fastify.io.fetchSockets(); + for (const socket of sockets) { + const clientInfo = clientChat.get(socket.id); + if (!clientInfo?.user) { + console.log(color.red, `Skipping socket ${socket.id} (no user found)`); + continue; } - }); + let blockMsgFlag: boolean = false; + const UserByID = getUserByName(AllusersBlocked, clientInfo.user)?.id ?? ''; + if (UserByID === '') return; + blockMsgFlag = checkNamePair(list, data.SenderUserID, UserByID) || false; + + if (socket.id === sender) { + console.log(color.red, 'sKip Sender ', socket.id); + continue; + } + if (!blockMsgFlag) { + console.log(color.blue, 'Emit message: ', data.command, 'blockMsgFlag: ', blockMsgFlag); + socket.emit('MsgObjectServer', { message: data }); + } + } } \ No newline at end of file