Skip to content

Commit 9400731

Browse files
committed
Make timezone offset used for creates/updates configurable
Doesn't really take dst into account since these are offsets. Should probably take an actual timezone instead but this is still better than hardcoded offsets in code.
1 parent 82b7d67 commit 9400731

File tree

8 files changed

+38
-13
lines changed

8 files changed

+38
-13
lines changed

src/bin/server.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ async fn main() -> Result<(), anyhow::Error> {
9898
{
9999
let dbpool = dbpool.clone();
100100
let templates = templates.clone();
101-
move || handlers::get_archive_handler(None, dbpool.clone(), templates.clone())
101+
let c = site_config.clone();
102+
move || handlers::get_archive_handler(None, dbpool.clone(), templates.clone(), c.clone())
102103
}
103104
),
104105
)
@@ -109,7 +110,8 @@ async fn main() -> Result<(), anyhow::Error> {
109110
{
110111
let dbpool = dbpool.clone();
111112
let templates = Arc::new(crate::templates::Templates::atom_default(atom_ctx));
112-
move || handlers::get_atom_handler(dbpool.clone(), templates.clone())
113+
let c = site_config.clone();
114+
move || handlers::get_atom_handler(dbpool.clone(), templates.clone(), c.clone())
113115
}
114116
),
115117
)
@@ -185,8 +187,9 @@ async fn main() -> Result<(), anyhow::Error> {
185187
{
186188
let dbpool = dbpool.clone();
187189
let templates = templates.clone();
190+
let c = site_config.clone();
188191
move |Path(tag): Path<String>| {
189-
handlers::get_archive_handler(Some(tag), dbpool.clone(), templates.clone())
192+
handlers::get_archive_handler(Some(tag), dbpool.clone(), templates.clone(), c.clone())
190193
}
191194
}
192195
),
@@ -210,9 +213,10 @@ async fn main() -> Result<(), anyhow::Error> {
210213
MethodFilter::GET.union(MethodFilter::HEAD),
211214
{
212215
let dbpool = dbpool.clone();
216+
let c = site_config.clone();
213217
move |Path(post_slug): Path<String>| {
214218
info!("in get post handler");
215-
handlers::get_post_handler(post_slug, dbpool.clone(), templates.clone())
219+
handlers::get_post_handler(post_slug, dbpool.clone(), templates.clone(), c.clone())
216220
}
217221
}
218222
)

src/config.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ pub struct MicropubConfig {
3232
#[serde(default = "default_max_upload_length")]
3333
pub media_endpoint_max_upload_length: usize,
3434
pub micropub_endpoint: String,
35+
36+
#[serde(deserialize_with="offset_deserialize::deserialize")]
37+
pub current_timezone_offset: chrono::FixedOffset,
3538
}
3639

3740
fn default_auth_token_endpoint() -> String {
@@ -45,3 +48,20 @@ fn default_auth_endpoint() -> String {
4548
fn default_max_upload_length() -> usize {
4649
crate::DEFAULT_MAX_CONTENT_LENGTH
4750
}
51+
52+
mod offset_deserialize {
53+
use chrono::FixedOffset;
54+
use serde::{self, Deserialize, Deserializer};
55+
56+
pub fn deserialize<'de, D>(
57+
deserializer: D,
58+
) -> Result<FixedOffset, D::Error>
59+
where
60+
D: Deserializer<'de>,
61+
{
62+
let offset_seconds = i32::deserialize(deserializer)?;
63+
let offset = FixedOffset::east_opt(offset_seconds)
64+
.ok_or(serde::de::Error::custom("invalid offset"))?;
65+
Ok(offset)
66+
}
67+
}

src/handlers/archive.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub async fn get_archive_handler(
1919
tag: Option<String>,
2020
pool: Arc<r2d2::Pool<r2d2::ConnectionManager<SqliteConnection>>>,
2121
templates: Arc<templates::Templates>,
22+
site_config: Arc<crate::MicropubSiteConfig>,
2223
) -> Result<impl IntoResponse, StatusCode> {
2324
let tag_ref = tag.as_ref().map(|t| t.as_str());
2425
let db = MicropubDB::new(pool);
@@ -46,7 +47,7 @@ pub async fn get_archive_handler(
4647
for mut post in posts {
4748
// TODO this is copied from FetchHandler. Both should not do this and should instead be
4849
// handled e.g. at the view model creation time.
49-
let datetime = post_util::get_local_datetime(&post.created_at, None).map_err(|e| {
50+
let datetime = post_util::get_local_datetime(&post.created_at, &site_config.micropub.current_timezone_offset).map_err(|e| {
5051
error!("date parsing error: {:?}", e);
5152
// TODO shouldn't be a template error but realistically this would only happen if
5253
// the DB had malformed data for template rendering...

src/handlers/atom.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct AtomHandler<DB: WithDB> {
2323
pub async fn get_atom_handler(
2424
pool: Arc<r2d2::Pool<r2d2::ConnectionManager<SqliteConnection>>>,
2525
templates: Arc<templates::Templates>,
26+
site_config: Arc<crate::MicropubSiteConfig>,
2627
) -> Result<impl IntoResponse, StatusCode> {
2728
let db = MicropubDB::new(pool);
2829
let mut conn = db.dbconn()?;
@@ -60,7 +61,7 @@ pub async fn get_atom_handler(
6061
for mut post in posts {
6162
// TODO this is copied from FetchHandler. Both should not do this and should instead be
6263
// handled e.g. at the view model creation time.
63-
let datetime = post_util::get_local_datetime(&post.created_at, None).map_err(|e| {
64+
let datetime = post_util::get_local_datetime(&post.created_at, &site_config.micropub.current_timezone_offset).map_err(|e| {
6465
error!("date parsing error: {:?}", e);
6566
// TODO shouldn't be a template error but realistically this would only happen if
6667
// the DB had malformed data for template rendering...

src/handlers/fetch.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub async fn get_post_handler(
2121
url_slug: String,
2222
pool: Arc<r2d2::Pool<r2d2::ConnectionManager<SqliteConnection>>>,
2323
templates: Arc<templates::Templates>,
24+
site_config: Arc<crate::MicropubSiteConfig>,
2425
) -> Result<impl IntoResponse, StatusCode> {
2526
info!("fetch_post url_slug:{:?}", url_slug);
2627
let db = MicropubDB::new(pool);
@@ -45,7 +46,7 @@ pub async fn get_post_handler(
4546
.map_err(|e| db.handle_errors(e))?;
4647

4748
debug!("input datetime: {:?}", post.created_at);
48-
let datetime = post_util::get_local_datetime(&post.created_at, None).map_err(|e| {
49+
let datetime = post_util::get_local_datetime(&post.created_at, &site_config.micropub.current_timezone_offset).map_err(|e| {
4950
error!("date parsing error: {:?}", e);
5051
// TODO shouldn't be a template error but realistically this would only happen if
5152
// the DB had malformed data for template rendering...

src/handlers/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub async fn get_index_handler(
4646
.add_context("TOKEN_ENDPOINT", &site_config.micropub.auth_token_endpoint)
4747
.add_context("MICROPUB_ENDPOINT", &site_config.micropub.micropub_endpoint);
4848

49-
let datetime = post_util::get_local_datetime(&post.created_at, None).map_err(|e| {
49+
let datetime = post_util::get_local_datetime(&post.created_at, &site_config.micropub.current_timezone_offset).map_err(|e| {
5050
error!("date parsing error: {:?}", e);
5151
// TODO shouldn't be a template error but realistically this would only happen if
5252
// the DB had malformed data for template rendering...

src/handlers/micropub.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,9 +812,7 @@ async fn handle_update(
812812
// TODO consider saving copies of the old post in a history table before updating? Inserting a
813813
// new version into same table?
814814
db.run_txn(|conn| {
815-
// TODO make timezone part of server config
816-
let chicago = chrono::offset::FixedOffset::west_opt(6 * 3600).unwrap();
817-
let new_updated_at = Local::now().with_timezone(&chicago)
815+
let new_updated_at = Local::now().with_timezone(&site_config.micropub.current_timezone_offset)
818816
.format("%Y-%m-%d %H:%M:%S");
819817

820818
use crate::schema::posts::dsl::*;

src/post_util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ pub fn get_slug(name: Option<&str>, now_fn: fn() -> DateTime<Local>) -> String {
2626

2727
pub fn get_local_datetime(
2828
datetime: &str,
29-
offset: Option<chrono::FixedOffset>,
29+
offset: &chrono::FixedOffset,
3030
) -> Result<DateTime<Local>, chrono::format::ParseError> {
3131
chrono::NaiveDateTime::parse_from_str(datetime, "%Y-%m-%d %H:%M:%S").map(|ndt| {
3232
chrono::DateTime::<chrono::Local>::from_utc(
3333
ndt,
34-
offset.unwrap_or(chrono::FixedOffset::west_opt(8 * 3600).unwrap()),
34+
*offset,
3535
)
3636
})
3737
}

0 commit comments

Comments
 (0)