Added and Fixed bug of Blocage a User in the Chat
This commit is contained in:
parent
5ee7e7c0b7
commit
f49505cc74
3 changed files with 40 additions and 39 deletions
|
|
@ -325,7 +325,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
||||||
connected(socket);
|
connected(socket);
|
||||||
}
|
}
|
||||||
console.log('stahe eeee :', blockMessage);
|
console.log('stahe eeee :', blockMessage);
|
||||||
if (chatWindow && data.message.destination === "" && !blockMessage) {
|
if (chatWindow && data.message.destination === "") {
|
||||||
const messageElement = document.createElement("div");
|
const messageElement = document.createElement("div");
|
||||||
messageElement.textContent = `${data.message.user}: ${data.message.text}`;
|
messageElement.textContent = `${data.message.user}: ${data.message.text}`;
|
||||||
chatWindow.appendChild(messageElement);
|
chatWindow.appendChild(messageElement);
|
||||||
|
|
|
||||||
|
|
@ -392,8 +392,8 @@ async function onReady(fastify: FastifyInstance) {
|
||||||
if (isUser_BlockedBy_me(fastify, UserAskingToBlock!.id, UserToBlock!.id)) {
|
if (isUser_BlockedBy_me(fastify, UserAskingToBlock!.id, UserToBlock!.id)) {
|
||||||
const message: blockedUnBlocked = {
|
const message: blockedUnBlocked = {
|
||||||
userState: 'un-block',
|
userState: 'un-block',
|
||||||
userTarget: '',
|
userTarget: UserToBlock.name,
|
||||||
by: '',
|
by: UserAskingToBlock.name,
|
||||||
};
|
};
|
||||||
socket.emit('blockBtn', message);
|
socket.emit('blockBtn', message);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,28 @@ import { clientChat } from './app';
|
||||||
import { FastifyInstance } from 'fastify';
|
import { FastifyInstance } from 'fastify';
|
||||||
import { getUserByName } from './getUserByName';
|
import { getUserByName } from './getUserByName';
|
||||||
import type { User } from '@shared/database/mixin/user';
|
import type { User } from '@shared/database/mixin/user';
|
||||||
|
import { color } from './app';
|
||||||
|
|
||||||
type BlockRelation = {
|
type BlockRelation = {
|
||||||
blocked: string;
|
blocked: string;
|
||||||
blocker: 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 [] {
|
function whoBlockedMe(fastify: FastifyInstance, myID: string): BlockRelation [] {
|
||||||
const usersBlocked =
|
const usersBlocked =
|
||||||
fastify.db.getAllBlockedUsers() ?? [];
|
fastify.db.getAllBlockedUsers() ?? [];
|
||||||
|
|
@ -21,45 +37,30 @@ function whoBlockedMe(fastify: FastifyInstance, myID: string): BlockRelation []
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function broadcast(fastify: FastifyInstance, data: ClientMessage, sender?: string) {
|
export async function broadcast(fastify: FastifyInstance, data: ClientMessage, sender?: string) {
|
||||||
|
|
||||||
const AllusersBlocked: User[] = fastify.db.getAllUsers() ?? [];
|
const AllusersBlocked: User[] = fastify.db.getAllUsers() ?? [];
|
||||||
// console.log(color.yellow, 'me:', getUserByName(AllusersBlocked, data.user)?.id)
|
|
||||||
const UserID = getUserByName(AllusersBlocked, data.user)?.id ?? '';
|
const UserID = getUserByName(AllusersBlocked, data.user)?.id ?? '';
|
||||||
const list:BlockRelation[] = whoBlockedMe(fastify, UserID);
|
const list:BlockRelation[] = whoBlockedMe(fastify, UserID);
|
||||||
const blockers = list.map(read => read.blocker);
|
const sockets = await fastify.io.fetchSockets();
|
||||||
const blocked = list.map(read => read.blocked);
|
for (const socket of sockets) {
|
||||||
console.log('All blockers:', blockers);
|
const clientInfo = clientChat.get(socket.id);
|
||||||
console.log('All blocked:', blocked);
|
if (!clientInfo?.user) {
|
||||||
// console.log(color.yellow, 'list:', list)
|
console.log(color.red, `Skipping socket ${socket.id} (no user found)`);
|
||||||
fastify.io.fetchSockets().then((sockets) => {
|
continue;
|
||||||
for (const socket of sockets) {
|
|
||||||
// Skip sender's own socket
|
|
||||||
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)`);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// console.log('BLOCKED MAYBE', getUserById(sender));
|
|
||||||
// console.log('TARGET',socket.id );
|
|
||||||
// Emit structured JSON object
|
|
||||||
// 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);
|
|
||||||
// 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}`);
|
|
||||||
}
|
}
|
||||||
});
|
let blockMsgFlag: boolean = false;
|
||||||
|
const UserByID = getUserByName(AllusersBlocked, clientInfo.user)?.id ?? '';
|
||||||
|
if (UserByID === '') return;
|
||||||
|
blockMsgFlag = checkNamePair(list, data.SenderUserID, UserByID) || false;
|
||||||
|
|
||||||
|
if (socket.id === sender) {
|
||||||
|
console.log(color.red, 'sKip Sender ', socket.id);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!blockMsgFlag) {
|
||||||
|
console.log(color.blue, 'Emit message: ', data.command, 'blockMsgFlag: ', blockMsgFlag);
|
||||||
|
socket.emit('MsgObjectServer', { message: data });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue