ddotthomas commited on
Commit
6e9250c
·
1 Parent(s): 7d762b3

fix: safe search url parameter ignored

Browse files
Files changed (1) hide show
  1. src/server/routes/search.rs +29 -6
src/server/routes/search.rs CHANGED
@@ -63,19 +63,21 @@ pub async fn search(
63
  };
64
 
65
  // Get search settings using the user's cookie or from the server's config
66
- let search_settings: server_models::Cookie = match req.cookie("appCookie") {
67
  Some(cookie_value) => {
68
- if let Ok(cookie) = serde_json::from_str(cookie_value.value()) {
69
- cookie
70
- // If there's an issue parsing the cookie's value, default to the config
71
- } else {
72
- build_cookie()
73
  }
74
  }
75
  // If there's no cookie saved, use the server's config
76
  None => build_cookie(),
77
  };
78
 
 
 
 
79
  // Closure wrapping the results function capturing local references
80
  let get_results = |page| results(&config, &cache, query, page, &search_settings);
81
 
@@ -228,3 +230,24 @@ fn is_match_from_filter_list(
228
 
229
  Ok(false)
230
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  };
64
 
65
  // Get search settings using the user's cookie or from the server's config
66
+ let mut search_settings: server_models::Cookie = match req.cookie("appCookie") {
67
  Some(cookie_value) => {
68
+ match serde_json::from_str(cookie_value.value()) {
69
+ Ok(cookie) => cookie,
70
+ // If there's an issue parsing the cookie's value, default to the config
71
+ Err(_) => build_cookie(),
 
72
  }
73
  }
74
  // If there's no cookie saved, use the server's config
75
  None => build_cookie(),
76
  };
77
 
78
+ search_settings.safe_search_level =
79
+ get_safesearch_level(&params.safesearch, config.safe_search);
80
+
81
  // Closure wrapping the results function capturing local references
82
  let get_results = |page| results(&config, &cache, query, page, &search_settings);
83
 
 
230
 
231
  Ok(false)
232
  }
233
+
234
+ /// A helper function which returns the safe search level based on the url params
235
+ /// and cookie value.
236
+ ///
237
+ /// # Argurments
238
+ ///
239
+ /// * `safe_search` - Safe search level from the url.
240
+ /// * `cookie` - User's cookie
241
+ /// * `default` - Safe search level to fall back to
242
+ fn get_safesearch_level(safe_search: &Option<u8>, default: u8) -> u8 {
243
+ match safe_search {
244
+ Some(ss) => {
245
+ if *ss >= 3 {
246
+ default
247
+ } else {
248
+ *ss
249
+ }
250
+ }
251
+ None => default,
252
+ }
253
+ }