summary refs log tree commit diff
path: root/search.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-07-11 14:04:25 -0400
committerJune McEnroe <june@causal.agency>2020-07-11 14:04:25 -0400
commit24c7965d9d0aaf27985d29c5cbb12a4e08ac7d9c (patch)
tree2a9e3955d59c375edb7d02ae0bd195bf966d57ea /search.c
parentImplement basic search page (diff)
downloadscooper-24c7965d9d0aaf27985d29c5cbb12a4e08ac7d9c.tar.gz
scooper-24c7965d9d0aaf27985d29c5cbb12a4e08ac7d9c.zip
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.
Diffstat (limited to '')
-rw-r--r--search.c26
1 files changed, 9 insertions, 17 deletions
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));