From 742d3be6ca6822f92900bdb6ddf7144c4bd27bd2 Mon Sep 17 00:00:00 2001 From: WorkUser Date: Wed, 28 Jun 2023 02:39:34 +0300 Subject: [PATCH] wip: board/global search --- .env | 1 + src/controllers/boards.js | 21 ++++++++++++++++++++- src/index.js | 4 ++++ src/routes/main.js | 24 ++++++++++++++++++++++++ src/views/board_search.njk | 38 ++++++++++++++++++++++++++++++++++++++ src/views/search.njk | 27 +++++++++++++++++++++++++++ 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/views/board_search.njk create mode 100644 src/views/search.njk diff --git a/.env b/.env index 071121b..c631b13 100644 --- a/.env +++ b/.env @@ -3,6 +3,7 @@ PORT=3000 APP_NAME=PrivacyChan API_BASE_URL="https://a.4cdn.org/" +P4_API_BASE_URL="https://p.4chan.org/api/" API_BOARDS_ENDPOINT="boards.json" API_CATALOG_ENDPOINT="catalog.json" diff --git a/src/controllers/boards.js b/src/controllers/boards.js index 1e80853..f596906 100644 --- a/src/controllers/boards.js +++ b/src/controllers/boards.js @@ -153,7 +153,26 @@ const GetThreadForBoard = async (board_code, thread_id) => { }) } +const Search = async (query, board_code) => { + return new Promise((resolve, reject) => { + fetch(`${process.env.P4_API_BASE_URL}search?` + new URLSearchParams({ + q: query, + b: board_code + }), { + headers: { + "X-Requested-With": "p4 f" + } + }).then(result => { + result.json().then(json => { + if (json.errors) {resolve({})} + resolve(json) + }).catch(reject) + }).catch(reject) + }) +} + module.exports = { GetThreadsForBoard, - GetThreadForBoard + GetThreadForBoard, + Search } \ No newline at end of file diff --git a/src/index.js b/src/index.js index e5998cf..7093920 100644 --- a/src/index.js +++ b/src/index.js @@ -44,6 +44,10 @@ nunjucksEnv.addFilter("split", (string, separator) => { return string[0].split(separator) }) +nunjucksEnv.addFilter("json", (string) => { + return JSON.stringify(string) +}) + app.use(express.json()) app.use(express.urlencoded({extended: false})) diff --git a/src/routes/main.js b/src/routes/main.js index 7bdbc07..77f1d34 100644 --- a/src/routes/main.js +++ b/src/routes/main.js @@ -10,6 +10,16 @@ const BoardsController = require("../controllers/boards") router.get("/", (req, res) => res.render("index", {boards: boards})) +router.get("/search", (req, res) => res.render("search")) +router.post("/search", async (req, res, next) => { + const {query} = req.body + if (typeof(query) != "string") {return next("Query must be a string.")} + + BoardsController.Search(query).then(result => { + res.render("search", {search: {count: result.nhits || 0, threads: result.threads, query}}) + }).catch(err => next(err)) +}) + router.get("/:board_code/", CheckBoard, async (req, res, next) => { const {board_code} = req.params @@ -18,6 +28,20 @@ router.get("/:board_code/", CheckBoard, async (req, res, next) => { }).catch(err => next(err)) }) +router.get("/:board_code/search", CheckBoard, (req, res) => { + const {board_code} = req.params + res.render("board_search", {board: boardList[board_code]}) +}) +router.post("/:board_code/search", CheckBoard, async (req, res, next) => { + const {board_code} = req.params + const {query} = req.body + + if (typeof(query) != "string") {return next("Query must be a string.")} + BoardsController.Search(query, board_code).then(result => { + res.render("board_search", {board: boardList[board_code], search: {count: result.nhits || 0, threads: result.threads, query}}) + }).catch(err => next(err)) +}) + router.get("/:board_code/:index", CheckBoard, CheckPage, async (req, res, next) => { const {board_code, index} = req.params diff --git a/src/views/board_search.njk b/src/views/board_search.njk new file mode 100644 index 0000000..02da911 --- /dev/null +++ b/src/views/board_search.njk @@ -0,0 +1,38 @@ +{% set title = ["/", board.board, "/"] | join %} +{% extends "core/layout.njk" %} +{% from "core/components/thread.njk" import thread with context %} + +{% block includes %} + {% if board.country_flags %} + + {% endif %} + + {% if board.board_flags %} + + {% endif %} + +{% endblock %} + +{% block body %} +{{ super() }} + +

/{{ board.board }}/ - {{ board.title }}

+ +
+ + +
+ +{% if search %} + {% if search.count == 0 %} +

No results found.

+ {% endif %} + +
+ {% for threadInfo in search.threads %} + {{ thread(threadInfo) }} + {% endfor %} +
+{% endif %} + +{% endblock %} \ No newline at end of file diff --git a/src/views/search.njk b/src/views/search.njk new file mode 100644 index 0000000..5d05281 --- /dev/null +++ b/src/views/search.njk @@ -0,0 +1,27 @@ +{% set title = "Search All Boards" %} +{% extends "core/layout.njk" %} +{% from "core/components/thread.njk" import thread with context %} + +{% block body %} +{{ super() }} + +

Search All Boards

+ +
+ + +
+ +{% if search %} + {% if search.count == 0 %} +

No results found.

+ {% endif %} + +
+ {% for threadInfo in search.threads %} + {{ thread(threadInfo) }} + {% endfor %} +
+{% endif %} + +{% endblock %} \ No newline at end of file