Added blocage Message Private

This commit is contained in:
NigeParis 2025-12-20 17:42:18 +01:00 committed by Maix0
parent f49505cc74
commit 97d7318cf9

View file

@ -1,6 +1,40 @@
import type { ClientMessage } from './chat_types'; import type { ClientMessage } from './chat_types';
import { clientChat, color } from './app'; import { clientChat, color } from './app';
import { FastifyInstance } from 'fastify'; import { FastifyInstance } from 'fastify';
import { getUserByName } from './getUserByName';
import type { User } from '@shared/database/mixin/user';
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 * function looks up the socket of a user online in the chat and sends a message
@ -10,8 +44,12 @@ import { FastifyInstance } from 'fastify';
* @param sender * @param sender
*/ */
export function sendPrivMessage(fastify: FastifyInstance, data: ClientMessage, sender?: string) { export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMessage, sender?: string) {
fastify.io.fetchSockets().then((sockets) => {
const AllusersBlocked: User[] = fastify.db.getAllUsers() ?? [];
const UserID = getUserByName(AllusersBlocked, data.user)?.id ?? '';
const list:BlockRelation[] = whoBlockedMe(fastify, UserID);
const sockets = await fastify.io.fetchSockets();
const senderSocket = sockets.find(socket => socket.id === sender); const senderSocket = sockets.find(socket => socket.id === sender);
for (const socket of sockets) { for (const socket of sockets) {
if (socket.id === sender) continue; if (socket.id === sender) continue;
@ -20,6 +58,11 @@ export function sendPrivMessage(fastify: FastifyInstance, data: ClientMessage, s
console.log(color.yellow, `DEBUG LOG: Skipping socket ${socket.id} (no user found)`); console.log(color.yellow, `DEBUG LOG: Skipping socket ${socket.id} (no user found)`);
continue; continue;
} }
let blockMsgFlag: boolean = false;
const UserByID = getUserByName(AllusersBlocked, clientInfo.user)?.id ?? '';
if (UserByID === '') {
blockMsgFlag = checkNamePair(list, data.SenderUserID, UserByID) || false;
}
const user: string = clientChat.get(socket.id)?.user ?? ''; const user: string = clientChat.get(socket.id)?.user ?? '';
const atUser = `@${user}`; const atUser = `@${user}`;
if (atUser !== data.command || atUser === '') { if (atUser !== data.command || atUser === '') {
@ -27,13 +70,15 @@ export function sendPrivMessage(fastify: FastifyInstance, data: ClientMessage, s
continue; continue;
} }
if (data.text !== '') { if (data.text !== '') {
socket.emit('MsgObjectServer', { message: data }); if (!blockMsgFlag) {
console.log(color.blue, 'Emit message: ', data.command, 'blockMsgFlag: ', blockMsgFlag);
// socket.emit('MsgObjectServer', { message: data });
console.log(color.yellow, `DEBUG LOG: User: '${atUser}' command FOUND: '${data.command}' `); console.log(color.yellow, `DEBUG LOG: User: '${atUser}' command FOUND: '${data.command}' `);
if (senderSocket) { if (senderSocket) {
senderSocket.emit('privMessageCopy', `${data.command}: ${data.text}🔒`); senderSocket.emit('privMessageCopy', `${data.command}: ${data.text}🔒`);
} }
} }
}
console.log(color.green, `DEBUG LOG: 'Priv to:', ${data.command} message: ${data.text}`); console.log(color.green, `DEBUG LOG: 'Priv to:', ${data.command} message: ${data.text}`);
} }
});
} }