From 427336973652832b29ba2ad7ef64e2ce020bbebb Mon Sep 17 00:00:00 2001 From: biglyderv Date: Mon, 25 Nov 2024 14:12:44 -0500 Subject: [PATCH] cleanup --- client/captcha.js | 2 +- client/e404.js | 3 ++- client/login.js | 3 ++- client/main.js | 3 ++- client/messages.js | 3 ++- client/player.js | 3 ++- client/register.js | 3 ++- client/settings.js | 5 +++-- client/tou.js | 3 ++- client/upload.js | 3 ++- client/user.js | 3 ++- db.js | 26 ++++++++++++++------------ form/comment.js | 3 +-- form/delete_video.js | 5 ++--- form/follow.js | 10 ++++------ form/login.js | 4 +--- form/register.js | 4 +--- form/settings.js | 7 +++---- form/upload.js | 15 +++++++-------- routes.js | 1 + 20 files changed, 56 insertions(+), 53 deletions(-) diff --git a/client/captcha.js b/client/captcha.js index cd6b07b..57952c6 100644 --- a/client/captcha.js +++ b/client/captcha.js @@ -7,7 +7,6 @@ import crypto from "node:crypto"; let db = await initDb(); -// TODO: rewrite let main = new Route([], async function (req, res, input) { const canvas = new Canvas.Canvas(250, 100) const ctx = canvas.getContext('2d') @@ -47,6 +46,7 @@ let main = new Route([], async function (req, res, input) { res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0') res.header('Expires', 'Sun, 19 May 1984 02:00:00 GMT') canvas.jpegStream().pipe(res) + return true; }); export default main; \ No newline at end of file diff --git a/client/e404.js b/client/e404.js index f650a01..2dba940 100644 --- a/client/e404.js +++ b/client/e404.js @@ -2,9 +2,10 @@ import Route from "../route.js"; import auth from "../form/auth.js"; let main = new Route([auth], async function (req, res, input) { - return res.render('404', { + res.render('404', { ...input }); + return true; }); export default main; \ No newline at end of file diff --git a/client/login.js b/client/login.js index b9cc184..a300e59 100644 --- a/client/login.js +++ b/client/login.js @@ -3,7 +3,7 @@ import auth from "../form/auth.js"; // TODO: rewrite let main = new Route([auth], async function (req, res, input) { - return res.render('form', { + res.render('form', { data: [ { label: "Username", type: "text", name: "user" }, { label: "Password", type: "password", name: "pass" }, @@ -12,6 +12,7 @@ let main = new Route([auth], async function (req, res, input) { 'title': 'Login', ...input }); + return true; }); export default main; \ No newline at end of file diff --git a/client/main.js b/client/main.js index 772bb79..8d60c02 100644 --- a/client/main.js +++ b/client/main.js @@ -11,11 +11,12 @@ let main = new Route([auth], async function (req, res, input) { let videosFollow = await db.all('SELECT * FROM video WHERE username IN (SELECT target FROM follow WHERE username = ?) ORDER BY date DESC LIMIT 20', [ username ]); - return res.render('main', { + res.render('main', { ...input, videos, videosFollow }); + return true; }); export default main; \ No newline at end of file diff --git a/client/messages.js b/client/messages.js index fa1defc..0cf2a19 100644 --- a/client/messages.js +++ b/client/messages.js @@ -16,10 +16,11 @@ let main = new Route([auth], async function (req, res, input) { username ]); - return res.render('messages', { + res.render('messages', { ...input, msgs }); + return true; }); export default main; \ No newline at end of file diff --git a/client/player.js b/client/player.js index ab198cf..09434da 100644 --- a/client/player.js +++ b/client/player.js @@ -10,11 +10,12 @@ let main = new Route([comment], async function (req, res, input) { ]); if (videoData.length == 0) return; videoData = videoData[0]; - return res.render('player', { + res.render('player', { ...input, videoData, ogType: 'video' }); + return true; }); export default main; \ No newline at end of file diff --git a/client/register.js b/client/register.js index 32a15bf..1d99314 100644 --- a/client/register.js +++ b/client/register.js @@ -3,7 +3,7 @@ import auth from "../form/auth.js"; // TODO: rewrite let main = new Route([auth], async function (req, res, input) { - return res.render('form', { + res.render('form', { data: [ { label: "Username", type: "text", name: "user" }, { label: "Password", type: "password", name: "pass" }, @@ -13,6 +13,7 @@ let main = new Route([auth], async function (req, res, input) { 'title': 'Join', ...input }); + return true; }); export default main; \ No newline at end of file diff --git a/client/settings.js b/client/settings.js index bc27265..4e17072 100644 --- a/client/settings.js +++ b/client/settings.js @@ -3,7 +3,7 @@ import auth from "../form/auth.js"; // TODO: rewrite let main = new Route([auth], async function (req, res, input) { - return res.render('form', { + res.render('form', { data: [ { label: "Description", type: "textarea", name: "desc" }, { label: "Avatar", type: "file", name: "file" } @@ -11,7 +11,8 @@ let main = new Route([auth], async function (req, res, input) { 'route': '/api/upload/settings', 'title': 'User Settings', ...input - }); /* todo: form in not a file */ + }); + return true; }); export default main; \ No newline at end of file diff --git a/client/tou.js b/client/tou.js index 06e6a37..58db9fd 100644 --- a/client/tou.js +++ b/client/tou.js @@ -2,9 +2,10 @@ import Route from "../route.js"; import auth from "../form/auth.js"; let main = new Route([auth], async function (req, res, input) { - return res.render('guidelines', { + res.render('guidelines', { ...input }); + return true; }); export default main; \ No newline at end of file diff --git a/client/upload.js b/client/upload.js index 000276f..a74a42c 100644 --- a/client/upload.js +++ b/client/upload.js @@ -3,7 +3,7 @@ import auth from "../form/auth.js"; // TODO: rewrite let main = new Route([auth], async function (req, res, input) { - return res.render('form', { + res.render('form', { data: [ { label: "Video file", type: "file", name: "file" }, { label: "Name", type: "text", name: "title" }, @@ -13,6 +13,7 @@ let main = new Route([auth], async function (req, res, input) { 'title': 'Upload Content', ...input }); + return true; }); export default main; \ No newline at end of file diff --git a/client/user.js b/client/user.js index 12584e7..e8a9064 100644 --- a/client/user.js +++ b/client/user.js @@ -22,7 +22,7 @@ let main = new Route([comment], async function (req, res, input) { req.query.id ]); - return res.render('user', { + res.render('user', { ...input, id, videos, @@ -31,6 +31,7 @@ let main = new Route([comment], async function (req, res, input) { user, icon: `${process.env.URL}/pfp/${id}.png` }); + return true; }); export default main; \ No newline at end of file diff --git a/db.js b/db.js index 2246ba5..745aa42 100644 --- a/db.js +++ b/db.js @@ -3,6 +3,15 @@ import { open } from 'sqlite' let db; +const sql = `CREATE TABLE IF NOT EXISTS auth (username TEXT, password TEXT); +CREATE TABLE IF NOT EXISTS token (username TEXT, token TEXT); +CREATE TABLE IF NOT EXISTS video (id TEXT, title TEXT, desc TEXT, username TEXT, date REAL); +CREATE TABLE IF NOT EXISTS comment (username TEXT, targetType TEXT, targetId TEXT, date REAL, content TEXT, id TEXT); +CREATE TABLE IF NOT EXISTS follow (username TEXT, target TEXT); +CREATE TABLE IF NOT EXISTS user (username TEXT, bio TEXT); +CREATE TABLE IF NOT EXISTS captcha (key TEXT, solution TEXT); +CREATE TABLE IF NOT EXISTS message (username TEXT, targetType TEXT, targetId TEXT, date REAL, content TEXT, read TEXT);` + async function initDb() { if (db) return db; @@ -11,19 +20,12 @@ async function initDb() { driver: sqlite3.Database }); - await db.run(`CREATE TABLE IF NOT EXISTS auth (username TEXT, password TEXT);`); - await db.run(`CREATE TABLE IF NOT EXISTS token (username TEXT, token TEXT);`); - - await db.run(`CREATE TABLE IF NOT EXISTS video (id TEXT, title TEXT, desc TEXT, username TEXT, date REAL);`); - await db.run('CREATE TABLE IF NOT EXISTS comment (username TEXT, targetType TEXT, targetId TEXT, date REAL, content TEXT, id TEXT);'); - - await db.run('CREATE TABLE IF NOT EXISTS follow (username TEXT, target TEXT);'); - - await db.run(`CREATE TABLE IF NOT EXISTS user (username TEXT, bio TEXT);`); - await db.run(`CREATE TABLE IF NOT EXISTS captcha (key TEXT, solution TEXT);`); - - await db.run(`CREATE TABLE IF NOT EXISTS message (username TEXT, targetType TEXT, targetId TEXT, date REAL, content TEXT, read TEXT);`); + let sqlCmds = sql.split('\n'); + for (let cmd of sqlCmds) { + await db.run(cmd); + } + return db; } diff --git a/form/comment.js b/form/comment.js index b826560..6198c9e 100644 --- a/form/comment.js +++ b/form/comment.js @@ -5,14 +5,13 @@ import auth from "../form/auth.js"; let db = await initDb(); -// TODO: rewrite let main = new Route([auth], async function (req, res, input) { let { username } = input; let id = randomUUID(); let { targetType, targetId, content } = req.body; if (!targetType || !targetId || !content || username == '!nobody') - return { 'success': false, 'message': 'Some fields are missing' }; // probably should not re-use these strings + return { 'success': false, 'message': 'Some fields are missing' }; if (content.length > 8192) return { 'success': false, 'message': 'Comment is too long' }; diff --git a/form/delete_video.js b/form/delete_video.js index e483db9..d2b93c9 100644 --- a/form/delete_video.js +++ b/form/delete_video.js @@ -4,19 +4,18 @@ import auth from "../form/auth.js"; let db = await initDb(); -// TODO: rewrite let main = new Route([auth], async function (req, res, input) { let { username } = input; let { target } = req.body; - if (!target ) return { 'success': false, 'message': 'Some fields are missing' }; // probably should not re-use these strings + if (!target) return { 'success': false, 'message': 'Some fields are missing' }; await db.run('DELETE FROM video WHERE id = ? AND username = ?', [ target, username ]); - return { 'message': 'Video deleted', 'success': true}; + return { 'message': 'Video deleted', 'success': true }; }); export default main; \ No newline at end of file diff --git a/form/follow.js b/form/follow.js index cc5a782..ae43006 100644 --- a/form/follow.js +++ b/form/follow.js @@ -1,20 +1,18 @@ import Route from "../route.js"; import initDb from "../db.js"; -import { randomUUID } from 'node:crypto'; import auth from "../form/auth.js"; let db = await initDb(); function isValid(user) { return user.search(/[^A-Za-z0-9\-\_]/g) == -1; } -// TODO: rewrite + let main = new Route([auth], async function (req, res, input) { let { username } = input; - let id = randomUUID(); let { target } = req.body; if (!isValid(target) || username == '!nobody') - return { 'success': false, 'message': 'Some fields are missing' }; // probably should not re-use these strings + return { 'success': false, 'message': 'Some fields are missing' }; let isFollowing = await db.all('SELECT * FROM follow WHERE username = ? AND target = ?', [ username, @@ -26,13 +24,13 @@ let main = new Route([auth], async function (req, res, input) { username, target ]); - res.send({'success': true, 'message': 'User unfollowed'}); + res.send({ 'success': true, 'message': 'User unfollowed' }); } else { await db.run('INSERT INTO follow (username,target) VALUES (?,?)', [ username, target ]); - res.send({'success': true, 'message': 'User followed'}); + res.send({ 'success': true, 'message': 'User followed' }); } }); diff --git a/form/login.js b/form/login.js index 3ca74bc..f9b3d7d 100644 --- a/form/login.js +++ b/form/login.js @@ -1,4 +1,3 @@ -// from an old project: https://git.zenoverse.net/bigly-archive/auth-thing/raw/branch/main/src/routes/login/+page.server.js import Route from "../route.js"; import initDb from "../db.js"; import { compare } from "bcrypt"; @@ -7,10 +6,9 @@ import captcha from "./captcha.js"; let db = await initDb(); -// TODO: rewrite let main = new Route([captcha], async function (req, res, input) { let { user, pass } = req.body; - let { captchaMatch} = input; + let { captchaMatch } = input; if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' }; diff --git a/form/register.js b/form/register.js index ecb6607..2b3b8ca 100644 --- a/form/register.js +++ b/form/register.js @@ -1,4 +1,3 @@ -// from an old project: https://git.zenoverse.net/bigly-archive/auth-thing/raw/branch/main/src/routes/register/+page.server.js import Route from "../route.js"; import initDb from "../db.js"; import { hash } from "bcrypt"; @@ -13,10 +12,9 @@ function isValid(user) { return user.search(/[^A-Za-z0-9\-\_]/g) == -1; } -// TODO: rewrite let main = new Route([captcha], async function (req, res, input) { let { user, pass, pass2 } = req.body; - let { captchaMatch} = input; + let { captchaMatch } = input; if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' }; diff --git a/form/settings.js b/form/settings.js index 56e3958..8d97ed5 100644 --- a/form/settings.js +++ b/form/settings.js @@ -1,7 +1,7 @@ import Route from "../route.js"; import initDb from "../db.js"; import auth from "../form/auth.js"; -import {exec} from 'node:child_process'; +import { exec } from 'node:child_process'; import { promisify } from "node:util"; import captcha from "./captcha.js"; @@ -9,9 +9,8 @@ const execP = promisify(exec); let db = await initDb(); -// TODO: rewrite -let main = new Route([auth,captcha], async function (req, res, input) { - let { captchaMatch} = input; +let main = new Route([auth, captcha], async function (req, res, input) { + let { captchaMatch } = input; if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' }; let { path } = req.file; diff --git a/form/upload.js b/form/upload.js index a9f5807..2005500 100644 --- a/form/upload.js +++ b/form/upload.js @@ -1,7 +1,7 @@ import Route from "../route.js"; import initDb from "../db.js"; import { randomUUID } from 'node:crypto'; -import {exec} from 'node:child_process'; +import { exec } from 'node:child_process'; import { promisify } from "node:util"; import auth from "../form/auth.js"; import captcha from "./captcha.js"; @@ -10,24 +10,23 @@ const execP = promisify(exec); let db = await initDb(); -// TODO: rewrite -let main = new Route([auth,captcha], async function (req, res, input) { - let { captchaMatch} = input; +let main = new Route([auth, captcha], async function (req, res, input) { + let { captchaMatch } = input; if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' }; let { username } = input; let id = randomUUID(); let { title, desc } = req.body; - if (!title || !desc || !req.file || username == '!nobody') return { 'success': false, 'message': 'Some fields are missing' }; // probably should not re-use these strings + if (!title || !desc || !req.file || username == '!nobody') return { 'success': false, 'message': 'Some fields are missing' }; - let {path} = req.file; + let { path } = req.file; try { await execP(`ffmpeg -i ${path} videos/${id}.webm`); await execP(`ffmpeg -i videos/${id}.webm -frames:v 1 videos/${id}.png`); } catch (err) { - return { 'success': false, 'message': 'Video is invalid'} + return { 'success': false, 'message': 'Video is invalid' } } await db.run('INSERT INTO video (id, title, desc, username, date) VALUES (?, ?, ?, ?, ?)', [ @@ -38,7 +37,7 @@ let main = new Route([auth,captcha], async function (req, res, input) { +new Date() ]); - return { 'message': 'Video created', 'success': true, 'redirect': '/client/video?id='+id }; + return { 'message': 'Video created', 'success': true, 'redirect': '/client/video?id=' + id }; }); export default main; \ No newline at end of file diff --git a/routes.js b/routes.js index dca2298..b05e7d7 100644 --- a/routes.js +++ b/routes.js @@ -66,6 +66,7 @@ async function iterate(req, res, index) { let dat = await routesI[cmd].run(req, res, {}); if (!dat && !res.headersSent) { + console.log(res) return await routes.client['e404'].run(req, res, {}); }