add comments
This commit is contained in:
parent
e8cc01e66f
commit
59c1daeac5
11 changed files with 94 additions and 11 deletions
25
client/comment.js
Normal file
25
client/comment.js
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import Route from "../route.js";
|
||||||
|
import auth from "../form/auth.js";
|
||||||
|
import initDb from "../db.js";
|
||||||
|
|
||||||
|
let db = await initDb();
|
||||||
|
|
||||||
|
let main = new Route([auth], async function (req, res, input) {
|
||||||
|
let {route} = req.params;
|
||||||
|
let {id} = req.query;
|
||||||
|
let { username } = input;
|
||||||
|
|
||||||
|
let comments = await db.all('SELECT * FROM comment WHERE targetType = ? AND targetId = ?', [
|
||||||
|
route,
|
||||||
|
id
|
||||||
|
]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...input,
|
||||||
|
route,
|
||||||
|
id,
|
||||||
|
comments
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
export default main;
|
|
@ -1,17 +1,16 @@
|
||||||
import Route from "../route.js";
|
import Route from "../route.js";
|
||||||
import auth from "../form/auth.js";
|
import comment from "./comment.js";
|
||||||
import initDb from "../db.js";
|
import initDb from "../db.js";
|
||||||
|
|
||||||
let db = await initDb();
|
let db = await initDb();
|
||||||
|
|
||||||
let main = new Route([auth], async function (req, res, input) {
|
let main = new Route([comment], async function (req, res, input) {
|
||||||
let { username } = input;
|
|
||||||
let videoData = await db.all('SELECT * FROM video WHERE id = ?', [
|
let videoData = await db.all('SELECT * FROM video WHERE id = ?', [
|
||||||
req.query.id
|
req.query.id
|
||||||
]);
|
]);
|
||||||
videoData = videoData[0];
|
videoData = videoData[0];
|
||||||
return res.render('player', {
|
return res.render('player', {
|
||||||
username,
|
...input,
|
||||||
videoData
|
videoData
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
import Route from "../route.js";
|
import Route from "../route.js";
|
||||||
import auth from "../form/auth.js";
|
import comment from "./comment.js";
|
||||||
import initDb from "../db.js";
|
import initDb from "../db.js";
|
||||||
|
|
||||||
let db = await initDb();
|
let db = await initDb();
|
||||||
|
|
||||||
let main = new Route([auth], async function (req, res, input) {
|
let main = new Route([comment], async function (req, res, input) {
|
||||||
let { id } = req.query;
|
let { id } = req.query;
|
||||||
let { username } = input;
|
|
||||||
let videos = await db.all('SELECT * FROM video WHERE username = ? ORDER BY date DESC', [
|
let videos = await db.all('SELECT * FROM video WHERE username = ? ORDER BY date DESC', [
|
||||||
req.query.id
|
req.query.id
|
||||||
]);
|
]);
|
||||||
return res.render('user', {
|
return res.render('user', {
|
||||||
username,
|
...input,
|
||||||
id,
|
id,
|
||||||
videos
|
videos
|
||||||
});
|
});
|
||||||
|
|
1
db.js
1
db.js
|
@ -15,6 +15,7 @@ async function initDb() {
|
||||||
await db.run(`CREATE TABLE IF NOT EXISTS token (username TEXT, token 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 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);');
|
||||||
|
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
33
form/comment.js
Normal file
33
form/comment.js
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import Route from "../route.js";
|
||||||
|
import initDb from "../db.js";
|
||||||
|
import { randomUUID } from 'node:crypto';
|
||||||
|
import { exec } from 'node:child_process';
|
||||||
|
import { promisify } from "node:util";
|
||||||
|
import auth from "../form/auth.js";
|
||||||
|
|
||||||
|
const execP = promisify(exec);
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
await db.run('INSERT INTO comment (username, targetType, targetId, date, content, id) VALUES (?,?,?,?,?,?)', [
|
||||||
|
username,
|
||||||
|
targetType,
|
||||||
|
targetId,
|
||||||
|
+new Date(),
|
||||||
|
content,
|
||||||
|
id
|
||||||
|
]);
|
||||||
|
|
||||||
|
res.send({ redir: `?` });
|
||||||
|
});
|
||||||
|
|
||||||
|
export default main;
|
|
@ -15,7 +15,7 @@ let main = new Route([auth], async function (req, res, input) {
|
||||||
let id = randomUUID();
|
let id = randomUUID();
|
||||||
|
|
||||||
let { title, desc } = req.body;
|
let { title, desc } = req.body;
|
||||||
if (!title || !desc || !req.file) 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' }; // probably should not re-use these strings
|
||||||
|
|
||||||
let {path} = req.file;
|
let {path} = req.file;
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ import loginB from "./form/login.js";
|
||||||
import registerB from "./form/register.js";
|
import registerB from "./form/register.js";
|
||||||
import uploadB from "./form/upload.js";
|
import uploadB from "./form/upload.js";
|
||||||
import auth from "./form/auth.js";
|
import auth from "./form/auth.js";
|
||||||
|
import commentB from "./form/comment.js";
|
||||||
|
|
||||||
const routes = {
|
const routes = {
|
||||||
get: {},
|
get: {},
|
||||||
|
@ -30,7 +31,8 @@ routes.form = {
|
||||||
login: loginB,
|
login: loginB,
|
||||||
register: registerB,
|
register: registerB,
|
||||||
upload: uploadB,
|
upload: uploadB,
|
||||||
auth
|
auth,
|
||||||
|
comment: commentB
|
||||||
};
|
};
|
||||||
|
|
||||||
async function iterate(req, res, index) {
|
async function iterate(req, res, index) {
|
||||||
|
|
|
@ -37,7 +37,8 @@ textarea,
|
||||||
form,
|
form,
|
||||||
.video,
|
.video,
|
||||||
.controls,
|
.controls,
|
||||||
.user {
|
.user,
|
||||||
|
.comment {
|
||||||
border: solid var(--dark-2) 3px;
|
border: solid var(--dark-2) 3px;
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
|
|
||||||
|
|
21
views/comments.ejs
Normal file
21
views/comments.ejs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<form enctype='multipart/form-data' method='POST' action='/api/form/comment'>
|
||||||
|
<h1>Comments</h1>
|
||||||
|
<!-- probably component-ify this -->
|
||||||
|
<div class='form-message'></div>
|
||||||
|
<input name='targetId' hidden value='<%= id %>'>
|
||||||
|
<input name='targetType' hidden value='<%= route %>'>
|
||||||
|
<textarea name='content'></textarea>
|
||||||
|
<input type='submit'>
|
||||||
|
|
||||||
|
<% for (let comment of comments) { %>
|
||||||
|
<div class='comment'>
|
||||||
|
<div><b>
|
||||||
|
<a href='/client/user?id=<%= comment.username %>'><%= comment.username %></a>
|
||||||
|
</b></div>
|
||||||
|
<div><b>
|
||||||
|
<%= (new Date(comment.date)+'').split(/(GMT|UTC)/g)[0] %>
|
||||||
|
</b></div>
|
||||||
|
<pre><%= comment.content %></pre>
|
||||||
|
</div>
|
||||||
|
<% } %>
|
||||||
|
</form>
|
|
@ -19,4 +19,5 @@
|
||||||
</a></div>
|
</a></div>
|
||||||
<pre><%= videoData.desc %></pre>
|
<pre><%= videoData.desc %></pre>
|
||||||
</div>
|
</div>
|
||||||
|
<%- include('comments.ejs') -%>
|
||||||
<%- include('footer.ejs') -%>
|
<%- include('footer.ejs') -%>
|
|
@ -14,4 +14,5 @@
|
||||||
<%- include('videos.ejs') -%>
|
<%- include('videos.ejs') -%>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<%- include('comments.ejs') -%>
|
||||||
<%- include('footer.ejs') -%>
|
<%- include('footer.ejs') -%>
|
Loading…
Reference in a new issue