From 26d125cc637087b26b4e6c341d5835faed976387 Mon Sep 17 00:00:00 2001 From: Lukas Trombach Date: Mon, 28 Aug 2023 21:09:20 +1200 Subject: [PATCH] Sitemap - use UTC time (#3914) * change local filter to use `eq` function * use Utc time for sitemap --- crates/api/src/sitemap.rs | 20 ++++++++++---------- crates/db_schema/src/impls/post.rs | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/api/src/sitemap.rs b/crates/api/src/sitemap.rs index cb47e8e85..438a8b8e8 100644 --- a/crates/api/src/sitemap.rs +++ b/crates/api/src/sitemap.rs @@ -3,22 +3,20 @@ use actix_web::{ web::Data, HttpResponse, }; -use chrono::{DateTime, FixedOffset}; use lemmy_api_common::context::LemmyContext; use lemmy_db_schema::{newtypes::DbUrl, source::post::Post}; use lemmy_utils::error::LemmyResult; use sitemap_rs::{url::Url, url_set::UrlSet}; use tracing::info; -async fn generate_urlset(posts: Vec<(DbUrl, chrono::NaiveDateTime)>) -> LemmyResult { +async fn generate_urlset( + posts: Vec<(DbUrl, chrono::DateTime)>, +) -> LemmyResult { let urls = posts .into_iter() .map_while(|post| { Url::builder(post.0.to_string()) - .last_modified(DateTime::from_utc( - post.1, - FixedOffset::east_opt(0).expect("Error setting timezone offset"), // TODO what is the proper timezone offset here? - )) + .last_modified(post.1.into()) .build() .ok() }) @@ -48,27 +46,29 @@ pub(crate) mod tests { #![allow(clippy::unwrap_used)] use crate::sitemap::generate_urlset; - use chrono::{NaiveDate, NaiveDateTime}; + use chrono::{DateTime, NaiveDate, Utc}; use elementtree::Element; use lemmy_db_schema::newtypes::DbUrl; use url::Url; #[tokio::test] async fn test_generate_urlset() { - let posts: Vec<(DbUrl, NaiveDateTime)> = vec![ + let posts: Vec<(DbUrl, DateTime)> = vec![ ( Url::parse("https://example.com").unwrap().into(), NaiveDate::from_ymd_opt(2022, 12, 1) .unwrap() .and_hms_opt(9, 10, 11) - .unwrap(), + .unwrap() + .and_utc(), ), ( Url::parse("https://lemmy.ml").unwrap().into(), NaiveDate::from_ymd_opt(2023, 1, 1) .unwrap() .and_hms_opt(1, 2, 3) - .unwrap(), + .unwrap() + .and_utc(), ), ]; diff --git a/crates/db_schema/src/impls/post.rs b/crates/db_schema/src/impls/post.rs index 630eac426..514aceaba 100644 --- a/crates/db_schema/src/impls/post.rs +++ b/crates/db_schema/src/impls/post.rs @@ -101,16 +101,16 @@ impl Post { pub async fn list_for_sitemap( pool: &mut DbPool<'_>, - ) -> Result, Error> { + ) -> Result)>, Error> { let conn = &mut get_conn(pool).await?; post .select((ap_id, coalesce(updated, published))) - .filter(local) + .filter(local.eq(true)) .filter(deleted.eq(false)) .filter(removed.eq(false)) .filter(published.ge(Utc::now().naive_utc() - Duration::days(1))) .order(published.desc()) - .load::<(DbUrl, chrono::NaiveDateTime)>(conn) + .load::<(DbUrl, chrono::DateTime)>(conn) .await }