const express = require('express'); const jwt = require('jsonwebtoken'); const { getConnection, getUser, getChannels, getChannel, addChannel, getMessages, addMessage, deleteMessage, getLastMessages } = require('../libs/mysql'); const router = express.Router(); router.get('/', async (req, res) => { const connection = await getConnection(); const channels = await getChannels(connection); connection.end(); res.send(channels); }); router.get('/:name', async (req, res) => { const name = req.params.name; const connection = await getConnection(); const channel = await getChannel(connection, name); connection.end(); if (channel[0]) { res.send(channel[0]); } else { res.send('No channel found'); } }); router.get('/:name/messages', async (req, res) => { const name = req.params.name; const connection = await getConnection(); const channel = await getChannel(connection, name); if (!channel[0]) { connection.end(); return res.send('No channel found'); } const messages = await getMessages(connection, channel[0].id); connection.end(); res.send(messages); }); router.post('/:name/messages/send', async (req, res) => { const { token, message } = req.body; const name = req.params.name; const connection = await getConnection(); const decoded = jwt.verify(token, process.env.JWT_SECRET); const user = await getUser(connection, decoded.id); if (!user[0]) { connection.end(); return res.status(401).send({ error: 'Invalid token' }); } const channel = await getChannel(connection, name); if (!channel[0]) { connection.end(); return res.send('No channel found'); } await addMessage(connection, channel[0].id, user[0].id, message.replace("\"", "'")); connection.end(); res.send({ message: 'Message sent' }); }); router.post('/:name/messages/delete', async (req, res) => { const { token, message_id } = req.body; const name = req.params.name; const connection = await getConnection(); const decoded = jwt.verify(token, process.env.JWT_SECRET); const user = await getUser(connection, decoded.id); if (!user[0]) { connection.end(); return res.status(401).send({ error: 'Invalid token' }); } const channel = await getChannel(connection, name); if (!channel[0]) { connection.end(); return res.status(400).send({ error: 'No channel found' }); } if (user[0].id !== channel[0].owner_id && user[0].id !== message_id && user[0].admin !== 1) { connection.end(); return res.status(401).send({ error: 'Unauthorized' }); } await deleteMessage(connection, message_id); connection.end(); res.send({ message: 'Message deleted' }); }); router.post('/add', async (req, res) => { const { name, description, token } = req.body; const connection = await getConnection(); const decoded = jwt.verify(token, process.env.JWT_SECRET); const user = await getUser(connection, decoded.id); if (!user[0]) { connection.end(); return res.status(401).send({ error: 'Invalid token' }); } const channel = await getChannel(connection, name); if (channel[0]) { connection.end(); return res.status(400).send({ error: 'Channel already exists' }); } if (!/^[a-zA-Z0-9-_]+$/.test(name)) { connection.end(); return res.status(400).send({ error: 'Invalid channel name' }); } await addChannel(connection, name, description, user[0].id); connection.end(); res.send({ message: 'Channel added' }); }); module.exports = router;