WIP trying to fix a bug with blocking Broadcast message from a user on the chat

This commit is contained in:
NigeParis 2025-12-18 20:03:24 +01:00
parent f506fb44bf
commit a7fcb351f8
6 changed files with 115 additions and 74 deletions

View file

@ -9,7 +9,7 @@ import { getProfil } from './getProfil';
import { addMessage } from './addMessage';
import { broadcastMsg } from './broadcastMsg';
import { isLoggedIn } from './isLoggedIn';
import type { ClientMessage, ClientProfil } from './types_front';
import type { ClientMessage, ClientProfil, obj } from './types_front';
import { openProfilePopup } from './openProfilePopup';
import { actionBtnPopUpClear } from './actionBtnPopUpClear';
import { actionBtnPopUpBlock } from './actionBtnPopUpBlock';
@ -31,20 +31,6 @@ export type blockedUnBlocked =
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
const machineHostName = window.location.hostname;
@ -176,11 +162,6 @@ function quitChat (socket: Socket) {
};
// const bconnected = document.getElementById('b-help') as HTMLButtonElement;
// if (bconnected) {
// bconnected.click();
// }
function logout(socket: Socket) {
socket.emit("logout"); // notify server
socket.disconnect(); // actually close the socket
@ -321,6 +302,11 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
const chatWindow = document.getElementById("t-chatbox") as HTMLDivElement;
const bconnected = document.getElementById('b-help') as HTMLButtonElement;
console.log('UserSender:', data.message.SenderUserID);
console.log('User:', getUser()?.id);
if (bconnected) {
connected(socket);
}
@ -419,7 +405,6 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
let toggle = false
window.addEventListener("focus", async () => {
//nst bwhoami = document.getElementById('b-whoami') as HTMLButtonElement;
setTimeout(() => {
connected(socket);
@ -440,10 +425,6 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
toggle = false;
});
// setInterval(async () => {
// //connected(socket);
// },10000); // every 10 sec
socket.on('listBud', async (myBuddies: string[]) => {
const buddies = document.getElementById('div-buddies') as HTMLDivElement;
console.log('%cList buddies connected ',color.yellow, myBuddies);

View file

@ -30,4 +30,20 @@ export type ClientProfil = {
SenderID: string,
Sendertext: 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 { BlockedData } from '@shared/database/mixin/blocked';
import { broadcast } from './broadcast';
import type { ClientProfil, ClientMessage } from './chat_types';
import type { ClientProfil, ClientMessage, obj } from './chat_types';
import { sendPrivMessage } from './sendPrivMessage';
import { sendBlocked } from './sendBlocked';
import { sendInvite } from './sendInvite';
@ -37,6 +37,7 @@ declare const __SERVICE_NAME: string;
// key = socket, value = clientname
interface ClientInfo {
user: string;
socket: string
lastSeen: number;
}
@ -49,37 +50,6 @@ export type blockedUnBlocked =
};
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,
};
// function setAboutPlayer(about: string): string {
// if (!about) {
// about = 'Player is good Shape - This is a default description';
// }
// return about;
// };
// function setGameLink(link: string): string {
// if (!link) {
// link = '<a href=\'https://google.com\' style=\'color: blue; text-decoration: underline; cursor: pointer;\'>Click me</a>';
// }
// return link;
// };
export const clientChat = new Map<string, ClientInfo>();
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
@ -192,7 +162,7 @@ async function onReady(fastify: FastifyInstance) {
// 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);
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}`);
socket.emit('welcome', { msg: 'Welcome to the chat! : ' });
// Send object directly — DO NOT wrap it in a string
@ -204,21 +174,21 @@ async function onReady(fastify: FastifyInstance) {
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;
// 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(filter_Blocked_user(fastify, obj, IDUser?? ""));
}
});
// }
// });

View file

@ -1,23 +1,80 @@
import type { ClientMessage } from './chat_types';
import { clientChat, color } from './app';
import { FastifyInstance } from 'fastify';
import { getUserById } from './getUserById';
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 =
fastify.db.getAllBlockedUsers() ?? [];
return usersBlocked
.filter(entry => entry.blocked === myName)
.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(r => r.blocker);
const blocked = list.map(r => r.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;
//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)`);
//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);
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}`);
}

View file

@ -32,3 +32,19 @@ export type ClientProfil = {
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

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