]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/community.rs
Hide community v2 (#2055)
[lemmy.git] / crates / db_schema / src / impls / community.rs
index 19a3a95486f583d17596321335972aa5b2e6cd61..43b7db3e29621a17a601a12e115c7a3d1d55ad0b 100644 (file)
@@ -13,9 +13,17 @@ use crate::{
     CommunityPersonBanForm,
     CommunitySafe,
   },
-  traits::{Bannable, Crud, DeleteableOrRemoveable, Followable, Joinable},
+  traits::{ApubActor, Bannable, Crud, DeleteableOrRemoveable, Followable, Joinable},
+};
+use diesel::{
+  dsl::*,
+  result::Error,
+  ExpressionMethods,
+  PgConnection,
+  QueryDsl,
+  RunQueryDsl,
+  TextExpressionMethods,
 };
-use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
 use url::Url;
 
 mod safe_type {
@@ -35,6 +43,7 @@ mod safe_type {
     local,
     icon,
     banner,
+    hidden,
   );
 
   impl ToSafe for Community {
@@ -54,6 +63,7 @@ mod safe_type {
         local,
         icon,
         banner,
+        hidden,
       )
     }
   }
@@ -92,14 +102,6 @@ impl Crud for Community {
 }
 
 impl Community {
-  pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
-    use crate::schema::community::dsl::*;
-    community
-      .filter(local.eq(true))
-      .filter(lower(name).eq(lower(community_name)))
-      .first::<Self>(conn)
-  }
-
   pub fn update_deleted(
     conn: &PgConnection,
     community_id: CommunityId,
@@ -122,9 +124,9 @@ impl Community {
       .get_result::<Self>(conn)
   }
 
-  pub fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> {
+  pub fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<DbUrl>, Error> {
     use crate::schema::community::dsl::*;
-    community.select(actor_id).distinct().load::<String>(conn)
+    community.select(actor_id).distinct().load::<DbUrl>(conn)
   }
 
   pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
@@ -136,17 +138,6 @@ impl Community {
       .set(community_form)
       .get_result::<Self>(conn)
   }
-  pub fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
-    use crate::schema::community::dsl::*;
-    let object_id: DbUrl = object_id.into();
-    Ok(
-      community
-        .filter(actor_id.eq(object_id))
-        .first::<Community>(conn)
-        .ok()
-        .map(Into::into),
-    )
-  }
 }
 
 impl Joinable for CommunityModerator {
@@ -225,6 +216,9 @@ impl Bannable for CommunityPersonBan {
     use crate::schema::community_person_ban::dsl::*;
     insert_into(community_person_ban)
       .values(community_person_ban_form)
+      .on_conflict((community_id, person_id))
+      .do_update()
+      .set(community_person_ban_form)
       .get_result::<Self>(conn)
   }
 
@@ -296,6 +290,40 @@ impl Followable for CommunityFollower {
   }
 }
 
+impl ApubActor for Community {
+  fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
+    use crate::schema::community::dsl::*;
+    let object_id: DbUrl = object_id.into();
+    Ok(
+      community
+        .filter(actor_id.eq(object_id))
+        .first::<Community>(conn)
+        .ok()
+        .map(Into::into),
+    )
+  }
+
+  fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
+    use crate::schema::community::dsl::*;
+    community
+      .filter(local.eq(true))
+      .filter(lower(name).eq(lower(community_name)))
+      .first::<Self>(conn)
+  }
+
+  fn read_from_name_and_domain(
+    conn: &PgConnection,
+    community_name: &str,
+    protocol_domain: &str,
+  ) -> Result<Community, Error> {
+    use crate::schema::community::dsl::*;
+    community
+      .filter(lower(name).eq(lower(community_name)))
+      .filter(actor_id.like(format!("{}%", protocol_domain)))
+      .first::<Self>(conn)
+  }
+}
+
 #[cfg(test)]
 mod tests {
   use crate::{
@@ -346,6 +374,7 @@ mod tests {
       followers_url: inserted_community.followers_url.to_owned(),
       inbox_url: inserted_community.inbox_url.to_owned(),
       shared_inbox_url: None,
+      hidden: false,
     };
 
     let community_follower_form = CommunityFollowerForm {
@@ -383,6 +412,7 @@ mod tests {
     let community_person_ban_form = CommunityPersonBanForm {
       community_id: inserted_community.id,
       person_id: inserted_person.id,
+      expires: None,
     };
 
     let inserted_community_person_ban =
@@ -393,6 +423,7 @@ mod tests {
       community_id: inserted_community.id,
       person_id: inserted_person.id,
       published: inserted_community_person_ban.published,
+      expires: None,
     };
 
     let read_community = Community::read(&conn, inserted_community.id).unwrap();