]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/community.rs
Implement instance actor (#1798)
[lemmy.git] / crates / db_schema / src / impls / community.rs
index 228cf23ffdba4fadedc1c6f19ee81a42bdcd8e9e..b2b3a6d8188d85c67d36d6ad8590c8d6c8f61245 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 {
@@ -92,14 +100,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 +122,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 +136,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 {
@@ -299,6 +288,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::{