const express = require('express'); const { getConnection, getUsers, getUserByUsername, getUserLastMessages, getMentions, deleteUser, setUserPfp, getMessageAttachments, getUnusedAttachments, deleteUnusedAttachments, searchUser } = require('../libs/mysql'); const { checkAuth } = require("../libs/middlewares") const path = require('path'); const fs = require('node:fs'); const router = express.Router(); router.get('/', async (req, res) => { const { search } = req.query; const connection = await getConnection(); if (search || search === '') { const users = await searchUser(connection, search); connection.end(); return res.send(users); } const users = await getUsers(connection); connection.end(); res.send(users); }); router.get('/:username', async (req, res) => { const username = req.params.username; const connection = await getConnection(); const user = await getUserByUsername(connection, username); connection.end(); if (user[0]) { res.send({id: user[0].id, username: user[0].username, admin: user[0].admin, description: user[0].description}); } else { return res.status(400).send({ error: 'No user found' }); } }); router.get('/:username/lastmessages', async (req, res) => { const username = req.params.username; const connection = await getConnection(); const messages = await getUserLastMessages(connection, username); for (const message of messages) { if (message.content.includes('@')) { const mentions = await getMentions(connection, message.id); message.mentions = mentions; } else { message.mentions = []; } message.replies = []; if (message.has_attachments) { const attachments = await getMessageAttachments(connection, message.id); message.attachments = attachments; } else { message.attachments = []; } } connection.end(); res.send(messages); }); router.get('/:username/pfp', async (req, res) => { const username = req.params.username; const connection = await getConnection(); const user = await getUserByUsername(connection, username); connection.end(); if (!user[0]) { return res.status(400).send({ error: 'No user found' }); } const pfp = user[0].pfp; if (!pfp || !fs.existsSync(path.join(__dirname, `../data/pfps/${pfp}`))) { return res.sendFile(path.join(__dirname, `../images/default.png`), { headers: { 'Content-Type': 'image' } }); } res.sendFile(path.join(__dirname, `../data/pfps/${pfp}`), { headers: { 'Content-Type': 'image' } }); }); router.post('/:username/delete', checkAuth, async (req, res) => { const username = req.params.username; const user = req.user; const connection = await getConnection(); const userToDelete = await getUserByUsername(connection, username); if (!userToDelete[0]) { connection.end(); return res.status(400).send({ error: 'No user found' }); } if (user.admin !== 1) { connection.end(); return res.status(401).send({ error: 'Unauthorized' }); } if (userToDelete[0].pfp && fs.existsSync(path.join(__dirname, `../data/pfps/${userToDelete[0].pfp}`))) { fs.unlinkSync(path.join(__dirname, `../data/pfps/${userToDelete[0].pfp}`)); } await deleteUser(connection, userToDelete[0].id); const attachments = await getUnusedAttachments(connection); for (const attachment of attachments) { if (fs.existsSync(`data/attachments/${attachment.file_name}`)) { fs.unlinkSync(`data/attachments/${attachment.file_name}`); } } await deleteUnusedAttachments(connection); connection.end(); req.sockets.emit({ type: 'delete_user', user_id: userToDelete[0].id, }); res.send({ message: 'User deleted' }); }); router.post('/:username/deletepfp', checkAuth, async (req, res) => { const username = req.params.username; const user = req.user; const connection = await getConnection(); const userToDelete = await getUserByUsername(connection, username); if (!userToDelete[0]) { connection.end(); return res.status(400).send({ error: 'No user found' }); } if (user.admin !== 1) { connection.end(); return res.status(401).send({ error: 'Unauthorized' }); } if (userToDelete[0].pfp) { await setUserPfp(connection, userToDelete[0].id, null); } if (fs.existsSync(path.join(__dirname, `../data/pfps/${userToDelete[0].pfp}`))) { fs.unlinkSync(path.join(__dirname, `../data/pfps/${userToDelete[0].pfp}`)); } connection.end(); res.send({ message: 'User pfp deleted' }); }); module.exports = router;