From: Nutomic Date: Tue, 5 Jul 2022 21:40:44 +0000 (+0000) Subject: Be more explicit about returning deleted actors or not (#2335) X-Git-Url: http://these/git/%22https:/nerdica.net/%24%7B%60data:application/%7Bthis.captchaPngSrc%28%29%7D?a=commitdiff_plain;h=b7a2677b4d5d633094e455609a2d733b80fae906;p=lemmy.git Be more explicit about returning deleted actors or not (#2335) * Be more explicit about returning deleted actors or not * simplify db queries --- diff --git a/crates/apub/src/fetcher/mod.rs b/crates/apub/src/fetcher/mod.rs index 072cf7dc..8daef433 100644 --- a/crates/apub/src/fetcher/mod.rs +++ b/crates/apub/src/fetcher/mod.rs @@ -58,7 +58,7 @@ where let identifier = identifier.to_string(); Ok( blocking(context.pool(), move |conn| { - DbActor::read_from_name(conn, &identifier) + DbActor::read_from_name(conn, &identifier, false) }) .await??, ) diff --git a/crates/apub/src/http/community.rs b/crates/apub/src/http/community.rs index 8765959c..6f68e8fc 100644 --- a/crates/apub/src/http/community.rs +++ b/crates/apub/src/http/community.rs @@ -35,7 +35,7 @@ pub(crate) async fn get_apub_community_http( context: web::Data, ) -> Result { let community: ApubCommunity = blocking(context.pool(), move |conn| { - Community::read_from_name(conn, &info.community_name) + Community::read_from_name(conn, &info.community_name, true) }) .await?? .into(); @@ -66,7 +66,7 @@ pub(crate) async fn get_apub_community_followers( context: web::Data, ) -> Result { let community = blocking(context.pool(), move |conn| { - Community::read_from_name(conn, &info.community_name) + Community::read_from_name(conn, &info.community_name, false) }) .await??; let followers = GroupFollowers::new(community, &context).await?; @@ -80,7 +80,7 @@ pub(crate) async fn get_apub_community_outbox( context: web::Data, ) -> Result { let community = blocking(context.pool(), move |conn| { - Community::read_from_name(conn, &info.community_name) + Community::read_from_name(conn, &info.community_name, false) }) .await??; let id = ObjectId::new(generate_outbox_url(&community.actor_id)?); @@ -97,7 +97,7 @@ pub(crate) async fn get_apub_community_moderators( context: web::Data, ) -> Result { let community: ApubCommunity = blocking(context.pool(), move |conn| { - Community::read_from_name(conn, &info.community_name) + Community::read_from_name(conn, &info.community_name, false) }) .await?? .into(); diff --git a/crates/apub/src/http/person.rs b/crates/apub/src/http/person.rs index f887b265..16c937b3 100644 --- a/crates/apub/src/http/person.rs +++ b/crates/apub/src/http/person.rs @@ -28,7 +28,7 @@ pub(crate) async fn get_apub_person_http( let user_name = info.into_inner().user_name; // TODO: this needs to be able to read deleted persons, so that it can send tombstones let person: ApubPerson = blocking(context.pool(), move |conn| { - Person::read_from_name(conn, &user_name) + Person::read_from_name(conn, &user_name, true) }) .await?? .into(); @@ -60,7 +60,7 @@ pub(crate) async fn get_apub_person_outbox( context: web::Data, ) -> Result { let person = blocking(context.pool(), move |conn| { - Person::read_from_name(conn, &info.user_name) + Person::read_from_name(conn, &info.user_name, false) }) .await??; let outbox_id = generate_outbox_url(&person.actor_id)?.into(); diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index f957fb26..a0a1dde2 100644 --- a/crates/db_schema/src/impls/community.rs +++ b/crates/db_schema/src/impls/community.rs @@ -332,12 +332,20 @@ impl ApubActor for Community { ) } - fn read_from_name(conn: &PgConnection, community_name: &str) -> Result { + fn read_from_name( + conn: &PgConnection, + community_name: &str, + include_deleted: bool, + ) -> Result { use crate::schema::community::dsl::*; - community + let mut q = community + .into_boxed() .filter(local.eq(true)) - .filter(lower(name).eq(lower(community_name))) - .first::(conn) + .filter(lower(name).eq(lower(community_name))); + if !include_deleted { + q = q.filter(deleted.eq(false)).filter(removed.eq(false)); + } + q.first::(conn) } fn read_from_name_and_domain( diff --git a/crates/db_schema/src/impls/person.rs b/crates/db_schema/src/impls/person.rs index 1389544e..2608ef5b 100644 --- a/crates/db_schema/src/impls/person.rs +++ b/crates/db_schema/src/impls/person.rs @@ -305,12 +305,19 @@ impl ApubActor for Person { ) } - fn read_from_name(conn: &PgConnection, from_name: &str) -> Result { - person - .filter(deleted.eq(false)) + fn read_from_name( + conn: &PgConnection, + from_name: &str, + include_deleted: bool, + ) -> Result { + let mut q = person + .into_boxed() .filter(local.eq(true)) - .filter(lower(name).eq(lower(from_name))) - .first::(conn) + .filter(lower(name).eq(lower(from_name))); + if !include_deleted { + q = q.filter(deleted.eq(false)) + } + q.first::(conn) } fn read_from_name_and_domain( diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index 2b8e3422..22cf669d 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -168,7 +168,13 @@ pub trait ApubActor { fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result, Error> where Self: Sized; - fn read_from_name(conn: &PgConnection, actor_name: &str) -> Result + /// - actor_name is the name of the community or user to read. + /// - include_deleted, if true, will return communities or users that were deleted/removed + fn read_from_name( + conn: &PgConnection, + actor_name: &str, + include_deleted: bool, + ) -> Result where Self: Sized; fn read_from_name_and_domain( diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index 40225858..e0130eca 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -181,7 +181,7 @@ fn get_feed_user( protocol_and_hostname: &str, ) -> Result { let site_view = SiteView::read_local(conn)?; - let person = Person::read_from_name(conn, user_name)?; + let person = Person::read_from_name(conn, user_name, false)?; let posts = PostQueryBuilder::create(conn) .listing_type(ListingType::All) @@ -210,7 +210,7 @@ fn get_feed_community( protocol_and_hostname: &str, ) -> Result { let site_view = SiteView::read_local(conn)?; - let community = Community::read_from_name(conn, community_name)?; + let community = Community::read_from_name(conn, community_name, false)?; let posts = PostQueryBuilder::create(conn) .listing_type(ListingType::Community) diff --git a/crates/routes/src/webfinger.rs b/crates/routes/src/webfinger.rs index f3856eb5..4c015675 100644 --- a/crates/routes/src/webfinger.rs +++ b/crates/routes/src/webfinger.rs @@ -46,13 +46,13 @@ async fn get_webfinger_response( let name_ = name.clone(); let user_id: Option = blocking(context.pool(), move |conn| { - Person::read_from_name(conn, &name_) + Person::read_from_name(conn, &name_, false) }) .await? .ok() .map(|c| c.actor_id.into()); let community_id: Option = blocking(context.pool(), move |conn| { - Community::read_from_name(conn, &name) + Community::read_from_name(conn, &name, false) }) .await? .ok()