First Major Clean Up Done

This commit is contained in:
NigeParis 2026-01-02 15:11:34 +01:00 committed by Maix0
parent 949e5a259a
commit afd79e334c
29 changed files with 46 additions and 118 deletions

View file

@ -2,18 +2,18 @@ import './chat.css';
import { addRoute, setTitle, type RouteHandlerParams, type RouteHandlerReturn } from "@app/routing";
import { showError } from "@app/toast";
import authHtml from './chat.html?raw';
import client from '@app/api'
import { getUser, updateUser } from "@app/auth";
import io, { Socket } from 'socket.io-client';
import { listBuddies } from './listBuddies';
import { getProfil } from './getProfil';
import { addMessage } from './addMessage';
import { broadcastMsg } from './broadcastMsg';
import { isLoggedIn } from './isLoggedIn';
import { listBuddies } from './chatHelperFunctions/listBuddies';
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 { openProfilePopup } from './openProfilePopup';
import { actionBtnPopUpBlock } from './actionBtnPopUpBlock';
import { windowStateHidden } from './windowStateHidden';
import { openProfilePopup } from './chatHelperFunctions/openProfilePopup';
import { actionBtnPopUpBlock } from './chatHelperFunctions/actionBtnPopUpBlock';
import { windowStateHidden } from './chatHelperFunctions/windowStateHidden';
import type { blockedUnBlocked, obj } from './types_front';
export const color = {
red: 'color: red;',
@ -24,28 +24,6 @@ export const color = {
};
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,
};
const MAX_SYSTEM_MESSAGES = 10;
let inviteMsgFlag: boolean = false;
@ -513,10 +491,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
SenderID: userID,
};
//socket.emit('MsgObjectServer', message);
// addMessage(message.command);
socket.emit('privMessage', JSON.stringify(message));
// addMessage(JSON.stringify(message));
break;
}
// Clear the input in all cases
@ -530,7 +505,6 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
if (chatWindow) {
chatWindow.innerHTML = '';
}
//clearChatWindow(socket); //DEV testing broadcastGames
});
// Dev Game message button
@ -550,12 +524,6 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
sendButton?.click();
}
});
// Whoami button to display user name addMessage(msgCommand[0]);
// bwhoami?.addEventListener('click', async () => {
// whoami(socket);
// });
}
}

View file

@ -1,5 +1,5 @@
import { Socket } from 'socket.io-client';
import type { ClientProfil } from './types_front';
import type { ClientProfil } from '../types_front';
import { blockUser } from './blockUser';
export function actionBtnPopUpBlock(block: ClientProfil, senderSocket: Socket) {

View file

@ -1,5 +1,3 @@
import { color } from './chat';
/**
* function adds a message to the frontend chatWindow
* @param text
@ -13,6 +11,5 @@ export function addMessage(text: string) {
messageElement.textContent = text;
chatWindow.appendChild(messageElement);
chatWindow.scrollTop = chatWindow.scrollHeight;
console.log(`%c DEBUG LOG: Added new message:%c ${text}`, color.red, color.reset);
return ;
};

View file

@ -1,11 +1,9 @@
import { Socket } from 'socket.io-client';
import type { ClientProfil } from './types_front';
import type { ClientProfil } from '../types_front';
import { getUser } from "@app/auth";
export function blockUser(profil: ClientProfil, senderSocket: Socket) {
profil.SenderName = getUser()?.name ?? '';
if (profil.SenderName === profil.user) return;
// addMessage(`${profil.Sendertext}: ${profil.user}⛔`)
senderSocket.emit('blockUser', JSON.stringify(profil));
};

View file

@ -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";
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

View file

@ -1,4 +1,4 @@
import io, { Socket } from 'socket.io-client';
import { Socket } from 'socket.io-client';
/**
* function clears all messages in the chat window
@ -9,5 +9,4 @@ export function clearChatWindow(senderSocket: Socket) {
const chatWindow = document.getElementById("t-chatbox") as HTMLDivElement;
if (!chatWindow) return;
chatWindow.innerHTML = "";
// senderSocket.emit('nextGame');
}

View file

@ -19,6 +19,5 @@ export function getProfil(socket: Socket, user: string) {
timestamp: Date.now(),
SenderWindowID: socket.id,
};
// addMessage(JSON.stringify(profil));
socket.emit('profilMessage', JSON.stringify(profil));
}

View file

@ -27,20 +27,16 @@ export async function listBuddies(socket: Socket, buddies: HTMLDivElement, listB
const user = getUser()?.name ?? "";
buddies.appendChild(buddiesElement);
buddies.scrollTop = buddies.scrollHeight;
console.log(`Added buddies: ${bud}`);
buddiesElement.style.cursor = "pointer";
buddiesElement.addEventListener("click", () => {
navigator.clipboard.writeText(bud);
if (bud !== user && user !== "") {
sendtextbox.value = `@${bud}: `;
console.log("Copied to clipboard:", bud);
sendtextbox.focus();
}
});
buddiesElement.addEventListener("dblclick", () => {
console.log("Open profile:", bud);
getProfil(socket, bud);
sendtextbox.value = "";
});

View file

@ -1,4 +1,4 @@
import type { ClientProfil } from './types_front';
import type { ClientProfil } from '../types_front';
export async function openProfilePopup(profil: ClientProfil) {
const modalname = document.getElementById("modal-name") ?? null;

View file

@ -1,4 +1,4 @@
import { __socket } from './chat';
import { __socket } from '../chat';
import { updateUser } from "@app/auth";
export async function windowStateHidden() {

View file

@ -34,6 +34,13 @@ export type ClientProfil = {
};
export type blockedUnBlocked =
{
userState: string,
userTarget: string,
by: string,
};
export type obj =
{
command: string,

View file

@ -8,18 +8,18 @@ import * as utils from '@shared/utils';
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 { broadcast } from './chatBackHelperFunctions/broadcast';
import type { ClientProfil, ClientMessage, obj } from './chat_types';
import { sendPrivMessage } from './sendPrivMessage';
import { sendBlocked } from './sendBlocked';
import { sendInvite } from './sendInvite';
import { getUserByName } from './getUserByName';
import { makeProfil } from './makeProfil';
import { isBlocked } from './isBlocked';
import { sendProfil } from './sendProfil';
import { sendPrivMessage } from './chatBackHelperFunctions/sendPrivMessage';
import { sendBlocked } from './chatBackHelperFunctions/sendBlocked';
import { sendInvite } from './chatBackHelperFunctions/sendInvite';
import { getUserByName } from './chatBackHelperFunctions/getUserByName';
import { makeProfil } from './chatBackHelperFunctions/makeProfil';
import { isBlocked } from './chatBackHelperFunctions/isBlocked';
import { sendProfil } from './chatBackHelperFunctions/sendProfil';
import { setGameLink } from './setGameLink';
import { nextGame_SocketListener } from './nextGame_SocketListener';
import { list_SocketListener } from './list_SocketListener';
import { list_SocketListener } from './chatBackHelperFunctions/list_SocketListener';
// colors for console.log
export const color = {

View file

@ -1,5 +1,5 @@
import type { ClientMessage, BlockRelation } from './chat_types';
import { clientChat } from './app';
import type { ClientMessage, BlockRelation } from '../chat_types';
import { clientChat } from '../app';
import { FastifyInstance } from 'fastify';
import { getUserByName } from './getUserByName';
import type { User } from '@shared/database/mixin/user';

View file

@ -1,4 +1,4 @@
import type { BlockRelation } from './chat_types';
import type { BlockRelation } from '../chat_types';
export function checkNamePair(list: BlockRelation[], name1: string, name2: string): (boolean) {
const matches: BlockRelation[] = [];

View file

@ -1,4 +1,3 @@
// colors for console.log
export const color = {
red: '\x1b[31m',
green: '\x1b[32m',

View file

@ -1,4 +1,4 @@
import { clientChat } from './app';
import { clientChat } from '../app';
import { Server, Socket } from 'socket.io';
/**

View file

@ -1,4 +1,4 @@
import type { ClientMessage } from './chat_types';
import type { ClientMessage } from '../chat_types';
import { FastifyInstance } from 'fastify';
import type { User } from '@shared/database/mixin/user';
import { getUserById } from './getUserById';
@ -17,11 +17,6 @@ export function filter_Blocked_user(fastify: FastifyInstance, data: ClientMessag
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)) {

View file

@ -3,7 +3,6 @@ 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
@ -19,20 +18,15 @@ export function isUser_BlockedBy_me(fastify: FastifyInstance, blockedBy_Id : str
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 '';
};

View file

@ -1,8 +1,7 @@
import type { FastifyInstance } from 'fastify';
import { Socket } from 'socket.io';
import { clientChat } from './app';
import { clientChat } from '../app';
import { connectedUser } from './connectedUser';
// import { color } from './app';
export function list_SocketListener(fastify: FastifyInstance, socket: Socket) {
@ -10,11 +9,8 @@ export function list_SocketListener(fastify: FastifyInstance, socket: Socket) {
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) {
@ -23,5 +19,4 @@ export function list_SocketListener(fastify: FastifyInstance, socket: Socket) {
}
connectedUser(fastify.io, socket.id);
});
}

View file

@ -1,5 +1,5 @@
import { FastifyInstance } from 'fastify';
import type { ClientProfil } from './chat_types';
import type { ClientProfil } from '../chat_types';
import type { User } from '@shared/database/mixin/user';
import { getUserByName } from './getUserByName';
import { Socket } from 'socket.io';

View file

@ -1,5 +1,5 @@
import type { ClientProfil } from './chat_types';
import { clientChat } from './app';
import type { ClientProfil } from '../chat_types';
import { clientChat } from '../app';
import { FastifyInstance } from 'fastify';
/**

View file

@ -1,5 +1,5 @@
import type { ClientMessage, ClientProfil } from './chat_types';
import { clientChat, color } from './app';
import type { ClientMessage, ClientProfil } from '../chat_types';
import { clientChat } from '../app';
import { FastifyInstance } from 'fastify';
import { sendPrivMessage } from './sendPrivMessage';
@ -16,15 +16,12 @@ export function sendInvite(fastify: FastifyInstance, innerHtml: string, profil:
fastify.io.fetchSockets().then((sockets) => {
let targetSocket;
for (const socket of sockets) {
console.log(color.yellow, 'DEBUG LOG: sendInvite Function');
const clientInfo: string = clientChat.get(socket.id)?.user || '';
console.log(color.green, 'AskingName=', profil.SenderName);
targetSocket = socket || null;
if (!targetSocket) continue;
if (clientInfo === profil.user) {
profil.innerHtml = innerHtml ?? '';
if (targetSocket.id) {
const data: ClientMessage = {
command: `@${clientInfo}`,
destination: 'inviteMsg',
@ -41,18 +38,8 @@ export function sendInvite(fastify: FastifyInstance, innerHtml: string, profil:
SenderUserID: '',
Sendertext: '',
innerHtml: innerHtml,
};
console.log(color.yellow, 'DEBUG LOG: sendInvite Function -> sendPrivMessage :');
sendPrivMessage(fastify, data, '');
// targetSocket.emit('MsgObjectServer', { message: data });
// targetSocket.emit('inviteGame', profil);
}
return;
}

View file

@ -1,5 +1,5 @@
import type { ClientMessage } from './chat_types';
import { clientChat, color } from './app';
import type { ClientMessage } from '../chat_types';
import { clientChat } from '../app';
import { FastifyInstance } from 'fastify';
import { getUserByName } from './getUserByName';
import type { User } from '@shared/database/mixin/user';
@ -20,7 +20,6 @@ function checkNamePair(list: BlockRelation[], name1: string, name2: string): (bo
return true;;
}
}
console.log(color.red, `'RETURN FLAG CheckNamePair ${exists}' item: ${name1} item2: ${name2}`);
}
return exists;
}
@ -55,7 +54,6 @@ export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMess
const list:BlockRelation[] = whoBlockedMe(fastify, UserID);
const clientInfo = clientChat.get(socket.id);
if (!clientInfo?.user) {
console.log(color.red, `Skipping socket ${socket.id} (no user found)`);
continue;
}
let blockMsgFlag: boolean = false;
@ -67,14 +65,11 @@ export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMess
const user: string = clientChat.get(socket.id)?.user ?? '';
const atUser = `@${user}`;
if (atUser !== data.command || atUser === '' || data.text === '') {
console.log(color.red, 'DEBUG: atUser !== data.command');
continue;
}
blockMsgFlag = checkNamePair(list, UserID, UserByID.id) || false;
if (socket.id === sender) {
console.log(color.blue, 'sKip Sender ', socket.id);
continue;
}
if (!blockMsgFlag) {

View file

@ -1,5 +1,5 @@
import { FastifyInstance } from 'fastify';
import type { ClientProfil } from './chat_types';
import type { ClientProfil } from '../chat_types';
/**
* function takes a user profil and sends it to the asker by window id
@ -12,7 +12,6 @@ export function sendProfil(fastify: FastifyInstance, profil: ClientProfil, Sende
fastify.io.fetchSockets().then((sockets) => {
const senderSocket = sockets.find(socket => socket.id === SenderWindowID);
if (senderSocket) {
// console.log(color.yellow, 'DEBUG LOG: profil.info:', profil.user);
senderSocket.emit('profilMessage', profil);
}
});

View file

@ -1,5 +1,5 @@
import { FastifyInstance } from 'fastify';
import type { BlockRelation } from './chat_types';
import type { BlockRelation } from '../chat_types';
export function whoBlockedMe(fastify: FastifyInstance, myID: string): BlockRelation [] {
const usersBlocked =