This commit is contained in:
NigeParis 2026-01-08 17:27:18 +01:00 committed by Nigel
parent dda51c1499
commit 8086444098
6 changed files with 106 additions and 197 deletions

View file

@ -9,12 +9,13 @@ import { getProfil } from './chatHelperFunctions/getProfil';
import { addMessage } from './chatHelperFunctions/addMessage'; import { addMessage } from './chatHelperFunctions/addMessage';
import { broadcastMsg } from './chatHelperFunctions/broadcastMsg'; import { broadcastMsg } from './chatHelperFunctions/broadcastMsg';
import { isLoggedIn } from './chatHelperFunctions/isLoggedIn'; import { isLoggedIn } from './chatHelperFunctions/isLoggedIn';
import type { ClientMessage, ClientProfil } from './types_front'; import type { ClientMessage, ClientProfil, ClientProfilPartial } from './types_front';
import { openProfilePopup } from './chatHelperFunctions/openProfilePopup'; import { openProfilePopup } from './chatHelperFunctions/openProfilePopup';
import { actionBtnPopUpBlock } from './chatHelperFunctions/actionBtnPopUpBlock'; import { actionBtnPopUpBlock } from './chatHelperFunctions/actionBtnPopUpBlock';
import { windowStateHidden } from './chatHelperFunctions/windowStateHidden'; import { windowStateHidden } from './chatHelperFunctions/windowStateHidden';
import type { blockedUnBlocked, obj } from './types_front'; import type { blockedUnBlocked, obj } from './types_front';
import { blockUser } from './chatHelperFunctions/blockUser'; import { blockUser } from './chatHelperFunctions/blockUser';
import type { User } from '@app/auth';
const MAX_SYSTEM_MESSAGES = 10; const MAX_SYSTEM_MESSAGES = 10;
let inviteMsgFlag: boolean = false; let inviteMsgFlag: boolean = false;
@ -425,6 +426,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
case '@msg': case '@msg':
broadcastMsg(socket, msgCommand); broadcastMsg(socket, msgCommand);
break; break;
case '@block': case '@block':
if (msgCommand[1] === '') {break;}; if (msgCommand[1] === '') {break;};
if (!userAskingToBlock) return; if (!userAskingToBlock) return;
@ -434,18 +436,14 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
destination: '', destination: '',
type: 'chat', type: 'chat',
user: msgCommand[1], user: msgCommand[1],
loginName: '',
userID: userId, userID: userId,
text: '',
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: '', SenderWindowID: socket.id,
SenderName: userAskingToBlock, SenderName: userAskingToBlock,
SenderID: '',
Sendertext: '',
innerHtml: '',
} }
blockUser(userToBlock, socket); blockUser(userToBlock, socket);
break; break;
case '@notify': case '@notify':
if (notify === null) {break;}; if (notify === null) {break;};
if (inviteMsgFlag === false) { if (inviteMsgFlag === false) {
@ -455,7 +453,6 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
notify.innerText = '🔕'; notify.innerText = '🔕';
inviteMsgFlag = false; inviteMsgFlag = false;
} }
break; break;
case '@guest': case '@guest':
@ -471,20 +468,14 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
noGuestFlag = false; noGuestFlag = false;
} }
if (guest) {noGuestFlag = true; noGuest.innerText = ''; sendtextbox.value = '';}; if (guest) {noGuestFlag = true; noGuest.innerText = ''; sendtextbox.value = '';};
const userProfile: ClientProfil = { const userProfile: ClientProfilPartial = {
command: '@noguest', command: '@noguest',
destination: '', destination: '',
type: 'chat', type: 'chat',
user: '', user: userAskingToBlock,
loginName: '',
userID: userId, userID: userId,
text: '',
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: '', SenderWindowID: '',
SenderName: userAskingToBlock,
SenderID: '',
Sendertext: '',
innerHtml: '',
guestmsg: noGuestFlag, guestmsg: noGuestFlag,
} }
socket.emit('guestmsg', JSON.stringify(userProfile)); socket.emit('guestmsg', JSON.stringify(userProfile));
@ -512,25 +503,26 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
addMessage('** *********************************** **'); addMessage('** *********************************** **');
addMessage('*'); addMessage('*');
break; break;
case '@quit': case '@quit':
quitChat(socket); quitChat(socket);
break; break;
default: default:
const user = getUser()?.name; const user: User | null = getUser();
const userID = getUser()?.id; if (!user) return;
// Ensure we have a user AND socket is connected
if (!user || !socket.connected) return; if (!user || !socket.connected) return;
const message = { const message: ClientProfilPartial = {
command: msgCommand[0], command: msgCommand[0],
destination: '', destination: '',
type: "chat", type: "chat",
user: user, user: user.name,
token: document.cookie ?? "", userID: user.id,
token: document.cookie ?? '',
text: msgCommand[1], text: msgCommand[1],
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id ?? '',
SenderID: userID, SenderID: user.id,
}; };
socket.emit('privMessage', JSON.stringify(message)); socket.emit('privMessage', JSON.stringify(message));
break; break;

View file

@ -1,4 +1,5 @@
import { Socket } from 'socket.io-client'; import { Socket } from 'socket.io-client';
import type { ClientProfil } from '../types_front';
/** /**
* getProfil of a user * getProfil of a user
@ -9,13 +10,14 @@ import { Socket } from 'socket.io-client';
export function getProfil(socket: Socket, user: string) { export function getProfil(socket: Socket, user: string) {
if (!socket.connected) return; if (!socket.connected) return;
const profil = { const profil: ClientProfil = {
command: '@profile', command: '@profile',
destination: 'profilMessage', destination: 'profilMessage',
type: "chat", type: "chat",
user: user, user: user,
token: document.cookie ?? "", token: document.cookie ?? "",
text: user, text: user,
userID: '',
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
}; };

View file

@ -17,24 +17,30 @@ export type ClientMessage = {
}; };
export type ClientProfil = { export type ClientProfil = ClientProfilPartial & {
command: string, loginName?: string,
destination: string, SenderName?: string,
type: string, Sendertext?: string,
user: string,
loginName: string,
userID: string,
text: string,
timestamp: number,
SenderWindowID:string,
SenderName: string,
SenderID: string,
Sendertext: string,
innerHtml?: string, innerHtml?: string,
guestmsg?: boolean,
}; };
export type ClientProfilPartial = {
command: string,
type: string,
destination: string,
user: string,
userID: string,
timestamp: number,
SenderWindowID?:string,
SenderID?: string,
text?: string,
token?: string
guestmsg?: boolean,
}
export type blockedUnBlocked = export type blockedUnBlocked =
{ {
userState: string, userState: string,

View file

@ -9,7 +9,7 @@ import { Server, Socket } from 'socket.io';
import type { User } from '@shared/database/mixin/user'; import type { User } from '@shared/database/mixin/user';
import type { BlockedData } from '@shared/database/mixin/blocked'; import type { BlockedData } from '@shared/database/mixin/blocked';
import { broadcast } from './chatBackHelperFunctions/broadcast'; import { broadcast } from './chatBackHelperFunctions/broadcast';
import type { ClientProfil, ClientMessage, obj } from './chat_types'; import type { ClientProfil, ClientMessage } from './chat_types';
import { sendPrivMessage } from './chatBackHelperFunctions/sendPrivMessage'; import { sendPrivMessage } from './chatBackHelperFunctions/sendPrivMessage';
import { sendBlocked } from './chatBackHelperFunctions/sendBlocked'; import { sendBlocked } from './chatBackHelperFunctions/sendBlocked';
import { sendInvite } from './chatBackHelperFunctions/sendInvite'; import { sendInvite } from './chatBackHelperFunctions/sendInvite';
@ -20,6 +20,8 @@ import { sendProfil } from './chatBackHelperFunctions/sendProfil';
import { setGameLink } from './setGameLink'; import { setGameLink } from './setGameLink';
import { nextGame_SocketListener } from './nextGame_SocketListener'; import { nextGame_SocketListener } from './nextGame_SocketListener';
import { list_SocketListener } from './chatBackHelperFunctions/list_SocketListener'; import { list_SocketListener } from './chatBackHelperFunctions/list_SocketListener';
import { isUser_BlockedBy_me } from './chatBackHelperFunctions/isUser_BlockedBy_me';
declare const __SERVICE_NAME: string; declare const __SERVICE_NAME: string;
@ -98,35 +100,6 @@ declare module 'fastify' {
} }
} }
/**
* 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) {
return '';
};
if (!UserAskingToBlock) {
return '';
};
const usersBlocked: BlockedData[] = fastify.db.getAllBlockedUsers() ?? [];
const userAreBlocked: boolean = isBlocked(UserAskingToBlock, UserToBlock, usersBlocked);
if (userAreBlocked) {
return UserAskingToBlock.name;
}
return '';
};
async function onReady(fastify: FastifyInstance) { async function onReady(fastify: FastifyInstance) {
const session = process.env.SESSION_MANAGER ?? ''; const session = process.env.SESSION_MANAGER ?? '';
if (session) { if (session) {
@ -138,6 +111,7 @@ async function onReady(fastify: FastifyInstance) {
fastify.io.on('connection', (socket: Socket) => { fastify.io.on('connection', (socket: Socket) => {
socket.on('message', (message: string) => { socket.on('message', (message: string) => {
const obj: ClientMessage = JSON.parse(message) as ClientMessage; const obj: ClientMessage = JSON.parse(message) as ClientMessage;
if (!obj.user || !socket.id) return;
clientChat.set(socket.id, { user: obj.user, socket: socket.id, lastSeen: Date.now() }); clientChat.set(socket.id, { user: obj.user, socket: socket.id, lastSeen: Date.now() });
socket.emit('welcome', { msg: 'Welcome to the chat! : ' }); socket.emit('welcome', { msg: 'Welcome to the chat! : ' });
broadcast(fastify, obj, obj.SenderWindowID); broadcast(fastify, obj, obj.SenderWindowID);
@ -166,16 +140,9 @@ async function onReady(fastify: FastifyInstance) {
destination: 'system-info', destination: 'system-info',
type: 'chat' as const, type: 'chat' as const,
user: clientName, user: clientName,
token: '',
text: 'LEFT the chat', text: 'LEFT the chat',
frontendUserName: '',
frontendUser: '',
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID: '',
}; };
broadcast(fastify, obj, socket.id); broadcast(fastify, obj, socket.id);
clientChat.delete(socket.id); clientChat.delete(socket.id);
@ -192,16 +159,9 @@ async function onReady(fastify: FastifyInstance) {
destination: 'system-info', destination: 'system-info',
type: 'chat', type: 'chat',
user: clientName, user: clientName,
token: '',
text: 'LEFT the chat', text: 'LEFT the chat',
frontendUserName: '',
frontendUser: '',
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID: '',
}; };
broadcast(fastify, obj, obj.SenderWindowID); broadcast(fastify, obj, obj.SenderWindowID);
} }
@ -216,16 +176,9 @@ async function onReady(fastify: FastifyInstance) {
destination: 'system-info', destination: 'system-info',
type: 'chat', type: 'chat',
user: clientName, user: clientName,
token: '',
text: 'LEFT the chat but the window is still open', text: 'LEFT the chat but the window is still open',
frontendUserName: '',
frontendUser: '',
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID: '',
}; };
broadcast(fastify, obj, obj.SenderWindowID); broadcast(fastify, obj, obj.SenderWindowID);
} }
@ -241,16 +194,9 @@ async function onReady(fastify: FastifyInstance) {
destination: 'privateMsg', destination: 'privateMsg',
type: 'chat', type: 'chat',
user: clientName, user: clientName,
token: '',
text: prvMessage.text, text: prvMessage.text,
frontendUserName: '',
frontendUser: '',
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID:'',
}; };
sendPrivMessage(fastify, obj, obj.SenderWindowID); sendPrivMessage(fastify, obj, obj.SenderWindowID);
} }
@ -275,6 +221,7 @@ async function onReady(fastify: FastifyInstance) {
socket.on('profilMessage', async (data: string) => { socket.on('profilMessage', async (data: string) => {
const clientName: string = clientChat.get(socket.id)?.user || ''; const clientName: string = clientChat.get(socket.id)?.user || '';
const profilMessage: ClientMessage = JSON.parse(data) || ''; const profilMessage: ClientMessage = JSON.parse(data) || '';
if (!profilMessage.user) return;
const profile: ClientProfil = await makeProfil(fastify, profilMessage.user, socket); const profile: ClientProfil = await makeProfil(fastify, profilMessage.user, socket);
if (clientName !== null) { if (clientName !== null) {
sendProfil(fastify, profile, profile.SenderWindowID); sendProfil(fastify, profile, profile.SenderWindowID);
@ -347,15 +294,11 @@ async function onReady(fastify: FastifyInstance) {
if (clientName !== null) { if (clientName !== null) {
const blockedMessage = 'I have un-blocked you'; const blockedMessage = 'I have un-blocked you';
if (clientName !== null) { if (clientName !== null) {
const obj: obj = { const obj: ClientProfil = {
command: 'message', command: 'message',
destination: 'privateMsg', destination: 'privateMsg',
type: 'chat', type: 'chat',
user: clientName, user: clientName,
token: '',
text: '',
frontendUserName: '',
frontendUser: '',
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: 'You have un-blocked', Sendertext: 'You have un-blocked',
@ -368,19 +311,15 @@ async function onReady(fastify: FastifyInstance) {
else { else {
fastify.db.addBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id); fastify.db.addBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id);
if (clientName !== null) { if (clientName !== null) {
const blockedMessage = 'I have blocked you'; const blockedMessage: string = 'I have blocked you';
profilBlock.Sendertext = 'You have blocked '; profilBlock.Sendertext = 'You have blocked ';
if (clientName !== null) { if (clientName !== null) {
const obj: obj = { const obj: ClientMessage = {
command: 'message', command: 'message',
destination: 'privateMsg', destination: 'privateMsg',
type: 'chat', type: 'chat',
user: clientName, user: clientName,
token: '',
text: '',
timestamp: Date.now(), timestamp: Date.now(),
frontendUserName: '',
frontendUser: '',
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: 'You have blocked', Sendertext: 'You have blocked',
}; };
@ -418,14 +357,9 @@ async function onReady(fastify: FastifyInstance) {
user: clientName, user: clientName,
frontendUserName: userNameFromFrontend, frontendUserName: userNameFromFrontend,
frontendUser: userFromFrontend, frontendUser: userFromFrontend,
token: '',
text: text, text: text,
timestamp: Date.now(), timestamp: Date.now(),
SenderWindowID: socket.id, SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID: '',
}; };
broadcast(fastify, obj, obj.SenderWindowID); broadcast(fastify, obj, obj.SenderWindowID);
} }

View file

@ -3,39 +3,14 @@ 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 { checkNamePair } from './checkNamePair';
import { whoBlockedMe } from './whoBlockedMe';
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 [] {
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
* it also sends a copy of the message to the sender * it also sends a copy of the message to the sender

View file

@ -2,17 +2,17 @@ export type ClientMessage = {
command: string command: string
destination: string; destination: string;
type: string, type: string,
user: string; user?: string;
userID: string, userID?: string,
token: string token?: string
frontendUserName: string, frontendUserName?: string,
frontendUser: string, frontendUser?: string,
text: string; text?: string;
SenderWindowID: string, SenderWindowID?: string,
SenderUserName: string, SenderUserName?: string,
SenderUserID: string, SenderUserID?: string,
timestamp: number, timestamp?: number,
Sendertext: string, Sendertext?: string,
innerHtml?: string, innerHtml?: string,
}; };
@ -22,34 +22,34 @@ export type ClientProfil = {
destination: string, destination: string,
type: string, type: string,
user: string, user: string,
loginName: string, loginName?: string,
userID: string, userID?: string,
text: string, text?: string,
timestamp: number, timestamp?: number,
SenderWindowID:string, SenderWindowID?:string,
SenderName: string, SenderName?: string,
SenderID: string, SenderID?: string,
Sendertext: string, Sendertext?: string,
innerHtml?: string, innerHtml?: string,
guestmsg?: boolean, guestmsg?: boolean,
}; };
export type obj = // export type obj =
{ // {
command: string, // command: string,
destination: string, // destination: string,
type: string, // type: string,
user: string, // user: string,
frontendUserName: string, // frontendUserName: string,
frontendUser: string, // frontendUser: string,
token: string, // token: string,
text: string, // text: string,
timestamp: number, // timestamp: number,
SenderWindowID: string, // SenderWindowID: string,
Sendertext: string, // Sendertext: string,
}; // };
export type BlockRelation = { export type BlockRelation = {
blocked: string; blocked: string;