]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/person.rs
Removing the site creator, adding leave_admin. Fixes #1808 (#2052)
[lemmy.git] / crates / db_schema / src / impls / person.rs
index 5b7cb941687d788ea1295fd7402ed1b11a6c7c9c..17833253bcc0083b7f4967b1f7623b7c1aa3049b 100644 (file)
@@ -1,14 +1,12 @@
 use crate::{
+  functions::lower,
   naive_now,
   newtypes::{DbUrl, PersonId},
   schema::person::dsl::*,
-  source::person::{Person, PersonForm},
+  source::person::{Person, PersonForm, PersonSafe},
   traits::Crud,
 };
-use chrono::NaiveDateTime;
-use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, *};
-use lemmy_apub_lib::traits::{ActorType, ApubObject};
-use lemmy_utils::LemmyError;
+use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
 use url::Url;
 
 mod safe_type {
@@ -32,6 +30,7 @@ mod safe_type {
     matrix_user_id,
     admin,
     bot_account,
+    ban_expires,
   );
 
   impl ToSafe for Person {
@@ -55,6 +54,7 @@ mod safe_type {
         matrix_user_id,
         admin,
         bot_account,
+        ban_expires,
       )
     }
   }
@@ -81,6 +81,7 @@ mod safe_type_alias_1 {
     matrix_user_id,
     admin,
     bot_account,
+    ban_expires,
   );
 
   impl ToSafe for PersonAlias1 {
@@ -104,6 +105,7 @@ mod safe_type_alias_1 {
         matrix_user_id,
         admin,
         bot_account,
+        ban_expires,
       )
     }
   }
@@ -130,6 +132,7 @@ mod safe_type_alias_2 {
     matrix_user_id,
     admin,
     bot_account,
+    ban_expires,
   );
 
   impl ToSafe for PersonAlias2 {
@@ -153,6 +156,7 @@ mod safe_type_alias_2 {
         matrix_user_id,
         admin,
         bot_account,
+        ban_expires,
       )
     }
   }
@@ -181,9 +185,14 @@ impl Crud for Person {
 }
 
 impl Person {
-  pub fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Self, Error> {
+  pub fn ban_person(
+    conn: &PgConnection,
+    person_id: PersonId,
+    ban: bool,
+    expires: Option<chrono::NaiveDateTime>,
+  ) -> Result<Self, Error> {
     diesel::update(person.find(person_id))
-      .set(banned.eq(ban))
+      .set((banned.eq(ban), ban_expires.eq(expires)))
       .get_result::<Self>(conn)
   }
 
@@ -197,7 +206,7 @@ impl Person {
     person
       .filter(deleted.eq(false))
       .filter(local.eq(true))
-      .filter(name.ilike(from_name))
+      .filter(lower(name).eq(lower(from_name)))
       .first::<Person>(conn)
   }
 
@@ -237,61 +246,53 @@ impl Person {
       .set(person_form)
       .get_result::<Self>(conn)
   }
-}
-
-impl ApubObject for Person {
-  type DataType = PgConnection;
-
-  fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
-    Some(self.last_refreshed_at)
-  }
 
-  fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, LemmyError> {
+  pub fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
     use crate::schema::person::dsl::*;
     let object_id: DbUrl = object_id.into();
     Ok(
       person
         .filter(deleted.eq(false))
         .filter(actor_id.eq(object_id))
-        .first::<Self>(conn)
-        .ok(),
+        .first::<Person>(conn)
+        .ok()
+        .map(Into::into),
     )
   }
 
-  fn delete(self, conn: &PgConnection) -> Result<(), LemmyError> {
+  pub fn update_deleted(
+    conn: &PgConnection,
+    person_id: PersonId,
+    new_deleted: bool,
+  ) -> Result<Person, Error> {
     use crate::schema::person::dsl::*;
-    diesel::update(person.find(self.id))
-      .set((deleted.eq(true), updated.eq(naive_now())))
-      .get_result::<Self>(conn)?;
-    Ok(())
-  }
-}
-
-impl ActorType for Person {
-  fn is_local(&self) -> bool {
-    self.local
-  }
-  fn actor_id(&self) -> Url {
-    self.actor_id.to_owned().into_inner()
-  }
-  fn name(&self) -> String {
-    self.name.clone()
+    diesel::update(person.find(person_id))
+      .set(deleted.eq(new_deleted))
+      .get_result::<Self>(conn)
   }
 
-  fn public_key(&self) -> Option<String> {
-    self.public_key.to_owned()
+  pub fn is_banned(&self) -> bool {
+    is_banned(self.banned, self.ban_expires)
   }
 
-  fn private_key(&self) -> Option<String> {
-    self.private_key.to_owned()
+  pub fn leave_admin(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
+    diesel::update(person.find(person_id))
+      .set(admin.eq(false))
+      .get_result::<Self>(conn)
   }
+}
 
-  fn inbox_url(&self) -> Url {
-    self.inbox_url.clone().into()
+impl PersonSafe {
+  pub fn is_banned(&self) -> bool {
+    is_banned(self.banned, self.ban_expires)
   }
+}
 
-  fn shared_inbox_url(&self) -> Option<Url> {
-    self.shared_inbox_url.clone().map(|s| s.into_inner())
+fn is_banned(banned_: bool, expires: Option<chrono::NaiveDateTime>) -> bool {
+  if let Some(expires) = expires {
+    banned_ && expires.gt(&naive_now())
+  } else {
+    banned_
   }
 }
 
@@ -305,6 +306,7 @@ mod tests {
 
     let new_person = PersonForm {
       name: "holly".into(),
+      public_key: "nada".to_owned(),
       ..PersonForm::default()
     };
 
@@ -326,11 +328,12 @@ mod tests {
       bot_account: false,
       admin: false,
       private_key: None,
-      public_key: None,
+      public_key: "nada".to_owned(),
       last_refreshed_at: inserted_person.published,
       inbox_url: inserted_person.inbox_url.to_owned(),
       shared_inbox_url: None,
       matrix_user_id: None,
+      ban_expires: None,
     };
 
     let read_person = Person::read(&conn, inserted_person.id).unwrap();