diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-07-11 13:11:50 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-07-11 13:27:49 +0200 |
| commit | 38f3f82a77ed4782a9c4e1b4e5a6d9d4c71390a2 (patch) | |
| tree | 4a0d1c0a76f1e38562ad544f0fca5ce80cec32d6 /page.c | |
| parent | 59e17afe291f9df52d2334b92e88a889c2fe8f6a (diff) | |
Limit amount of search results
When a user enters a search term that is too generic, slcl would
generate a long list of search results, where this generation could have
a big impact on the server performance and its available resources.
Therefore, it is reasonable to limit the number of search results to an
arbitrary limit, so that users are forced to enter a more specific
search term in order to achieve more relevant results.
Diffstat (limited to 'page.c')
| -rw-r--r-- | page.c | 74 |
1 files changed, 72 insertions, 2 deletions
@@ -1766,6 +1766,76 @@ static int add_search_results(struct html_node *const n, return 0; } +static int prepare_search_limit_exceeded(struct html_node *const n, + const struct page_search *const s) +{ + static const char msg[] = "Too many results were found. Please " + "set up a more specific search term in order to provide " + "fewer and more relevant results"; + struct html_node *const p = html_node_add_child(n, "p"); + + if (!p) + { + fprintf(stderr, "%s: html_node_add_child p failed\n", __func__); + return -1; + } + else if (html_node_set_value(p, msg)) + { + fprintf(stderr, "%s: html_node_set_value failed\n", __func__); + return -1; + } + + return 0; +} + +static int prepare_search_result_count(struct html_node *const n, + const struct page_search *const s) +{ + int ret = -1; + struct html_node *const p = html_node_add_child(n, "p"); + struct dynstr d; + + dynstr_init(&d); + + if (!p) + { + fprintf(stderr, "%s: html_node_add_child p failed\n", __func__); + goto end; + } + else if (s->limit_exceeded && prepare_search_limit_exceeded(n, s)) + { + fprintf(stderr, "%s: prepare_search_limit_exceeded msg failed\n", + __func__); + goto end; + } + else if (!s->n && html_node_set_value(p, "No results found")) + { + fprintf(stderr, "%s: html_node_set_value msg failed\n", __func__); + goto end; + } + else if (s->n) + { + const char *const results = s->n == 1 ? "result" : "results"; + + if (dynstr_append(&d, "%zu %s found", s->n, results)) + { + fprintf(stderr, "%s: dynstr_append failed\n", __func__); + goto end; + } + else if (html_node_set_value(p, d.str)) + { + fprintf(stderr, "%s: html_node_set_value p failed\n", __func__); + goto end; + } + } + + ret = 0; + +end: + dynstr_free(&d); + return ret; +} + int page_search(struct http_response *const r, const struct page_search *const s) { @@ -1800,9 +1870,9 @@ int page_search(struct http_response *const r, fprintf(stderr, "%s: add_search_results failed\n", __func__); goto end; } - else if (!s->n && html_node_set_value(body, "No results found")) + else if (prepare_search_result_count(body, s)) { - fprintf(stderr, "%s: html_node_set_value msg failed\n", __func__); + fprintf(stderr, "%s: prepare_limit_exceeded failed\n", __func__); goto end; } else if (prepare_footer(body)) |
