diff options
| author | julian laplace <julescarbon@gmail.com> | 2022-10-26 17:05:14 +0200 |
|---|---|---|
| committer | julian laplace <julescarbon@gmail.com> | 2022-10-26 17:05:14 +0200 |
| commit | dd72ab05da17309fd5ee6005cdc1fae686b5fa9e (patch) | |
| tree | 9edc695fffa73a85d94e571f44c7d4c97de71654 /bucky | |
| parent | 3de2a5872fd0481568e918a1ea798b3f75ace610 (diff) | |
filter by keyword, thread, or username
Diffstat (limited to 'bucky')
| -rw-r--r-- | bucky/app/api.js | 11 | ||||
| -rw-r--r-- | bucky/app/bucky.js | 19 | ||||
| -rw-r--r-- | bucky/app/pages.js | 16 | ||||
| -rw-r--r-- | bucky/db/index.js | 30 | ||||
| -rw-r--r-- | bucky/util/middleware.js | 54 |
5 files changed, 89 insertions, 41 deletions
diff --git a/bucky/app/api.js b/bucky/app/api.js index a2f85d0..0645145 100644 --- a/bucky/app/api.js +++ b/bucky/app/api.js @@ -120,6 +120,7 @@ function route(app) { bucky.checkMail, function (req, res) { res.json({ + query: res.query, threads: res.threads, files: res.files, comments: res.comments, @@ -138,6 +139,16 @@ function route(app) { res.json({ keyword: res.keyword }); } ); + + app.post( + "/api/keyword/new", + bucky.ensureLastlog, + middleware.ensureAuthenticated, + bucky.createKeyword, + function (req, res) { + res.json({ keyword: res.keyword }); + } + ); app.get( "/api/keyword/:keyword", bucky.ensureLastlog, diff --git a/bucky/app/bucky.js b/bucky/app/bucky.js index 03a8b87..86982f8 100644 --- a/bucky/app/bucky.js +++ b/bucky/app/bucky.js @@ -67,17 +67,20 @@ var bucky = (module.exports = { }); }, ensureHootstream: function (req, res, next) { + const query = { + thread: parseInt(req.query?.thread) || null, + keyword: req.query?.keyword || null, + username: req.query?.username || null, + limit: req.query?.limit || 20, + offset: req.query?.offset || 0, + }; + query.has_query = query.thread || query.keyword || query.username; Promise.all([ - db.getHootstreamFiles({ - limit: req.query.limit || 20, - offset: req.query.offset || 0, - }), - db.getHootstreamComments({ - limit: req.query.limit || 20, - offset: req.query.offset || 0, - }), + db.getHootstreamFiles(query), + db.getHootstreamComments(query), ]).then(([files, comments]) => { db.getHootstreamThreads({ files, comments }).then((threads) => { + res.query = query; res.files = files; res.comments = comments.map((comment) => { comment.comment = comment.comment.toString(); diff --git a/bucky/app/pages.js b/bucky/app/pages.js index 94ae46e..0967b9b 100644 --- a/bucky/app/pages.js +++ b/bucky/app/pages.js @@ -31,6 +31,22 @@ function route(app) { hoot_text: fortune("hoots"), }); }); + app.get("/stream/:type", middleware.ensureAuthenticated, function (req, res) { + res.render("pages/stream", { + title: fortune("titles"), + hoot_text: fortune("hoots"), + }); + }); + app.get( + "/stream/:type/:id", + middleware.ensureAuthenticated, + function (req, res) { + res.render("pages/stream", { + title: fortune("titles"), + hoot_text: fortune("hoots"), + }); + } + ); app.get("/keywords", middleware.ensureAuthenticated, function (req, res) { res.render("pages/keywords", { title: "Bucky's keywords" }); }); diff --git a/bucky/db/index.js b/bucky/db/index.js index 36541f8..2a1a3bf 100644 --- a/bucky/db/index.js +++ b/bucky/db/index.js @@ -116,19 +116,41 @@ db.getLastlog = function (limit) { /** HOOTSTREAM */ -db.getHootstreamFiles = ({ limit, offset }) => +db.getHootstreamFiles = ({ limit, offset, thread, keyword, username }) => knex("files") .join("threads", "threads.id", "=", "files.thread") .select("files.*") - .where("threads.privacy", false) + .where((builder) => { + builder.where("threads.privacy", false); + if (keyword) { + builder.where("threads.keyword", keyword); + } + if (thread) { + builder.where("threads.id", thread); + } + if (username) { + builder.where("comments.username", username); + } + }) .orderBy("files.id", "desc") .offset(offset) .limit(limit); -db.getHootstreamComments = ({ limit, offset }) => +db.getHootstreamComments = ({ limit, offset, thread, keyword, username }) => knex("comments") .join("threads", "threads.id", "=", "comments.thread") .select("comments.*") - .where("threads.privacy", false) + .where((builder) => { + builder.where("threads.privacy", false); + if (keyword) { + builder.where("threads.keyword", keyword); + } + if (thread) { + builder.where("threads.id", thread); + } + if (username) { + builder.where("comments.username", username); + } + }) .orderBy("comments.id", "desc") .offset(offset) .limit(limit); diff --git a/bucky/util/middleware.js b/bucky/util/middleware.js index b91631d..37ec124 100644 --- a/bucky/util/middleware.js +++ b/bucky/util/middleware.js @@ -1,38 +1,34 @@ const buildDate = +Date.now(); -var middleware = module.exports = { - +var middleware = (module.exports = { ensureAuthenticated: function (req, res, next) { - if (! req.isAuthenticated()) { - req.session.returnTo = req.path - return res.redirect('/login') + if (!req.isAuthenticated()) { + req.session.returnTo = req.path; + return res.redirect("/login"); } - next() + next(); }, - + ensureLocals: function (req, res, next) { - res.locals.csrfToken = req.csrfToken ? req.csrfToken() : 'csrf' - res.locals.title = "bucky" - res.locals.buildDate = buildDate, - res.locals.env = process.env.NODE_ENV + res.locals.csrfToken = req.csrfToken ? req.csrfToken() : "csrf"; + res.locals.title = "bucky"; + (res.locals.buildDate = buildDate), (res.locals.env = process.env.NODE_ENV); if (req.isAuthenticated()) { - res.locals.show_header = true - res.locals.preload = JSON.stringify({ - env: res.locals.env, - buildDate: buildDate, - s3: { - bucket: process.env.S3_BUCKET, - path: process.env.S3_PATH, - } - }) - } - else { - res.locals.show_header = false - res.locals.preload = JSON.stringify({ - env: res.locals.env, - }) + res.locals.show_header = true; + res.locals.preload = JSON.stringify({ + env: res.locals.env, + buildDate: buildDate, + s3: { + bucket: process.env.S3_BUCKET, + path: process.env.S3_PATH, + }, + }); + } else { + res.locals.show_header = false; + res.locals.preload = JSON.stringify({ + env: res.locals.env, + }); } - next() + next(); }, - -} +}); |
