Login Name: '${allUsers?.login ?? 'Guest'}'
+
+
+ Joined: xx/xx/xx
+ Last connection: ${lastSeen}
+ About: No description
+ `;
+ break;
+ }
+ };
+ return profilHtmlPopup;
+ };
+
+ function sendProfil(data: ClientMessage, clientProfil?: string) {
+
+ fastify.io.fetchSockets().then((sockets) => {
+ const senderSocket = sockets.find(socket => socket.id === clientProfil);
+ for (const socket of sockets) {
+ const clientInfo = clientChat.get(socket.id);
+ if (clientInfo?.user === data.user) {
+ if (senderSocket) {
+ socket.emit('profilMessage', `${data.text}`);
+ }
+ }
+ }
+ });
+ }
function sendPrivMessage(data: ClientMessage, sender?: string) {
fastify.io.fetchSockets().then((sockets) => {
@@ -186,20 +239,20 @@ async function onReady(fastify: FastifyInstance) {
console.log(color.yellow, `Skipping socket ${s.id} (no user found)`);
continue;
}
- let user: string = clientChat.get(s.id)?.user ?? "";
+ const user: string = clientChat.get(s.id)?.user ?? '';
const atUser = `@${user}`;
- if (atUser !== data.command || atUser === "") {
+ if (atUser !== data.command || atUser === '') {
console.log(color.yellow, `DEBUG LOG: User: '${atUser}' command NOT FOUND: '${data.command[0]}' `);
continue;
}
- if (data.text !== "") {
+ if (data.text !== '') {
s.emit('MsgObjectServer', { message: data });
console.log(color.yellow, `DEBUG LOG: User: '${atUser}' command FOUND: '${data.command}' `);
- if (senderSocket)
- senderSocket.emit('privMessageCopy',`${data.command}: ${data.text}🔒`);
- // Debug logs
+ if (senderSocket) {
+ senderSocket.emit('privMessageCopy', `${data.command}: ${data.text}🔒`);
+ }
}
- console.log(color.green, `'Priv to:', ${data.command} message: ${data.text}`);
+ console.log(color.green, `DEBUG LOG: 'Priv to:', ${data.command} message: ${data.text}`);
}
});
}
@@ -355,12 +408,12 @@ async function onReady(fastify: FastifyInstance) {
socket.on('privMessage', (data) => {
- const clientName: string = clientChat.get(socket.id)?.user || "";
- const prvMessage: ClientMessage = JSON.parse(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
+ prvMessage.command,
);
if (clientName !== null) {
@@ -374,12 +427,42 @@ async function onReady(fastify: FastifyInstance) {
timestamp: Date.now(),
SenderWindowID: socket.id,
};
- console.log(color.blue, 'PRIV MESSAGE OUT :', obj.SenderWindowID);
+ console.log(color.blue, 'DEBUG LOG: PRIV MESSAGE OUT :', obj.SenderWindowID);
sendPrivMessage(obj, obj.SenderWindowID);
// clientChat.delete(obj.user);
}
});
+ socket.on('profilMessage', async (data) => {
+ const clientName: string = clientChat.get(socket.id)?.user || '';
+ const profilMessage: ClientMessage = JSON.parse(data) || '';
+ 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 profileHtml: string = await getProfil(profilMessage.user);
+ if (clientName !== null) {
+ const testuser: User | null = getUserByName(users, profilMessage.user);
+ console.log(color.yellow, 'user:', testuser?.login ?? 'Guest');
+ const obj = {
+ command: profilMessage.command,
+ destination: 'profilMsg',
+ type: 'chat',
+ user: clientName,
+ token: '',
+ text: profileHtml,
+ timestamp: Date.now(),
+ SenderWindowID: socket.id,
+ };
+ console.log(color.blue, 'DEBUG - profil message MESSAGE OUT :', obj.SenderWindowID);
+ sendProfil(obj, obj.SenderWindowID);
+ // clientChat.delete(obj.user);
+ }
+ });
+
socket.on('client_entered', (data) => {
// data may be undefined (when frontend calls emit with no payload)