bigly-chat/form/follow.js

37 lines
1 KiB
JavaScript
Raw Permalink Normal View History

2024-11-25 14:12:43 -05:00
import Route from "../route.js";
import initDb from "../db.js";
import auth from "../form/auth.js";
let db = await initDb();
function isValid(user) {
return user.search(/[^A-Za-z0-9\-\_]/g) == -1;
}
2024-11-25 14:12:44 -05:00
2024-11-25 14:12:43 -05:00
let main = new Route([auth], async function (req, res, input) {
let { username } = input;
let { target } = req.body;
if (!isValid(target) || username == '!nobody')
2024-11-25 14:12:44 -05:00
return { 'success': false, 'message': 'Some fields are missing' };
2024-11-25 14:12:43 -05:00
let isFollowing = await db.all('SELECT * FROM follow WHERE username = ? AND target = ?', [
username,
target
]);
if (isFollowing.length > 0) {
await db.run('DELETE FROM follow WHERE username = ? AND target = ?', [
username,
target
]);
2024-11-25 14:12:44 -05:00
res.send({ 'success': true, 'message': 'User unfollowed' });
2024-11-25 14:12:43 -05:00
} else {
await db.run('INSERT INTO follow (username,target) VALUES (?,?)', [
username,
target
]);
2024-11-25 14:12:44 -05:00
res.send({ 'success': true, 'message': 'User followed' });
2024-11-25 14:12:43 -05:00
}
});
export default main;