]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/person.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / db_schema / src / impls / person.rs
index 1389544e180e72748ae7c3c08e820052b04029af..de99d4b63dc2223a58daa749636ea9085a34404a 100644 (file)
@@ -1,7 +1,7 @@
 use crate::{
   newtypes::{DbUrl, PersonId},
   schema::person::dsl::*,
-  source::person::{Person, PersonForm, PersonSafe},
+  source::person::{Person, PersonForm},
   traits::{ApubActor, Crud},
   utils::{functions::lower, naive_now},
 };
@@ -66,124 +66,26 @@ mod safe_type {
   }
 }
 
-mod safe_type_alias_1 {
-  use crate::{schema::person_alias_1::columns::*, source::person::PersonAlias1, traits::ToSafe};
-
-  type Columns = (
-    id,
-    name,
-    display_name,
-    avatar,
-    banned,
-    published,
-    updated,
-    actor_id,
-    bio,
-    local,
-    banner,
-    deleted,
-    inbox_url,
-    shared_inbox_url,
-    matrix_user_id,
-    admin,
-    bot_account,
-    ban_expires,
-  );
-
-  impl ToSafe for PersonAlias1 {
-    type SafeColumns = Columns;
-    fn safe_columns_tuple() -> Self::SafeColumns {
-      (
-        id,
-        name,
-        display_name,
-        avatar,
-        banned,
-        published,
-        updated,
-        actor_id,
-        bio,
-        local,
-        banner,
-        deleted,
-        inbox_url,
-        shared_inbox_url,
-        matrix_user_id,
-        admin,
-        bot_account,
-        ban_expires,
-      )
-    }
-  }
-}
-
-mod safe_type_alias_2 {
-  use crate::{schema::person_alias_2::columns::*, source::person::PersonAlias2, traits::ToSafe};
-
-  type Columns = (
-    id,
-    name,
-    display_name,
-    avatar,
-    banned,
-    published,
-    updated,
-    actor_id,
-    bio,
-    local,
-    banner,
-    deleted,
-    inbox_url,
-    shared_inbox_url,
-    matrix_user_id,
-    admin,
-    bot_account,
-    ban_expires,
-  );
-
-  impl ToSafe for PersonAlias2 {
-    type SafeColumns = Columns;
-    fn safe_columns_tuple() -> Self::SafeColumns {
-      (
-        id,
-        name,
-        display_name,
-        avatar,
-        banned,
-        published,
-        updated,
-        actor_id,
-        bio,
-        local,
-        banner,
-        deleted,
-        inbox_url,
-        shared_inbox_url,
-        matrix_user_id,
-        admin,
-        bot_account,
-        ban_expires,
-      )
-    }
-  }
-}
-
 impl Crud for Person {
   type Form = PersonForm;
   type IdType = PersonId;
-  fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
+  fn read(conn: &mut PgConnection, person_id: PersonId) -> Result<Self, Error> {
     person
       .filter(deleted.eq(false))
       .find(person_id)
       .first::<Self>(conn)
   }
-  fn delete(conn: &PgConnection, person_id: PersonId) -> Result<usize, Error> {
+  fn delete(conn: &mut PgConnection, person_id: PersonId) -> Result<usize, Error> {
     diesel::delete(person.find(person_id)).execute(conn)
   }
-  fn create(conn: &PgConnection, form: &PersonForm) -> Result<Self, Error> {
+  fn create(conn: &mut PgConnection, form: &PersonForm) -> Result<Self, Error> {
     insert_into(person).values(form).get_result::<Self>(conn)
   }
-  fn update(conn: &PgConnection, person_id: PersonId, form: &PersonForm) -> Result<Self, Error> {
+  fn update(
+    conn: &mut PgConnection,
+    person_id: PersonId,
+    form: &PersonForm,
+  ) -> Result<Self, Error> {
     diesel::update(person.find(person_id))
       .set(form)
       .get_result::<Self>(conn)
@@ -192,7 +94,7 @@ impl Crud for Person {
 
 impl Person {
   pub fn ban_person(
-    conn: &PgConnection,
+    conn: &mut PgConnection,
     person_id: PersonId,
     ban: bool,
     expires: Option<chrono::NaiveDateTime>,
@@ -202,19 +104,23 @@ impl Person {
       .get_result::<Self>(conn)
   }
 
-  pub fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<Self, Error> {
+  pub fn add_admin(
+    conn: &mut PgConnection,
+    person_id: PersonId,
+    added: bool,
+  ) -> Result<Self, Error> {
     diesel::update(person.find(person_id))
       .set(admin.eq(added))
       .get_result::<Self>(conn)
   }
 
-  pub fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
+  pub fn mark_as_updated(conn: &mut PgConnection, person_id: PersonId) -> Result<Person, Error> {
     diesel::update(person.find(person_id))
       .set((last_refreshed_at.eq(naive_now()),))
       .get_result::<Self>(conn)
   }
 
-  pub fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
+  pub fn delete_account(conn: &mut PgConnection, person_id: PersonId) -> Result<Person, Error> {
     use crate::schema::local_user;
 
     // Set the local user info to none
@@ -238,7 +144,7 @@ impl Person {
       .get_result::<Self>(conn)
   }
 
-  pub fn upsert(conn: &PgConnection, person_form: &PersonForm) -> Result<Person, Error> {
+  pub fn upsert(conn: &mut PgConnection, person_form: &PersonForm) -> Result<Person, Error> {
     insert_into(person)
       .values(person_form)
       .on_conflict(actor_id)
@@ -248,7 +154,7 @@ impl Person {
   }
 
   pub fn update_deleted(
-    conn: &PgConnection,
+    conn: &mut PgConnection,
     person_id: PersonId,
     new_deleted: bool,
   ) -> Result<Person, Error> {
@@ -258,17 +164,16 @@ impl Person {
       .get_result::<Self>(conn)
   }
 
-  pub fn is_banned(&self) -> bool {
-    is_banned(self.banned, self.ban_expires)
-  }
-
-  pub fn leave_admin(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
+  pub fn leave_admin(conn: &mut PgConnection, person_id: PersonId) -> Result<Self, Error> {
     diesel::update(person.find(person_id))
       .set(admin.eq(false))
       .get_result::<Self>(conn)
   }
 
-  pub fn remove_avatar_and_banner(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
+  pub fn remove_avatar_and_banner(
+    conn: &mut PgConnection,
+    person_id: PersonId,
+  ) -> Result<Self, Error> {
     diesel::update(person.find(person_id))
       .set((
         avatar.eq::<Option<String>>(None),
@@ -278,13 +183,7 @@ impl Person {
   }
 }
 
-impl PersonSafe {
-  pub fn is_banned(&self) -> bool {
-    is_banned(self.banned, self.ban_expires)
-  }
-}
-
-fn is_banned(banned_: bool, expires: Option<chrono::NaiveDateTime>) -> bool {
+pub fn is_banned(banned_: bool, expires: Option<chrono::NaiveDateTime>) -> bool {
   if let Some(expires) = expires {
     banned_ && expires.gt(&naive_now())
   } else {
@@ -293,7 +192,7 @@ fn is_banned(banned_: bool, expires: Option<chrono::NaiveDateTime>) -> bool {
 }
 
 impl ApubActor for Person {
-  fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Option<Self>, Error> {
+  fn read_from_apub_id(conn: &mut PgConnection, object_id: &DbUrl) -> Result<Option<Self>, Error> {
     use crate::schema::person::dsl::*;
     Ok(
       person
@@ -305,16 +204,23 @@ 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: &mut 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(
-    conn: &PgConnection,
+    conn: &mut PgConnection,
     person_name: &str,
     protocol_domain: &str,
   ) -> Result<Person, Error> {
@@ -332,15 +238,15 @@ mod tests {
 
   #[test]
   fn test_crud() {
-    let conn = establish_unpooled_connection();
+    let conn = &mut establish_unpooled_connection();
 
     let new_person = PersonForm {
       name: "holly".into(),
-      public_key: "nada".to_owned(),
+      public_key: Some("nada".to_owned()),
       ..PersonForm::default()
     };
 
-    let inserted_person = Person::create(&conn, &new_person).unwrap();
+    let inserted_person = Person::create(conn, &new_person).unwrap();
 
     let expected_person = Person {
       id: inserted_person.id,
@@ -366,9 +272,9 @@ mod tests {
       ban_expires: None,
     };
 
-    let read_person = Person::read(&conn, inserted_person.id).unwrap();
-    let updated_person = Person::update(&conn, inserted_person.id, &new_person).unwrap();
-    let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
+    let read_person = Person::read(conn, inserted_person.id).unwrap();
+    let updated_person = Person::update(conn, inserted_person.id, &new_person).unwrap();
+    let num_deleted = Person::delete(conn, inserted_person.id).unwrap();
 
     assert_eq!(expected_person, read_person);
     assert_eq!(expected_person, inserted_person);