mirror of
https://github.com/neon-mmd/websurfx.git
synced 2024-11-21 21:48:21 -05:00
refactor: changed Cookie to use Cow to facilitate using references when building
This commit is contained in:
parent
a47e28587c
commit
80e950de3b
@ -1,5 +1,7 @@
|
|||||||
//! This module provides the models to parse cookies and search parameters from the search
|
//! This module provides the models to parse cookies and search parameters from the search
|
||||||
//! engine website.
|
//! engine website.
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use super::parser_models::Style;
|
use super::parser_models::Style;
|
||||||
@ -23,23 +25,23 @@ pub struct SearchParams {
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct Cookie<'a> {
|
pub struct Cookie<'a> {
|
||||||
/// It stores the theme name used in the website.
|
/// It stores the theme name used in the website.
|
||||||
pub theme: &'a str,
|
pub theme: Cow<'a, str>,
|
||||||
/// It stores the colorscheme name used for the website theme.
|
/// It stores the colorscheme name used for the website theme.
|
||||||
pub colorscheme: &'a str,
|
pub colorscheme: Cow<'a, str>,
|
||||||
/// It stores the user selected upstream search engines selected from the UI.
|
/// It stores the user selected upstream search engines selected from the UI.
|
||||||
pub engines: Vec<String>,
|
pub engines: Cow<'a, Vec<Cow<'a, str>>>,
|
||||||
/// It stores the user selected safe search level from the UI.
|
/// It stores the user selected safe search level from the UI.
|
||||||
pub safe_search_level: u8,
|
pub safe_search_level: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Cookie<'a> {
|
impl<'a> Cookie<'a> {
|
||||||
/// server_models::Cookie contructor function
|
/// server_models::Cookie contructor function
|
||||||
pub fn build(style: &'a Style, mut engines: Vec<String>, safe_search_level: u8) -> Self {
|
pub fn build(style: &'a Style, mut engines: Vec<Cow<'a, str>>, safe_search_level: u8) -> Self {
|
||||||
engines.sort();
|
engines.sort();
|
||||||
Self {
|
Self {
|
||||||
theme: &style.theme,
|
theme: Cow::Borrowed(&style.theme),
|
||||||
colorscheme: &style.colorscheme,
|
colorscheme: Cow::Borrowed(&style.colorscheme),
|
||||||
engines,
|
engines: Cow::Owned(engines),
|
||||||
safe_search_level,
|
safe_search_level,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ use crate::{
|
|||||||
use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
|
use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::{
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{BufRead, BufReader, Read},
|
io::{BufRead, BufReader, Read},
|
||||||
};
|
};
|
||||||
@ -48,37 +49,30 @@ pub async fn search(
|
|||||||
.finish());
|
.finish());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Closure to build a server_models::Cookie capturing local references
|
|
||||||
let build_cookie = || {
|
|
||||||
server_models::Cookie::build(
|
|
||||||
&config.style,
|
|
||||||
config
|
|
||||||
.upstream_search_engines
|
|
||||||
.clone()
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|engine_map| engine_map.1.then_some(engine_map.0))
|
|
||||||
.collect(),
|
|
||||||
config.safe_search,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let cookie = req.cookie("appCookie");
|
let cookie = req.cookie("appCookie");
|
||||||
|
|
||||||
// Get search settings using the user's cookie or from the server's config
|
// Get search settings using the user's cookie or from the server's config
|
||||||
let mut search_settings: server_models::Cookie<'_> = match cookie {
|
let mut search_settings: server_models::Cookie<'_> = cookie
|
||||||
Some(ref cookie_value) => {
|
.and_then(|cookie_value| serde_json::from_str(cookie_value.value()).ok())
|
||||||
match serde_json::from_str(cookie_value.value()) {
|
.unwrap_or_else(|| {
|
||||||
Ok(cookie) => cookie,
|
server_models::Cookie::build(
|
||||||
// If there's an issue parsing the cookie's value, default to the config
|
&config.style,
|
||||||
Err(_) => build_cookie(),
|
config
|
||||||
}
|
.upstream_search_engines
|
||||||
}
|
.iter()
|
||||||
// If there's no cookie saved, use the server's config
|
.filter_map(|(engine, enabled)| {
|
||||||
None => build_cookie(),
|
enabled.then_some(Cow::Borrowed(engine.as_str()))
|
||||||
};
|
})
|
||||||
|
.collect(),
|
||||||
|
config.safe_search,
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
search_settings.safe_search_level =
|
get_safesearch_level(
|
||||||
get_safesearch_level(¶ms.safesearch, config.safe_search);
|
&mut search_settings.safe_search_level,
|
||||||
|
¶ms.safesearch,
|
||||||
|
config.safe_search,
|
||||||
|
);
|
||||||
|
|
||||||
// Closure wrapping the results function capturing local references
|
// Closure wrapping the results function capturing local references
|
||||||
let get_results = |page| results(&config, &cache, query, page, &search_settings);
|
let get_results = |page| results(&config, &cache, query, page, &search_settings);
|
||||||
@ -178,8 +172,7 @@ async fn results(
|
|||||||
config.debug,
|
config.debug,
|
||||||
&search_settings
|
&search_settings
|
||||||
.engines
|
.engines
|
||||||
.clone()
|
.iter()
|
||||||
.into_iter()
|
|
||||||
.filter_map(|engine| EngineHandler::new(&engine).ok())
|
.filter_map(|engine| EngineHandler::new(&engine).ok())
|
||||||
.collect::<Vec<EngineHandler>>(),
|
.collect::<Vec<EngineHandler>>(),
|
||||||
config.request_timeout,
|
config.request_timeout,
|
||||||
@ -233,23 +226,21 @@ fn is_match_from_filter_list(
|
|||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A helper function which returns the safe search level based on the url params
|
/// A helper function to modify the safe search level based on the url params.
|
||||||
/// and cookie value.
|
/// The `safe_search` is the one in the user's cookie or
|
||||||
|
/// the default set by the server config if the cookie was missing.
|
||||||
///
|
///
|
||||||
/// # Argurments
|
/// # Argurments
|
||||||
///
|
///
|
||||||
/// * `safe_search` - Safe search level from the url.
|
/// * `url_level` - Safe search level from the url.
|
||||||
/// * `cookie` - User's cookie
|
/// * `safe_search` - User's cookie, or the safe search level set by the server
|
||||||
/// * `default` - Safe search level to fall back to
|
/// * `config_level` - Safe search level to fall back to
|
||||||
fn get_safesearch_level(safe_search: &Option<u8>, default: u8) -> u8 {
|
fn get_safesearch_level(safe_search: &mut u8, url_level: &Option<u8>, config_level: u8) {
|
||||||
match safe_search {
|
if let Some(search_level) = url_level {
|
||||||
Some(ss) => {
|
if *search_level >= 3 {
|
||||||
if *ss >= 3 {
|
*safe_search = config_level
|
||||||
default
|
} else {
|
||||||
} else {
|
*safe_search = *search_level;
|
||||||
*ss
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None => default,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user