]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/community.rs
Implement federated user following (fixes #752) (#2577)
[lemmy.git] / crates / db_schema / src / impls / community.rs
index d23baed98831b0fb6cf14ca1be91a47bed6ad608..5f29f1359b84afa93909fb31d5aaa6d24b6f530c 100644 (file)
@@ -1,6 +1,6 @@
 use crate::{
   newtypes::{CommunityId, DbUrl, PersonId},
-  schema::community::dsl::*,
+  schema::community::dsl::{actor_id, community, deleted, local, name, removed},
   source::{
     actor_language::{CommunityLanguage, SiteLanguage},
     community::{
@@ -20,11 +20,32 @@ use crate::{
   utils::{functions::lower, get_conn, DbPool},
   SubscribedType,
 };
-use diesel::{dsl::*, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
+use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
 use diesel_async::RunQueryDsl;
 
 mod safe_type {
-  use crate::{schema::community::*, source::community::Community, traits::ToSafe};
+  use crate::{
+    schema::community::{
+      actor_id,
+      banner,
+      deleted,
+      description,
+      hidden,
+      icon,
+      id,
+      instance_id,
+      local,
+      name,
+      nsfw,
+      posting_restricted_to_mods,
+      published,
+      removed,
+      title,
+      updated,
+    },
+    source::community::Community,
+    traits::ToSafe,
+  };
 
   type Columns = (
     id,
@@ -129,7 +150,7 @@ impl Joinable for CommunityModerator {
     pool: &DbPool,
     community_moderator_form: &CommunityModeratorForm,
   ) -> Result<Self, Error> {
-    use crate::schema::community_moderator::dsl::*;
+    use crate::schema::community_moderator::dsl::community_moderator;
     let conn = &mut get_conn(pool).await?;
     insert_into(community_moderator)
       .values(community_moderator_form)
@@ -141,7 +162,7 @@ impl Joinable for CommunityModerator {
     pool: &DbPool,
     community_moderator_form: &CommunityModeratorForm,
   ) -> Result<usize, Error> {
-    use crate::schema::community_moderator::dsl::*;
+    use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id};
     let conn = &mut get_conn(pool).await?;
     diesel::delete(
       community_moderator
@@ -155,7 +176,7 @@ impl Joinable for CommunityModerator {
 
 impl DeleteableOrRemoveable for CommunitySafe {
   fn blank_out_deleted_or_removed_info(mut self) -> Self {
-    self.title = "".into();
+    self.title = String::new();
     self.description = None;
     self.icon = None;
     self.banner = None;
@@ -165,7 +186,7 @@ impl DeleteableOrRemoveable for CommunitySafe {
 
 impl DeleteableOrRemoveable for Community {
   fn blank_out_deleted_or_removed_info(mut self) -> Self {
-    self.title = "".into();
+    self.title = String::new();
     self.description = None;
     self.icon = None;
     self.banner = None;
@@ -178,7 +199,7 @@ impl CommunityModerator {
     pool: &DbPool,
     for_community_id: CommunityId,
   ) -> Result<usize, Error> {
-    use crate::schema::community_moderator::dsl::*;
+    use crate::schema::community_moderator::dsl::{community_id, community_moderator};
     let conn = &mut get_conn(pool).await?;
 
     diesel::delete(community_moderator.filter(community_id.eq(for_community_id)))
@@ -190,7 +211,7 @@ impl CommunityModerator {
     pool: &DbPool,
     for_person_id: PersonId,
   ) -> Result<Vec<CommunityId>, Error> {
-    use crate::schema::community_moderator::dsl::*;
+    use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id};
     let conn = &mut get_conn(pool).await?;
     community_moderator
       .filter(person_id.eq(for_person_id))
@@ -207,7 +228,7 @@ impl Bannable for CommunityPersonBan {
     pool: &DbPool,
     community_person_ban_form: &CommunityPersonBanForm,
   ) -> Result<Self, Error> {
-    use crate::schema::community_person_ban::dsl::*;
+    use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id};
     let conn = &mut get_conn(pool).await?;
     insert_into(community_person_ban)
       .values(community_person_ban_form)
@@ -222,7 +243,7 @@ impl Bannable for CommunityPersonBan {
     pool: &DbPool,
     community_person_ban_form: &CommunityPersonBanForm,
   ) -> Result<usize, Error> {
-    use crate::schema::community_person_ban::dsl::*;
+    use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id};
     let conn = &mut get_conn(pool).await?;
     diesel::delete(
       community_person_ban
@@ -238,7 +259,7 @@ impl CommunityFollower {
   pub fn to_subscribed_type(follower: &Option<Self>) -> SubscribedType {
     match follower {
       Some(f) => {
-        if f.pending.unwrap_or(false) {
+        if f.pending {
           SubscribedType::Pending
         } else {
           SubscribedType::Subscribed
@@ -253,17 +274,14 @@ impl CommunityFollower {
 #[async_trait]
 impl Followable for CommunityFollower {
   type Form = CommunityFollowerForm;
-  async fn follow(
-    pool: &DbPool,
-    community_follower_form: &CommunityFollowerForm,
-  ) -> Result<Self, Error> {
-    use crate::schema::community_follower::dsl::*;
+  async fn follow(pool: &DbPool, form: &CommunityFollowerForm) -> Result<Self, Error> {
+    use crate::schema::community_follower::dsl::{community_follower, community_id, person_id};
     let conn = &mut get_conn(pool).await?;
     insert_into(community_follower)
-      .values(community_follower_form)
+      .values(form)
       .on_conflict((community_id, person_id))
       .do_update()
-      .set(community_follower_form)
+      .set(form)
       .get_result::<Self>(conn)
       .await
   }
@@ -272,7 +290,12 @@ impl Followable for CommunityFollower {
     community_id_: CommunityId,
     person_id_: PersonId,
   ) -> Result<Self, Error> {
-    use crate::schema::community_follower::dsl::*;
+    use crate::schema::community_follower::dsl::{
+      community_follower,
+      community_id,
+      pending,
+      person_id,
+    };
     let conn = &mut get_conn(pool).await?;
     diesel::update(
       community_follower
@@ -283,31 +306,17 @@ impl Followable for CommunityFollower {
     .get_result::<Self>(conn)
     .await
   }
-  async fn unfollow(
-    pool: &DbPool,
-    community_follower_form: &CommunityFollowerForm,
-  ) -> Result<usize, Error> {
-    use crate::schema::community_follower::dsl::*;
+  async fn unfollow(pool: &DbPool, form: &CommunityFollowerForm) -> Result<usize, Error> {
+    use crate::schema::community_follower::dsl::{community_follower, community_id, person_id};
     let conn = &mut get_conn(pool).await?;
     diesel::delete(
       community_follower
-        .filter(community_id.eq(&community_follower_form.community_id))
-        .filter(person_id.eq(&community_follower_form.person_id)),
+        .filter(community_id.eq(&form.community_id))
+        .filter(person_id.eq(&form.person_id)),
     )
     .execute(conn)
     .await
   }
-  // TODO: this function name only makes sense if you call it with a remote community. for a local
-  //       community, it will also return true if only remote followers exist
-  async fn has_local_followers(pool: &DbPool, community_id_: CommunityId) -> Result<bool, Error> {
-    use crate::schema::community_follower::dsl::*;
-    let conn = &mut get_conn(pool).await?;
-    diesel::select(exists(
-      community_follower.filter(community_id.eq(community_id_)),
-    ))
-    .get_result(conn)
-    .await
-  }
 }
 
 #[async_trait]
@@ -357,7 +366,21 @@ impl ApubActor for Community {
 #[cfg(test)]
 mod tests {
   use crate::{
-    source::{community::*, instance::Instance, person::*},
+    source::{
+      community::{
+        Community,
+        CommunityFollower,
+        CommunityFollowerForm,
+        CommunityInsertForm,
+        CommunityModerator,
+        CommunityModeratorForm,
+        CommunityPersonBan,
+        CommunityPersonBanForm,
+        CommunityUpdateForm,
+      },
+      instance::Instance,
+      person::{Person, PersonInsertForm},
+    },
     traits::{Bannable, Crud, Followable, Joinable},
     utils::build_db_pool_for_tests,
   };
@@ -397,15 +420,15 @@ mod tests {
       deleted: false,
       published: inserted_community.published,
       updated: None,
-      actor_id: inserted_community.actor_id.to_owned(),
+      actor_id: inserted_community.actor_id.clone(),
       local: true,
       private_key: None,
       public_key: "pubkey".to_owned(),
       last_refreshed_at: inserted_community.published,
       icon: None,
       banner: None,
-      followers_url: inserted_community.followers_url.to_owned(),
-      inbox_url: inserted_community.inbox_url.to_owned(),
+      followers_url: inserted_community.followers_url.clone(),
+      inbox_url: inserted_community.inbox_url.clone(),
       shared_inbox_url: None,
       hidden: false,
       posting_restricted_to_mods: false,
@@ -426,7 +449,7 @@ mod tests {
       id: inserted_community_follower.id,
       community_id: inserted_community.id,
       person_id: inserted_person.id,
-      pending: Some(false),
+      pending: false,
       published: inserted_community_follower.published,
     };