Merge pull request #65 from Maix0/nigel/functionBlockMessage
Nigel/function block message
This commit is contained in:
commit
5ee7e7c0b7
14 changed files with 484 additions and 212 deletions
|
|
@ -3,10 +3,10 @@
|
|||
# ::: :::::::: #
|
||||
# Docker.mk :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
|
||||
# By: nrobinso <nrobinso@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2025/06/11 18:10:26 by maiboyer #+# #+# #
|
||||
# Updated: 2025/12/12 14:56:10 by maiboyer ### ########.fr #
|
||||
# Updated: 2025/12/19 14:33:21 by maiboyer ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { addMessage } from "./addMessage";
|
||||
import { Socket } from 'socket.io-client';
|
||||
import { getUser } from "@app/auth";
|
||||
|
||||
import type { ClientMessage } from "./types_front";
|
||||
/**
|
||||
* function sends socket.emit to the backend to active and broadcast a message to all sockets
|
||||
* echos the message with addMessage to the sender
|
||||
|
|
@ -13,15 +13,21 @@ export function broadcastMsg (socket: Socket, msgCommand: string[]): void {
|
|||
addMessage(msgText);
|
||||
const user = getUser();
|
||||
if (user && socket?.connected) {
|
||||
const message = {
|
||||
command: msgCommand,
|
||||
const message: ClientMessage = {
|
||||
command: msgCommand[0],
|
||||
destination: '',
|
||||
type: "chat",
|
||||
user: user.name,
|
||||
token: document.cookie,
|
||||
text: msgText,
|
||||
timestamp: Date.now(),
|
||||
SenderWindowID: socket.id,
|
||||
SenderWindowID: socket.id ?? "",
|
||||
SenderUserName: user.name,
|
||||
SenderUserID: user.id,
|
||||
userID: '',
|
||||
frontendUserName: '',
|
||||
frontendUser: '',
|
||||
Sendertext: '',
|
||||
};
|
||||
socket.emit('message', JSON.stringify(message));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,29 @@ export const color = {
|
|||
reset: '',
|
||||
};
|
||||
|
||||
|
||||
export type blockedUnBlocked =
|
||||
{
|
||||
userState: string,
|
||||
userTarget: string,
|
||||
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;
|
||||
console.log('connect to login at %chttps://' + machineHostName + ':8888/app/login',color.yellow);
|
||||
|
|
@ -64,22 +87,6 @@ function actionBtnPopUpInvite(invite: ClientProfil, senderSocket: Socket) {
|
|||
}, 0)
|
||||
};
|
||||
|
||||
// async function windowStateHidden() {
|
||||
// const socketId = __socket || undefined;
|
||||
// // let oldName = localStorage.getItem("oldName") ?? undefined;
|
||||
// let oldName: string;
|
||||
// if (socketId === undefined) return;
|
||||
// let userName = await updateUser();
|
||||
// oldName = userName?.name ?? "";
|
||||
// if (oldName === "") return;
|
||||
// localStorage.setItem('oldName', oldName);
|
||||
// socketId.emit('client_left', {
|
||||
// user: userName?.name,
|
||||
// why: 'tab window hidden - socket not dead',
|
||||
// });
|
||||
// return;
|
||||
// };
|
||||
|
||||
async function windowStateVisable() {
|
||||
|
||||
const buddies = document.getElementById('div-buddies') as HTMLDivElement;
|
||||
|
|
@ -141,36 +148,6 @@ function parseCmdMsg(msgText: string): string[] | undefined {
|
|||
return command;
|
||||
}
|
||||
|
||||
// async function listBuddies(socket: Socket, buddies: HTMLDivElement, listBuddies: string) {
|
||||
|
||||
// if (!buddies) return;
|
||||
// const sendtextbox = document.getElementById('t-chat-window') as HTMLButtonElement;
|
||||
// const buddiesElement = document.createElement("div-buddies-list");
|
||||
// buddiesElement.textContent = listBuddies + '\n';
|
||||
// const user = getUser()?.name ?? "";
|
||||
// buddies.appendChild(buddiesElement);
|
||||
// buddies.scrollTop = buddies.scrollHeight;
|
||||
// console.log(`Added buddies: ${listBuddies}`);
|
||||
|
||||
// buddiesElement.style.cursor = "pointer";
|
||||
// buddiesElement.addEventListener("click", () => {
|
||||
// navigator.clipboard.writeText(listBuddies);
|
||||
// if (listBuddies !== user && user !== "") {
|
||||
// sendtextbox.value = `@${listBuddies}: `;
|
||||
// console.log("Copied to clipboard:", listBuddies);
|
||||
// sendtextbox.focus();
|
||||
// }
|
||||
// });
|
||||
|
||||
// buddiesElement.addEventListener("dblclick", () => {
|
||||
// console.log("Open profile:", listBuddies);
|
||||
// getProfil(socket, listBuddies);
|
||||
// sendtextbox.value = "";
|
||||
// });
|
||||
|
||||
// }
|
||||
|
||||
|
||||
function waitSocketConnected(socket: Socket): Promise<void> {
|
||||
return new Promise(resolve => {
|
||||
if (socket.connected) return resolve();
|
||||
|
|
@ -199,11 +176,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
|
||||
|
|
@ -216,6 +188,7 @@ function logout(socket: Socket) {
|
|||
|
||||
async function connected(socket: Socket): Promise<void> {
|
||||
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
const buddies = document.getElementById('div-buddies') as HTMLDivElement;
|
||||
const loggedIn = isLoggedIn();
|
||||
|
|
@ -224,23 +197,22 @@ async function connected(socket: Socket): Promise<void> {
|
|||
let oldUser = localStorage.getItem("oldName") ?? "";
|
||||
console.log('%coldUser:',color.yellow, oldUser);
|
||||
if (loggedIn?.name === undefined) {console.log('');return ;}
|
||||
setTimeout(() => {
|
||||
oldUser = loggedIn.name ?? "";
|
||||
}, 0);
|
||||
// const res = await client.guestLogin();
|
||||
let user = await updateUser();
|
||||
console.log('%cUser?name:',color.yellow, user?.name);
|
||||
localStorage.setItem("oldName", oldUser);
|
||||
buddies.textContent = "";
|
||||
socket.emit('list', {
|
||||
oldUser: oldUser,
|
||||
user: user?.name,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("Login error:", e);
|
||||
showError('Failed to login: Unknown error');
|
||||
}
|
||||
};
|
||||
// const res = await client.guestLogin();
|
||||
let user = await updateUser();
|
||||
console.log('%cUser?name:',color.yellow, user?.name);
|
||||
localStorage.setItem("oldName", oldUser);
|
||||
buddies.textContent = "";
|
||||
socket.emit('list', {
|
||||
oldUser: oldUser,
|
||||
user: user?.name,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("Login error:", e);
|
||||
showError('Failed to login: Unknown error');
|
||||
}
|
||||
}, 16);
|
||||
};
|
||||
|
||||
async function whoami(socket: Socket) {
|
||||
try {
|
||||
|
|
@ -270,30 +242,6 @@ async function whoami(socket: Socket) {
|
|||
}
|
||||
};
|
||||
|
||||
// async function openProfilePopup(profil: ClientProfil) {
|
||||
|
||||
|
||||
// const modalname = document.getElementById("modal-name") ?? null;
|
||||
// if (modalname)
|
||||
// modalname.innerHTML = `
|
||||
// <div class="profile-info">
|
||||
// <div-profil-name id="profilName"> Profil of ${profil.user} </div>
|
||||
// <div-login-name id="loginName"> Login Name: '${profil.loginName ?? 'Guest'}' </div>
|
||||
// </br>
|
||||
// <div-login-name id="loginName"> Login ID: '${profil.userID ?? ''}' </div>
|
||||
// </br>
|
||||
// <button id="popup-b-clear" class="btn-style popup-b-clear">Clear Text</button>
|
||||
// <button id="popup-b-invite" class="btn-style popup-b-invite">U Game ?</button>
|
||||
// <button id="popup-b-block" class="btn-style popup-b-block">Block User</button>
|
||||
// <div id="profile-about">About: '${profil.text}' </div>
|
||||
// </div>
|
||||
// `;
|
||||
// const profilList = document.getElementById("profile-modal") ?? null;
|
||||
// if (profilList)
|
||||
// profilList.classList.remove("hidden");
|
||||
// // The popup now exists → attach the event
|
||||
// }
|
||||
|
||||
let count = 0;
|
||||
function incrementCounter(): number {
|
||||
count += 1;
|
||||
|
|
@ -304,12 +252,12 @@ async function openMessagePopup(message: string) {
|
|||
|
||||
const modalmessage = document.getElementById("modal-message") ?? null;
|
||||
if(!message) return
|
||||
const obj:any = JSON.parse(message);
|
||||
const obj:string = JSON.parse(message);
|
||||
if (modalmessage) {
|
||||
const messageElement = document.createElement("div");
|
||||
messageElement.innerHTML = `
|
||||
<div class="profile-info">
|
||||
<div id="profile-about">Next Game Message ${incrementCounter()}: ${obj.link}</div>
|
||||
<div class="profile-info"
|
||||
<div id="profile-about">Next Game Message ${incrementCounter()}: ${obj}</div>
|
||||
</div>`;
|
||||
modalmessage.appendChild(messageElement);
|
||||
modalmessage.scrollTop = modalmessage.scrollHeight;
|
||||
|
|
@ -328,6 +276,8 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
|
||||
|
||||
let socket = getSocket();
|
||||
let blockMessage: boolean;
|
||||
setTimeout(async () => {
|
||||
|
||||
// Listen for the 'connect' event
|
||||
socket.on("connect", async () => {
|
||||
|
|
@ -336,6 +286,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
await waitSocketConnected(socket);
|
||||
console.log("I AM Connected to the server:", socket.id);
|
||||
const user = getUser()?.name;
|
||||
const userID = getUser()?.id;
|
||||
// Ensure we have a user AND socket is connected
|
||||
if (!user || !socket.connected) return;
|
||||
const message = {
|
||||
|
|
@ -347,6 +298,8 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
text: " has Just ARRIVED in the chat",
|
||||
timestamp: Date.now(),
|
||||
SenderWindowID: socket.id,
|
||||
SenderID: userID,
|
||||
|
||||
};
|
||||
socket.emit('message', JSON.stringify(message));
|
||||
const messageElement = document.createElement("div");
|
||||
|
|
@ -354,6 +307,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
systemWindow.appendChild(messageElement);
|
||||
systemWindow.scrollTop = systemWindow.scrollHeight;
|
||||
});
|
||||
}, 0);
|
||||
|
||||
// Listen for messages from the server "MsgObjectServer"
|
||||
socket.on("MsgObjectServer", (data: { message: ClientMessage}) => {
|
||||
|
|
@ -362,11 +316,16 @@ 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);
|
||||
}
|
||||
|
||||
if (chatWindow && data.message.destination === "") {
|
||||
console.log('stahe eeee :', blockMessage);
|
||||
if (chatWindow && data.message.destination === "" && !blockMessage) {
|
||||
const messageElement = document.createElement("div");
|
||||
messageElement.textContent = `${data.message.user}: ${data.message.text}`;
|
||||
chatWindow.appendChild(messageElement);
|
||||
|
|
@ -397,7 +356,13 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
});
|
||||
|
||||
socket.on('profilMessage', (profil: ClientProfil) => {
|
||||
profil.SenderID = getUser()?.id ?? "";
|
||||
profil.SenderName = getUser()?.name ?? "";
|
||||
openProfilePopup(profil);
|
||||
socket.emit('isBlockdBtn', profil);
|
||||
console.log(`DEBUG LOG: userId:${profil.userID}: senderID${profil.SenderID}' senderID:${getUser()?.id}`);
|
||||
console.log(`DEBUG LOG: user:${profil.user}: sender:${profil.SenderName}' senderID:${getUser()?.name}`);
|
||||
socket.emit('check_Block_button', profil);
|
||||
actionBtnPopUpClear(profil, socket);
|
||||
actionBtnPopUpInvite(profil, socket);
|
||||
actionBtnPopUpBlock(profil, socket);
|
||||
|
|
@ -414,15 +379,28 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
|
||||
socket.on('blockUser', (blocked: ClientProfil) => {
|
||||
let icon = '⛔';
|
||||
|
||||
const chatWindow = document.getElementById("t-chatbox") as HTMLDivElement;
|
||||
const messageElement = document.createElement("div");
|
||||
if (`${blocked.text}` === '\'I have un-blocked you\'' ) { icon = '💚'};
|
||||
if (`${blocked.text}` === 'I have un-blocked you' ) {icon = '💚'};
|
||||
messageElement.innerText =`${icon}${blocked.SenderName}: ${blocked.text}`;
|
||||
chatWindow.appendChild(messageElement);
|
||||
chatWindow.scrollTop = chatWindow.scrollHeight;
|
||||
});
|
||||
|
||||
|
||||
socket.on('blockBtn', (data: blockedUnBlocked) => {
|
||||
const blockUserBtn = document.querySelector("#popup-b-block");
|
||||
if (blockUserBtn) {
|
||||
|
||||
console.log(' =================== >>> User State:', data.userState);
|
||||
console.log(' =================== >>> UserTarget:', data.userTarget);
|
||||
console.log(' =================== >>> By:', data.by);
|
||||
let message = "";
|
||||
if (data.userState === "block") {message = "un-block", blockMessage = true} else{message = "block", blockMessage = false}
|
||||
blockUserBtn.textContent = message;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
socket.on('logout', () => {
|
||||
|
|
@ -441,11 +419,10 @@ 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);
|
||||
}, 0);
|
||||
}, 16);
|
||||
if (window.location.pathname === '/app/chat') {
|
||||
console.log('%cWindow is focused on /chat:' + socket.id, color.green);
|
||||
if (socket.id) {
|
||||
|
|
@ -462,10 +439,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);
|
||||
|
|
@ -553,6 +526,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
break;
|
||||
default:
|
||||
const user = getUser()?.name;
|
||||
const userID = getUser()?.id;
|
||||
// Ensure we have a user AND socket is connected
|
||||
if (!user || !socket.connected) return;
|
||||
const message = {
|
||||
|
|
@ -564,6 +538,8 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
text: msgCommand[1],
|
||||
timestamp: Date.now(),
|
||||
SenderWindowID: socket.id,
|
||||
SenderID: userID,
|
||||
|
||||
};
|
||||
//socket.emit('MsgObjectServer', message);
|
||||
socket.emit('privMessage', JSON.stringify(message));
|
||||
|
|
@ -609,5 +585,6 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
|||
});
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
addRoute('/chat', handleChat, { bypass_auth: true });
|
||||
addRoute('/chat', handleChat);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,18 @@
|
|||
export type ClientMessage = {
|
||||
command: string
|
||||
destination: string;
|
||||
type: string,
|
||||
user: string;
|
||||
userID: string,
|
||||
token: string
|
||||
frontendUserName: string,
|
||||
frontendUser: string,
|
||||
text: string;
|
||||
SenderWindowID: string;
|
||||
SenderWindowID: string,
|
||||
SenderUserName: string,
|
||||
SenderUserID: string,
|
||||
timestamp: number,
|
||||
Sendertext: string,
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -18,6 +27,23 @@ export type ClientProfil = {
|
|||
timestamp: number,
|
||||
SenderWindowID:string,
|
||||
SenderName: string,
|
||||
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,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
|
@ -36,22 +36,18 @@ declare const __SERVICE_NAME: string;
|
|||
// key = socket, value = clientname
|
||||
interface ClientInfo {
|
||||
user: string;
|
||||
socket: string
|
||||
lastSeen: number;
|
||||
}
|
||||
|
||||
// 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 type blockedUnBlocked =
|
||||
{
|
||||
userState: string,
|
||||
userTarget: string,
|
||||
by: string,
|
||||
};
|
||||
|
||||
|
||||
export const clientChat = new Map<string, ClientInfo>();
|
||||
|
||||
|
|
@ -107,13 +103,47 @@ declare module 'fastify' {
|
|||
client_left: (userName: string, why: string) => void;
|
||||
list: (oldUser: string, user: string) => void;
|
||||
updateClientName: (oldUser: string, user: string) => void;
|
||||
blockBtn: (data: blockedUnBlocked) => void;
|
||||
isBlockdBtn: (data: blockedUnBlocked) => void;
|
||||
check_Block_button: (data: blockedUnBlocked) => void;
|
||||
}>;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
console.log(color.blue, `'User: ${UserAskingToBlock?.id} has not blocked' ${isBlocked_Id}`);
|
||||
return '';
|
||||
};
|
||||
if (!UserAskingToBlock) {
|
||||
console.log(color.blue, `'User: ${UserToBlock?.id} has not blocked by' ${blockedBy_Id}`);
|
||||
return '';
|
||||
};
|
||||
const usersBlocked: BlockedData[] = fastify.db.getAllBlockedUsers() ?? [];
|
||||
const userAreBlocked: boolean = isBlocked(UserAskingToBlock, UserToBlock, usersBlocked);
|
||||
if (userAreBlocked) {
|
||||
console.log(color.yellow, `'User :${UserAskingToBlock.name}) Hhas UN blocked ${UserToBlock.name}`);
|
||||
return UserAskingToBlock.name;
|
||||
}
|
||||
console.log(color.blue, `'User :${UserAskingToBlock.name}) has BBBblocked ${UserToBlock.name}`);
|
||||
return '';
|
||||
};
|
||||
|
||||
async function onReady(fastify: FastifyInstance) {
|
||||
|
||||
|
||||
// shows address for connection au server transcendance
|
||||
const session = process.env.SESSION_MANAGER ?? '';
|
||||
if (session) {
|
||||
|
|
@ -122,45 +152,36 @@ async function onReady(fastify: FastifyInstance) {
|
|||
console.log(color.yellow, 'Connect at : https://' + machineName + ':8888/app/login');
|
||||
}
|
||||
|
||||
|
||||
fastify.io.on('connection', (socket: Socket) => {
|
||||
|
||||
socket.on('message', (message: string) => {
|
||||
// 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
|
||||
broadcast(fastify, obj, obj.SenderWindowID);
|
||||
|
||||
// const users: User[] = fastify.db.getAllUsers() ?? [];
|
||||
// console.log('DEBUG: senderWindow :', getUserById(users, obj.SenderUserID)?.name);
|
||||
// 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(color.red, 'DEBUG LOG: connected in the Chat :', connectedUser(fastify.io), color.reset);
|
||||
});
|
||||
|
||||
nextGame_SocketListener(fastify, socket);
|
||||
|
||||
list_SocketListener(fastify, socket);
|
||||
|
||||
// socket.on('list', (object) => {
|
||||
|
||||
// const userFromFrontend = object || null;
|
||||
// const client = clientChat.get(socket.id) || null;
|
||||
|
||||
// //console.log(color.red, 'DEBUG LOG: list activated', userFromFrontend, color.reset, socket.id);
|
||||
|
||||
// if (userFromFrontend.oldUser !== userFromFrontend.user) {
|
||||
// //console.log(color.red, 'DEBUG LOG: list activated', userFromFrontend.oldUser, color.reset);
|
||||
// // if (client?.user === null) {
|
||||
// // console.log('ERROR: clientName is NULL');
|
||||
// // return;
|
||||
// // };
|
||||
// if (client) {
|
||||
// client.user = userFromFrontend.user;
|
||||
// }
|
||||
// }
|
||||
// connectedUser(fastify.io, socket.id);
|
||||
// });
|
||||
|
||||
socket.on('updateClientName', (object) => {
|
||||
const userFromFrontend = object || null;
|
||||
const client = clientChat.get(socket.id) || null;
|
||||
|
|
@ -183,16 +204,22 @@ async function onReady(fastify: FastifyInstance) {
|
|||
const clientName = clientInfo?.user;
|
||||
|
||||
if (!clientName) return;
|
||||
console.log(color.green, `Client logging out: ${clientName} (${socket.id})`);
|
||||
const obj = {
|
||||
// console.log(color.green, `Client logging out: ${clientName} (${socket.id})`);
|
||||
const obj: ClientMessage = {
|
||||
command: '',
|
||||
destination: 'system-info',
|
||||
type: 'chat' as const,
|
||||
user: clientName,
|
||||
token: '',
|
||||
text: 'LEFT the chat',
|
||||
timestamp: Date.now(),
|
||||
SenderWindowID: socket.id,
|
||||
frontendUserName: '',
|
||||
frontendUser: '',
|
||||
timestamp: Date.now(),
|
||||
SenderWindowID: socket.id,
|
||||
Sendertext: '',
|
||||
userID: '',
|
||||
SenderUserName: '',
|
||||
SenderUserID: '',
|
||||
};
|
||||
broadcast(fastify, obj, socket.id);
|
||||
// Optional: remove from map
|
||||
|
|
@ -203,23 +230,25 @@ async function onReady(fastify: FastifyInstance) {
|
|||
|
||||
socket.on('disconnecting', (reason) => {
|
||||
const clientName = clientChat.get(socket.id)?.user || null;
|
||||
console.log(
|
||||
color.green,
|
||||
`Client disconnecting: ${clientName} (${socket.id}) reason:`,
|
||||
reason,
|
||||
);
|
||||
// console.log(color.green, `Client disconnecting: ${clientName} (${socket.id}) reason:`, reason,);
|
||||
if (reason === 'transport error') return;
|
||||
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
const obj: ClientMessage = {
|
||||
command: '',
|
||||
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);
|
||||
|
|
@ -227,24 +256,25 @@ async function onReady(fastify: FastifyInstance) {
|
|||
});
|
||||
|
||||
socket.on('client_left', (data) => {
|
||||
void data;
|
||||
const clientName = clientChat.get(socket.id)?.user || null;
|
||||
const leftChat = data || null;
|
||||
console.log(
|
||||
color.green,
|
||||
`Left the Chat User: ${clientName} id Socket: ${socket.id} reason:`,
|
||||
leftChat.why,
|
||||
);
|
||||
|
||||
// const leftChat = data || null; console.log(color.green, `Left the Chat User: ${clientName} id Socket: ${socket.id} reason:`, leftChat.why,);
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
const obj: ClientMessage = {
|
||||
command: '',
|
||||
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: '',
|
||||
};
|
||||
// console.log(color.blue, 'DEBUG LOG: BROADCASTS OUT :', obj.SenderWindowID);
|
||||
|
||||
|
|
@ -257,42 +287,43 @@ async function onReady(fastify: FastifyInstance) {
|
|||
socket.on('privMessage', (data) => {
|
||||
const clientName: string = clientChat.get(socket.id)?.user || '';
|
||||
const prvMessage: ClientMessage = JSON.parse(data) || '';
|
||||
console.log(
|
||||
color.blue,
|
||||
`DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target Name:`,
|
||||
prvMessage.command,
|
||||
);
|
||||
|
||||
// console.log(color.blue, `DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target Name:`, prvMessage.command,);
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
const obj: ClientMessage = {
|
||||
command: prvMessage.command,
|
||||
destination: 'privateMsg',
|
||||
type: 'chat',
|
||||
user: clientName,
|
||||
token: '',
|
||||
text: prvMessage.text,
|
||||
frontendUserName: '',
|
||||
frontendUser: '',
|
||||
timestamp: Date.now(),
|
||||
SenderWindowID: socket.id,
|
||||
Sendertext: '',
|
||||
userID: '',
|
||||
SenderUserName: '',
|
||||
SenderUserID:'',
|
||||
};
|
||||
// console.log(color.blue, 'DEBUG LOG: PRIV MESSAGE OUT :', obj.SenderWindowID);
|
||||
sendPrivMessage(fastify, obj, obj.SenderWindowID);
|
||||
// clientChat.delete(obj.user);
|
||||
// clientChat.delete(obj.user);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('profilMessage', async (data: string) => {
|
||||
const clientName: string = clientChat.get(socket.id)?.user || '';
|
||||
const profilMessage: ClientMessage = JSON.parse(data) || '';
|
||||
const users: User[] = fastify.db.getAllUsers() ?? [];
|
||||
// const users: User[] = fastify.db.getAllUsers() ?? [];
|
||||
// console.log(color.yellow, 'DEBUG LOG: ALL USERS EVER CONNECTED:', users);
|
||||
// console.log(color.blue, `DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target profil:`, profilMessage.user);
|
||||
const profile: ClientProfil = await makeProfil(fastify, profilMessage.user, socket);
|
||||
if (clientName !== null) {
|
||||
const testuser: User | null = getUserByName(users, profilMessage.user);
|
||||
console.log(color.yellow, 'user:', testuser?.name ?? 'Guest');
|
||||
console.log(color.blue, 'DEBUG - profil message MESSAGE OUT :', profile.SenderWindowID);
|
||||
// const testuser: User | null = getUserByName(users, profilMessage.user);
|
||||
// console.log(color.yellow, 'user:', testuser?.name ?? 'Guest');
|
||||
// console.log(color.blue, 'DEBUG - profil message MESSAGE OUT :', profile.SenderWindowID);
|
||||
sendProfil(fastify, profile, profile.SenderWindowID);
|
||||
// clientChat.delete(obj.user);
|
||||
// clientChat.delete(obj.user);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -309,6 +340,39 @@ async function onReady(fastify: FastifyInstance) {
|
|||
}
|
||||
});
|
||||
|
||||
socket.on('isBlockdBtn', async (data: ClientProfil) => {
|
||||
const profilBlock: ClientProfil = data || '';
|
||||
const users: User[] = fastify.db.getAllUsers() ?? [];
|
||||
const UserToBlock: User | null = getUserByName(users, `${profilBlock.user}`);
|
||||
const UserAskingToBlock: User | null = getUserByName(users, `${profilBlock.SenderName}`);
|
||||
|
||||
// console.log(color.yellow, `user to block: ${profilBlock.user}`);
|
||||
// console.log(color.yellow, UserToBlock);
|
||||
// console.log(color.yellow, `user Asking to block: ${profilBlock.SenderName}`);
|
||||
// console.log(color.yellow, UserAskingToBlock);
|
||||
// console.log(color.red, clientName);
|
||||
|
||||
if (!UserAskingToBlock || !UserToBlock) return;
|
||||
|
||||
if (isUser_BlockedBy_me(fastify, UserAskingToBlock!.id, UserToBlock!.id)) {
|
||||
const message: blockedUnBlocked = {
|
||||
userState: 'block',
|
||||
userTarget: UserToBlock.name,
|
||||
by: UserAskingToBlock.name,
|
||||
};
|
||||
socket.emit('blockBtn', message);
|
||||
}
|
||||
else {
|
||||
|
||||
const message: blockedUnBlocked = {
|
||||
userState: 'un-block',
|
||||
userTarget: UserToBlock.name,
|
||||
by: UserAskingToBlock.name,
|
||||
};
|
||||
socket.emit('blockBtn', message);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('blockUser', async (data: string) => {
|
||||
const clientName: string = clientChat.get(socket.id)?.user || '';
|
||||
const profilBlock: ClientProfil = JSON.parse(data) || '';
|
||||
|
|
@ -316,34 +380,52 @@ async function onReady(fastify: FastifyInstance) {
|
|||
const UserToBlock: User | null = getUserByName(users, `${profilBlock.user}`);
|
||||
const UserAskingToBlock: User | null = getUserByName(users, `${profilBlock.SenderName}`);
|
||||
|
||||
console.log(color.yellow, `user to block: ${profilBlock.user}`);
|
||||
console.log(color.yellow, UserToBlock);
|
||||
console.log(color.yellow, `user Asking to block: ${profilBlock.SenderName}`);
|
||||
console.log(color.yellow, UserAskingToBlock);
|
||||
// console.log(color.yellow, `user to block: ${profilBlock.user}`);
|
||||
// console.log(color.yellow, UserToBlock);
|
||||
// console.log(color.yellow, `user Asking to block: ${profilBlock.SenderName}`);
|
||||
// console.log(color.yellow, UserAskingToBlock);
|
||||
// console.log(color.red, clientName);
|
||||
|
||||
const usersBlocked: BlockedData[] = fastify.db.getAllBlockedUsers() ?? [];
|
||||
if (!UserAskingToBlock || !UserToBlock || !usersBlocked) return;
|
||||
const userAreBlocked: boolean = isBlocked(UserAskingToBlock, UserToBlock, usersBlocked);
|
||||
if (isUser_BlockedBy_me(fastify, UserAskingToBlock!.id, UserToBlock!.id)) {
|
||||
const message: blockedUnBlocked = {
|
||||
userState: 'un-block',
|
||||
userTarget: '',
|
||||
by: '',
|
||||
};
|
||||
socket.emit('blockBtn', message);
|
||||
}
|
||||
else {
|
||||
const message: blockedUnBlocked = {
|
||||
userState: 'block',
|
||||
userTarget: UserToBlock.name,
|
||||
by: UserAskingToBlock.name,
|
||||
};
|
||||
socket.emit('blockBtn', message);
|
||||
}
|
||||
|
||||
if (userAreBlocked) {
|
||||
console.log(color.green, 'Both users are blocked as requested');
|
||||
// console.log(color.green, 'Both users are blocked as requested');
|
||||
// return true; // or any other action you need to take
|
||||
|
||||
|
||||
console.log(color.red, 'ALL BLOCKED USERS:', usersBlocked);
|
||||
// console.log(color.red, 'ALL BLOCKED USERS:', usersBlocked);
|
||||
fastify.db.removeBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id);
|
||||
const usersBlocked2 = fastify.db.getAllBlockedUsers();
|
||||
console.log(color.green, 'remove ALL BLOCKED USERS:', usersBlocked2);
|
||||
// const usersBlocked2 = fastify.db.getAllBlockedUsers();
|
||||
// console.log(color.green, 'remove A BLOCKED USER:', usersBlocked2);
|
||||
if (clientName !== null) {
|
||||
const blockedMessage = 'I have un-blocked you';
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
const obj: obj = {
|
||||
command: 'message',
|
||||
destination: 'privateMsg',
|
||||
type: 'chat',
|
||||
user: clientName,
|
||||
token: '',
|
||||
text: '',
|
||||
frontendUserName: '',
|
||||
frontendUser: '',
|
||||
timestamp: Date.now(),
|
||||
SenderWindowID: socket.id,
|
||||
Sendertext: 'You have un-blocked',
|
||||
|
|
@ -354,20 +436,28 @@ async function onReady(fastify: FastifyInstance) {
|
|||
}
|
||||
// profilBlock.Sendertext = `'You have un-blocked '`;
|
||||
sendBlocked(fastify, blockedMessage, profilBlock);
|
||||
// const message: blockedUnBlocked =
|
||||
// {
|
||||
// userState: "un-block",
|
||||
// userTarget: "",
|
||||
// by: "",
|
||||
|
||||
// };
|
||||
// socket.emit('blockBtn', message);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
console.log(color.red, 'The users are not blocked in this way');
|
||||
console.log(color.red, 'ALL BLOCKED USERS:', usersBlocked);
|
||||
// console.log(color.red, 'The users are not blocked in this way');
|
||||
// console.log(color.red, 'ALL BLOCKED USERS:', usersBlocked);
|
||||
fastify.db.addBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id);
|
||||
const usersBlocked2 = fastify.db.getAllBlockedUsers();
|
||||
console.log(color.green, 'ALL BLOCKED USERS:', usersBlocked2);
|
||||
// const usersBlocked2 = fastify.db.getAllBlockedUsers();
|
||||
// console.log(color.green, 'ALL BLOCKED USERS:', usersBlocked2);
|
||||
if (clientName !== null) {
|
||||
const blockedMessage = 'I have blocked you';
|
||||
profilBlock.Sendertext = 'You have blocked ';
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
const obj: obj = {
|
||||
command: 'message',
|
||||
destination: 'privateMsg',
|
||||
type: 'chat',
|
||||
|
|
@ -375,6 +465,8 @@ async function onReady(fastify: FastifyInstance) {
|
|||
token: '',
|
||||
text: '',
|
||||
timestamp: Date.now(),
|
||||
frontendUserName: '',
|
||||
frontendUser: '',
|
||||
SenderWindowID: socket.id,
|
||||
Sendertext: 'You have blocked',
|
||||
};
|
||||
|
|
@ -383,6 +475,14 @@ async function onReady(fastify: FastifyInstance) {
|
|||
// clientChat.delete(obj.user);
|
||||
}
|
||||
sendBlocked(fastify, blockedMessage, profilBlock);
|
||||
// const message: blockedUnBlocked =
|
||||
// {
|
||||
// userState: "block",
|
||||
// userTarget: UserToBlock.name,
|
||||
// by: UserAskingToBlock.name,
|
||||
|
||||
// };
|
||||
// socket.emit('blockBtn', message);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -411,12 +511,12 @@ async function onReady(fastify: FastifyInstance) {
|
|||
// client.user = clientName;
|
||||
// }
|
||||
}
|
||||
console.log(
|
||||
color.green,
|
||||
`Client entered the Chat: ${clientName} (${socket.id})`,
|
||||
);
|
||||
// console.log(
|
||||
// color.green,
|
||||
// `Client entered the Chat: ${clientName} (${socket.id})`,
|
||||
// );
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
const obj: ClientMessage = {
|
||||
command: '',
|
||||
destination: 'system-info',
|
||||
type: 'chat',
|
||||
|
|
@ -427,6 +527,10 @@ async function onReady(fastify: FastifyInstance) {
|
|||
text: text,
|
||||
timestamp: Date.now(),
|
||||
SenderWindowID: socket.id,
|
||||
Sendertext: '',
|
||||
userID: '',
|
||||
SenderUserName: '',
|
||||
SenderUserID: '',
|
||||
};
|
||||
broadcast(fastify, obj, obj.SenderWindowID);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,37 @@
|
|||
import type { ClientMessage } from './chat_types';
|
||||
import { clientChat, color } from './app';
|
||||
import { clientChat } from './app';
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { getUserByName } from './getUserByName';
|
||||
import type { User } from '@shared/database/mixin/user';
|
||||
|
||||
type BlockRelation = {
|
||||
blocked: string;
|
||||
blocker: string;
|
||||
};
|
||||
|
||||
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,
|
||||
}));
|
||||
}
|
||||
|
||||
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 UserID = getUserByName(AllusersBlocked, data.user)?.id ?? '';
|
||||
const list:BlockRelation[] = whoBlockedMe(fastify, UserID);
|
||||
const blockers = list.map(read => read.blocker);
|
||||
const blocked = list.map(read => read.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
|
||||
|
|
@ -10,10 +39,24 @@ export function broadcast(fastify: FastifyInstance, data: ClientMessage, sender?
|
|||
// 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 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}`);
|
||||
|
|
|
|||
|
|
@ -1,11 +1,21 @@
|
|||
export type ClientMessage = {
|
||||
command: string
|
||||
destination: string;
|
||||
type: string,
|
||||
user: string;
|
||||
userID: string,
|
||||
token: string
|
||||
frontendUserName: string,
|
||||
frontendUser: string,
|
||||
text: string;
|
||||
SenderWindowID: string;
|
||||
SenderWindowID: string,
|
||||
SenderUserName: string,
|
||||
SenderUserID: string,
|
||||
timestamp: number,
|
||||
Sendertext: string,
|
||||
};
|
||||
|
||||
|
||||
export type ClientProfil = {
|
||||
command: string,
|
||||
destination: string,
|
||||
|
|
@ -17,7 +27,24 @@ export type ClientProfil = {
|
|||
timestamp: number,
|
||||
SenderWindowID:string,
|
||||
SenderName: string,
|
||||
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,
|
||||
};
|
||||
|
|
|
|||
8
src/chat/src/color.ts
Normal file
8
src/chat/src/color.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// colors for console.log
|
||||
export const color = {
|
||||
red: '\x1b[31m',
|
||||
green: '\x1b[32m',
|
||||
yellow: '\x1b[33m',
|
||||
blue: '\x1b[34m',
|
||||
reset: '\x1b[0m',
|
||||
};
|
||||
33
src/chat/src/filter_Blocked_user.ts
Normal file
33
src/chat/src/filter_Blocked_user.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import type { ClientMessage } from './chat_types';
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import type { User } from '@shared/database/mixin/user';
|
||||
import { getUserById } from './getUserById';
|
||||
import { isUser_BlockedBy_me } from './isUser_BlockedBy_me';
|
||||
|
||||
/**
|
||||
* function to check if blocked or not - checks with ID
|
||||
* @param fastify
|
||||
* @param data
|
||||
* @returns true or false - true if blocked user by a user
|
||||
*/
|
||||
|
||||
export function filter_Blocked_user(fastify: FastifyInstance, data: ClientMessage, id: string): boolean {
|
||||
|
||||
const users: User[] = fastify.db.getAllUsers() ?? [];
|
||||
const UserToBlock: string = id;
|
||||
const UserAskingToBlock: User | null = getUserById(users, `${data.SenderUserID}`);
|
||||
if (!UserAskingToBlock) {
|
||||
// console.log('SOMETHING NULL', data);
|
||||
// console.log('UsetToBlock', UserToBlock?.id);
|
||||
// console.log('UsetToBlock', UserToBlock?.name);
|
||||
// console.log('UsetAskingToBlock', UserAskingToBlock?.id);
|
||||
// console.log('UsetAskingToBlock', UserAskingToBlock?.name);
|
||||
return false;
|
||||
}
|
||||
if (isUser_BlockedBy_me(fastify, UserAskingToBlock!.id, UserToBlock)) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
10
src/chat/src/getUserById.ts
Normal file
10
src/chat/src/getUserById.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import type { User } from '@shared/database/mixin/user';
|
||||
/**
|
||||
* function get the object user in an array of users[] by name
|
||||
* @param users
|
||||
* @param name
|
||||
* @returns
|
||||
*/
|
||||
export function getUserById(users: User[], id: string) {
|
||||
return users.find(user => user.id === id) || null;
|
||||
};
|
||||
38
src/chat/src/isUser_BlockedBy_me.ts
Normal file
38
src/chat/src/isUser_BlockedBy_me.ts
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import { FastifyInstance } from 'fastify';
|
||||
import type { User } from '@shared/database/mixin/user';
|
||||
import type { BlockedData } from '@shared/database/mixin/blocked';
|
||||
import { isBlocked } from './isBlocked';
|
||||
import { getUserById } from './getUserById';
|
||||
import { color } from './color';
|
||||
|
||||
/**
|
||||
* checks the Db for the two matching Ids
|
||||
* @param fastify
|
||||
* @param blockedBy_Id
|
||||
* @param isBlocked_Id
|
||||
* @returns Null if not blocked
|
||||
*/
|
||||
|
||||
export 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) {
|
||||
console.log(color.blue, `'User: ${UserAskingToBlock?.id} has not blocked' ${isBlocked_Id}`);
|
||||
return '';
|
||||
}
|
||||
if (!UserAskingToBlock) {
|
||||
console.log(color.blue, `'User: ${UserToBlock?.id} has not blocked by' ${blockedBy_Id}`);
|
||||
return '';
|
||||
}
|
||||
const usersBlocked: BlockedData[] = fastify.db.getAllBlockedUsers() ?? [];
|
||||
const userAreBlocked: boolean = isBlocked(UserAskingToBlock, UserToBlock, usersBlocked);
|
||||
if (userAreBlocked) {
|
||||
console.log(color.yellow, `'User :${UserAskingToBlock.name}) Hhas UN blocked ${UserToBlock.name}`);
|
||||
return UserAskingToBlock.name;
|
||||
}
|
||||
console.log(color.blue, `'User :${UserAskingToBlock.name}) has BBBblocked ${UserToBlock.name}`);
|
||||
|
||||
return '';
|
||||
};
|
||||
|
|
@ -35,6 +35,7 @@ export async function makeProfil(fastify: FastifyInstance, user: string, socket:
|
|||
SenderWindowID: socket.id,
|
||||
SenderName: '',
|
||||
Sendertext: '',
|
||||
SenderID: '',
|
||||
innerHtml: '',
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { FastifyPluginAsync } from 'fastify';
|
||||
import { Static, Type } from 'typebox';
|
||||
import { broadcast } from '../broadcast';
|
||||
|
||||
export const ChatReq = Type.Object({
|
||||
message: Type.String(),
|
||||
|
|
@ -19,7 +18,7 @@ const route: FastifyPluginAsync = async (fastify): Promise<void> => {
|
|||
config: { requireAuth: false },
|
||||
},
|
||||
async function(req, res) {
|
||||
broadcast(this, { command: '', destination: '', user: 'CMwaLeSever!!', text: req.body.message, SenderWindowID: 'server' });
|
||||
// broadcast(this, { command: '', destination: '', user: 'CMwaLeSever!!', text: req.body.message, SenderWindowID: 'server' });
|
||||
void res;
|
||||
},
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue