Skip to content

Commit 859c7a5

Browse files
patrickkempffHookedBehemoth
authored andcommitted
article_limit setting to set the number of articles per page
1 parent 7ce4d20 commit 859c7a5

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ fn main() {
173173
.get("/world/")
174174
.unwrap_or_else(|| panic!("Section 'world' not found"));
175175

176-
render_section(&client, section, offset, 8)
176+
render_section(&client, section, offset, settings.article_limit)
177177
}
178178
"/about" => render_about(),
179179
"/settings" => return handle_settings(request, &settings),
@@ -195,7 +195,7 @@ fn main() {
195195
let offset = request
196196
.get_param("offset")
197197
.map_or(0, |s| s.parse::<u32>().unwrap_or(0));
198-
render_section(&client, section, offset, 8)
198+
render_section(&client, section, offset, settings.article_limit)
199199
} else if path.starts_with("/authors/") {
200200
let offset = request
201201
.get_param("offset")

src/routes/settings.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rouille::{post_input, try_or_400, Request, Response};
33

44
use crate::{
55
document,
6-
settings::{Settings, EMBED_EMBEDS, EMBED_IMAGES, FAST_REDIRECT, PROXY_IMAGES, REDIRECT_TIMER},
6+
settings::{Settings, EMBED_EMBEDS, EMBED_IMAGES, FAST_REDIRECT, PROXY_IMAGES, REDIRECT_TIMER, ARTICLE_LIMIT},
77
};
88

99
fn render_settings(
@@ -12,6 +12,7 @@ fn render_settings(
1212
proxy_images: bool,
1313
fast_redirect: bool,
1414
redirect_timer: u32,
15+
article_limit: u32,
1516
) -> Markup {
1617
document!(
1718
"Settings",
@@ -46,6 +47,11 @@ fn render_settings(
4647
input type="number" id=(REDIRECT_TIMER) name=(REDIRECT_TIMER) value=(redirect_timer) {}
4748
}
4849

50+
label for=(ARTICLE_LIMIT) {
51+
"Articles to display"
52+
input type="number" id=(ARTICLE_LIMIT) name=(ARTICLE_LIMIT) value=(article_limit) {}
53+
}
54+
4955
button type="submit" {
5056
"Save"
5157
}
@@ -60,6 +66,7 @@ fn store_settings(
6066
proxy_images: bool,
6167
fast_redirect: bool,
6268
redirect_timer: u32,
69+
article_limit: u32,
6370
) -> Response {
6471
Response::redirect_303("/settings")
6572
.with_additional_header(
@@ -77,14 +84,21 @@ fn store_settings(
7784
.with_additional_header(
7885
"Set-Cookie",
7986
format!("{FAST_REDIRECT}={}; Path=/; SameSite=Strict", fast_redirect),
80-
)
87+
)
8188
.with_additional_header(
8289
"Set-Cookie",
8390
format!(
8491
"{REDIRECT_TIMER}={}; Path=/; SameSite=Strict",
8592
redirect_timer
8693
),
8794
)
95+
.with_additional_header(
96+
"Set-Cookie",
97+
format!(
98+
"{ARTICLE_LIMIT}={}; Path=/; SameSite=Strict",
99+
article_limit
100+
),
101+
)
88102
}
89103

90104
pub fn handle_settings(request: &Request, settings: &Settings) -> Response {
@@ -95,6 +109,7 @@ pub fn handle_settings(request: &Request, settings: &Settings) -> Response {
95109
proxy_images: bool,
96110
fast_redirect: bool,
97111
redirect_timer: i32,
112+
article_limit: i32,
98113
}));
99114

100115
store_settings(
@@ -103,6 +118,7 @@ pub fn handle_settings(request: &Request, settings: &Settings) -> Response {
103118
settings.proxy_images,
104119
settings.fast_redirect,
105120
settings.redirect_timer.clamp(0, 600) as u32,
121+
settings.article_limit.clamp(0, 100) as u32,
106122
)
107123
} else {
108124
let page = render_settings(
@@ -111,6 +127,7 @@ pub fn handle_settings(request: &Request, settings: &Settings) -> Response {
111127
settings.proxy_images,
112128
settings.fast_redirect,
113129
settings.redirect_timer,
130+
settings.article_limit,
114131
);
115132
Response::html(page).with_status_code(200)
116133
}

src/settings.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ pub const EMBED_EMBEDS: &str = "embed_embeds";
55
pub const PROXY_IMAGES: &str = "proxy_images";
66
pub const FAST_REDIRECT: &str = "fast_redirect";
77
pub const REDIRECT_TIMER: &str = "redirect_timer";
8+
pub const ARTICLE_LIMIT: &str = "article_limit";
89

910
pub struct Settings {
1011
pub embed_images: bool,
1112
pub embed_embeds: bool,
1213
pub proxy_images: bool,
1314
pub fast_redirect: bool,
1415
pub redirect_timer: u32,
16+
pub article_limit: u32,
1517
}
1618

1719
impl Settings {
@@ -21,13 +23,15 @@ impl Settings {
2123
let mut proxy_images = true;
2224
let mut fast_redirect = false;
2325
let mut redirect_timer = 5;
26+
let mut article_limit = 8;
2427
for (key, value) in input::cookies(request) {
2528
match key {
2629
EMBED_IMAGES => embed_images = value == "true",
2730
EMBED_EMBEDS => embed_embeds = value == "true",
2831
PROXY_IMAGES => proxy_images = value == "true",
2932
FAST_REDIRECT => fast_redirect = value == "true",
3033
REDIRECT_TIMER => redirect_timer = value.parse().unwrap_or(5),
34+
ARTICLE_LIMIT => article_limit = value.parse().unwrap_or(5),
3135
_ => {}
3236
}
3337
}
@@ -38,6 +42,7 @@ impl Settings {
3842
proxy_images,
3943
fast_redirect,
4044
redirect_timer,
45+
article_limit
4146
}
4247
}
4348
}

0 commit comments

Comments
 (0)