add: improved frontend and added descriptions to users

This commit is contained in:
Lukian 2025-05-17 00:22:31 +02:00
parent 6342377aa0
commit eca9efc170
14 changed files with 266 additions and 29 deletions

View file

@ -1,7 +1,7 @@
const express = require('express');
const sha256 = require("sha256");
const jwt = require('jsonwebtoken');
const { getConnection, getUserByUsername, addUser, setUserPfp, setUserUsername, setUserPassword } = require('../libs/mysql');
const { getConnection, getUserByUsername, addUser, setUserPfp, setUserUsername, setUserPassword, setUserDescription } = require('../libs/mysql');
const { checkAuth } = require('../libs/middlewares');
const multer = require('multer')
const rateLimit = require("express-rate-limit");
@ -82,7 +82,7 @@ router.post('/register', speedLimiter, limiter, async (req, res) => {
router.post('/me', checkAuth, async (req, res) => {
const user = req.user;
res.send({ id: user.id, username: user.username, admin: user.admin });
res.send({ id: user.id, username: user.username, admin: user.admin , description: user.description });
});
router.post('/me/uploadpfp', upload.single('pfp'), checkAuth, async (req, res) => {
@ -151,4 +151,18 @@ router.post('/me/setpassword', checkAuth, async (req, res) => {
res.send({ message: 'Password changed.' });
});
router.post('/me/setdescription', checkAuth, async (req, res) => {
const { description } = req.body;
const user = req.user;
if (!description) {
return res.status(400).send({ error: 'Invalid description' });
}
const connection = await getConnection();
await setUserDescription(connection, user.id, description);
connection.end();
res.send({ message: 'Description changed.' });
});
module.exports = router;

View file

@ -37,7 +37,7 @@ router.get('/:username', async (req, res) => {
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});
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' });
}

View file

@ -47,7 +47,7 @@ function deleteUser(connection, id) {
function getUsers(connection) {
return new Promise((resolve, reject) => {
connection.query(
`SELECT id, username, admin FROM users`,
`SELECT id, username, admin, description FROM users`,
(error, result) => {
if (error) {
reject(new Error(error));
@ -148,6 +148,21 @@ function setUserPassword(connection, id, password) {
});
}
function setUserDescription(connection, id, description) {
return new Promise((resolve, reject) => {
connection.query(
`UPDATE users SET description = ? WHERE id = ?`,
[description, id],
(error, result) => {
if (error) {
reject(new Error(error));
}
resolve(result);
}
);
});
}
// +---------------------------+
// | Channels |
// +---------------------------+
@ -185,7 +200,9 @@ function deleteChannel(connection, channel_id) {
function getChannels(connection) {
return new Promise((resolve, reject) => {
connection.query(
`SELECT * FROM channels`,
`SELECT channels.id, name, channels.description, owner_id, username AS owner_username
FROM channels
JOIN users ON channels.owner_id = users.id`,
(error, result) => {
if (error) {
reject(new Error(error));
@ -199,7 +216,7 @@ function getChannels(connection) {
function getChannel(connection, name) {
return new Promise((resolve, reject) => {
connection.query(
`SELECT channels.id, name, description, owner_id, username AS owner_username
`SELECT channels.id, name, channels.description, owner_id, username AS owner_username
FROM channels
JOIN users ON channels.owner_id = users.id
WHERE name = ?`,
@ -217,7 +234,7 @@ function getChannel(connection, name) {
function getActiveChannels(connection) {
return new Promise((resolve, reject) => {
connection.query(
`SELECT channels.id, name, description, owner_id, username AS owner_username, count(*) AS message_count
`SELECT channels.id, name, channels.description, owner_id, username AS owner_username, count(*) AS message_count
FROM messages
JOIN channels ON messages.channel_id = channels.id
JOIN users ON messages.user_id = users.id
@ -238,7 +255,7 @@ function getActiveChannels(connection) {
function getNewChannels(connection) {
return new Promise((resolve, reject) => {
connection.query(
`SELECT channels.id, name, description, owner_id, username AS owner_username
`SELECT channels.id, name, channels.description, owner_id, username AS owner_username
FROM channels
JOIN users ON channels.owner_id = users.id
ORDER BY channels.id DESC LIMIT 5`,
@ -255,7 +272,7 @@ function getNewChannels(connection) {
function searchChannels(connection, search) {
return new Promise((resolve, reject) => {
connection.query(
`SELECT channels.id, name, description, owner_id, username AS owner_username
`SELECT channels.id, name, channels.description, owner_id, username AS owner_username
FROM channels
JOIN users ON channels.owner_id = users.id
WHERE name LIKE ?
@ -673,6 +690,7 @@ module.exports = {
setUserPfp,
setUserUsername,
setUserPassword,
setUserDescription,
addChannel,
deleteChannel,