Sitemap - use UTC time (#3914)

* change local filter to use `eq` function

* use Utc time for sitemap
This commit is contained in:
Lukas Trombach 2023-08-28 21:09:20 +12:00 committed by GitHub
parent 514f2222e0
commit 26d125cc63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 13 deletions

View file

@ -3,22 +3,20 @@ use actix_web::{
web::Data, web::Data,
HttpResponse, HttpResponse,
}; };
use chrono::{DateTime, FixedOffset};
use lemmy_api_common::context::LemmyContext; use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{newtypes::DbUrl, source::post::Post}; use lemmy_db_schema::{newtypes::DbUrl, source::post::Post};
use lemmy_utils::error::LemmyResult; use lemmy_utils::error::LemmyResult;
use sitemap_rs::{url::Url, url_set::UrlSet}; use sitemap_rs::{url::Url, url_set::UrlSet};
use tracing::info; use tracing::info;
async fn generate_urlset(posts: Vec<(DbUrl, chrono::NaiveDateTime)>) -> LemmyResult<UrlSet> { async fn generate_urlset(
posts: Vec<(DbUrl, chrono::DateTime<chrono::Utc>)>,
) -> LemmyResult<UrlSet> {
let urls = posts let urls = posts
.into_iter() .into_iter()
.map_while(|post| { .map_while(|post| {
Url::builder(post.0.to_string()) Url::builder(post.0.to_string())
.last_modified(DateTime::from_utc( .last_modified(post.1.into())
post.1,
FixedOffset::east_opt(0).expect("Error setting timezone offset"), // TODO what is the proper timezone offset here?
))
.build() .build()
.ok() .ok()
}) })
@ -48,27 +46,29 @@ pub(crate) mod tests {
#![allow(clippy::unwrap_used)] #![allow(clippy::unwrap_used)]
use crate::sitemap::generate_urlset; use crate::sitemap::generate_urlset;
use chrono::{NaiveDate, NaiveDateTime}; use chrono::{DateTime, NaiveDate, Utc};
use elementtree::Element; use elementtree::Element;
use lemmy_db_schema::newtypes::DbUrl; use lemmy_db_schema::newtypes::DbUrl;
use url::Url; use url::Url;
#[tokio::test] #[tokio::test]
async fn test_generate_urlset() { async fn test_generate_urlset() {
let posts: Vec<(DbUrl, NaiveDateTime)> = vec![ let posts: Vec<(DbUrl, DateTime<Utc>)> = vec![
( (
Url::parse("https://example.com").unwrap().into(), Url::parse("https://example.com").unwrap().into(),
NaiveDate::from_ymd_opt(2022, 12, 1) NaiveDate::from_ymd_opt(2022, 12, 1)
.unwrap() .unwrap()
.and_hms_opt(9, 10, 11) .and_hms_opt(9, 10, 11)
.unwrap(), .unwrap()
.and_utc(),
), ),
( (
Url::parse("https://lemmy.ml").unwrap().into(), Url::parse("https://lemmy.ml").unwrap().into(),
NaiveDate::from_ymd_opt(2023, 1, 1) NaiveDate::from_ymd_opt(2023, 1, 1)
.unwrap() .unwrap()
.and_hms_opt(1, 2, 3) .and_hms_opt(1, 2, 3)
.unwrap(), .unwrap()
.and_utc(),
), ),
]; ];

View file

@ -101,16 +101,16 @@ impl Post {
pub async fn list_for_sitemap( pub async fn list_for_sitemap(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
) -> Result<Vec<(DbUrl, chrono::NaiveDateTime)>, Error> { ) -> Result<Vec<(DbUrl, chrono::DateTime<Utc>)>, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
post post
.select((ap_id, coalesce(updated, published))) .select((ap_id, coalesce(updated, published)))
.filter(local) .filter(local.eq(true))
.filter(deleted.eq(false)) .filter(deleted.eq(false))
.filter(removed.eq(false)) .filter(removed.eq(false))
.filter(published.ge(Utc::now().naive_utc() - Duration::days(1))) .filter(published.ge(Utc::now().naive_utc() - Duration::days(1)))
.order(published.desc()) .order(published.desc())
.load::<(DbUrl, chrono::NaiveDateTime)>(conn) .load::<(DbUrl, chrono::DateTime<Utc>)>(conn)
.await .await
} }