playing wiith database Blocked Users
This commit is contained in:
parent
d1496e1697
commit
b35c462648
13 changed files with 291 additions and 168 deletions
|
|
@ -21,18 +21,18 @@
|
|||
"@fastify/autoload": "^6.3.1",
|
||||
"@fastify/formbody": "^8.0.2",
|
||||
"@fastify/multipart": "^9.3.0",
|
||||
"@fastify/sensible": "^6.0.3",
|
||||
"@fastify/sensible": "^6.0.4",
|
||||
"@fastify/static": "^8.3.0",
|
||||
"@fastify/websocket": "^11.2.0",
|
||||
"@sinclair/typebox": "^0.34.41",
|
||||
"fastify": "^5.6.2",
|
||||
"fastify-plugin": "^5.1.0",
|
||||
"socket.io": "^4.8.1"
|
||||
"socket.io": "^4.8.1",
|
||||
"typebox": "^1.0.59"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.19.1",
|
||||
"rollup-plugin-node-externals": "^8.1.2",
|
||||
"vite": "^7.2.4",
|
||||
"vite": "^7.2.6",
|
||||
"vite-tsconfig-paths": "^5.1.4"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,27 @@
|
|||
import { FastifyPluginAsync } from 'fastify';
|
||||
import { MakeStaticResponse, typeResponse } from '@shared/utils';
|
||||
import { Type } from '@sinclair/typebox';
|
||||
import { Type } from 'typebox';
|
||||
import { UserId } from '@shared/database/mixin/user';
|
||||
import { Server } from 'socket.io';
|
||||
|
||||
// colors for console.log
|
||||
export const color = {
|
||||
red: '\x1b[31m',
|
||||
green: '\x1b[32m',
|
||||
yellow: '\x1b[33m',
|
||||
blue: '\x1b[34m',
|
||||
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', {
|
||||
|
|
@ -12,6 +33,56 @@ export const ChatRes = {
|
|||
|
||||
export type ChatResType = MakeStaticResponse<typeof ChatRes>;
|
||||
|
||||
|
||||
function connectedUser(io: Server | undefined, targetSocketId?: string): number {
|
||||
let count = 0;
|
||||
|
||||
// Track unique usernames (avoid duplicates)
|
||||
const seenUsers = new Set<string>();
|
||||
|
||||
for (const [socketId, info] of clientChat) {
|
||||
|
||||
// Validate entry
|
||||
if (!info || typeof info.user !== "string" || info.user.trim() === "") {
|
||||
clientChat.delete(socketId);
|
||||
continue;
|
||||
}
|
||||
|
||||
const username = info.user;
|
||||
|
||||
// Validate socket exists if io is passed
|
||||
if (io) {
|
||||
const socket = io.sockets.sockets.get(socketId);
|
||||
|
||||
// Remove disconnected sockets
|
||||
if (!socket || socket.disconnected) {
|
||||
clientChat.delete(socketId);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Skip duplicates
|
||||
if (seenUsers.has(username))
|
||||
continue;
|
||||
|
||||
seenUsers.add(username);
|
||||
count++;
|
||||
|
||||
// Send to target only
|
||||
if (io && targetSocketId) {
|
||||
io.to(targetSocketId).emit("listBud", username);
|
||||
}
|
||||
|
||||
console.log(color.yellow, "Client:", color.reset, username);
|
||||
console.log(color.yellow, "Socket ID:", color.reset, socketId);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify): Promise<void> => {
|
||||
fastify.get(
|
||||
'/api/chat/test',
|
||||
|
|
@ -23,8 +94,25 @@ const route: FastifyPluginAsync = async (fastify): Promise<void> => {
|
|||
config: { requireAuth: true },
|
||||
},
|
||||
async (req, res) => {
|
||||
// console.log('/api/chat/test called =================>');
|
||||
res.makeResponse(200, 'success', 'CCChat.success', { name: 'My_namw', 'id': req.authUser!.id, guest: false });
|
||||
|
||||
|
||||
|
||||
let 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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
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