From 622c5fee25e7d86914d343ca3f873dc4bd55ffad Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 22 Nov 2024 19:48:31 +0000 Subject: rework invite and admins a bit --- src/db.js | 17 ++++++++++---- src/invite.js | 8 +++++++ src/mixins/post.pug | 61 +++++++++++++++++------------------------------- src/mixins/postUtils.pug | 39 +++++++++++++++++++++++++++++++ src/routes/index.js | 24 +++++++------------ src/views/comments.pug | 28 ++++++++++------------ 6 files changed, 102 insertions(+), 75 deletions(-) create mode 100644 src/mixins/postUtils.pug (limited to 'src') diff --git a/src/db.js b/src/db.js index 747168a..c1fecac 100644 --- a/src/db.js +++ b/src/db.js @@ -15,16 +15,16 @@ function runMigration(name, migrationFn) { } // users table -db.query(` +db.run(` CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, password_hash TEXT ) -`).run(); +`); // subs table -db.query(` +db.run(` CREATE TABLE IF NOT EXISTS subscriptions ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, @@ -32,7 +32,16 @@ db.query(` FOREIGN KEY(user_id) REFERENCES users(id), UNIQUE(user_id, subreddit) ) -`).run(); +`); + +db.run(` + CREATE TABLE IF NOT EXISTS invites ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + token TEXT NOT NULL, + createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + usedAt TIMESTAMP + ) +`); // migrations table db.query(` diff --git a/src/invite.js b/src/invite.js index 7e357ac..f0bc9b9 100644 --- a/src/invite.js +++ b/src/invite.js @@ -1,6 +1,14 @@ const { db } = require("./db"); const validateInviteToken = async (req, res, next) => { + const isFirstUser = db.query("SELECT 1 FROM users LIMIT 1").get() === null; + + if (isFirstUser) { + req.isFirstUser = true; + next(); + return; + } + const token = req.query.token; if (!token) { diff --git a/src/mixins/post.pug b/src/mixins/post.pug index 172da32..487f366 100644 --- a/src/mixins/post.pug +++ b/src/mixins/post.pug @@ -1,4 +1,5 @@ include ../utils +include postUtils mixin post(p) article.post div.post-container @@ -20,62 +21,42 @@ mixin post(p) |  ·  a(href=`/comments/${p.id}`) #{fmtnum (p.num_comments)} ↩ div.media-preview - if p.is_gallery && p.is_gallery == true - if p.gallery_data - if p.gallery_data.items - - var item = p.gallery_data.items[0] - - var url = `https://i.redd.it/${item.media_id}.jpg` - img(src=url onclick=`toggleDetails('${p.id}')`) - else if p.post_hint == "image" && p.thumbnail && p.thumbnail != "self" && p.thumbnail != "default" + if isPostGallery(p) + - var item = postGalleryItems(p)[0] + img(src=item.url onclick=`toggleDetails('${p.id}')`) + else if isPostImage(p) img(src=p.thumbnail onclick=`toggleDetails('${p.id}')`) - else if p.post_hint == "hosted:video" + else if isPostVideo(p) - var url = p.secure_media.reddit_video.scrubber_media_url video(src=url data-dashjs-player width='100px' height='100px' onclick=`toggleDetails('${p.id}')`) - else if p.post_hint == "link" + else if isPostLink(p) a(href=p.url) | ↗ - if p.is_gallery && p.is_gallery == true - if p.gallery_data - if p.gallery_data.items - details(id=`${p.id}`) - summary.expand-post expand gallery - div.gallery - - var total = p.gallery_data.items.length - - var idx = 0 - - var metadata = p.media_metadata - - - var img_ext = (id) => { - if (metadata[id].status == 'valid') { - return stripPrefix(metadata[id].m, "image/"); - } else { - // dosent matter - return 'jpg'; - } - } - each item in p.gallery_data.items - - var id = item.media_id - - var ext = img_ext(item.media_id) - - var url = `https://i.redd.it/${id}.${ext}` - div.gallery-item - a(href=`/media/${url}`) - img(src=url loading="lazy") - div.gallery-item-idx - | #{`${++idx}/${total}`} - button(onclick=`toggleDetails('${p.id}')`) close - if p.post_hint == "image" && p.thumbnail && p.thumbnail != "self" && p.thumbnail != "default" + if isPostGallery(p) + details(id=`${p.id}`) + summary.expand-post expand gallery + div.gallery + each item in postGalleryItems(p) + div.gallery-item + div.gallery-item-idx + | #{`${item.idx}/${item.total}`} + a(href=`/media/${item.url}`) + img(src=item.url loading="lazy") + button(onclick=`toggleDetails('${p.id}')`) close + else if isPostImage(p) details(id=`${p.id}`) summary.expand-post expand image a(href=`/media/${p.url}`) img(src=p.url loading="lazy").post-media button(onclick=`toggleDetails('${p.id}')`) close - else if p.post_hint == "hosted:video" + else if isPostVideo(p) details(id=`${p.id}`) summary.expand-post expand video - var url = p.secure_media.reddit_video.dash_url video(src=url controls data-dashjs-player loading="lazy").post-media button(onclick=`toggleDetails('${p.id}')`) close - else if p.post_hint == "link" + else if isPostLink(p) details(id=`${p.id}`) summary.expand-post expand link a(href=`${p.url}`) diff --git a/src/mixins/postUtils.pug b/src/mixins/postUtils.pug new file mode 100644 index 0000000..4f480b6 --- /dev/null +++ b/src/mixins/postUtils.pug @@ -0,0 +1,39 @@ +- + function isPostGallery(p) { + return (p.is_gallery && p.is_gallery == true); + } +- + function isPostImage(p) { + return (p.post_hint == "image" && p.thumbnail && p.thumbnail != "self" && p.thumbnail != "default"); + } +- + function isPostVideo(p) { + return (p.post_hint == "hosted:video"); + } +- + function isPostLink(p) { + return (p.post_hint == "link"); + } +- + function imgExt(p, id) { + var metadata = p.media_metadata; + if (metadata[id].status == 'valid') { + return stripPrefix(metadata[id].m, "image/"); + } else { + // dosent matter + return 'jpg'; + } + } +- + function postGalleryItems(p) { + if (p.gallery_data && p.gallery_data.items) { + return p.gallery_data.items.map((item, idx) => ({ + id: item.media_id, + url: `https://i.redd.it/${item.media_id}.${imgExt(p, item.media_id)}`, + total: p.gallery_data.items.length, + idx: idx+1, + })); + } else { + return null; + } + } diff --git a/src/routes/index.js b/src/routes/index.js index 9a415be..e585d3d 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -136,15 +136,6 @@ router.get("/create-invite", authenticateAdmin, async (req, res) => { } try { - db.run(` - CREATE TABLE IF NOT EXISTS invites ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - token TEXT NOT NULL, - createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - usedAt TIMESTAMP - ) - `); - createInvite(); return res.redirect("/dashboard"); } catch (err) { @@ -201,19 +192,22 @@ router.post("/register", validateInviteToken, async (req, res) => { try { const hashedPassword = await Bun.password.hash(password); - db.query( - "UPDATE invites SET usedAt = CURRENT_TIMESTAMP WHERE id = $id", - ).run({ - id: req.invite.id, - }); + if (!req.isFirstUser) { + db.query( + "UPDATE invites SET usedAt = CURRENT_TIMESTAMP WHERE id = $id", + ).run({ + id: req.invite.id, + }); + } const insertedRecord = db .query( - "INSERT INTO users (username, password_hash) VALUES ($username, $hashedPassword)", + "INSERT INTO users (username, password_hash, isAdmin) VALUES ($username, $hashedPassword, $isAdmin)", ) .run({ username, hashedPassword, + isAdmin: req.isFirstUser ? 1 : 0, }); const id = insertedRecord.lastInsertRowid; const token = jwt.sign({ username, id }, JWT_KEY, { expiresIn: "5d" }); diff --git a/src/views/comments.pug b/src/views/comments.pug index 541a7bd..e9bd332 100644 --- a/src/views/comments.pug +++ b/src/views/comments.pug @@ -1,6 +1,7 @@ include ../mixins/comment include ../mixins/header include ../mixins/head +include ../mixins/postUtils include ../utils - var post = data.post @@ -33,25 +34,20 @@ html h2.post-title != post.title - if post.is_gallery && post.is_gallery == true - if post.gallery_data - if post.gallery_data.items - div.gallery - - var total = post.gallery_data.items.length - - var idx = 0 - each item in post.gallery_data.items - - var url = `https://i.redd.it/${item.media_id}.jpg` - div.gallery-item - div.gallery-item-idx - | #{`${++idx}/${total}`} - a(href=`/media/${url}`) - img(src=url loading="lazy") - else if post.post_hint == "image" && post.thumbnail && post.thumbnail != "self" && post.thumbnail != "default" + if isPostGallery(post) + div.gallery + each item in postGalleryItems(post) + div.gallery-item + div.gallery-item-idx + | #{`${item.idx}/${item.total}`} + a(href=`/media/${item.url}`) + img(src=item.url loading="lazy") + else if isPostImage(post) img(src=post.url).post-media - else if post.post_hint == 'hosted:video' + else if isPostVideo(post) - var url = post.secure_media.reddit_video.dash_url video(controls data-dashjs-player src=`${url}`).post-media - else if post.post_hint == "link" + else if isPostLink(post) a(href=post.url) | #{post.url} -- cgit v1.2.3