fastify.log.info done

This commit is contained in:
NigeParis 2026-01-05 15:30:29 +01:00 committed by Maix0
parent 69ba60b03d
commit b69a452810
13 changed files with 82 additions and 118 deletions

View file

@ -21,26 +21,14 @@ import { setGameLink } from './setGameLink';
import { nextGame_SocketListener } from './nextGame_SocketListener';
import { list_SocketListener } from './chatBackHelperFunctions/list_SocketListener';
// colors for console.log
export const color = {
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
reset: '\x1b[0m',
};
declare const __SERVICE_NAME: string;
// Global map of clients
// key = socket, value = clientname
interface ClientInfo {
user: string;
socket: string
lastSeen: number;
}
export type blockedUnBlocked =
{
userState: string,
@ -125,20 +113,16 @@ function isUser_BlockedBy_me(fastify: FastifyInstance, blockedBy_Id : string, is
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 '';
};
@ -147,7 +131,7 @@ async function onReady(fastify: FastifyInstance) {
if (session) {
const part = session.split('/')[1];
const machineName = part.split('.')[0];
console.log(color.yellow, 'Connect at : https://' + machineName + ':8888/app/login');
fastify.log.info(`Connect at : https://${machineName}:8888/`);
}
fastify.io.on('connection', (socket: Socket) => {
@ -156,6 +140,7 @@ async function onReady(fastify: FastifyInstance) {
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);
fastify.log.info(`Client connected: ${socket.id}`);
});
nextGame_SocketListener(fastify, socket);
list_SocketListener(fastify, socket);
@ -166,7 +151,6 @@ async function onReady(fastify: FastifyInstance) {
if (userFromFrontend.oldUser !== userFromFrontend.user) {
if (client) {
client.user = userFromFrontend.user;
console.log(color.yellow, `'DEBUG LOG: client.user is, '${client.user}'`);
}
}
});
@ -218,7 +202,6 @@ async function onReady(fastify: FastifyInstance) {
SenderUserName: '',
SenderUserID: '',
};
broadcast(fastify, obj, obj.SenderWindowID);
}
});
@ -272,6 +255,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) || '';
@ -287,7 +271,6 @@ async function onReady(fastify: FastifyInstance) {
const inviteHtml: string = 'invites you to a game ' + setGameLink('');
if (clientName !== null) {
console.log(`DEBUG LOG: clientName =${clientName}`);
sendInvite(fastify, inviteHtml, profilInvite);
}
});
@ -397,29 +380,18 @@ async function onReady(fastify: FastifyInstance) {
const userNameFromFrontend = data?.userName || null;
const userFromFrontend = data?.user || null;
let clientName = clientChat.get(socket.id)?.user || null;
// const client = clientChat.get(socket.id) || null;
let text = 'is back in the chat';
if (clientName === null) {
console.log('ERROR: clientName is NULL'); return;
fastify.log.error('ERROR: clientName is NULL'); return;
};
// if (client === null) {
// console.log('ERROR: client is NULL'); return;
// };
if (userNameFromFrontend !== userFromFrontend) {
text = `'is back in the chat, I used to be called '${userNameFromFrontend}`;
clientName = userFromFrontend;
if (clientName === null) {
console.log('ERROR: clientName is NULL'); return;
fastify.log.error('ERROR: clientName is NULL'); return;
};
// if (client) {
// client.user = clientName;
// }
}
// console.log(
// color.green,
// `Client entered the Chat: ${clientName} (${socket.id})`,
// );
if (clientName !== null) {
const obj: ClientMessage = {
command: '',

View file

@ -9,7 +9,6 @@ import { clientChat } from './app';
export async function broadcastNextGame(fastify: FastifyInstance, gameLink?: Promise<string>) {
const link = gameLink ? await gameLink : undefined;
const sockets = await fastify.io.fetchSockets();
// fastify.io.fetchSockets().then((sockets) => {
for (const socket of sockets) {
const clientInfo = clientChat.get(socket.id);
if (!clientInfo?.user) {
@ -18,6 +17,5 @@ export async function broadcastNextGame(fastify: FastifyInstance, gameLink?: Pro
if (link) {
socket.emit('nextGame', link);
}
// console.log(color.green, `'DEBUG LOG: Broadcast to:', ${data.command} message: ${data.text}`);
}
};

View file

@ -27,6 +27,9 @@ export async function broadcast(fastify: FastifyInstance, data: ClientMessage, s
}
if (!blockMsgFlag) {
socket.emit('MsgObjectServer', { message: data });
if (data.SenderUserID) {
fastify.log.info({ senderID: data.SenderUserID, msgBroadcast: data.text });
}
}
}
}

View file

@ -3,6 +3,16 @@ import { clientChat } from '../app';
import { FastifyInstance } from 'fastify';
import { sendPrivMessage } from './sendPrivMessage';
/**
* TODO
* function needed to transfer the game number
*/
function getGameNumber():string {
const gameNumber = '123456GameNum';
return gameNumber;
}
/**
* function looks for the user online in the chat
* and sends emit to invite - format HTML to make clickable
@ -10,39 +20,38 @@ import { sendPrivMessage } from './sendPrivMessage';
* @param fastify
* @param innerHtml
* @param profil
*/
*/
export function sendInvite(fastify: FastifyInstance, innerHtml: string, profil: ClientProfil) {
fastify.io.fetchSockets().then((sockets) => {
let targetSocket;
for (const socket of sockets) {
const clientInfo: string = clientChat.get(socket.id)?.user || '';
targetSocket = socket || null;
if (!targetSocket) continue;
if (clientInfo === profil.user) {
profil.innerHtml = innerHtml ?? '';
if (targetSocket.id) {
const data: ClientMessage = {
command: `@${clientInfo}`,
destination: 'inviteMsg',
type: 'chat',
user: profil.SenderName,
token: '',
text: ' needs some text to work',
timestamp: Date.now(),
SenderWindowID: socket.id,
userID: '',
frontendUserName: '',
frontendUser: '',
SenderUserName: profil.SenderName,
SenderUserID: '',
Sendertext: '',
innerHtml: innerHtml,
};
sendPrivMessage(fastify, data, '');
}
return;
export async function sendInvite(fastify: FastifyInstance, innerHtml: string, profil: ClientProfil) {
const sockets = await fastify.io.fetchSockets();
let targetSocket;
for (const socket of sockets) {
const clientInfo: string = clientChat.get(socket.id)?.user || '';
targetSocket = socket || null;
if (!targetSocket) continue;
if (clientInfo === profil.user) {
profil.innerHtml = innerHtml ?? '';
if (targetSocket.id) {
const data: ClientMessage = {
command: `@${clientInfo}`,
destination: 'inviteMsg',
type: 'chat',
user: profil.SenderName,
token: '',
text: getGameNumber(),
timestamp: Date.now(),
SenderWindowID: socket.id,
userID: '',
frontendUserName: '',
frontendUser: '',
SenderUserName: profil.SenderName,
SenderUserID: '',
Sendertext: '',
innerHtml: innerHtml,
};
sendPrivMessage(fastify, data, '');
}
return;
}
});
}
}
}

View file

@ -42,22 +42,22 @@ function whoBlockedMe(fastify: FastifyInstance, myID: string): BlockRelation []
* @param fastify
* @param data
* @param sender
*/
*/
export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMessage, sender?: string) {
const sockets = await fastify.io.fetchSockets();
const AllusersBlocked: User[] = fastify.db.getAllUsers() ?? [];
const allUsers: User[] = fastify.db.getAllUsers() ?? [];
const senderSocket = sockets.find(socket => socket.id === sender);
for (const socket of sockets) {
const UserID = getUserByName(AllusersBlocked, data.user)?.id ?? '';
const UserID = getUserByName(allUsers, data.user)?.id ?? '';
const list:BlockRelation[] = whoBlockedMe(fastify, UserID);
const clientInfo = clientChat.get(socket.id);
if (!clientInfo?.user) {
continue;
}
let blockMsgFlag: boolean = false;
const UserByID = getUserByName(AllusersBlocked, clientInfo.user) ?? '';
const UserByID = getUserByName(allUsers, clientInfo.user) ?? '';
if (UserByID === '') {
return;
}
@ -74,6 +74,7 @@ export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMess
}
if (!blockMsgFlag) {
socket.emit('MsgObjectServer', { message: data });
fastify.log.info({ senderID: `${UserID}`, msgPriv: data.text, target: `${UserByID.id}` });
if (senderSocket) {
senderSocket.emit('privMessageCopy', `${data.command}: ${data.text}🔒`);
}

View file

@ -6,13 +6,12 @@ import type { ClientProfil } from '../chat_types';
* @param fastify
* @param profil
* @param SenderWindowID
*/
*/
export function sendProfil(fastify: FastifyInstance, profil: ClientProfil, SenderWindowID?: string) {
fastify.io.fetchSockets().then((sockets) => {
const senderSocket = sockets.find(socket => socket.id === SenderWindowID);
if (senderSocket) {
senderSocket.emit('profilMessage', profil);
}
});
export async function sendProfil(fastify: FastifyInstance, profil: ClientProfil, SenderWindowID?: string) {
const sockets = await fastify.io.fetchSockets();
const senderSocket = sockets.find(socket => socket.id === SenderWindowID);
if (senderSocket) {
senderSocket.emit('profilMessage', profil);
}
}