diff --git a/Docker.mk b/Docker.mk index f63b928..1ea71bb 100644 --- a/Docker.mk +++ b/Docker.mk @@ -6,7 +6,7 @@ # By: nrobinso +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/06/11 18:10:26 by maiboyer #+# #+# # -# Updated: 2025/12/17 11:34:56 by nrobinso ### ########.fr # +# Updated: 2025/12/19 09:19:02 by nrobinso ### ########.fr # # # # **************************************************************************** # @@ -14,12 +14,12 @@ # TODO: REMOVE THIS BEFORE LAUNCH # this allows the us to only start the non-monitoring sercices -DOCKER_SERVICE= \ - auth \ - chat \ - frontend \ - nginx \ - user \ +# DOCKER_SERVICE= +# auth \ +# chat \ +# frontend \ +# nginx \ +# user \ all: build docker compose up -d $(DOCKER_SERVICE) diff --git a/src/chat/src/app.ts b/src/chat/src/app.ts index 4c887e0..3a392d1 100644 --- a/src/chat/src/app.ts +++ b/src/chat/src/app.ts @@ -20,7 +20,6 @@ import { sendProfil } from './sendProfil'; import { setGameLink } from './setGameLink'; import { nextGame_SocketListener } from './nextGame_SocketListener'; import { list_SocketListener } from './list_SocketListener'; -import { filter_Blocked_user } from './filter_Blocked_user' // colors for console.log export const color = { @@ -42,7 +41,7 @@ interface ClientInfo { } -export type blockedUnBlocked = +export type blockedUnBlocked = { userState: string, userTarget: string, @@ -104,15 +103,13 @@ declare module 'fastify' { client_left: (userName: string, why: string) => void; list: (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; + 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 @@ -128,25 +125,25 @@ function isUser_BlockedBy_me(fastify: FastifyInstance, blockedBy_Id : string, is 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 ""}; + 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}`) + 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 ""; + console.log(color.blue, `'User :${UserAskingToBlock.name}) has BBBblocked ${UserToBlock.name}`); + return ''; }; - - - async function onReady(fastify: FastifyInstance) { - - // shows address for connection au server transcendance const session = process.env.SESSION_MANAGER ?? ''; if (session) { @@ -155,9 +152,7 @@ async function onReady(fastify: FastifyInstance) { console.log(color.yellow, 'Connect at : https://' + machineName + ':8888/app/login'); } - fastify.io.on('connection', (socket: Socket) => { - socket.on('message', (message: string) => { // 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); @@ -167,45 +162,26 @@ async function onReady(fastify: FastifyInstance) { socket.emit('welcome', { msg: 'Welcome to the chat! : ' }); // Send object directly — DO NOT wrap it in a string broadcast(fastify, obj, obj.SenderWindowID); - - const users: User[] = fastify.db.getAllUsers() ?? []; + + // 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?? "")); - - - // } - // }); - - - - - - - - - + // 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); }); - nextGame_SocketListener(fastify, socket); - list_SocketListener(fastify, socket); - socket.on('updateClientName', (object) => { const userFromFrontend = object || null; const client = clientChat.get(socket.id) || null; @@ -241,9 +217,9 @@ async function onReady(fastify: FastifyInstance) { timestamp: Date.now(), SenderWindowID: socket.id, Sendertext: '', - userID: '', - SenderUserName: '', - SenderUserID: '', + userID: '', + SenderUserName: '', + SenderUserID: '', }; broadcast(fastify, obj, socket.id); // Optional: remove from map @@ -254,11 +230,7 @@ async function onReady(fastify: FastifyInstance) { socket.on('disconnecting', (reason) => { const clientName = clientChat.get(socket.id)?.user || null; - // console.log( - // color.green, - // `Client disconnecting: ${clientName} (${socket.id}) reason:`, - // reason, - // ); + // console.log(color.green, `Client disconnecting: ${clientName} (${socket.id}) reason:`, reason,); if (reason === 'transport error') return; if (clientName !== null) { @@ -275,7 +247,7 @@ async function onReady(fastify: FastifyInstance) { SenderWindowID: socket.id, Sendertext: '', userID: '', - SenderUserName: '', + SenderUserName: '', SenderUserID: '', }; @@ -284,14 +256,9 @@ async function onReady(fastify: FastifyInstance) { }); socket.on('client_left', (data) => { + void data; const clientName = clientChat.get(socket.id)?.user || null; - //const leftChat = data || null; - // console.log( - // color.green, - // `Left the Chat User: ${clientName} id Socket: ${socket.id} reason:`, - // leftChat.why, - // ); - + // const leftChat = data || null; console.log(color.green, `Left the Chat User: ${clientName} id Socket: ${socket.id} reason:`, leftChat.why,); if (clientName !== null) { const obj: ClientMessage = { command: '', @@ -305,8 +272,8 @@ async function onReady(fastify: FastifyInstance) { timestamp: Date.now(), SenderWindowID: socket.id, Sendertext: '', - userID: '', - SenderUserName: '', + userID: '', + SenderUserName: '', SenderUserID: '', }; // console.log(color.blue, 'DEBUG LOG: BROADCASTS OUT :', obj.SenderWindowID); @@ -320,12 +287,7 @@ async function onReady(fastify: FastifyInstance) { socket.on('privMessage', (data) => { const clientName: string = clientChat.get(socket.id)?.user || ''; const prvMessage: ClientMessage = JSON.parse(data) || ''; - // console.log( - // color.blue, - // `DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target Name:`, - // prvMessage.command, - // ); - + // console.log(color.blue, `DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target Name:`, prvMessage.command,); if (clientName !== null) { const obj: ClientMessage = { command: prvMessage.command, @@ -339,13 +301,13 @@ async function onReady(fastify: FastifyInstance) { timestamp: Date.now(), SenderWindowID: socket.id, Sendertext: '', - userID: '', - SenderUserName: '', + userID: '', + SenderUserName: '', SenderUserID:'', }; // console.log(color.blue, 'DEBUG LOG: PRIV MESSAGE OUT :', obj.SenderWindowID); sendPrivMessage(fastify, obj, obj.SenderWindowID); - // clientChat.delete(obj.user); + // clientChat.delete(obj.user); } }); @@ -361,7 +323,7 @@ async function onReady(fastify: FastifyInstance) { // console.log(color.yellow, 'user:', testuser?.name ?? 'Guest'); // console.log(color.blue, 'DEBUG - profil message MESSAGE OUT :', profile.SenderWindowID); sendProfil(fastify, profile, profile.SenderWindowID); - // clientChat.delete(obj.user); + // clientChat.delete(obj.user); } }); @@ -392,30 +354,24 @@ async function onReady(fastify: FastifyInstance) { 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); - - - } }); + 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) => { const clientName: string = clientChat.get(socket.id)?.user || ''; @@ -433,35 +389,27 @@ async function onReady(fastify: FastifyInstance) { const usersBlocked: BlockedData[] = fastify.db.getAllBlockedUsers() ?? []; if (!UserAskingToBlock || !UserToBlock || !usersBlocked) return; 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 (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) { // console.log(color.green, 'Both users are blocked as requested'); // return true; // or any other action you need to take - // console.log(color.red, 'ALL BLOCKED USERS:', usersBlocked); fastify.db.removeBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id); // const usersBlocked2 = fastify.db.getAllBlockedUsers(); @@ -488,7 +436,7 @@ async function onReady(fastify: FastifyInstance) { } // profilBlock.Sendertext = `'You have un-blocked '`; sendBlocked(fastify, blockedMessage, profilBlock); - // const message: blockedUnBlocked = + // const message: blockedUnBlocked = // { // userState: "un-block", // userTarget: "", @@ -527,7 +475,7 @@ async function onReady(fastify: FastifyInstance) { // clientChat.delete(obj.user); } sendBlocked(fastify, blockedMessage, profilBlock); - // const message: blockedUnBlocked = + // const message: blockedUnBlocked = // { // userState: "block", // userTarget: UserToBlock.name, @@ -579,9 +527,9 @@ async function onReady(fastify: FastifyInstance) { text: text, timestamp: Date.now(), SenderWindowID: socket.id, - Sendertext: "", - userID: '', - SenderUserName: '', + Sendertext: '', + userID: '', + SenderUserName: '', SenderUserID: '', }; broadcast(fastify, obj, obj.SenderWindowID); diff --git a/src/chat/src/broadcast.ts b/src/chat/src/broadcast.ts index c9d2b5b..58e2485 100644 --- a/src/chat/src/broadcast.ts +++ b/src/chat/src/broadcast.ts @@ -1,54 +1,37 @@ import type { ClientMessage } from './chat_types'; -import { clientChat, color } from './app'; +import { clientChat } from './app'; import { FastifyInstance } from 'fastify'; -import type { BlockedData } from '@shared/database/mixin/blocked'; import { getUserByName } from './getUserByName'; import type { User } from '@shared/database/mixin/user'; - - type BlockRelation = { blocked: string; blocker: string; }; -function whoBlockedMe( - fastify: FastifyInstance, - myName: string -):BlockRelation [] { - const usersBlocked = +function whoBlockedMe(fastify: FastifyInstance, myID: string): BlockRelation [] { + const usersBlocked = fastify.db.getAllBlockedUsers() ?? []; return usersBlocked - .filter(entry => entry.blocked === myName) + .filter(entry => entry.blocked === myID) .map(entry => ({ blocked: entry.user, blocker: entry.blocked, })); } - - - - 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 UserByName = getUserByName(AllusersBlocked, data.user)?.id ?? ""; - const list:BlockRelation[] = whoBlockedMe(fastify, UserByName); - - - - 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) + // 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 @@ -56,32 +39,25 @@ export function broadcast(fastify: FastifyInstance, data: ClientMessage, sender? // 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)`); + // 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 UserByName = getUserByName(AllusersBlocked, data.user)?.name ?? ""; - const UserByID = getUserByName(AllusersBlocked, data.user)?.id ?? ""; - //console.log(color.blue, 'Asking:', UserByName); - //console.log(color.blue, 'Asking ID:', UserByID); - + // 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); - + // 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; + // 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}`); } diff --git a/src/chat/src/filter_Blocked_user.ts b/src/chat/src/filter_Blocked_user.ts index 3f96bef..3cdded5 100644 --- a/src/chat/src/filter_Blocked_user.ts +++ b/src/chat/src/filter_Blocked_user.ts @@ -1,4 +1,4 @@ -import type { ClientMessage, ClientProfil } from './chat_types'; +import type { ClientMessage } from './chat_types'; import { FastifyInstance } from 'fastify'; import type { User } from '@shared/database/mixin/user'; import { getUserById } from './getUserById'; @@ -6,8 +6,8 @@ import { isUser_BlockedBy_me } from './isUser_BlockedBy_me'; /** * function to check if blocked or not - checks with ID - * @param fastify - * @param data + * @param fastify + * @param data * @returns true or false - true if blocked user by a user */ @@ -15,25 +15,19 @@ export function filter_Blocked_user(fastify: FastifyInstance, data: ClientMessag 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); + 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); - console.log(''); - - - return false; - } - if(isUser_BlockedBy_me(fastify, UserAskingToBlock!.id, UserToBlock)) - { - return true; - } - else - { return false; - } -} - + } + if (isUser_BlockedBy_me(fastify, UserAskingToBlock!.id, UserToBlock)) { + return true; + } + else { + return false; + } +} \ No newline at end of file diff --git a/src/chat/src/isUser_BlockedBy_me.ts b/src/chat/src/isUser_BlockedBy_me.ts index dcd7bb4..797e027 100644 --- a/src/chat/src/isUser_BlockedBy_me.ts +++ b/src/chat/src/isUser_BlockedBy_me.ts @@ -7,9 +7,9 @@ import { color } from './color'; /** * checks the Db for the two matching Ids - * @param fastify - * @param blockedBy_Id - * @param isBlocked_Id + * @param fastify + * @param blockedBy_Id + * @param isBlocked_Id * @returns Null if not blocked */ @@ -18,15 +18,21 @@ export function isUser_BlockedBy_me(fastify: FastifyInstance, blockedBy_Id : str 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 ""}; + 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}`) + 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}`) + console.log(color.blue, `'User :${UserAskingToBlock.name}) has BBBblocked ${UserToBlock.name}`); - return ""; + return ''; }; diff --git a/src/chat/src/routes/broadcast.ts b/src/chat/src/routes/broadcast.ts index de46038..e33cbed 100644 --- a/src/chat/src/routes/broadcast.ts +++ b/src/chat/src/routes/broadcast.ts @@ -1,6 +1,5 @@ import { FastifyPluginAsync } from 'fastify'; import { Static, Type } from 'typebox'; -import { broadcast } from '../broadcast'; export const ChatReq = Type.Object({ message: Type.String(), @@ -19,7 +18,7 @@ const route: FastifyPluginAsync = async (fastify): Promise => { config: { requireAuth: false }, }, 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; }, );