add: added channel admins

This commit is contained in:
Lukian 2025-03-24 20:59:33 +01:00
parent 5d13a51f8e
commit 7dea3612af
6 changed files with 79 additions and 9 deletions

View file

@ -1,6 +1,6 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const { getConnection, getUser, getChannels, getChannel, addChannel, getMessages, addMessage } = require('../libs/mysql');
const { getConnection, getUser, getChannels, getChannel, addChannel, getMessages, addMessage, deleteMessage } = require('../libs/mysql');
const router = express.Router();
@ -59,6 +59,34 @@ router.post('/:name/messages/send', async (req, res) => {
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) {
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();
@ -81,7 +109,7 @@ router.post('/add', async (req, res) => {
return res.status(400).send({ error: 'Invalid channel name' });
}
await addChannel(connection, name, description);
await addChannel(connection, name, description, user[0].id);
connection.end();
res.send({ message: 'Channel added' });
});