Added block invites

This commit is contained in:
NigeParis 2025-12-29 12:15:19 +01:00 committed by Maix0
parent 69c00f909f
commit da8298dce8
6 changed files with 66 additions and 11 deletions

View file

@ -340,6 +340,25 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
}
if (chatWindow && data.message.destination === "inviteMsg") {
const messageElement = document.createElement("div-private");
const chatWindow = document.getElementById("t-chatbox") as HTMLDivElement;
messageElement.innerHTML = `🎃${data.message.SenderUserName}: ${data.message.innerHtml}`;
chatWindow.appendChild(messageElement);
chatWindow.scrollTop = chatWindow.scrollHeight;
}
const MAX_SYSTEM_MESSAGES = 10;
if (systemWindow && data.message.destination === "system-info") {

View file

@ -13,6 +13,7 @@ export type ClientMessage = {
SenderUserID: string,
timestamp: number,
Sendertext: string,
innerHtml?: string,
};

View file

@ -334,6 +334,7 @@ async function onReady(fastify: FastifyInstance) {
const inviteHtml: string = 'invites you to a game ' + setGameLink('');
if (clientName !== null) {
console.log(`DEBUG LOG: clientName =${clientName}`);
// const testuser: User | null = getUserByName(users, profilInvite.user ?? '');
// console.log(color.yellow, 'user:', testuser?.name ?? 'Guest');
sendInvite(fastify, inviteHtml, profilInvite);

View file

@ -13,6 +13,7 @@ export type ClientMessage = {
SenderUserID: string,
timestamp: number,
Sendertext: string,
innerHtml?: string,
};

View file

@ -1,6 +1,7 @@
import type { ClientProfil } from './chat_types';
import type { ClientMessage, ClientProfil } from './chat_types';
import { clientChat, color } from './app';
import { FastifyInstance } from 'fastify';
import { sendPrivMessage } from './sendPrivMessage';
/**
* function looks for the user online in the chat
@ -15,14 +16,43 @@ 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;
console.log(color.yellow, 'DEBUG LOG: user online found', profil.user, 'socket', targetSocket.id);
if (clientInfo === profil.user) {
profil.innerHtml = innerHtml ?? '';
if (targetSocket.id) {
targetSocket.emit('inviteGame', profil);
const data: ClientMessage = {
command: `@${clientInfo}`,
destination: 'inviteMsg',
type: "chat",
user: profil.SenderName,
token: '',
text: ' needs this to work',
timestamp: Date.now(),
SenderWindowID: socket.id,
userID: '',
frontendUserName: '',
frontendUser: '',
SenderUserName: profil.SenderName,
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

@ -9,7 +9,7 @@ type BlockRelation = {
blocker: string;
};
function checkNamePair(list: BlockRelation[], name1: string, name2: string): (boolean) {
function checkNamePair(list: BlockRelation[], name1: string, name2: string): (boolean) {
const matches: BlockRelation[] = [];
let exists: boolean = false;
for (const item of list) {
@ -46,7 +46,7 @@ function whoBlockedMe(fastify: FastifyInstance, myID: string): BlockRelation []
*/
export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMessage, sender?: string) {
const sockets = await fastify.io.fetchSockets();
const AllusersBlocked: User[] = fastify.db.getAllUsers() ?? [];
const senderSocket = sockets.find(socket => socket.id === sender);
@ -60,17 +60,20 @@ export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMess
}
let blockMsgFlag: boolean = false;
const UserByID = getUserByName(AllusersBlocked, clientInfo.user) ?? '';
if (UserByID === '') return;
if (UserByID === ''){
return;
}
const user: string = clientChat.get(socket.id)?.user ?? '';
const atUser = `@${user}`;
console.log(color.red, `'DEBUG LOG:'${atUser}' '${data.command}'`)
if (atUser !== data.command || atUser === '' || data.text === '') {
console.log(color.red, 'DEBUG: atUser !== data.command');
continue;
}
console.log(color.green, `USER ID: ${UserID} userName: ${UserByID.name} iD:${UserByID.id}`);
blockMsgFlag = checkNamePair(list, UserID, UserByID.id) || false;
console.log(color.green, `USER ID: ${UserID} data.user: ${data.user} blockFlag: ${blockMsgFlag} userName: ${UserByID.name} iD:${UserByID.id}`);
if (socket.id === sender) {
console.log(color.blue, 'sKip Sender ', socket.id);
@ -78,7 +81,7 @@ export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMess
}
console.log(color.yellow, `blockFlag=${blockMsgFlag}: Target ${clientInfo.user}`);
if (!blockMsgFlag) {
console.log(color.blue, 'Emit message: ', data.command, 'blockMsgFlag: ', blockMsgFlag);
console.log(color.blue, 'DEBUG Emit message: ', data.command, 'blockMsgFlag: ', blockMsgFlag);
socket.emit('MsgObjectServer', { message: data });
if (senderSocket) {
senderSocket.emit('privMessageCopy', `${data.command}: ${data.text}🔒`);