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

View file

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

View file

@ -17,24 +17,30 @@ export type ClientMessage = {
};
export type ClientProfil = {
command: string,
destination: string,
type: string,
user: string,
loginName: string,
userID: string,
text: string,
timestamp: number,
SenderWindowID:string,
SenderName: string,
SenderID: string,
Sendertext: string,
export type ClientProfil = ClientProfilPartial & {
loginName?: string,
SenderName?: string,
Sendertext?: 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 =
{
userState: 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 './chatBackHelperFunctions/broadcast';
import type { ClientProfil, ClientMessage, obj } from './chat_types';
import type { ClientProfil, ClientMessage } from './chat_types';
import { sendPrivMessage } from './chatBackHelperFunctions/sendPrivMessage';
import { sendBlocked } from './chatBackHelperFunctions/sendBlocked';
import { sendInvite } from './chatBackHelperFunctions/sendInvite';
@ -20,6 +20,8 @@ import { sendProfil } from './chatBackHelperFunctions/sendProfil';
import { setGameLink } from './setGameLink';
import { nextGame_SocketListener } from './nextGame_SocketListener';
import { list_SocketListener } from './chatBackHelperFunctions/list_SocketListener';
import { isUser_BlockedBy_me } from './chatBackHelperFunctions/isUser_BlockedBy_me';
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) {
const session = process.env.SESSION_MANAGER ?? '';
if (session) {
@ -138,6 +111,7 @@ async function onReady(fastify: FastifyInstance) {
fastify.io.on('connection', (socket: Socket) => {
socket.on('message', (message: string) => {
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() });
socket.emit('welcome', { msg: 'Welcome to the chat! : ' });
broadcast(fastify, obj, obj.SenderWindowID);
@ -166,16 +140,9 @@ async function onReady(fastify: FastifyInstance) {
destination: 'system-info',
type: 'chat' as const,
user: clientName,
token: '',
text: 'LEFT the chat',
frontendUserName: '',
frontendUser: '',
timestamp: Date.now(),
SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID: '',
};
broadcast(fastify, obj, socket.id);
clientChat.delete(socket.id);
@ -192,16 +159,9 @@ async function onReady(fastify: FastifyInstance) {
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);
}
@ -216,16 +176,9 @@ async function onReady(fastify: FastifyInstance) {
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: '',
};
broadcast(fastify, obj, obj.SenderWindowID);
}
@ -241,16 +194,9 @@ async function onReady(fastify: FastifyInstance) {
destination: 'privateMsg',
type: 'chat',
user: clientName,
token: '',
text: prvMessage.text,
frontendUserName: '',
frontendUser: '',
timestamp: Date.now(),
SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID:'',
};
sendPrivMessage(fastify, obj, obj.SenderWindowID);
}
@ -275,6 +221,7 @@ async function onReady(fastify: FastifyInstance) {
socket.on('profilMessage', async (data: string) => {
const clientName: string = clientChat.get(socket.id)?.user || '';
const profilMessage: ClientMessage = JSON.parse(data) || '';
if (!profilMessage.user) return;
const profile: ClientProfil = await makeProfil(fastify, profilMessage.user, socket);
if (clientName !== null) {
sendProfil(fastify, profile, profile.SenderWindowID);
@ -347,15 +294,11 @@ async function onReady(fastify: FastifyInstance) {
if (clientName !== null) {
const blockedMessage = 'I have un-blocked you';
if (clientName !== null) {
const obj: obj = {
const obj: ClientProfil = {
command: 'message',
destination: 'privateMsg',
type: 'chat',
user: clientName,
token: '',
text: '',
frontendUserName: '',
frontendUser: '',
timestamp: Date.now(),
SenderWindowID: socket.id,
Sendertext: 'You have un-blocked',
@ -368,19 +311,15 @@ async function onReady(fastify: FastifyInstance) {
else {
fastify.db.addBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id);
if (clientName !== null) {
const blockedMessage = 'I have blocked you';
const blockedMessage: string = 'I have blocked you';
profilBlock.Sendertext = 'You have blocked ';
if (clientName !== null) {
const obj: obj = {
const obj: ClientMessage = {
command: 'message',
destination: 'privateMsg',
type: 'chat',
user: clientName,
token: '',
text: '',
timestamp: Date.now(),
frontendUserName: '',
frontendUser: '',
SenderWindowID: socket.id,
Sendertext: 'You have blocked',
};
@ -418,14 +357,9 @@ async function onReady(fastify: FastifyInstance) {
user: clientName,
frontendUserName: userNameFromFrontend,
frontendUser: userFromFrontend,
token: '',
text: text,
timestamp: Date.now(),
SenderWindowID: socket.id,
Sendertext: '',
userID: '',
SenderUserName: '',
SenderUserID: '',
};
broadcast(fastify, obj, obj.SenderWindowID);
}

View file

@ -3,39 +3,14 @@ import { clientChat } from '../app';
import { FastifyInstance } from 'fastify';
import { getUserByName } from './getUserByName';
import type { User } from '@shared/database/mixin/user';
import { checkNamePair } from './checkNamePair';
import { whoBlockedMe } from './whoBlockedMe';
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
* it also sends a copy of the message to the sender

View file

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