use crate::PerformCrud;
use actix_web::web::Data;
use lemmy_api_common::{
- blocking,
- check_image_has_local_domain,
- get_local_user_view_from_jwt,
- is_admin,
site::{EditSite, SiteResponse},
- site_description_length_check,
+ utils::{blocking, get_local_user_view_from_jwt, is_admin, site_description_length_check},
};
use lemmy_db_schema::{
- diesel_option_overwrite,
- diesel_option_overwrite_to_url,
- naive_now,
source::{
local_user::LocalUser,
site::{Site, SiteForm},
},
traits::Crud,
+ utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
+ ListingType,
};
-use lemmy_db_views::site_view::SiteView;
-use lemmy_utils::{utils::check_slurs_opt, ConnectionId, LemmyError};
+use lemmy_db_views::structs::SiteView;
+use lemmy_utils::{error::LemmyError, utils::check_slurs_opt, ConnectionId};
use lemmy_websocket::{messages::SendAllMessage, LemmyContext, UserOperationCrud};
-use std::default::Default;
+use std::{default::Default, str::FromStr};
#[async_trait::async_trait(?Send)]
impl PerformCrud for EditSite {
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
- check_slurs_opt(&data.name, &context.settings().slur_regex())?;
- check_slurs_opt(&data.description, &context.settings().slur_regex())?;
- check_image_has_local_domain(&data.icon)?;
- check_image_has_local_domain(&data.banner)?;
-
// Make sure user is an admin
is_admin(&local_user_view)?;
let sidebar = diesel_option_overwrite(&data.sidebar);
let description = diesel_option_overwrite(&data.description);
let application_question = diesel_option_overwrite(&data.application_question);
+ let legal_information = diesel_option_overwrite(&data.legal_information);
let icon = diesel_option_overwrite_to_url(&data.icon)?;
let banner = diesel_option_overwrite_to_url(&data.banner)?;
+ check_slurs_opt(&data.name, &context.settings().slur_regex())?;
+ check_slurs_opt(&data.description, &context.settings().slur_regex())?;
+
if let Some(Some(desc)) = &description {
site_description_length_check(desc)?;
}
+ // Make sure if applications are required, that there is an application questionnaire
+ if data.require_application.unwrap_or(false)
+ && application_question.as_ref().unwrap_or(&None).is_none()
+ {
+ return Err(LemmyError::from_message("application_question_required"));
+ }
+
+ if let Some(default_post_listing_type) = &data.default_post_listing_type {
+ // only allow all or local as default listing types
+ let val = ListingType::from_str(default_post_listing_type);
+ if val != Ok(ListingType::All) && val != Ok(ListingType::Local) {
+ return Err(LemmyError::from_message(
+ "invalid_default_post_listing_type",
+ ));
+ }
+ }
+
let site_form = SiteForm {
name: data.name.to_owned().unwrap_or(local_site.name),
sidebar,
application_question,
private_instance: data.private_instance,
default_theme: data.default_theme.clone(),
+ default_post_listing_type: data.default_post_listing_type.clone(),
+ legal_information,
+ application_email_admins: data.application_email_admins,
+ hide_modlog_mod_names: data.hide_modlog_mod_names,
..SiteForm::default()
};