]> Untitled Git - lemmy.git/commitdiff
Improve application question check (#2628)
authorNutomic <me@nutomic.com>
Fri, 23 Dec 2022 01:31:39 +0000 (01:31 +0000)
committerGitHub <noreply@github.com>
Fri, 23 Dec 2022 01:31:39 +0000 (01:31 +0000)
The check was failing if only require_application was passed
and not application_question, despite application_question
being set in database.

crates/api_crud/src/lib.rs
crates/api_crud/src/private_message/create.rs
crates/api_crud/src/site/create.rs
crates/api_crud/src/site/update.rs
crates/utils/src/utils.rs

index d37dfbee243133ec56a8650f5d6487efb62f8e12..30c9ebd42d516f386050b4f27b1dfa33f14adea9 100644 (file)
@@ -1,5 +1,6 @@
 use actix_web::web::Data;
 use lemmy_api_common::context::LemmyContext;
+use lemmy_db_schema::source::local_site::LocalSite;
 use lemmy_utils::{error::LemmyError, ConnectionId};
 
 mod comment;
@@ -19,3 +20,18 @@ pub trait PerformCrud {
     websocket_id: Option<ConnectionId>,
   ) -> Result<Self::Response, LemmyError>;
 }
+
+/// Make sure if applications are required, that there is an application questionnaire
+pub fn check_application_question(
+  application_question: &Option<Option<String>>,
+  local_site: &LocalSite,
+  require_application: &Option<bool>,
+) -> Result<(), LemmyError> {
+  if require_application.unwrap_or(false)
+    && (application_question == &Some(None)
+      || (application_question.is_none() && local_site.application_question.is_none()))
+  {
+    return Err(LemmyError::from_message("application_question_required"));
+  }
+  Ok(())
+}
index 25c7be467d063dce24ab4cce2502276472a73713..6fa5f9134576dadf1a6a23792983e07de5e9bd6c 100644 (file)
@@ -98,7 +98,7 @@ impl PerformCrud for CreatePrivateMessage {
         &local_recipient,
         &lang.notification_private_message_subject(&local_recipient.person.name),
         &lang.notification_private_message_body(
-          &inbox_link,
+          inbox_link,
           &content_slurs_removed,
           &local_recipient.person.name,
         ),
index d04343b0c2e145814147fa7d8c5d93c93b7028e2..370159e432d96494b2b047af50115e7ce4ffb734 100644 (file)
@@ -1,4 +1,4 @@
-use crate::PerformCrud;
+use crate::{check_application_question, PerformCrud};
 use activitypub_federation::core::signatures::generate_actor_keypair;
 use actix_web::web::Data;
 use lemmy_api_common::{
@@ -26,7 +26,7 @@ use lemmy_db_schema::{
 use lemmy_db_views::structs::SiteView;
 use lemmy_utils::{
   error::LemmyError,
-  utils::{check_application_question, check_slurs, check_slurs_opt},
+  utils::{check_slurs, check_slurs_opt},
   ConnectionId,
 };
 use url::Url;
@@ -69,7 +69,11 @@ impl PerformCrud for CreateSite {
     }
 
     let application_question = diesel_option_overwrite(&data.application_question);
-    check_application_question(&application_question, &data.require_application)?;
+    check_application_question(
+      &application_question,
+      &local_site,
+      &data.require_application,
+    )?;
 
     let actor_id: DbUrl = Url::parse(&context.settings().get_protocol_and_hostname())?.into();
     let inbox_url = Some(generate_site_inbox_url(&actor_id)?);
index 3909a297254913f8a251aaadce5275b6b1be4268..818d38753e8a9205022de71ca84c1cd7cd6b56ac 100644 (file)
@@ -1,4 +1,4 @@
-use crate::PerformCrud;
+use crate::{check_application_question, PerformCrud};
 use actix_web::web::Data;
 use lemmy_api_common::{
   context::LemmyContext,
@@ -28,11 +28,7 @@ use lemmy_db_schema::{
   ListingType,
 };
 use lemmy_db_views::structs::SiteView;
-use lemmy_utils::{
-  error::LemmyError,
-  utils::{check_application_question, check_slurs_opt},
-  ConnectionId,
-};
+use lemmy_utils::{error::LemmyError, utils::check_slurs_opt, ConnectionId};
 use std::str::FromStr;
 
 #[async_trait::async_trait(?Send)]
@@ -63,7 +59,11 @@ impl PerformCrud for EditSite {
     }
 
     let application_question = diesel_option_overwrite(&data.application_question);
-    check_application_question(&application_question, &data.require_application)?;
+    check_application_question(
+      &application_question,
+      &local_site,
+      &data.require_application,
+    )?;
 
     if let Some(default_post_listing_type) = &data.default_post_listing_type {
       // only allow all or local as default listing types
index 851be049771a1c129a2cb32b88da4d6b5d10b55d..f3c4289f3d4e1d530b7319983aaa672a87df333a 100644 (file)
@@ -100,20 +100,6 @@ pub(crate) fn slurs_vec_to_str(slurs: &[&str]) -> String {
   [start, combined].concat()
 }
 
-/// Make sure if applications are required, that there is an application questionnaire
-pub fn check_application_question(
-  application_question: &Option<Option<String>>,
-  require_application: &Option<bool>,
-) -> Result<(), LemmyError> {
-  if require_application.unwrap_or(false)
-    && application_question.as_ref().unwrap_or(&None).is_none()
-  {
-    Err(LemmyError::from_message("application_question_required"))
-  } else {
-    Ok(())
-  }
-}
-
 pub fn generate_random_string() -> String {
   thread_rng()
     .sample_iter(&Alphanumeric)