2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
|
|
|
site::{EditSite, SiteResponse},
|
2022-05-10 17:08:13 +00:00
|
|
|
utils::{blocking, get_local_user_view_from_jwt, is_admin, site_description_length_check},
|
2021-04-07 11:40:35 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2021-12-15 19:49:59 +00:00
|
|
|
source::{
|
|
|
|
local_user::LocalUser,
|
|
|
|
site::{Site, SiteForm},
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
2022-05-06 20:55:07 +00:00
|
|
|
utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
|
|
|
|
ListingType,
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views::structs::SiteView;
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::{error::LemmyError, utils::check_slurs_opt, ConnectionId};
|
2021-03-25 19:30:15 +00:00
|
|
|
use lemmy_websocket::{messages::SendAllMessage, LemmyContext, UserOperationCrud};
|
2022-04-19 19:05:08 +00:00
|
|
|
use std::{default::Default, str::FromStr};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for EditSite {
|
|
|
|
type Response = SiteResponse;
|
2021-12-06 14:54:47 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip(context, websocket_id))]
|
2021-03-25 19:19:40 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<SiteResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &EditSite = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Make sure user is an admin
|
|
|
|
is_admin(&local_user_view)?;
|
|
|
|
|
2022-03-01 17:01:23 +00:00
|
|
|
let local_site = blocking(context.pool(), Site::read_local_site).await??;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-04-07 11:40:35 +00:00
|
|
|
let sidebar = diesel_option_overwrite(&data.sidebar);
|
|
|
|
let description = diesel_option_overwrite(&data.description);
|
2021-12-15 19:49:59 +00:00
|
|
|
let application_question = diesel_option_overwrite(&data.application_question);
|
2021-03-25 19:19:40 +00:00
|
|
|
let icon = diesel_option_overwrite_to_url(&data.icon)?;
|
|
|
|
let banner = diesel_option_overwrite_to_url(&data.banner)?;
|
|
|
|
|
2022-04-04 20:23:18 +00:00
|
|
|
check_slurs_opt(&data.name, &context.settings().slur_regex())?;
|
|
|
|
check_slurs_opt(&data.description, &context.settings().slur_regex())?;
|
|
|
|
|
2021-04-07 11:40:35 +00:00
|
|
|
if let Some(Some(desc)) = &description {
|
|
|
|
site_description_length_check(desc)?;
|
|
|
|
}
|
|
|
|
|
2022-04-04 20:23:18 +00:00
|
|
|
// 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"));
|
|
|
|
}
|
|
|
|
|
2022-04-19 19:05:08 +00:00
|
|
|
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",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
let site_form = SiteForm {
|
2022-03-01 17:01:23 +00:00
|
|
|
name: data.name.to_owned().unwrap_or(local_site.name),
|
2021-04-07 11:40:35 +00:00
|
|
|
sidebar,
|
|
|
|
description,
|
2021-03-25 19:19:40 +00:00
|
|
|
icon,
|
|
|
|
banner,
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
enable_downvotes: data.enable_downvotes,
|
|
|
|
open_registration: data.open_registration,
|
|
|
|
enable_nsfw: data.enable_nsfw,
|
2021-04-22 23:42:58 +00:00
|
|
|
community_creation_admin_only: data.community_creation_admin_only,
|
2021-12-15 19:49:59 +00:00
|
|
|
require_email_verification: data.require_email_verification,
|
|
|
|
require_application: data.require_application,
|
|
|
|
application_question,
|
|
|
|
private_instance: data.private_instance,
|
2022-02-23 16:40:36 +00:00
|
|
|
default_theme: data.default_theme.clone(),
|
2022-04-19 19:05:08 +00:00
|
|
|
default_post_listing_type: data.default_post_listing_type.clone(),
|
2022-05-19 20:32:40 +00:00
|
|
|
legal_information: data.legal_information.clone(),
|
2022-02-07 19:23:12 +00:00
|
|
|
..SiteForm::default()
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
|
|
|
|
2021-12-15 19:49:59 +00:00
|
|
|
let update_site = blocking(context.pool(), move |conn| {
|
2022-03-01 17:01:23 +00:00
|
|
|
Site::update(conn, local_site.id, &site_form)
|
2021-12-15 19:49:59 +00:00
|
|
|
})
|
|
|
|
.await?
|
2022-03-16 20:11:49 +00:00
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_site"))?;
|
2021-12-15 19:49:59 +00:00
|
|
|
|
|
|
|
// TODO can't think of a better way to do this.
|
|
|
|
// If the server suddenly requires email verification, or required applications, no old users
|
|
|
|
// will be able to log in. It really only wants this to be a requirement for NEW signups.
|
|
|
|
// So if it was set from false, to true, you need to update all current users columns to be verified.
|
|
|
|
|
2022-03-01 17:01:23 +00:00
|
|
|
if !local_site.require_application && update_site.require_application {
|
2021-12-15 19:49:59 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
LocalUser::set_all_users_registration_applications_accepted(conn)
|
|
|
|
})
|
2021-10-13 19:50:21 +00:00
|
|
|
.await?
|
2022-03-16 20:11:49 +00:00
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_registrations_accepted"))?;
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 17:01:23 +00:00
|
|
|
if !local_site.require_email_verification && update_site.require_email_verification {
|
2021-12-15 19:49:59 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
LocalUser::set_all_users_email_verified(conn)
|
|
|
|
})
|
|
|
|
.await?
|
2022-03-16 20:11:49 +00:00
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_email_verified"))?;
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2022-03-08 12:52:33 +00:00
|
|
|
let site_view = blocking(context.pool(), SiteView::read_local).await??;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
let res = SiteResponse { site_view };
|
|
|
|
|
|
|
|
context.chat_server().do_send(SendAllMessage {
|
2021-03-25 19:30:15 +00:00
|
|
|
op: UserOperationCrud::EditSite,
|
2021-03-25 19:19:40 +00:00
|
|
|
response: res.clone(),
|
|
|
|
websocket_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|