From 0d4cdb579cdbc6d02385b435c29d635a519d0fd9 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Fri, 23 Dec 2022 01:31:39 +0000 Subject: [PATCH] Improve application question check (#2628) 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 | 16 ++++++++++++++++ crates/api_crud/src/private_message/create.rs | 2 +- crates/api_crud/src/site/create.rs | 10 +++++++--- crates/api_crud/src/site/update.rs | 14 +++++++------- crates/utils/src/utils.rs | 14 -------------- 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/crates/api_crud/src/lib.rs b/crates/api_crud/src/lib.rs index d37dfbee..30c9ebd4 100644 --- a/crates/api_crud/src/lib.rs +++ b/crates/api_crud/src/lib.rs @@ -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, ) -> Result; } + +/// Make sure if applications are required, that there is an application questionnaire +pub fn check_application_question( + application_question: &Option>, + local_site: &LocalSite, + require_application: &Option, +) -> 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(()) +} diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index 25c7be46..6fa5f913 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -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, ), diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index d04343b0..370159e4 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -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)?); diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index 3909a297..818d3875 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -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 diff --git a/crates/utils/src/utils.rs b/crates/utils/src/utils.rs index 851be049..f3c4289f 100644 --- a/crates/utils/src/utils.rs +++ b/crates/utils/src/utils.rs @@ -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>, - require_application: &Option, -) -> 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) -- 2.44.1