Skip to content

Commit

Permalink
Fix time zone handling (#3496)
Browse files Browse the repository at this point in the history
* convert naive time to utc time

* compounding fixes

* cargo fmt

* fix the rest

* fix down migration

* fix migrations

* fix after merge

* clippy fix

* ap-fed 0.5.0

---------

Co-authored-by: Nutomic <me@nutomic.com>
  • Loading branch information
phiresky and Nutomic committed Aug 24, 2023
1 parent 6047257 commit 514f222
Show file tree
Hide file tree
Showing 82 changed files with 1,094 additions and 352 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ lemmy_routes = { version = "=0.18.1", path = "./crates/routes" }
lemmy_db_views = { version = "=0.18.1", path = "./crates/db_views" }
lemmy_db_views_actor = { version = "=0.18.1", path = "./crates/db_views_actor" }
lemmy_db_views_moderator = { version = "=0.18.1", path = "./crates/db_views_moderator" }
activitypub_federation = { version = "0.4.6", default-features = false, features = [
activitypub_federation = { version = "0.5.0-beta.1", default-features = false, features = [
"actix-web",
] }
diesel = "2.1.0"
Expand Down
6 changes: 3 additions & 3 deletions crates/api_common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
site::FederatedInstances,
};
use anyhow::Context;
use chrono::NaiveDateTime;
use chrono::{DateTime, Utc};
use lemmy_db_schema::{
impls::person::is_banned,
newtypes::{CommunityId, DbUrl, LocalUserId, PersonId, PostId},
Expand Down Expand Up @@ -163,7 +163,7 @@ pub async fn local_user_view_from_jwt_opt(

/// Checks if user's token was issued before user's password reset.
pub fn check_validator_time(
validator_time: &NaiveDateTime,
validator_time: &DateTime<Utc>,
claims: &Claims,
) -> Result<(), LemmyError> {
let user_validation_time = validator_time.timestamp();
Expand All @@ -176,7 +176,7 @@ pub fn check_validator_time(

pub fn check_user_valid(
banned: bool,
ban_expires: Option<NaiveDateTime>,
ban_expires: Option<DateTime<Utc>>,
deleted: bool,
) -> Result<(), LemmyError> {
// Check for a site ban
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/assets/lemmy/activities/block/block_user.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"type": "Block",
"removeData": true,
"summary": "spam post",
"expires": "2021-11-01T12:23:50.151874+00:00",
"expires": "2021-11-01T12:23:50.151874Z",
"id": "http://enterprise.lemmy.ml/activities/block/5d42fffb-0903-4625-86d4-0b39bb344fc2"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "Block",
"removeData": true,
"summary": "spam post",
"expires": "2021-11-01T12:23:50.151874+00:00",
"expires": "2021-11-01T12:23:50.151874Z",
"id": "http://enterprise.lemmy.ml/activities/block/726f43ab-bd0e-4ab3-89c8-627e976f553c"
},
"cc": ["http://enterprise.lemmy.ml/c/main"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"commentsEnabled": true,
"sensitive": false,
"stickied": false,
"published": "2021-11-01T12:11:22.871846+00:00"
"published": "2021-11-01T12:11:22.871846Z"
},
"cc": ["http://enterprise.lemmy.ml/c/main"],
"type": "Create",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"name": "Deutsch"
}
],
"published": "2021-10-29T15:05:51.476984+00:00",
"updated": "2021-11-01T12:23:50.151874+00:00"
"published": "2021-10-29T15:05:51.476984Z",
"updated": "2021-11-01T12:23:50.151874Z"
},
"cc": ["http://enterprise.lemmy.ml/c/main"],
"audience": "http://enterprise.lemmy.ml/u/main",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"mediaType": "text/markdown"
},
"inReplyTo": "http://ds9.lemmy.ml/post/1",
"published": "2021-11-01T11:45:49.794920+00:00"
"published": "2021-11-01T11:45:49.794920Z"
},
"cc": [
"http://enterprise.lemmy.ml/c/main",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"identifier": "ko",
"name": "한국어"
},
"published": "2021-10-29T15:10:51.557399+00:00"
"published": "2021-10-29T15:10:51.557399Z"
},
"cc": ["http://enterprise.lemmy.ml/c/main"],
"audience": "https://enterprise.lemmy.ml/c/main",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"content": "hello",
"mediaType": "text/markdown"
},
"published": "2021-10-29T15:31:56.058289+00:00"
"published": "2021-10-29T15:31:56.058289Z"
},
"type": "Create"
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
],
"commentsEnabled": true,
"sensitive": false,
"published": "2021-10-29T15:10:51.557399+00:00",
"updated": "2021-10-29T15:11:35.976374+00:00"
"published": "2021-10-29T15:10:51.557399Z",
"updated": "2021-10-29T15:11:35.976374Z"
},
"cc": ["http://enterprise.lemmy.ml/c/main"],
"audience": "https://enterprise.lemmy.ml/c/main",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"attachment": [],
"commentsEnabled": true,
"sensitive": false,
"published": "2023-02-06T06:42:41.939437+00:00",
"published": "2023-02-06T06:42:41.939437Z",
"language": {
"identifier": "de",
"name": "Deutsch"
Expand All @@ -38,7 +38,7 @@
"attachment": [],
"commentsEnabled": true,
"sensitive": false,
"published": "2023-02-06T06:42:37.119567+00:00",
"published": "2023-02-06T06:42:37.119567Z",
"language": {
"identifier": "de",
"name": "Deutsch"
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/assets/lemmy/collections/group_outbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"commentsEnabled": true,
"sensitive": false,
"stickied": false,
"published": "2021-11-18T17:19:45.895163+00:00"
"published": "2021-11-18T17:19:45.895163Z"
}
},
"cc": ["https://ds9.lemmy.ml/c/testcom/followers"],
Expand Down Expand Up @@ -54,7 +54,7 @@
"commentsEnabled": true,
"sensitive": false,
"stickied": false,
"published": "2021-11-18T17:19:05.763109+00:00"
"published": "2021-11-18T17:19:05.763109Z"
}
},
"cc": ["https://ds9.lemmy.ml/c/testcom/followers"],
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/assets/lemmy/objects/chat_message.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"content": "Hello hello, testing",
"mediaType": "text/markdown"
},
"published": "2021-10-21T10:13:14.597721+00:00"
"published": "2021-10-21T10:13:14.597721Z"
}
4 changes: 2 additions & 2 deletions crates/apub/assets/lemmy/objects/group.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
"name": "Deutsch"
}
],
"published": "2019-06-02T16:43:50.799554+00:00",
"updated": "2021-03-10T17:18:10.498868+00:00"
"published": "2019-06-02T16:43:50.799554Z",
"updated": "2021-03-10T17:18:10.498868Z"
}
2 changes: 1 addition & 1 deletion crates/apub/assets/lemmy/objects/instance.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
"name": "Español"
}
],
"published": "2022-01-19T21:52:11.110741+00:00"
"published": "2022-01-19T21:52:11.110741Z"
}
4 changes: 2 additions & 2 deletions crates/apub/assets/lemmy/objects/note.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"identifier": "fr",
"name": "Français"
},
"published": "2021-03-01T13:42:43.966208+00:00",
"updated": "2021-03-01T13:43:03.955787+00:00"
"published": "2021-03-01T13:42:43.966208Z",
"updated": "2021-03-01T13:43:03.955787Z"
}
2 changes: 1 addition & 1 deletion crates/apub/assets/lemmy/objects/page.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
"identifier": "fr",
"name": "Français"
},
"published": "2021-02-26T12:35:34.292626+00:00"
"published": "2021-02-26T12:35:34.292626Z"
}
4 changes: 2 additions & 2 deletions crates/apub/assets/lemmy/objects/person.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"endpoints": {
"sharedInbox": "https://enterprise.lemmy.ml/inbox"
},
"published": "2020-01-17T01:38:22.348392+00:00",
"updated": "2021-08-13T00:11:15.941990+00:00",
"published": "2020-01-17T01:38:22.348392Z",
"updated": "2021-08-13T00:11:15.941990Z",
"publicKey": {
"id": "https://enterprise.lemmy.ml/u/picard#main-key",
"owner": "https://enterprise.lemmy.ml/u/picard",
Expand Down
12 changes: 6 additions & 6 deletions crates/apub/src/activities/block/block_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use activitypub_federation::{
traits::{ActivityHandler, Actor},
};
use anyhow::anyhow;
use chrono::NaiveDateTime;
use chrono::{DateTime, Utc};
use lemmy_api_common::{
context::LemmyContext,
utils::{remove_user_data, remove_user_data_in_community, sanitize_html_opt},
Expand All @@ -38,7 +38,7 @@ use lemmy_db_schema::{
},
traits::{Bannable, Crud, Followable},
};
use lemmy_utils::{error::LemmyError, utils::time::convert_datetime};
use lemmy_utils::error::LemmyError;
use url::Url;

impl BlockUser {
Expand All @@ -48,7 +48,7 @@ impl BlockUser {
mod_: &ApubPerson,
remove_data: Option<bool>,
reason: Option<String>,
expires: Option<NaiveDateTime>,
expires: Option<DateTime<Utc>>,
context: &Data<LemmyContext>,
) -> Result<BlockUser, LemmyError> {
let audience = if let SiteOrCommunity::Community(c) = target {
Expand All @@ -70,7 +70,7 @@ impl BlockUser {
&context.settings().get_protocol_and_hostname(),
)?,
audience,
expires: expires.map(convert_datetime),
expires,
})
}

Expand All @@ -81,7 +81,7 @@ impl BlockUser {
mod_: &ApubPerson,
remove_data: bool,
reason: Option<String>,
expires: Option<NaiveDateTime>,
expires: Option<DateTime<Utc>>,
context: &Data<LemmyContext>,
) -> Result<(), LemmyError> {
let block = BlockUser::new(
Expand Down Expand Up @@ -148,7 +148,7 @@ impl ActivityHandler for BlockUser {

#[tracing::instrument(skip_all)]
async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
let expires = self.expires.map(|u| u.naive_local());
let expires = self.expires.map(Into::into);
let mod_person = self.actor.dereference(context).await?;
let blocked_person = self.object.dereference(context).await?;
let target = self.target.dereference(context).await?;
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/activities/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use activitypub_federation::{
fetch::object_id::ObjectId,
traits::{Actor, Object},
};
use chrono::NaiveDateTime;
use chrono::{DateTime, Utc};
use lemmy_api_common::{community::BanFromCommunity, context::LemmyContext, person::BanPerson};
use lemmy_db_schema::{
newtypes::CommunityId,
Expand Down Expand Up @@ -49,7 +49,7 @@ impl Object for SiteOrCommunity {
type Error = LemmyError;

#[tracing::instrument(skip_all)]
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
Some(match self {
SiteOrCommunity::Site(i) => i.last_refreshed_at,
SiteOrCommunity::Community(c) => c.last_refreshed_at,
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/src/activities/block/undo_block_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl ActivityHandler for UndoBlockUser {

#[tracing::instrument(skip_all)]
async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
let expires = self.object.expires.map(|u| u.naive_local());
let expires = self.object.expires.map(Into::into);
let mod_person = self.actor.dereference(context).await?;
let blocked_person = self.object.object.dereference(context).await?;
match self.object.target.dereference(context).await? {
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/fetcher/post_or_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
},
};
use activitypub_federation::{config::Data, traits::Object};
use chrono::NaiveDateTime;
use chrono::{DateTime, Utc};
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{
source::{community::Community, post::Post},
Expand Down Expand Up @@ -35,7 +35,7 @@ impl Object for PostOrComment {
type Kind = PageOrNote;
type Error = LemmyError;

fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
None
}

Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/fetcher/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use activitypub_federation::{
fetch::{object_id::ObjectId, webfinger::webfinger_resolve_actor},
traits::Object,
};
use chrono::NaiveDateTime;
use chrono::{DateTime, Utc};
use lemmy_api_common::context::LemmyContext;
use lemmy_utils::error::{LemmyError, LemmyErrorType};
use serde::Deserialize;
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Object for SearchableObjects {
type Kind = SearchableKinds;
type Error = LemmyError;

fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
match self {
SearchableObjects::Person(p) => p.last_refreshed_at(),
SearchableObjects::Community(c) => c.last_refreshed_at(),
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/fetcher/user_or_community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use activitypub_federation::{
config::Data,
traits::{Actor, Object},
};
use chrono::NaiveDateTime;
use chrono::{DateTime, Utc};
use lemmy_api_common::context::LemmyContext;
use lemmy_utils::error::LemmyError;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -37,7 +37,7 @@ impl Object for UserOrCommunity {
type Kind = PersonOrGroup;
type Error = LemmyError;

fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
Some(match self {
UserOrCommunity::User(p) => p.last_refreshed_at,
UserOrCommunity::Community(p) => p.last_refreshed_at,
Expand Down
11 changes: 6 additions & 5 deletions crates/apub/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::fetcher::post_or_comment::PostOrComment;
use activitypub_federation::config::{Data, UrlVerifier};
use anyhow::anyhow;
use async_trait::async_trait;
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{
Expand Down Expand Up @@ -38,24 +39,24 @@ pub struct VerifyUrlData(pub ActualDbPool);

#[async_trait]
impl UrlVerifier for VerifyUrlData {
async fn verify(&self, url: &Url) -> Result<(), &'static str> {
async fn verify(&self, url: &Url) -> Result<(), anyhow::Error> {
let local_site_data = local_site_data_cached(&mut (&self.0).into())
.await
.expect("read local site data");
check_apub_id_valid(url, &local_site_data).map_err(|err| match err {
LemmyError {
error_type: LemmyErrorType::FederationDisabled,
..
} => "Federation disabled",
} => anyhow!("Federation disabled"),
LemmyError {
error_type: LemmyErrorType::DomainBlocked(_),
..
} => "Domain is blocked",
} => anyhow!("Domain is blocked"),
LemmyError {
error_type: LemmyErrorType::DomainNotInAllowList(_),
..
} => "Domain is not in allowlist",
_ => "Failed validating apub id",
} => anyhow!("Domain is not in allowlist"),
_ => anyhow!("Failed validating apub id"),
})?;
Ok(())
}
Expand Down
Loading

0 comments on commit 514f222

Please sign in to comment.