added eslint norme to code
This commit is contained in:
parent
c086c27c4a
commit
ace8367f77
9 changed files with 128 additions and 157 deletions
|
|
@ -27,7 +27,7 @@
|
|||
"fastify": "^5.6.2",
|
||||
"fastify-plugin": "^5.1.0",
|
||||
"socket.io": "^4.8.1",
|
||||
"typebox": "^1.0.59"
|
||||
"typebox": "^1.0.61"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.19.1",
|
||||
|
|
|
|||
|
|
@ -75,14 +75,12 @@ const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
|
|||
export default app;
|
||||
export { app };
|
||||
|
||||
|
||||
|
||||
// When using .decorate you have to specify added properties for Typescript
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
io: Server<{
|
||||
hello: (message: string) => string;
|
||||
MsgObjectServer: (data: { message: ClientMessage } ) => void;
|
||||
MsgObjectServer: (data: { message: ClientMessage }) => void;
|
||||
message: (msg: string) => void;
|
||||
listBud: (msg: string) => void;
|
||||
testend: (sock_id_client: string) => void;
|
||||
|
|
@ -161,32 +159,27 @@ async function onReady(fastify: FastifyInstance) {
|
|||
return count;
|
||||
}
|
||||
|
||||
function broadcast(data: ClientMessage, sender?: string) {
|
||||
fastify.io.fetchSockets().then((sockets) => {
|
||||
for (const s of sockets) {
|
||||
|
||||
// Skip sender's own socket
|
||||
if (s.id === sender) continue;
|
||||
|
||||
// Get client name from map
|
||||
const clientInfo = clientChat.get(s.id);
|
||||
|
||||
if (!clientInfo?.user) {
|
||||
console.log(color.yellow, `Skipping socket ${s.id} (no user found)`);
|
||||
continue;
|
||||
function broadcast(data: ClientMessage, sender?: string) {
|
||||
fastify.io.fetchSockets().then((sockets) => {
|
||||
for (const s of sockets) {
|
||||
// Skip sender's own socket
|
||||
if (s.id === sender) continue;
|
||||
// Get client name from map
|
||||
const clientInfo = clientChat.get(s.id);
|
||||
if (!clientInfo?.user) {
|
||||
console.log(color.yellow, `Skipping socket ${s.id} (no user found)`);
|
||||
continue;
|
||||
}
|
||||
// Emit structured JSON object
|
||||
s.emit('MsgObjectServer', { message: data });
|
||||
// Debug logs
|
||||
console.log(color.green, 'Broadcast to:', clientInfo.user);
|
||||
console.log(' Target socket ID:', s.id);
|
||||
console.log(' Target rooms:', [...s.rooms]);
|
||||
console.log(' Sender socket ID:', sender ?? 'none');
|
||||
}
|
||||
|
||||
// Emit structured JSON object
|
||||
s.emit("MsgObjectServer", { message: data });
|
||||
|
||||
// Debug logs
|
||||
console.log(color.green, "Broadcast to:", clientInfo.user);
|
||||
console.log(" Target socket ID:", s.id);
|
||||
console.log(" Target rooms:", [...s.rooms]);
|
||||
console.log(" Sender socket ID:", sender ?? "none");
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fastify.io.on('connection', (socket: Socket) => {
|
||||
|
||||
|
|
@ -239,7 +232,7 @@ function broadcast(data: ClientMessage, sender?: string) {
|
|||
|
||||
if (userFromFrontend.oldUser !== userFromFrontend.user) {
|
||||
console.log(color.red, 'list activated', userFromFrontend.oldUser, color.reset);
|
||||
// if (client?.user === null) {
|
||||
// if (client?.user === null) {
|
||||
// console.log('ERROR: clientName is NULL');
|
||||
// return;
|
||||
// };
|
||||
|
|
@ -268,18 +261,18 @@ function broadcast(data: ClientMessage, sender?: string) {
|
|||
}
|
||||
});
|
||||
|
||||
socket.on("logout", () => {
|
||||
socket.on('logout', () => {
|
||||
const clientInfo = clientChat.get(socket.id);
|
||||
const clientName = clientInfo?.user;
|
||||
|
||||
|
||||
if (!clientName) return;
|
||||
console.log(color.green, `Client logging out: ${clientName} (${socket.id})`);
|
||||
const obj = {
|
||||
destination: "system-info",
|
||||
type: "chat" as const,
|
||||
destination: 'system-info',
|
||||
type: 'chat' as const,
|
||||
user: clientName,
|
||||
token: "",
|
||||
text: "LEFT the chat",
|
||||
token: '',
|
||||
text: 'LEFT the chat',
|
||||
timestamp: Date.now(),
|
||||
SenderWindowID: socket.id,
|
||||
};
|
||||
|
|
@ -287,11 +280,9 @@ function broadcast(data: ClientMessage, sender?: string) {
|
|||
// Optional: remove from map
|
||||
clientChat.delete(socket.id);
|
||||
// Ensure socket is fully disconnected
|
||||
if (socket.connected) socket.disconnect(true);
|
||||
if (socket.connected) socket.disconnect(true);
|
||||
});
|
||||
|
||||
|
||||
|
||||
socket.on('disconnecting', (reason) => {
|
||||
const clientName = clientChat.get(socket.id)?.user || null;
|
||||
console.log(
|
||||
|
|
@ -303,7 +294,7 @@ function broadcast(data: ClientMessage, sender?: string) {
|
|||
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
destination: "system-info",
|
||||
destination: 'system-info',
|
||||
type: 'chat',
|
||||
user: clientName,
|
||||
token: '',
|
||||
|
|
@ -327,7 +318,7 @@ function broadcast(data: ClientMessage, sender?: string) {
|
|||
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
destination: "system-info",
|
||||
destination: 'system-info',
|
||||
type: 'chat',
|
||||
user: clientName,
|
||||
token: '',
|
||||
|
|
@ -335,7 +326,7 @@ function broadcast(data: ClientMessage, sender?: string) {
|
|||
timestamp: Date.now(),
|
||||
SenderWindowID: socket.id,
|
||||
};
|
||||
console.log(color.blue, 'BROADCASTS OUT :',obj.SenderWindowID);
|
||||
console.log(color.blue, 'BROADCASTS OUT :', obj.SenderWindowID);
|
||||
broadcast(obj, obj.SenderWindowID);
|
||||
// clientChat.delete(obj.user);
|
||||
}
|
||||
|
|
@ -372,7 +363,7 @@ function broadcast(data: ClientMessage, sender?: string) {
|
|||
);
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
destination: "system-info",
|
||||
destination: 'system-info',
|
||||
type: 'chat',
|
||||
user: clientName,
|
||||
frontendUserName: userNameFromFrontend,
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
import { FastifyPluginAsync } from 'fastify';
|
||||
import { MakeStaticResponse, typeResponse } from '@shared/utils';
|
||||
import { Type } from 'typebox';
|
||||
import { UserId } from '@shared/database/mixin/user';
|
||||
import { Server } from 'socket.io';
|
||||
// import { UserId } from '@shared/database/mixin/user';
|
||||
// import { Server } from 'socket.io';
|
||||
|
||||
// colors for console.log
|
||||
export const color = {
|
||||
|
|
@ -13,16 +13,6 @@ export const color = {
|
|||
reset: '\x1b[0m',
|
||||
};
|
||||
|
||||
// Global map of clients
|
||||
// key = socket, value = clientname
|
||||
interface ClientInfo {
|
||||
user: string;
|
||||
lastSeen: number;
|
||||
}
|
||||
|
||||
const clientChat = new Map<string, ClientInfo>();
|
||||
|
||||
|
||||
export const ChatRes = {
|
||||
200: typeResponse('success', 'chat.success', {
|
||||
name: Type.String(),
|
||||
|
|
@ -33,8 +23,6 @@ export const ChatRes = {
|
|||
|
||||
export type ChatResType = MakeStaticResponse<typeof ChatRes>;
|
||||
|
||||
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify): Promise<void> => {
|
||||
fastify.get(
|
||||
'/api/chat/test',
|
||||
|
|
@ -47,23 +35,17 @@ const route: FastifyPluginAsync = async (fastify): Promise<void> => {
|
|||
},
|
||||
async (req, res) => {
|
||||
|
||||
|
||||
|
||||
let users = fastify.db.getAllUsers();
|
||||
console.log("ALL USERS EVER CONNECTED:", users);
|
||||
|
||||
const users = fastify.db.getAllUsers();
|
||||
console.log('ALL USERS EVER CONNECTED:', users);
|
||||
if (!users) return;
|
||||
for (const user of users) {
|
||||
console.log(color.yellow, "USER:", user.name);
|
||||
console.log(color.yellow, 'USER:', user.name);
|
||||
}
|
||||
|
||||
// const usersBlocked = fastify.db.getAllBlockedUsers();
|
||||
// console.log(color.red, "ALL BLOCKED USERS:", usersBlocked);
|
||||
fastify.db.addBlockedUserFor(users[0].id, users[1].id)
|
||||
let usersBlocked2;
|
||||
usersBlocked2 = fastify.db.getAllBlockedUsers();
|
||||
console.log(color.green, "ALL BLOCKED USERS:", usersBlocked2);
|
||||
|
||||
fastify.db.addBlockedUserFor(users[0].id, users[1].id);
|
||||
const usersBlocked2 = fastify.db.getAllBlockedUsers();
|
||||
console.log(color.green, 'ALL BLOCKED USERS:', usersBlocked2);
|
||||
res.makeResponse(200, 'success', 'CCChat.success', { name: 'name', 'id': req.authUser!.id, guest: false });
|
||||
},
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue