]> Untitled Git - lemmy.git/commitdiff
Be more explicit about returning deleted actors or not (#2335)
authorNutomic <me@nutomic.com>
Tue, 5 Jul 2022 21:40:44 +0000 (21:40 +0000)
committerGitHub <noreply@github.com>
Tue, 5 Jul 2022 21:40:44 +0000 (17:40 -0400)
* Be more explicit about returning deleted actors or not

* simplify db queries

crates/apub/src/fetcher/mod.rs
crates/apub/src/http/community.rs
crates/apub/src/http/person.rs
crates/db_schema/src/impls/community.rs
crates/db_schema/src/impls/person.rs
crates/db_schema/src/traits.rs
crates/routes/src/feeds.rs
crates/routes/src/webfinger.rs

index 072cf7dc7ea370c3dbbc3b183a069bc2401be2b2..8daef4331ac32b982b826f76dc62b9ddf9625d43 100644 (file)
@@ -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??,
     )
index 8765959c5a45f11a5e9af13e0290c50a513a1549..6f68e8fc4ec44319e781f262d19c0288b50780c3 100644 (file)
@@ -35,7 +35,7 @@ pub(crate) async fn get_apub_community_http(
   context: web::Data<LemmyContext>,
 ) -> Result<HttpResponse, LemmyError> {
   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<LemmyContext>,
 ) -> Result<HttpResponse, LemmyError> {
   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<LemmyContext>,
 ) -> Result<HttpResponse, LemmyError> {
   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<LemmyContext>,
 ) -> Result<HttpResponse, LemmyError> {
   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();
index f887b26520e38163296a4ee7c3aaf55a27019296..16c937b378989a401c5a667513fa5ac116295681 100644 (file)
@@ -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<LemmyContext>,
 ) -> Result<HttpResponse, LemmyError> {
   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();
index f957fb2617a258d3cd2dc79a694d0f66b3309ebb..a0a1dde2a99effd4fa2618869f6382ac0413123c 100644 (file)
@@ -332,12 +332,20 @@ impl ApubActor for Community {
     )
   }
 
-  fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
+  fn read_from_name(
+    conn: &PgConnection,
+    community_name: &str,
+    include_deleted: bool,
+  ) -> Result<Community, Error> {
     use crate::schema::community::dsl::*;
-    community
+    let mut q = community
+      .into_boxed()
       .filter(local.eq(true))
-      .filter(lower(name).eq(lower(community_name)))
-      .first::<Self>(conn)
+      .filter(lower(name).eq(lower(community_name)));
+    if !include_deleted {
+      q = q.filter(deleted.eq(false)).filter(removed.eq(false));
+    }
+    q.first::<Self>(conn)
   }
 
   fn read_from_name_and_domain(
index 1389544e180e72748ae7c3c08e820052b04029af..2608ef5be1eb4986d51877655c1aecc6566711cc 100644 (file)
@@ -305,12 +305,19 @@ impl ApubActor for Person {
     )
   }
 
-  fn read_from_name(conn: &PgConnection, from_name: &str) -> Result<Person, Error> {
-    person
-      .filter(deleted.eq(false))
+  fn read_from_name(
+    conn: &PgConnection,
+    from_name: &str,
+    include_deleted: bool,
+  ) -> Result<Person, Error> {
+    let mut q = person
+      .into_boxed()
       .filter(local.eq(true))
-      .filter(lower(name).eq(lower(from_name)))
-      .first::<Person>(conn)
+      .filter(lower(name).eq(lower(from_name)));
+    if !include_deleted {
+      q = q.filter(deleted.eq(false))
+    }
+    q.first::<Self>(conn)
   }
 
   fn read_from_name_and_domain(
index 2b8e3422e15a38cda2baa7e3c130764ed0807f0e..22cf669d7a418ee89ae2d7d3f1ff0751f52b8294 100644 (file)
@@ -168,7 +168,13 @@ pub trait ApubActor {
   fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Option<Self>, Error>
   where
     Self: Sized;
-  fn read_from_name(conn: &PgConnection, actor_name: &str) -> Result<Self, Error>
+  /// - 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<Self, Error>
   where
     Self: Sized;
   fn read_from_name_and_domain(
index 402258585e194b4a50040169a214e03a245b443e..e0130eca21853ff729b0d4ab47c5fdf80016cc6b 100644 (file)
@@ -181,7 +181,7 @@ fn get_feed_user(
   protocol_and_hostname: &str,
 ) -> Result<ChannelBuilder, LemmyError> {
   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<ChannelBuilder, LemmyError> {
   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)
index f3856eb5df6f78500ad688d89d75f9f6e46e6560..4c015675d11ab2d4382004063006ef10ab865f37 100644 (file)
@@ -46,13 +46,13 @@ async fn get_webfinger_response(
 
   let name_ = name.clone();
   let user_id: Option<Url> = 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<Url> = blocking(context.pool(), move |conn| {
-    Community::read_from_name(conn, &name)
+    Community::read_from_name(conn, &name, false)
   })
   .await?
   .ok()