summary refs log tree commit diff
path: root/search.c
diff options
context:
space:
mode:
Diffstat (limited to 'search.c')
-rw-r--r--search.c42
1 files changed, 18 insertions, 24 deletions
diff --git a/search.c b/search.c
index af29685..c284732 100644
--- a/search.c
+++ b/search.c
@@ -44,6 +44,21 @@ const char *SearchQuery = SQL(
 	LIMIT :offset, :limit;
 );
 
+static char *offsetURL(struct Scope scope, int64_t offset) {
+	const char *networkKey = (scope.network ? Keys[Network].name : NULL);
+	const char *contextKey = (scope.context ? Keys[Context].name : NULL);
+	char *url = khttp_urlpartx(
+		NULL, NULL, Pages[Search],
+		Keys[Query].name, KATTRX_STRING, scope.query,
+		Keys[Offset].name, KATTRX_INT, offset,
+		networkKey, KATTRX_STRING, scope.network,
+		contextKey, KATTRX_STRING, scope.context,
+		NULL
+	);
+	if (!url) err(EX_OSERR, "khttp_urlpartx");
+	return url;
+}
+
 enum kcgi_err searchPage(struct kreq *req) {
 	struct Scope scope = pageScope(req);
 	if (!scope.query) return httpFail(req, KHTTP_400);
@@ -67,18 +82,7 @@ enum kcgi_err searchPage(struct kreq *req) {
 
 	if (offset) {
 		int64_t prev = offset - eventsLimit;
-		if (prev < 0) prev = 0;
-		char *href = khttp_urlpartx(
-			NULL, NULL, Pages[Search],
-			Keys[Query].name, KATTRX_STRING, scope.query,
-			Keys[Offset].name, KATTRX_INT, prev,
-			(scope.network ? Keys[Network].name : NULL),
-			KATTRX_STRING, scope.network,
-			(scope.context ? Keys[Context].name : NULL),
-			KATTRX_STRING, scope.context,
-			NULL
-		);
-		if (!href) err(EX_OSERR, "khttp_urlpartx");
+		char *href = offsetURL(scope, (prev > 0 ? prev : 0));
 		error = 0
 			|| khtml_attr(&html, KELEM_TR, KATTR_CLASS, "page", KATTR__MAX)
 			|| khtml_attr(&html, KELEM_TH, KATTR_COLSPAN, "5", KATTR__MAX)
@@ -90,13 +94,13 @@ enum kcgi_err searchPage(struct kreq *req) {
 	}
 
 	sqlite3_reset(stmt.search);
+	dbBindText(stmt.search, ":highlight", "\26");
 	dbBindText(stmt.search, ":network", scope.network);
 	dbBindText(stmt.search, ":context", scope.context);
 	dbBindText(stmt.search, ":query", scope.query);
 	dbBindInt(stmt.search, ":public", contextsPublic);
 	dbBindInt(stmt.search, ":limit", eventsLimit);
 	dbBindInt(stmt.search, ":offset", offset);
-	dbBindText(stmt.search, ":highlight", "\26");
 
 	int rows;
 	int result;
@@ -126,17 +130,7 @@ enum kcgi_err searchPage(struct kreq *req) {
 	}
 
 	if (rows == eventsLimit) {
-		char *href = khttp_urlpartx(
-			NULL, NULL, Pages[Search],
-			Keys[Query].name, KATTRX_STRING, scope.query,
-			Keys[Offset].name, KATTRX_INT, offset + eventsLimit,
-			(scope.network ? Keys[Network].name : NULL),
-			KATTRX_STRING, scope.network,
-			(scope.context ? Keys[Context].name : NULL),
-			KATTRX_STRING, scope.context,
-			NULL
-		);
-		if (!href) err(EX_OSERR, "khttp_urlpartx");
+		char *href = offsetURL(scope, offset + eventsLimit);
 		error = 0
 			|| khtml_attr(&html, KELEM_TR, KATTR_CLASS, "page", KATTR__MAX)
 			|| khtml_attr(&html, KELEM_TH, KATTR_COLSPAN, "5", KATTR__MAX)