neon_arch commited on
Commit
72fec47
1 Parent(s): d2e4822

✨ feat: implement code to fetch results based on parameters, cookies & config (#210)

Browse files
Files changed (1) hide show
  1. src/server/routes/search.rs +38 -25
src/server/routes/search.rs CHANGED
@@ -67,61 +67,48 @@ pub async fn search(
67
  None => 1,
68
  };
69
 
70
- let safe_search: u8 = match config.safe_search {
71
- 3..=4 => config.safe_search,
72
- _ => match &params.safesearch {
73
- Some(safesearch) => match safesearch {
74
- 0..=2 => *safesearch,
75
- _ => 1,
76
- },
77
- None => config.safe_search,
78
- },
79
- };
80
-
81
  let (_, results, _) = join!(
82
  results(
83
  format!(
84
- "http://{}:{}/search?q={}&page={}&safesearch={}",
85
  config.binding_ip,
86
  config.port,
87
  query,
88
  page - 1,
89
- safe_search
90
  ),
91
  &config,
92
  &cache,
93
  query,
94
  page - 1,
95
  req.clone(),
96
- safe_search
97
  ),
98
  results(
99
  format!(
100
- "http://{}:{}/search?q={}&page={}&safesearch={}",
101
- config.binding_ip, config.port, query, page, safe_search
102
  ),
103
  &config,
104
  &cache,
105
  query,
106
  page,
107
  req.clone(),
108
- safe_search
109
  ),
110
  results(
111
  format!(
112
- "http://{}:{}/search?q={}&page={}&safesearch={}",
113
  config.binding_ip,
114
  config.port,
115
  query,
116
  page + 1,
117
- safe_search
118
  ),
119
  &config,
120
  &cache,
121
  query,
122
  page + 1,
123
  req.clone(),
124
- safe_search
125
  )
126
  );
127
 
@@ -156,7 +143,7 @@ async fn results(
156
  query: &str,
157
  page: u32,
158
  req: HttpRequest,
159
- safe_search: u8,
160
  ) -> Result<SearchResults, Box<dyn std::error::Error>> {
161
  // fetch the cached results json.
162
  let cached_results = cache.cached_json(&url).await;
@@ -165,7 +152,18 @@ async fn results(
165
  match cached_results {
166
  Ok(results) => Ok(results),
167
  Err(_) => {
168
- if safe_search == 4 {
 
 
 
 
 
 
 
 
 
 
 
169
  let mut results: SearchResults = SearchResults::default();
170
  let mut _flag: bool =
171
  is_match_from_filter_list(file_path(FileType::BlockList)?, query)?;
@@ -176,6 +174,7 @@ async fn results(
176
  results.add_style(&config.style);
177
  results.set_page_query(query);
178
  cache.cache_results(&results, &url).await?;
 
179
  return Ok(results);
180
  }
181
  }
@@ -195,6 +194,17 @@ async fn results(
195
  .filter_map(|name| EngineHandler::new(name))
196
  .collect();
197
 
 
 
 
 
 
 
 
 
 
 
 
198
  aggregate(
199
  query,
200
  page,
@@ -202,7 +212,7 @@ async fn results(
202
  config.debug,
203
  &engines,
204
  config.request_timeout,
205
- safe_search,
206
  )
207
  .await?
208
  }
@@ -214,7 +224,7 @@ async fn results(
214
  config.debug,
215
  &config.upstream_search_engines,
216
  config.request_timeout,
217
- safe_search,
218
  )
219
  .await?
220
  }
@@ -223,7 +233,10 @@ async fn results(
223
  results.set_filtered();
224
  }
225
  results.add_style(&config.style);
226
- cache.cache_results(&results, &url).await?;
 
 
 
227
  Ok(results)
228
  }
229
  }
 
67
  None => 1,
68
  };
69
 
 
 
 
 
 
 
 
 
 
 
 
70
  let (_, results, _) = join!(
71
  results(
72
  format!(
73
+ "http://{}:{}/search?q={}&page={}&safesearch=",
74
  config.binding_ip,
75
  config.port,
76
  query,
77
  page - 1,
 
78
  ),
79
  &config,
80
  &cache,
81
  query,
82
  page - 1,
83
  req.clone(),
84
+ &params.safesearch
85
  ),
86
  results(
87
  format!(
88
+ "http://{}:{}/search?q={}&page={}&safesearch=",
89
+ config.binding_ip, config.port, query, page
90
  ),
91
  &config,
92
  &cache,
93
  query,
94
  page,
95
  req.clone(),
96
+ &params.safesearch
97
  ),
98
  results(
99
  format!(
100
+ "http://{}:{}/search?q={}&page={}&safesearch=",
101
  config.binding_ip,
102
  config.port,
103
  query,
104
  page + 1,
 
105
  ),
106
  &config,
107
  &cache,
108
  query,
109
  page + 1,
110
  req.clone(),
111
+ &params.safesearch
112
  )
113
  );
114
 
 
143
  query: &str,
144
  page: u32,
145
  req: HttpRequest,
146
+ safe_search: &Option<u8>,
147
  ) -> Result<SearchResults, Box<dyn std::error::Error>> {
148
  // fetch the cached results json.
149
  let cached_results = cache.cached_json(&url).await;
 
152
  match cached_results {
153
  Ok(results) => Ok(results),
154
  Err(_) => {
155
+ let mut safe_search_level: u8 = match config.safe_search {
156
+ 3..=4 => config.safe_search,
157
+ _ => match safe_search {
158
+ Some(safesearch) => match safesearch {
159
+ 0..=2 => *safesearch,
160
+ _ => config.safe_search,
161
+ },
162
+ None => config.safe_search,
163
+ },
164
+ };
165
+
166
+ if safe_search_level == 4 {
167
  let mut results: SearchResults = SearchResults::default();
168
  let mut _flag: bool =
169
  is_match_from_filter_list(file_path(FileType::BlockList)?, query)?;
 
174
  results.add_style(&config.style);
175
  results.set_page_query(query);
176
  cache.cache_results(&results, &url).await?;
177
+ results.set_safe_search_level(safe_search_level);
178
  return Ok(results);
179
  }
180
  }
 
194
  .filter_map(|name| EngineHandler::new(name))
195
  .collect();
196
 
197
+ safe_search_level = match config.safe_search {
198
+ 3..=4 => config.safe_search,
199
+ _ => match safe_search {
200
+ Some(safesearch) => match safesearch {
201
+ 0..=2 => *safesearch,
202
+ _ => config.safe_search,
203
+ },
204
+ None => cookie_value.safe_search_level,
205
+ },
206
+ };
207
+
208
  aggregate(
209
  query,
210
  page,
 
212
  config.debug,
213
  &engines,
214
  config.request_timeout,
215
+ safe_search_level,
216
  )
217
  .await?
218
  }
 
224
  config.debug,
225
  &config.upstream_search_engines,
226
  config.request_timeout,
227
+ safe_search_level,
228
  )
229
  .await?
230
  }
 
233
  results.set_filtered();
234
  }
235
  results.add_style(&config.style);
236
+ cache
237
+ .cache_results(&results, &(format!("{url}{safe_search_level}")))
238
+ .await?;
239
+ results.set_safe_search_level(safe_search_level);
240
  Ok(results)
241
  }
242
  }