From 8086444098a358f4255a1f3b838a26d449e162a3 Mon Sep 17 00:00:00 2001 From: NigeParis Date: Thu, 8 Jan 2026 17:27:18 +0100 Subject: [PATCH] Clean-up --- frontend/src/pages/chat/chat.ts | 88 +++++++++---------- .../chat/chatHelperFunctions/getProfil.ts | 4 +- frontend/src/pages/chat/types_front.ts | 34 ++++--- src/chat/src/app.ts | 82 ++--------------- .../sendPrivMessage.ts | 29 +----- src/chat/src/chat_types.ts | 66 +++++++------- 6 files changed, 106 insertions(+), 197 deletions(-) diff --git a/frontend/src/pages/chat/chat.ts b/frontend/src/pages/chat/chat.ts index 4b27873..64544db 100644 --- a/frontend/src/pages/chat/chat.ts +++ b/frontend/src/pages/chat/chat.ts @@ -9,12 +9,13 @@ import { getProfil } from './chatHelperFunctions/getProfil'; import { addMessage } from './chatHelperFunctions/addMessage'; import { broadcastMsg } from './chatHelperFunctions/broadcastMsg'; import { isLoggedIn } from './chatHelperFunctions/isLoggedIn'; -import type { ClientMessage, ClientProfil } from './types_front'; +import type { ClientMessage, ClientProfil, ClientProfilPartial } from './types_front'; import { openProfilePopup } from './chatHelperFunctions/openProfilePopup'; import { actionBtnPopUpBlock } from './chatHelperFunctions/actionBtnPopUpBlock'; import { windowStateHidden } from './chatHelperFunctions/windowStateHidden'; import type { blockedUnBlocked, obj } from './types_front'; import { blockUser } from './chatHelperFunctions/blockUser'; +import type { User } from '@app/auth'; const MAX_SYSTEM_MESSAGES = 10; let inviteMsgFlag: boolean = false; @@ -425,6 +426,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn case '@msg': broadcastMsg(socket, msgCommand); break; + case '@block': if (msgCommand[1] === '') {break;}; if (!userAskingToBlock) return; @@ -434,18 +436,14 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn destination: '', type: 'chat', user: msgCommand[1], - loginName: '', userID: userId, - text: '', timestamp: Date.now(), - SenderWindowID: '', + SenderWindowID: socket.id, SenderName: userAskingToBlock, - SenderID: '', - Sendertext: '', - innerHtml: '', } blockUser(userToBlock, socket); break; + case '@notify': if (notify === null) {break;}; if (inviteMsgFlag === false) { @@ -455,40 +453,33 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn notify.innerText = '🔕'; inviteMsgFlag = false; } - break; - case '@guest': - if (!userId) {return;}; - if (!userAskingToBlock) {return;}; - if (noGuest === null) {break;}; - const guest = getUser()?.guest; - if (noGuestFlag === false && noGuest.innerText === '💔') { - noGuest.innerText = '❤️​'; - noGuestFlag = true; - } else { - noGuest.innerText = '💔'; - noGuestFlag = false; - } - if (guest) {noGuestFlag = true; noGuest.innerText = ''; sendtextbox.value = '';}; - const userProfile: ClientProfil = { - command: '@noguest', - destination: '', - type: 'chat', - user: '', - loginName: '', - userID: userId, - text: '', - timestamp: Date.now(), - SenderWindowID: '', - SenderName: userAskingToBlock, - SenderID: '', - Sendertext: '', - innerHtml: '', - guestmsg: noGuestFlag, - } - socket.emit('guestmsg', JSON.stringify(userProfile)); - break; + case '@guest': + if (!userId) {return;}; + if (!userAskingToBlock) {return;}; + if (noGuest === null) {break;}; + const guest = getUser()?.guest; + if (noGuestFlag === false && noGuest.innerText === '💔') { + noGuest.innerText = '❤️​'; + noGuestFlag = true; + } else { + noGuest.innerText = '💔'; + noGuestFlag = false; + } + if (guest) {noGuestFlag = true; noGuest.innerText = ''; sendtextbox.value = '';}; + const userProfile: ClientProfilPartial = { + command: '@noguest', + destination: '', + type: 'chat', + user: userAskingToBlock, + userID: userId, + timestamp: Date.now(), + SenderWindowID: '', + guestmsg: noGuestFlag, + } + socket.emit('guestmsg', JSON.stringify(userProfile)); + break; case '@profile': if (msgCommand[1] === '') {break;}; @@ -512,25 +503,26 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn addMessage('** *********************************** **'); addMessage('*'); break; + case '@quit': quitChat(socket); break; + default: - const user = getUser()?.name; - const userID = getUser()?.id; - // Ensure we have a user AND socket is connected + const user: User | null = getUser(); + if (!user) return; if (!user || !socket.connected) return; - const message = { + const message: ClientProfilPartial = { command: msgCommand[0], destination: '', type: "chat", - user: user, - token: document.cookie ?? "", + user: user.name, + userID: user.id, + token: document.cookie ?? '', text: msgCommand[1], timestamp: Date.now(), - SenderWindowID: socket.id, - SenderID: userID, - + SenderWindowID: socket.id ?? '', + SenderID: user.id, }; socket.emit('privMessage', JSON.stringify(message)); break; diff --git a/frontend/src/pages/chat/chatHelperFunctions/getProfil.ts b/frontend/src/pages/chat/chatHelperFunctions/getProfil.ts index 291f285..cec7ead 100644 --- a/frontend/src/pages/chat/chatHelperFunctions/getProfil.ts +++ b/frontend/src/pages/chat/chatHelperFunctions/getProfil.ts @@ -1,4 +1,5 @@ import { Socket } from 'socket.io-client'; +import type { ClientProfil } from '../types_front'; /** * getProfil of a user @@ -9,13 +10,14 @@ import { Socket } from 'socket.io-client'; export function getProfil(socket: Socket, user: string) { if (!socket.connected) return; - const profil = { + const profil: ClientProfil = { command: '@profile', destination: 'profilMessage', type: "chat", user: user, token: document.cookie ?? "", text: user, + userID: '', timestamp: Date.now(), SenderWindowID: socket.id, }; diff --git a/frontend/src/pages/chat/types_front.ts b/frontend/src/pages/chat/types_front.ts index e668e4a..c960681 100644 --- a/frontend/src/pages/chat/types_front.ts +++ b/frontend/src/pages/chat/types_front.ts @@ -17,24 +17,30 @@ export type ClientMessage = { }; -export type ClientProfil = { - command: string, - destination: string, - type: string, - user: string, - loginName: string, - userID: string, - text: string, - timestamp: number, - SenderWindowID:string, - SenderName: string, - SenderID: string, - Sendertext: string, +export type ClientProfil = ClientProfilPartial & { + loginName?: string, + SenderName?: string, + Sendertext?: string, innerHtml?: string, - guestmsg?: boolean, }; +export type ClientProfilPartial = { + command: string, + type: string, + destination: string, + user: string, + userID: string, + timestamp: number, + SenderWindowID?:string, + SenderID?: string, + text?: string, + token?: string + guestmsg?: boolean, +} + + + export type blockedUnBlocked = { userState: string, diff --git a/src/chat/src/app.ts b/src/chat/src/app.ts index fa29bba..c794a34 100644 --- a/src/chat/src/app.ts +++ b/src/chat/src/app.ts @@ -9,7 +9,7 @@ import { Server, Socket } from 'socket.io'; import type { User } from '@shared/database/mixin/user'; import type { BlockedData } from '@shared/database/mixin/blocked'; import { broadcast } from './chatBackHelperFunctions/broadcast'; -import type { ClientProfil, ClientMessage, obj } from './chat_types'; +import type { ClientProfil, ClientMessage } from './chat_types'; import { sendPrivMessage } from './chatBackHelperFunctions/sendPrivMessage'; import { sendBlocked } from './chatBackHelperFunctions/sendBlocked'; import { sendInvite } from './chatBackHelperFunctions/sendInvite'; @@ -20,6 +20,8 @@ import { sendProfil } from './chatBackHelperFunctions/sendProfil'; import { setGameLink } from './setGameLink'; import { nextGame_SocketListener } from './nextGame_SocketListener'; import { list_SocketListener } from './chatBackHelperFunctions/list_SocketListener'; +import { isUser_BlockedBy_me } from './chatBackHelperFunctions/isUser_BlockedBy_me'; + declare const __SERVICE_NAME: string; @@ -98,35 +100,6 @@ declare module 'fastify' { } } -/** - * 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) { - return ''; - }; - if (!UserAskingToBlock) { - return ''; - }; - const usersBlocked: BlockedData[] = fastify.db.getAllBlockedUsers() ?? []; - const userAreBlocked: boolean = isBlocked(UserAskingToBlock, UserToBlock, usersBlocked); - if (userAreBlocked) { - return UserAskingToBlock.name; - } - return ''; -}; - async function onReady(fastify: FastifyInstance) { const session = process.env.SESSION_MANAGER ?? ''; if (session) { @@ -138,6 +111,7 @@ async function onReady(fastify: FastifyInstance) { fastify.io.on('connection', (socket: Socket) => { socket.on('message', (message: string) => { const obj: ClientMessage = JSON.parse(message) as ClientMessage; + if (!obj.user || !socket.id) return; clientChat.set(socket.id, { user: obj.user, socket: socket.id, lastSeen: Date.now() }); socket.emit('welcome', { msg: 'Welcome to the chat! : ' }); broadcast(fastify, obj, obj.SenderWindowID); @@ -166,16 +140,9 @@ async function onReady(fastify: FastifyInstance) { destination: 'system-info', type: 'chat' as const, user: clientName, - token: '', text: 'LEFT the chat', - frontendUserName: '', - frontendUser: '', timestamp: Date.now(), SenderWindowID: socket.id, - Sendertext: '', - userID: '', - SenderUserName: '', - SenderUserID: '', }; broadcast(fastify, obj, socket.id); clientChat.delete(socket.id); @@ -192,16 +159,9 @@ async function onReady(fastify: FastifyInstance) { destination: 'system-info', type: 'chat', user: clientName, - token: '', text: 'LEFT the chat', - frontendUserName: '', - frontendUser: '', timestamp: Date.now(), SenderWindowID: socket.id, - Sendertext: '', - userID: '', - SenderUserName: '', - SenderUserID: '', }; broadcast(fastify, obj, obj.SenderWindowID); } @@ -216,16 +176,9 @@ async function onReady(fastify: FastifyInstance) { destination: 'system-info', type: 'chat', user: clientName, - token: '', text: 'LEFT the chat but the window is still open', - frontendUserName: '', - frontendUser: '', timestamp: Date.now(), SenderWindowID: socket.id, - Sendertext: '', - userID: '', - SenderUserName: '', - SenderUserID: '', }; broadcast(fastify, obj, obj.SenderWindowID); } @@ -241,16 +194,9 @@ async function onReady(fastify: FastifyInstance) { destination: 'privateMsg', type: 'chat', user: clientName, - token: '', text: prvMessage.text, - frontendUserName: '', - frontendUser: '', timestamp: Date.now(), SenderWindowID: socket.id, - Sendertext: '', - userID: '', - SenderUserName: '', - SenderUserID:'', }; sendPrivMessage(fastify, obj, obj.SenderWindowID); } @@ -275,6 +221,7 @@ async function onReady(fastify: FastifyInstance) { socket.on('profilMessage', async (data: string) => { const clientName: string = clientChat.get(socket.id)?.user || ''; const profilMessage: ClientMessage = JSON.parse(data) || ''; + if (!profilMessage.user) return; const profile: ClientProfil = await makeProfil(fastify, profilMessage.user, socket); if (clientName !== null) { sendProfil(fastify, profile, profile.SenderWindowID); @@ -347,15 +294,11 @@ async function onReady(fastify: FastifyInstance) { if (clientName !== null) { const blockedMessage = 'I have un-blocked you'; if (clientName !== null) { - const obj: obj = { + const obj: ClientProfil = { command: 'message', destination: 'privateMsg', type: 'chat', user: clientName, - token: '', - text: '', - frontendUserName: '', - frontendUser: '', timestamp: Date.now(), SenderWindowID: socket.id, Sendertext: 'You have un-blocked', @@ -368,19 +311,15 @@ async function onReady(fastify: FastifyInstance) { else { fastify.db.addBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id); if (clientName !== null) { - const blockedMessage = 'I have blocked you'; + const blockedMessage: string = 'I have blocked you'; profilBlock.Sendertext = 'You have blocked '; if (clientName !== null) { - const obj: obj = { + const obj: ClientMessage = { command: 'message', destination: 'privateMsg', type: 'chat', user: clientName, - token: '', - text: '', timestamp: Date.now(), - frontendUserName: '', - frontendUser: '', SenderWindowID: socket.id, Sendertext: 'You have blocked', }; @@ -418,14 +357,9 @@ async function onReady(fastify: FastifyInstance) { user: clientName, frontendUserName: userNameFromFrontend, frontendUser: userFromFrontend, - token: '', text: text, timestamp: Date.now(), SenderWindowID: socket.id, - Sendertext: '', - userID: '', - SenderUserName: '', - SenderUserID: '', }; broadcast(fastify, obj, obj.SenderWindowID); } diff --git a/src/chat/src/chatBackHelperFunctions/sendPrivMessage.ts b/src/chat/src/chatBackHelperFunctions/sendPrivMessage.ts index f500507..ecb5ff2 100644 --- a/src/chat/src/chatBackHelperFunctions/sendPrivMessage.ts +++ b/src/chat/src/chatBackHelperFunctions/sendPrivMessage.ts @@ -3,39 +3,14 @@ import { clientChat } from '../app'; import { FastifyInstance } from 'fastify'; import { getUserByName } from './getUserByName'; import type { User } from '@shared/database/mixin/user'; +import { checkNamePair } from './checkNamePair'; +import { whoBlockedMe } from './whoBlockedMe'; 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() ?? []; - - return usersBlocked - .filter(entry => entry.blocked === myID) - .map(entry => ({ - blocked: entry.user, - blocker: entry.blocked, - })); -} - /** * function looks up the socket of a user online in the chat and sends a message * it also sends a copy of the message to the sender diff --git a/src/chat/src/chat_types.ts b/src/chat/src/chat_types.ts index c8b937e..6acae9b 100644 --- a/src/chat/src/chat_types.ts +++ b/src/chat/src/chat_types.ts @@ -2,17 +2,17 @@ export type ClientMessage = { command: string destination: string; type: string, - user: string; - userID: string, - token: string - frontendUserName: string, - frontendUser: string, - text: string; - SenderWindowID: string, - SenderUserName: string, - SenderUserID: string, - timestamp: number, - Sendertext: string, + user?: string; + userID?: string, + token?: string + frontendUserName?: string, + frontendUser?: string, + text?: string; + SenderWindowID?: string, + SenderUserName?: string, + SenderUserID?: string, + timestamp?: number, + Sendertext?: string, innerHtml?: string, }; @@ -22,34 +22,34 @@ export type ClientProfil = { destination: string, type: string, user: string, - loginName: string, - userID: string, - text: string, - timestamp: number, - SenderWindowID:string, - SenderName: string, - SenderID: string, - Sendertext: string, + loginName?: string, + userID?: string, + text?: string, + timestamp?: number, + SenderWindowID?:string, + SenderName?: string, + SenderID?: string, + Sendertext?: string, innerHtml?: string, guestmsg?: boolean, }; -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, -}; +// 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, +// }; export type BlockRelation = { blocked: string;