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.
This commit is contained in:
Nutomic 2022-12-23 01:31:39 +00:00 committed by GitHub
parent 9f52d4ae53
commit 0d4cdb579c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 25 deletions

View file

@ -1,5 +1,6 @@
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::context::LemmyContext; use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::source::local_site::LocalSite;
use lemmy_utils::{error::LemmyError, ConnectionId}; use lemmy_utils::{error::LemmyError, ConnectionId};
mod comment; mod comment;
@ -19,3 +20,18 @@ pub trait PerformCrud {
websocket_id: Option<ConnectionId>, websocket_id: Option<ConnectionId>,
) -> Result<Self::Response, LemmyError>; ) -> 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(())
}

View file

@ -98,7 +98,7 @@ impl PerformCrud for CreatePrivateMessage {
&local_recipient, &local_recipient,
&lang.notification_private_message_subject(&local_recipient.person.name), &lang.notification_private_message_subject(&local_recipient.person.name),
&lang.notification_private_message_body( &lang.notification_private_message_body(
&inbox_link, inbox_link,
&content_slurs_removed, &content_slurs_removed,
&local_recipient.person.name, &local_recipient.person.name,
), ),

View file

@ -1,4 +1,4 @@
use crate::PerformCrud; use crate::{check_application_question, PerformCrud};
use activitypub_federation::core::signatures::generate_actor_keypair; use activitypub_federation::core::signatures::generate_actor_keypair;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{
@ -26,7 +26,7 @@ use lemmy_db_schema::{
use lemmy_db_views::structs::SiteView; use lemmy_db_views::structs::SiteView;
use lemmy_utils::{ use lemmy_utils::{
error::LemmyError, error::LemmyError,
utils::{check_application_question, check_slurs, check_slurs_opt}, utils::{check_slurs, check_slurs_opt},
ConnectionId, ConnectionId,
}; };
use url::Url; use url::Url;
@ -69,7 +69,11 @@ impl PerformCrud for CreateSite {
} }
let application_question = diesel_option_overwrite(&data.application_question); 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 actor_id: DbUrl = Url::parse(&context.settings().get_protocol_and_hostname())?.into();
let inbox_url = Some(generate_site_inbox_url(&actor_id)?); let inbox_url = Some(generate_site_inbox_url(&actor_id)?);

View file

@ -1,4 +1,4 @@
use crate::PerformCrud; use crate::{check_application_question, PerformCrud};
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
@ -28,11 +28,7 @@ use lemmy_db_schema::{
ListingType, ListingType,
}; };
use lemmy_db_views::structs::SiteView; use lemmy_db_views::structs::SiteView;
use lemmy_utils::{ use lemmy_utils::{error::LemmyError, utils::check_slurs_opt, ConnectionId};
error::LemmyError,
utils::{check_application_question, check_slurs_opt},
ConnectionId,
};
use std::str::FromStr; use std::str::FromStr;
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
@ -63,7 +59,11 @@ impl PerformCrud for EditSite {
} }
let application_question = diesel_option_overwrite(&data.application_question); 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 { if let Some(default_post_listing_type) = &data.default_post_listing_type {
// only allow all or local as default listing types // only allow all or local as default listing types

View file

@ -100,20 +100,6 @@ pub(crate) fn slurs_vec_to_str(slurs: &[&str]) -> String {
[start, combined].concat() [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 { pub fn generate_random_string() -> String {
thread_rng() thread_rng()
.sample_iter(&Alphanumeric) .sample_iter(&Alphanumeric)