mirror of
https://github.com/redlib-org/redlib.git
synced 2025-05-15 22:42:52 +00:00
Configure default settings using environment variables
This commit is contained in:
parent
83a667347d
commit
e4f9bd7b8d
7 changed files with 103 additions and 81 deletions
|
@ -2,7 +2,7 @@
|
|||
use crate::client::json;
|
||||
use crate::esc;
|
||||
use crate::server::RequestExt;
|
||||
use crate::utils::{cookie, error, format_num, format_url, param, rewrite_urls, template, time, val, Author, Comment, Flags, Flair, FlairPart, Media, Post, Preferences};
|
||||
use crate::utils::{error, format_num, format_url, param, rewrite_urls, setting, template, time, val, Author, Comment, Flags, Flair, FlairPart, Media, Post, Preferences};
|
||||
use hyper::{Body, Request, Response};
|
||||
|
||||
use async_recursion::async_recursion;
|
||||
|
@ -28,7 +28,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
|
|||
let mut sort: String = param(&path, "sort");
|
||||
|
||||
// Grab default comment sort method from Cookies
|
||||
let default_sort = cookie(&req, "comment_sort");
|
||||
let default_sort = setting(&req, "comment_sort");
|
||||
|
||||
// If there's no sort query but there's a default sort, set sort to default_sort
|
||||
if sort.is_empty() && !default_sort.is_empty() {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// CRATES
|
||||
use crate::utils::{catch_random, cookie, error, format_num, format_url, param, template, val, Post, Preferences};
|
||||
use crate::utils::{catch_random, error, format_num, format_url, param, setting, template, val, Post, Preferences};
|
||||
use crate::{client::json, RequestExt};
|
||||
use askama::Template;
|
||||
use hyper::{Body, Request, Response};
|
||||
|
@ -36,7 +36,7 @@ struct SearchTemplate {
|
|||
|
||||
// SERVICES
|
||||
pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
|
||||
let nsfw_results = if cookie(&req, "show_nsfw") == "on" { "&include_over_18=on" } else { "" };
|
||||
let nsfw_results = if setting(&req, "show_nsfw") == "on" { "&include_over_18=on" } else { "" };
|
||||
let path = format!("{}.json?{}{}", req.uri().path(), req.uri().query().unwrap_or_default(), nsfw_results);
|
||||
let sub = req.param("sub").unwrap_or_default();
|
||||
// Handle random subreddits
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// CRATES
|
||||
use crate::esc;
|
||||
use crate::utils::{catch_random, cookie, error, format_num, format_url, param, redirect, rewrite_urls, template, val, Post, Preferences, Subreddit};
|
||||
use crate::utils::{catch_random, error, format_num, format_url, param, redirect, rewrite_urls, setting, template, val, Post, Preferences, Subreddit};
|
||||
use crate::{client::json, server::ResponseExt, RequestExt};
|
||||
use askama::Template;
|
||||
use cookie::Cookie;
|
||||
|
@ -31,8 +31,8 @@ struct WikiTemplate {
|
|||
// SERVICES
|
||||
pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
|
||||
// Build Reddit API path
|
||||
let subscribed = cookie(&req, "subscriptions");
|
||||
let front_page = cookie(&req, "front_page");
|
||||
let subscribed = setting(&req, "subscriptions");
|
||||
let front_page = setting(&req, "front_page");
|
||||
let post_sort = req.cookie("post_sort").map_or_else(|| "hot".to_string(), |c| c.value().to_string());
|
||||
let sort = req.param("sort").unwrap_or_else(|| req.param("id").unwrap_or(post_sort));
|
||||
|
||||
|
@ -243,17 +243,22 @@ async fn moderators_list(sub: &str) -> Result<Vec<String>, String> {
|
|||
|
||||
// Retrieve response
|
||||
let response = json(path).await?["data"]["children"].clone();
|
||||
Ok(if let Some(response) = response.as_array() {
|
||||
Ok(
|
||||
// Traverse json tree and format into list of strings
|
||||
response
|
||||
.as_array()
|
||||
.unwrap_or(&Vec::new())
|
||||
.iter()
|
||||
.map(|m| m["name"].as_str().unwrap_or(""))
|
||||
.filter(|m| !m.is_empty())
|
||||
.map(std::string::ToString::to_string)
|
||||
.collect::<Vec<_>>()
|
||||
} else {
|
||||
vec![]
|
||||
})
|
||||
.filter_map(|moderator| {
|
||||
let name = moderator["name"].as_str().unwrap_or_default();
|
||||
if name.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(name.to_string())
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
}
|
||||
|
||||
// SUBREDDIT
|
||||
|
|
43
src/utils.rs
43
src/utils.rs
|
@ -397,16 +397,16 @@ impl Preferences {
|
|||
// Build preferences from cookies
|
||||
pub fn new(req: Request<Body>) -> Self {
|
||||
Self {
|
||||
theme: cookie(&req, "theme"),
|
||||
front_page: cookie(&req, "front_page"),
|
||||
layout: cookie(&req, "layout"),
|
||||
wide: cookie(&req, "wide"),
|
||||
show_nsfw: cookie(&req, "show_nsfw"),
|
||||
use_hls: cookie(&req, "use_hls"),
|
||||
hide_hls_notification: cookie(&req, "hide_hls_notification"),
|
||||
comment_sort: cookie(&req, "comment_sort"),
|
||||
post_sort: cookie(&req, "post_sort"),
|
||||
subscriptions: cookie(&req, "subscriptions").split('+').map(String::from).filter(|s| !s.is_empty()).collect(),
|
||||
theme: setting(&req, "theme"),
|
||||
front_page: setting(&req, "front_page"),
|
||||
layout: setting(&req, "layout"),
|
||||
wide: setting(&req, "wide"),
|
||||
show_nsfw: setting(&req, "show_nsfw"),
|
||||
use_hls: setting(&req, "use_hls"),
|
||||
hide_hls_notification: setting(&req, "hide_hls_notification"),
|
||||
comment_sort: setting(&req, "comment_sort"),
|
||||
post_sort: setting(&req, "post_sort"),
|
||||
subscriptions: setting(&req, "subscriptions").split('+').map(String::from).filter(|s| !s.is_empty()).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -423,10 +423,21 @@ pub fn param(path: &str, value: &str) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
// Parse a cookie value from request
|
||||
pub fn cookie(req: &Request<Body>, name: &str) -> String {
|
||||
let cookie = req.cookie(name).unwrap_or_else(|| Cookie::named(name));
|
||||
cookie.value().to_string()
|
||||
// Retrieve the value of a setting by name
|
||||
pub fn setting(req: &Request<Body>, name: &str) -> String {
|
||||
// Parse a cookie value from request
|
||||
req
|
||||
.cookie(name)
|
||||
.unwrap_or_else(|| {
|
||||
// If there is no cookie for this setting, try receiving a default from an environment variable
|
||||
if let Ok(default) = std::env::var(format!("LIBREDDIT_DEFAULT_{}", name.to_uppercase())) {
|
||||
Cookie::new(name, default)
|
||||
} else {
|
||||
Cookie::named(name)
|
||||
}
|
||||
})
|
||||
.value()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
// Detect and redirect in the event of a random subreddit
|
||||
|
@ -436,9 +447,9 @@ pub async fn catch_random(sub: &str, additional: &str) -> Result<Response<Body>,
|
|||
.as_str()
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
return Ok(redirect(format!("/r/{}{}", new_sub, additional)));
|
||||
Ok(redirect(format!("/r/{}{}", new_sub, additional)))
|
||||
} else {
|
||||
return Err("No redirect needed".to_string());
|
||||
Err("No redirect needed".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue