Skip to content
This repository was archived by the owner on Oct 10, 2024. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
21 changes: 20 additions & 1 deletion src/controllers/boards.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}))

Expand Down
24 changes: 24 additions & 0 deletions src/routes/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
38 changes: 38 additions & 0 deletions src/views/board_search.njk
Original file line number Diff line number Diff line change
@@ -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 %}
<link rel="stylesheet" href="/static/asset/css/flags.css" />
{% endif %}

{% if board.board_flags %}
<link rel="stylesheet" href="/static/asset/image/flags/{{ board.board }}/flags.css" />
{% endif %}

{% endblock %}

{% block body %}
{{ super() }}

<h1>/{{ board.board }}/ - {{ board.title }}</h1>

<form action="/{{ board.board }}/search" method="post">
<input type="text" name="query" placeholder="Search!" value="{{ search.query }}" required />
<input type="submit" value="Search" />
</form>

{% if search %}
{% if search.count == 0 %}
<p>No results found.</p>
{% endif %}

<div>
{% for threadInfo in search.threads %}
{{ thread(threadInfo) }}
{% endfor %}
</div>
{% endif %}

{% endblock %}
27 changes: 27 additions & 0 deletions src/views/search.njk
Original file line number Diff line number Diff line change
@@ -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() }}

<h1>Search All Boards</h1>

<form action="/search" method="post">
<input type="text" name="query" placeholder="Search!" value="{{ search.query }}" required />
<input type="submit" value="Search" />
</form>

{% if search %}
{% if search.count == 0 %}
<p>No results found.</p>
{% endif %}

<div>
{% for threadInfo in search.threads %}
{{ thread(threadInfo) }}
{% endfor %}
</div>
{% endif %}

{% endblock %}