Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn main() {
.get("/world/")
.unwrap_or_else(|| panic!("Section 'world' not found"));

render_section(&client, section, offset, 8)
render_section(&client, section, offset, settings.article_limit)
}
"/about" => render_about(),
"/settings" => return handle_settings(request, &settings),
Expand All @@ -195,7 +195,7 @@ fn main() {
let offset = request
.get_param("offset")
.map_or(0, |s| s.parse::<u32>().unwrap_or(0));
render_section(&client, section, offset, 8)
render_section(&client, section, offset, settings.article_limit)
} else if path.starts_with("/authors/") {
let offset = request
.get_param("offset")
Expand Down
21 changes: 19 additions & 2 deletions src/routes/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rouille::{post_input, try_or_400, Request, Response};

use crate::{
document,
settings::{Settings, EMBED_EMBEDS, EMBED_IMAGES, FAST_REDIRECT, PROXY_IMAGES, REDIRECT_TIMER},
settings::{Settings, EMBED_EMBEDS, EMBED_IMAGES, FAST_REDIRECT, PROXY_IMAGES, REDIRECT_TIMER, ARTICLE_LIMIT},
};

fn render_settings(
Expand All @@ -12,6 +12,7 @@ fn render_settings(
proxy_images: bool,
fast_redirect: bool,
redirect_timer: u32,
article_limit: u32,
) -> Markup {
document!(
"Settings",
Expand Down Expand Up @@ -46,6 +47,11 @@ fn render_settings(
input type="number" id=(REDIRECT_TIMER) name=(REDIRECT_TIMER) value=(redirect_timer) {}
}

label for=(ARTICLE_LIMIT) {
"Articles to display"
input type="number" id=(ARTICLE_LIMIT) name=(ARTICLE_LIMIT) value=(article_limit) {}
}

button type="submit" {
"Save"
}
Expand All @@ -60,6 +66,7 @@ fn store_settings(
proxy_images: bool,
fast_redirect: bool,
redirect_timer: u32,
article_limit: u32,
) -> Response {
Response::redirect_303("/settings")
.with_additional_header(
Expand All @@ -77,14 +84,21 @@ fn store_settings(
.with_additional_header(
"Set-Cookie",
format!("{FAST_REDIRECT}={}; Path=/; SameSite=Strict", fast_redirect),
)
)
.with_additional_header(
"Set-Cookie",
format!(
"{REDIRECT_TIMER}={}; Path=/; SameSite=Strict",
redirect_timer
),
)
.with_additional_header(
"Set-Cookie",
format!(
"{ARTICLE_LIMIT}={}; Path=/; SameSite=Strict",
article_limit
),
)
}

pub fn handle_settings(request: &Request, settings: &Settings) -> Response {
Expand All @@ -95,6 +109,7 @@ pub fn handle_settings(request: &Request, settings: &Settings) -> Response {
proxy_images: bool,
fast_redirect: bool,
redirect_timer: i32,
article_limit: i32,
}));

store_settings(
Expand All @@ -103,6 +118,7 @@ pub fn handle_settings(request: &Request, settings: &Settings) -> Response {
settings.proxy_images,
settings.fast_redirect,
settings.redirect_timer.clamp(0, 600) as u32,
settings.article_limit.clamp(0, 100) as u32,
)
} else {
let page = render_settings(
Expand All @@ -111,6 +127,7 @@ pub fn handle_settings(request: &Request, settings: &Settings) -> Response {
settings.proxy_images,
settings.fast_redirect,
settings.redirect_timer,
settings.article_limit,
);
Response::html(page).with_status_code(200)
}
Expand Down
5 changes: 5 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ pub const EMBED_EMBEDS: &str = "embed_embeds";
pub const PROXY_IMAGES: &str = "proxy_images";
pub const FAST_REDIRECT: &str = "fast_redirect";
pub const REDIRECT_TIMER: &str = "redirect_timer";
pub const ARTICLE_LIMIT: &str = "article_limit";

pub struct Settings {
pub embed_images: bool,
pub embed_embeds: bool,
pub proxy_images: bool,
pub fast_redirect: bool,
pub redirect_timer: u32,
pub article_limit: u32,
}

impl Settings {
Expand All @@ -21,13 +23,15 @@ impl Settings {
let mut proxy_images = true;
let mut fast_redirect = false;
let mut redirect_timer = 5;
let mut article_limit = 8;
for (key, value) in input::cookies(request) {
match key {
EMBED_IMAGES => embed_images = value == "true",
EMBED_EMBEDS => embed_embeds = value == "true",
PROXY_IMAGES => proxy_images = value == "true",
FAST_REDIRECT => fast_redirect = value == "true",
REDIRECT_TIMER => redirect_timer = value.parse().unwrap_or(5),
ARTICLE_LIMIT => article_limit = value.parse().unwrap_or(5),
_ => {}
}
}
Expand All @@ -38,6 +42,7 @@ impl Settings {
proxy_images,
fast_redirect,
redirect_timer,
article_limit
}
}
}
Loading