From da8298dce81818d07072dc0e6de842e225ea06a4 Mon Sep 17 00:00:00 2001 From: NigeParis Date: Mon, 29 Dec 2025 12:15:19 +0100 Subject: [PATCH] Added block invites --- frontend/src/pages/chat/chat.ts | 19 ++++++++++++++ frontend/src/pages/chat/types_front.ts | 1 + src/chat/src/app.ts | 1 + src/chat/src/chat_types.ts | 1 + src/chat/src/sendInvite.ts | 36 +++++++++++++++++++++++--- src/chat/src/sendPrivMessage.ts | 19 ++++++++------ 6 files changed, 66 insertions(+), 11 deletions(-) diff --git a/frontend/src/pages/chat/chat.ts b/frontend/src/pages/chat/chat.ts index 07f4537..65d421c 100644 --- a/frontend/src/pages/chat/chat.ts +++ b/frontend/src/pages/chat/chat.ts @@ -340,6 +340,25 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn } + + + + + if (chatWindow && data.message.destination === "inviteMsg") { + const messageElement = document.createElement("div-private"); + const chatWindow = document.getElementById("t-chatbox") as HTMLDivElement; + messageElement.innerHTML = `🎃${data.message.SenderUserName}: ${data.message.innerHtml}`; + chatWindow.appendChild(messageElement); + chatWindow.scrollTop = chatWindow.scrollHeight; + } + + + + + + + + const MAX_SYSTEM_MESSAGES = 10; if (systemWindow && data.message.destination === "system-info") { diff --git a/frontend/src/pages/chat/types_front.ts b/frontend/src/pages/chat/types_front.ts index 33206d4..9e74632 100644 --- a/frontend/src/pages/chat/types_front.ts +++ b/frontend/src/pages/chat/types_front.ts @@ -13,6 +13,7 @@ export type ClientMessage = { SenderUserID: string, timestamp: number, Sendertext: string, + innerHtml?: string, }; diff --git a/src/chat/src/app.ts b/src/chat/src/app.ts index f1dfc8f..3288ec2 100644 --- a/src/chat/src/app.ts +++ b/src/chat/src/app.ts @@ -334,6 +334,7 @@ async function onReady(fastify: FastifyInstance) { const inviteHtml: string = 'invites you to a game ' + setGameLink(''); if (clientName !== null) { + console.log(`DEBUG LOG: clientName =${clientName}`); // const testuser: User | null = getUserByName(users, profilInvite.user ?? ''); // console.log(color.yellow, 'user:', testuser?.name ?? 'Guest'); sendInvite(fastify, inviteHtml, profilInvite); diff --git a/src/chat/src/chat_types.ts b/src/chat/src/chat_types.ts index 31154b4..e9173db 100644 --- a/src/chat/src/chat_types.ts +++ b/src/chat/src/chat_types.ts @@ -13,6 +13,7 @@ export type ClientMessage = { SenderUserID: string, timestamp: number, Sendertext: string, + innerHtml?: string, }; diff --git a/src/chat/src/sendInvite.ts b/src/chat/src/sendInvite.ts index 7c252ec..3426c6e 100644 --- a/src/chat/src/sendInvite.ts +++ b/src/chat/src/sendInvite.ts @@ -1,6 +1,7 @@ -import type { ClientProfil } from './chat_types'; +import type { ClientMessage, ClientProfil } from './chat_types'; import { clientChat, color } from './app'; import { FastifyInstance } from 'fastify'; +import { sendPrivMessage } from './sendPrivMessage'; /** * function looks for the user online in the chat @@ -15,14 +16,43 @@ export function sendInvite(fastify: FastifyInstance, innerHtml: string, profil: fastify.io.fetchSockets().then((sockets) => { let targetSocket; for (const socket of sockets) { + console.log(color.yellow, 'DEBUG LOG: sendInvite Function'); const clientInfo: string = clientChat.get(socket.id)?.user || ''; + console.log(color.green, 'AskingName=', profil.SenderName); targetSocket = socket || null; if (!targetSocket) continue; - console.log(color.yellow, 'DEBUG LOG: user online found', profil.user, 'socket', targetSocket.id); if (clientInfo === profil.user) { profil.innerHtml = innerHtml ?? ''; if (targetSocket.id) { - targetSocket.emit('inviteGame', profil); + + const data: ClientMessage = { + command: `@${clientInfo}`, + destination: 'inviteMsg', + type: "chat", + user: profil.SenderName, + token: '', + text: ' needs this to work', + timestamp: Date.now(), + SenderWindowID: socket.id, + userID: '', + frontendUserName: '', + frontendUser: '', + SenderUserName: profil.SenderName, + SenderUserID: '', + Sendertext: '', + innerHtml: innerHtml, + + }; + + console.log(color.yellow, 'DEBUG LOG: sendInvite Function -> sendPrivMessage :'); + sendPrivMessage(fastify, data, ''); + + + // targetSocket.emit('MsgObjectServer', { message: data }); + + + // targetSocket.emit('inviteGame', profil); + } return; } diff --git a/src/chat/src/sendPrivMessage.ts b/src/chat/src/sendPrivMessage.ts index 86cd0c9..dd6f880 100644 --- a/src/chat/src/sendPrivMessage.ts +++ b/src/chat/src/sendPrivMessage.ts @@ -9,7 +9,7 @@ type BlockRelation = { blocker: string; }; -function checkNamePair(list: BlockRelation[], name1: string, name2: string): (boolean) { +function checkNamePair(list: BlockRelation[], name1: string, name2: string): (boolean) { const matches: BlockRelation[] = []; let exists: boolean = false; for (const item of list) { @@ -46,7 +46,7 @@ function whoBlockedMe(fastify: FastifyInstance, myID: string): BlockRelation [] */ export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMessage, sender?: string) { - + const sockets = await fastify.io.fetchSockets(); const AllusersBlocked: User[] = fastify.db.getAllUsers() ?? []; const senderSocket = sockets.find(socket => socket.id === sender); @@ -60,17 +60,20 @@ export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMess } let blockMsgFlag: boolean = false; const UserByID = getUserByName(AllusersBlocked, clientInfo.user) ?? ''; - if (UserByID === '') return; - - + if (UserByID === ''){ + return; + } + const user: string = clientChat.get(socket.id)?.user ?? ''; const atUser = `@${user}`; + console.log(color.red, `'DEBUG LOG:'${atUser}' '${data.command}'`) if (atUser !== data.command || atUser === '' || data.text === '') { + console.log(color.red, 'DEBUG: atUser !== data.command'); continue; } - + + console.log(color.green, `USER ID: ${UserID} userName: ${UserByID.name} iD:${UserByID.id}`); blockMsgFlag = checkNamePair(list, UserID, UserByID.id) || false; - console.log(color.green, `USER ID: ${UserID} data.user: ${data.user} blockFlag: ${blockMsgFlag} userName: ${UserByID.name} iD:${UserByID.id}`); if (socket.id === sender) { console.log(color.blue, 'sKip Sender ', socket.id); @@ -78,7 +81,7 @@ export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMess } console.log(color.yellow, `blockFlag=${blockMsgFlag}: Target ${clientInfo.user}`); if (!blockMsgFlag) { - console.log(color.blue, 'Emit message: ', data.command, 'blockMsgFlag: ', blockMsgFlag); + console.log(color.blue, 'DEBUG Emit message: ', data.command, 'blockMsgFlag: ', blockMsgFlag); socket.emit('MsgObjectServer', { message: data }); if (senderSocket) { senderSocket.emit('privMessageCopy', `${data.command}: ${data.text}🔒`);