]> Untitled Git - lemmy.git/blobdiff - lemmy_db/src/community.rs
Merge branch 'main' into move_views_to_diesel
[lemmy.git] / lemmy_db / src / community.rs
index 40f046804dac8d36eb0a8b9ca5fb1278617037c8..eda643ad5ca77c5a58a3162c654610a7b51c1da6 100644 (file)
@@ -1,6 +1,7 @@
 use crate::{
   naive_now,
   schema::{community, community_follower, community_moderator, community_user_ban},
+  ApubObject,
   Bannable,
   Crud,
   Followable,
@@ -149,19 +150,31 @@ impl Crud<CommunityForm> for Community {
   }
 }
 
-impl Community {
-  pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Self, Error> {
+impl ApubObject<CommunityForm> for Community {
+  fn read_from_apub_id(conn: &PgConnection, for_actor_id: &str) -> Result<Self, Error> {
     use crate::schema::community::dsl::*;
     community
-      .filter(local.eq(true))
-      .filter(name.eq(community_name))
+      .filter(actor_id.eq(for_actor_id))
       .first::<Self>(conn)
   }
 
-  pub fn read_from_actor_id(conn: &PgConnection, for_actor_id: &str) -> Result<Self, Error> {
+  fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
+    use crate::schema::community::dsl::*;
+    insert_into(community)
+      .values(community_form)
+      .on_conflict(actor_id)
+      .do_update()
+      .set(community_form)
+      .get_result::<Self>(conn)
+  }
+}
+
+impl Community {
+  pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Self, Error> {
     use crate::schema::community::dsl::*;
     community
-      .filter(actor_id.eq(for_actor_id))
+      .filter(local.eq(true))
+      .filter(name.eq(community_name))
       .first::<Self>(conn)
   }
 
@@ -231,16 +244,6 @@ impl Community {
       .unwrap_or_default()
       .contains(&user_id)
   }
-
-  pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
-    use crate::schema::community::dsl::*;
-    insert_into(community)
-      .values(community_form)
-      .on_conflict(actor_id)
-      .do_update()
-      .set(community_form)
-      .get_result::<Self>(conn)
-  }
 }
 
 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]