From 24c7965d9d0aaf27985d29c5cbb12a4e08ac7d9c Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sat, 11 Jul 2020 14:04:25 -0400 Subject: Factor out "scope" of pages Use this to keep the search query around and hide network and context from the search results if the scope is already limited. --- search.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index 767081b..6fba589 100644 --- a/search.c +++ b/search.c @@ -46,20 +46,12 @@ const char *SearchQuery = SQL( ); enum kcgi_err pageSearch(struct kreq *req) { - if (!req->fieldmap[Query]) { - return httpFail(req, KHTTP_400); - } - if (req->fieldmap[Context] && !req->fieldmap[Network]) { - return httpFail(req, KHTTP_400); - } - const char *query = req->fieldmap[Query]->parsed.s; + struct Scope scope = htmlScope(req); + if (!scope.query) return httpFail(req, KHTTP_400); + if (scope.context && !scope.network) return httpFail(req, KHTTP_400); int64_t offset = 0; - const char *network = NULL; - const char *context = NULL; if (req->fieldmap[Offset]) offset = req->fieldmap[Offset]->parsed.i; - if (req->fieldmap[Network]) network = req->fieldmap[Network]->parsed.s; - if (req->fieldmap[Context]) context = req->fieldmap[Context]->parsed.s; enum kcgi_err error = httpHead(req, KHTTP_200, KMIME_TEXT_HTML); if (req->method == KMETHOD_HEAD) return error; @@ -68,16 +60,16 @@ enum kcgi_err pageSearch(struct kreq *req) { error = error || khttp_body(req) || khtml_open(&html, req, KHTML_PRETTY) - || htmlHead(&html, query) - || htmlNav(&html, network, context) + || htmlHead(&html, scope.query) + || htmlNav(&html, scope) || khtml_elem(&html, KELEM_TABLE); if (error) return error; dbBindText(stmt.search, ":highlight", "\26"); - dbBindText(stmt.search, ":network", network); - dbBindText(stmt.search, ":context", context); + dbBindText(stmt.search, ":network", scope.network); + dbBindText(stmt.search, ":context", scope.context); if (pagePublic) dbBindInt(stmt.search, ":query", false); - dbBindText(stmt.search, ":search", query); + dbBindText(stmt.search, ":search", scope.query); dbBindInt(stmt.search, ":limit", pageLimit); dbBindInt(stmt.search, ":offset", offset); @@ -95,7 +87,7 @@ enum kcgi_err pageSearch(struct kreq *req) { .target = (const char *)sqlite3_column_text(stmt.search, 8), .message = (const char *)sqlite3_column_text(stmt.search, 9), }; - error = htmlEvent(&html, event); + error = htmlEvent(&html, scope, event); if (error) break; } if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); -- cgit 1.4.1