1 use crate::{site::check_application_question, PerformCrud};
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 site::{EditSite, SiteResponse},
7 get_local_user_view_from_jwt,
9 local_site_rate_limit_to_rate_limit_config,
10 local_site_to_slur_regex,
11 site_description_length_check,
13 websocket::UserOperationCrud,
15 use lemmy_db_schema::{
17 actor_language::SiteLanguage,
18 federation_allowlist::FederationAllowList,
19 federation_blocklist::FederationBlockList,
20 local_site::{LocalSite, LocalSiteUpdateForm, RegistrationMode},
21 local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitUpdateForm},
22 local_user::LocalUser,
23 site::{Site, SiteUpdateForm},
27 utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
30 use lemmy_db_views::structs::SiteView;
31 use lemmy_utils::{error::LemmyError, utils::slurs::check_slurs_opt, ConnectionId};
32 use std::str::FromStr;
34 #[async_trait::async_trait(?Send)]
35 impl PerformCrud for EditSite {
36 type Response = SiteResponse;
38 #[tracing::instrument(skip(context, websocket_id))]
41 context: &Data<LemmyContext>,
42 websocket_id: Option<ConnectionId>,
43 ) -> Result<SiteResponse, LemmyError> {
44 let data: &EditSite = self;
46 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
47 let local_site = LocalSite::read(context.pool()).await?;
49 // Make sure user is an admin
50 is_admin(&local_user_view)?;
52 let slur_regex = local_site_to_slur_regex(&local_site);
54 check_slurs_opt(&data.name, &slur_regex)?;
55 check_slurs_opt(&data.description, &slur_regex)?;
57 if let Some(desc) = &data.description {
58 site_description_length_check(desc)?;
61 let application_question = diesel_option_overwrite(&data.application_question);
62 check_application_question(
63 &application_question,
66 .unwrap_or(local_site.registration_mode),
69 if let Some(default_post_listing_type) = &data.default_post_listing_type {
70 // only allow all or local as default listing types
71 let val = ListingType::from_str(default_post_listing_type);
72 if val != Ok(ListingType::All) && val != Ok(ListingType::Local) {
73 return Err(LemmyError::from_message(
74 "invalid_default_post_listing_type",
79 let site_id = local_site.site_id;
80 if let Some(discussion_languages) = data.discussion_languages.clone() {
81 let site = Site::read(context.pool(), site_id).await?;
82 SiteLanguage::update(context.pool(), discussion_languages.clone(), &site).await?;
85 let name = data.name.clone();
86 let site_form = SiteUpdateForm::builder()
88 .sidebar(diesel_option_overwrite(&data.sidebar))
89 .description(diesel_option_overwrite(&data.description))
90 .icon(diesel_option_overwrite_to_url(&data.icon)?)
91 .banner(diesel_option_overwrite_to_url(&data.banner)?)
92 .updated(Some(Some(naive_now())))
95 Site::update(context.pool(), site_id, &site_form)
97 // Ignore errors for all these, so as to not throw errors if no update occurs
98 // Diesel will throw an error for empty update forms
101 let local_site_form = LocalSiteUpdateForm::builder()
102 .enable_downvotes(data.enable_downvotes)
103 .registration_mode(data.registration_mode)
104 .enable_nsfw(data.enable_nsfw)
105 .community_creation_admin_only(data.community_creation_admin_only)
106 .require_email_verification(data.require_email_verification)
107 .application_question(application_question)
108 .private_instance(data.private_instance)
109 .default_theme(data.default_theme.clone())
110 .default_post_listing_type(data.default_post_listing_type.clone())
111 .legal_information(diesel_option_overwrite(&data.legal_information))
112 .application_email_admins(data.application_email_admins)
113 .hide_modlog_mod_names(data.hide_modlog_mod_names)
114 .updated(Some(Some(naive_now())))
115 .slur_filter_regex(diesel_option_overwrite(&data.slur_filter_regex))
116 .actor_name_max_length(data.actor_name_max_length)
117 .federation_enabled(data.federation_enabled)
118 .federation_debug(data.federation_debug)
119 .federation_worker_count(data.federation_worker_count)
120 .captcha_enabled(data.captcha_enabled)
121 .captcha_difficulty(data.captcha_difficulty.clone())
122 .reports_email_admins(data.reports_email_admins)
125 let update_local_site = LocalSite::update(context.pool(), &local_site_form)
129 let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm::builder()
130 .message(data.rate_limit_message)
131 .message_per_second(data.rate_limit_message_per_second)
132 .post(data.rate_limit_post)
133 .post_per_second(data.rate_limit_post_per_second)
134 .register(data.rate_limit_register)
135 .register_per_second(data.rate_limit_register_per_second)
136 .image(data.rate_limit_image)
137 .image_per_second(data.rate_limit_image_per_second)
138 .comment(data.rate_limit_comment)
139 .comment_per_second(data.rate_limit_comment_per_second)
140 .search(data.rate_limit_search)
141 .search_per_second(data.rate_limit_search_per_second)
144 LocalSiteRateLimit::update(context.pool(), &local_site_rate_limit_form)
148 // Replace the blocked and allowed instances
149 let allowed = data.allowed_instances.clone();
150 FederationAllowList::replace(context.pool(), allowed).await?;
151 let blocked = data.blocked_instances.clone();
152 FederationBlockList::replace(context.pool(), blocked).await?;
154 // TODO can't think of a better way to do this.
155 // If the server suddenly requires email verification, or required applications, no old users
156 // will be able to log in. It really only wants this to be a requirement for NEW signups.
157 // So if it was set from false, to true, you need to update all current users columns to be verified.
159 let old_require_application =
160 local_site.registration_mode == RegistrationMode::RequireApplication;
161 let new_require_application = update_local_site
163 .map(|ols| ols.registration_mode == RegistrationMode::RequireApplication)
165 if !old_require_application && new_require_application {
166 LocalUser::set_all_users_registration_applications_accepted(context.pool())
168 .map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_registrations_accepted"))?;
171 let new_require_email_verification = update_local_site
173 .map(|ols| ols.require_email_verification)
175 if !local_site.require_email_verification && new_require_email_verification {
176 LocalUser::set_all_users_email_verified(context.pool())
178 .map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_email_verified"))?;
181 let taglines = data.taglines.clone();
182 Tagline::replace(context.pool(), local_site.id, taglines).await?;
184 let site_view = SiteView::read_local(context.pool()).await?;
186 let rate_limit_config =
187 local_site_rate_limit_to_rate_limit_config(&site_view.local_site_rate_limit);
189 .settings_updated_channel()
190 .send(rate_limit_config)
193 let res = SiteResponse { site_view };
197 .send_all_message(UserOperationCrud::EditSite, &res, websocket_id)