]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/local_user.rs
implement language tags for site/community in db and api (#2434)
[lemmy.git] / crates / db_schema / src / impls / local_user.rs
index 3a2d5769517a87a18188b02fc7b1d87ca2f41e4c..31eded1a4ce659669f38e441b2759243d7cb1634 100644 (file)
@@ -1,9 +1,12 @@
 use crate::{
-  naive_now,
   newtypes::LocalUserId,
   schema::local_user::dsl::*,
-  source::local_user::{LocalUser, LocalUserForm},
+  source::{
+    actor_language::{LocalUserLanguage, SiteLanguage},
+    local_user::{LocalUser, LocalUserForm},
+  },
   traits::Crud,
+  utils::naive_now,
 };
 use bcrypt::{hash, DEFAULT_COST};
 use diesel::{dsl::*, result::Error, *};
@@ -23,7 +26,7 @@ mod safe_settings_type {
     theme,
     default_sort_type,
     default_listing_type,
-    lang,
+    interface_language,
     show_avatars,
     send_notifications_to_email,
     validator_time,
@@ -31,6 +34,8 @@ mod safe_settings_type {
     show_scores,
     show_read_posts,
     show_new_post_notifs,
+    email_verified,
+    accepted_application,
   );
 
   impl ToSafeSettings for LocalUser {
@@ -46,7 +51,7 @@ mod safe_settings_type {
         theme,
         default_sort_type,
         default_listing_type,
-        lang,
+        interface_language,
         show_avatars,
         send_notifications_to_email,
         validator_time,
@@ -54,23 +59,27 @@ mod safe_settings_type {
         show_scores,
         show_read_posts,
         show_new_post_notifs,
+        email_verified,
+        accepted_application,
       )
     }
   }
 }
 
 impl LocalUser {
-  pub fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
+  pub fn register(conn: &mut PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
     let mut edited_user = form.clone();
-    let password_hash =
-      hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
+    let password_hash = form
+      .password_encrypted
+      .as_ref()
+      .map(|p| hash(p, DEFAULT_COST).expect("Couldn't hash password"));
     edited_user.password_encrypted = password_hash;
 
     Self::create(conn, &edited_user)
   }
 
   pub fn update_password(
-    conn: &PgConnection,
+    conn: &mut PgConnection,
     local_user_id: LocalUserId,
     new_password: &str,
   ) -> Result<Self, Error> {
@@ -83,24 +92,50 @@ impl LocalUser {
       ))
       .get_result::<Self>(conn)
   }
+
+  pub fn set_all_users_email_verified(conn: &mut PgConnection) -> Result<Vec<Self>, Error> {
+    diesel::update(local_user)
+      .set(email_verified.eq(true))
+      .get_results::<Self>(conn)
+  }
+
+  pub fn set_all_users_registration_applications_accepted(
+    conn: &mut PgConnection,
+  ) -> Result<Vec<Self>, Error> {
+    diesel::update(local_user)
+      .set(accepted_application.eq(true))
+      .get_results::<Self>(conn)
+  }
 }
 
 impl Crud for LocalUser {
   type Form = LocalUserForm;
   type IdType = LocalUserId;
-  fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
+  fn read(conn: &mut PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
     local_user.find(local_user_id).first::<Self>(conn)
   }
-  fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
+  fn delete(conn: &mut PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
     diesel::delete(local_user.find(local_user_id)).execute(conn)
   }
-  fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
-    insert_into(local_user)
+  fn create(conn: &mut PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
+    let local_user_ = insert_into(local_user)
       .values(form)
-      .get_result::<Self>(conn)
+      .get_result::<Self>(conn)?;
+
+    let site_languages = SiteLanguage::read_local(conn);
+    if let Ok(langs) = site_languages {
+      // if site exists, init user with site languages
+      LocalUserLanguage::update(conn, langs, local_user_.id)?;
+    } else {
+      // otherwise, init with all languages (this only happens during tests and
+      // for first admin user, which is created before site)
+      LocalUserLanguage::update(conn, vec![], local_user_.id)?;
+    }
+
+    Ok(local_user_)
   }
   fn update(
-    conn: &PgConnection,
+    conn: &mut PgConnection,
     local_user_id: LocalUserId,
     form: &LocalUserForm,
   ) -> Result<Self, Error> {