1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 site::{EditSite, SiteResponse},
7 get_local_user_view_from_jwt,
9 local_site_to_slur_regex,
10 site_description_length_check,
13 use lemmy_db_schema::{
15 actor_language::SiteLanguage,
16 federation_allowlist::FederationAllowList,
17 federation_blocklist::FederationBlockList,
18 local_site::{LocalSite, LocalSiteUpdateForm},
19 local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitUpdateForm},
20 local_user::LocalUser,
21 site::{Site, SiteUpdateForm},
24 utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
27 use lemmy_db_views::structs::SiteView;
30 utils::{check_application_question, check_slurs_opt},
33 use lemmy_websocket::{messages::SendAllMessage, LemmyContext, UserOperationCrud};
34 use std::str::FromStr;
36 #[async_trait::async_trait(?Send)]
37 impl PerformCrud for EditSite {
38 type Response = SiteResponse;
40 #[tracing::instrument(skip(context, websocket_id))]
43 context: &Data<LemmyContext>,
44 websocket_id: Option<ConnectionId>,
45 ) -> Result<SiteResponse, LemmyError> {
46 let data: &EditSite = self;
48 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
49 let local_site = blocking(context.pool(), LocalSite::read).await??;
51 // Make sure user is an admin
52 is_admin(&local_user_view)?;
54 let slur_regex = local_site_to_slur_regex(&local_site);
56 check_slurs_opt(&data.name, &slur_regex)?;
57 check_slurs_opt(&data.description, &slur_regex)?;
59 if let Some(desc) = &data.description {
60 site_description_length_check(desc)?;
63 let application_question = diesel_option_overwrite(&data.application_question);
64 check_application_question(&application_question, &data.require_application)?;
66 if let Some(default_post_listing_type) = &data.default_post_listing_type {
67 // only allow all or local as default listing types
68 let val = ListingType::from_str(default_post_listing_type);
69 if val != Ok(ListingType::All) && val != Ok(ListingType::Local) {
70 return Err(LemmyError::from_message(
71 "invalid_default_post_listing_type",
76 let site_id = local_site.site_id;
77 if let Some(discussion_languages) = data.discussion_languages.clone() {
78 blocking(context.pool(), move |conn| {
79 SiteLanguage::update(conn, discussion_languages.clone(), site_id)
84 let name = data.name.to_owned();
85 let site_form = SiteUpdateForm::builder()
87 .sidebar(diesel_option_overwrite(&data.sidebar))
88 .description(diesel_option_overwrite(&data.description))
89 .icon(diesel_option_overwrite_to_url(&data.icon)?)
90 .banner(diesel_option_overwrite_to_url(&data.banner)?)
91 .updated(Some(Some(naive_now())))
94 blocking(context.pool(), move |conn| {
95 Site::update(conn, site_id, &site_form)
98 // Ignore errors for all these, so as to not throw errors if no update occurs
99 // Diesel will throw an error for empty update forms
102 let local_site_form = LocalSiteUpdateForm::builder()
103 .enable_downvotes(data.enable_downvotes)
104 .open_registration(data.open_registration)
105 .enable_nsfw(data.enable_nsfw)
106 .community_creation_admin_only(data.community_creation_admin_only)
107 .require_email_verification(data.require_email_verification)
108 .require_application(data.require_application)
109 .application_question(application_question)
110 .private_instance(data.private_instance)
111 .default_theme(data.default_theme.clone())
112 .default_post_listing_type(data.default_post_listing_type.clone())
113 .legal_information(diesel_option_overwrite(&data.legal_information))
114 .application_email_admins(data.application_email_admins)
115 .hide_modlog_mod_names(data.hide_modlog_mod_names)
116 .updated(Some(Some(naive_now())))
117 .slur_filter_regex(diesel_option_overwrite(&data.slur_filter_regex))
118 .actor_name_max_length(data.actor_name_max_length)
119 .federation_enabled(data.federation_enabled)
120 .federation_debug(data.federation_debug)
121 .federation_strict_allowlist(data.federation_strict_allowlist)
122 .federation_http_fetch_retry_limit(data.federation_http_fetch_retry_limit)
123 .federation_worker_count(data.federation_worker_count)
124 .captcha_enabled(data.captcha_enabled)
125 .captcha_difficulty(data.captcha_difficulty.to_owned())
128 let update_local_site = blocking(context.pool(), move |conn| {
129 LocalSite::update(conn, &local_site_form)
134 let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm::builder()
135 .message(data.rate_limit_message)
136 .message_per_second(data.rate_limit_message_per_second)
137 .post(data.rate_limit_post)
138 .post_per_second(data.rate_limit_post_per_second)
139 .register(data.rate_limit_register)
140 .register_per_second(data.rate_limit_register_per_second)
141 .image(data.rate_limit_image)
142 .image_per_second(data.rate_limit_image_per_second)
143 .comment(data.rate_limit_comment)
144 .comment_per_second(data.rate_limit_comment_per_second)
145 .search(data.rate_limit_search)
146 .search_per_second(data.rate_limit_search_per_second)
149 blocking(context.pool(), move |conn| {
150 LocalSiteRateLimit::update(conn, &local_site_rate_limit_form)
155 // Replace the blocked and allowed instances
156 let allowed = data.allowed_instances.to_owned();
157 blocking(context.pool(), move |conn| {
158 FederationAllowList::replace(conn, allowed)
161 let blocked = data.blocked_instances.to_owned();
162 blocking(context.pool(), move |conn| {
163 FederationBlockList::replace(conn, blocked)
167 // TODO can't think of a better way to do this.
168 // If the server suddenly requires email verification, or required applications, no old users
169 // will be able to log in. It really only wants this to be a requirement for NEW signups.
170 // So if it was set from false, to true, you need to update all current users columns to be verified.
172 let new_require_application = update_local_site
177 .map(|ls| ls.require_application)
181 if !local_site.require_application && new_require_application {
182 blocking(context.pool(), move |conn| {
183 LocalUser::set_all_users_registration_applications_accepted(conn)
186 .map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_registrations_accepted"))?;
189 let new_require_email_verification = update_local_site
194 .map(|ls| ls.require_email_verification)
198 if !local_site.require_email_verification && new_require_email_verification {
199 blocking(context.pool(), move |conn| {
200 LocalUser::set_all_users_email_verified(conn)
203 .map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_email_verified"))?;
206 let site_view = blocking(context.pool(), SiteView::read_local).await??;
208 let res = SiteResponse { site_view };
210 context.chat_server().do_send(SendAllMessage {
211 op: UserOperationCrud::EditSite,
212 response: res.clone(),