Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sitemap - use UTC time #3914

Merged
merged 2 commits into from
Aug 28, 2023
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
20 changes: 10 additions & 10 deletions crates/api/src/sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UrlSet> {
async fn generate_urlset(
posts: Vec<(DbUrl, chrono::DateTime<chrono::Utc>)>,
) -> LemmyResult<UrlSet> {
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()
})
Expand Down Expand Up @@ -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<Utc>)> = 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(),
),
];

Expand Down
6 changes: 3 additions & 3 deletions crates/db_schema/src/impls/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ impl Post {

pub async fn list_for_sitemap(
pool: &mut DbPool<'_>,
) -> Result<Vec<(DbUrl, chrono::NaiveDateTime)>, Error> {
) -> Result<Vec<(DbUrl, chrono::DateTime<Utc>)>, 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<Utc>)>(conn)
.await
}

Expand Down