0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-21 21:48:21 -05:00

change: revert Cookie Strings back to &str

This commit is contained in:
ddotthomas 2024-01-08 11:46:21 -07:00
parent 6e9250c03a
commit d912bff94e
2 changed files with 16 additions and 14 deletions

View File

@ -21,24 +21,24 @@ pub struct SearchParams {
/// A named struct which is used to deserialize the cookies fetched from the client side.
#[allow(dead_code)]
#[derive(Deserialize)]
pub struct Cookie {
pub struct Cookie<'a> {
/// It stores the theme name used in the website.
pub theme: String,
pub theme: &'a str,
/// It stores the colorscheme name used for the website theme.
pub colorscheme: String,
pub colorscheme: &'a str,
/// It stores the user selected upstream search engines selected from the UI.
pub engines: Vec<String>,
/// It stores the user selected safe search level from the UI.
pub safe_search_level: u8,
}
impl Cookie {
impl<'a> Cookie<'a> {
/// server_models::Cookie contructor function
pub fn build(style: &Style, mut engines: Vec<String>, safe_search_level: u8) -> Self {
pub fn build(style: &'a Style, mut engines: Vec<String>, safe_search_level: u8) -> Self {
engines.sort();
Self {
theme: style.theme.clone(),
colorscheme: style.colorscheme.clone(),
theme: &style.theme,
colorscheme: &style.colorscheme,
engines,
safe_search_level,
}

View File

@ -62,9 +62,11 @@ pub async fn search(
)
};
let cookie = req.cookie("appCookie");
// Get search settings using the user's cookie or from the server's config
let mut search_settings: server_models::Cookie = match req.cookie("appCookie") {
Some(cookie_value) => {
let mut search_settings: server_models::Cookie<'_> = match cookie {
Some(ref cookie_value) => {
match serde_json::from_str(cookie_value.value()) {
Ok(cookie) => cookie,
// If there's an issue parsing the cookie's value, default to the config
@ -127,10 +129,10 @@ async fn results(
cache: &web::Data<SharedCache>,
query: &str,
page: u32,
user_settings: &server_models::Cookie,
search_settings: &server_models::Cookie<'_>,
) -> Result<SearchResults, Box<dyn std::error::Error>> {
// eagerly parse cookie value to evaluate safe search level
let safe_search_level = user_settings.safe_search_level;
let safe_search_level = search_settings.safe_search_level;
let cache_key = format!(
"http://{}:{}/search?q={}&page={}&safesearch={}&engines={}",
@ -139,7 +141,7 @@ async fn results(
query,
page,
safe_search_level,
user_settings.engines.join(",")
search_settings.engines.join(",")
);
// fetch the cached results json.
@ -167,14 +169,14 @@ async fn results(
// default selected upstream search engines from the config file otherwise
// parse the non-empty cookie and grab the user selected engines from the
// UI and use that.
let mut results: SearchResults = match user_settings.engines.is_empty() {
let mut results: SearchResults = match search_settings.engines.is_empty() {
false => {
aggregate(
query,
page,
config.aggregator.random_delay,
config.debug,
&user_settings
&search_settings
.engines
.clone()
.into_iter()