2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
|
|
|
blocking,
|
|
|
|
get_local_user_view_from_jwt,
|
|
|
|
is_admin,
|
|
|
|
site::{EditSite, SiteResponse},
|
2021-04-07 11:40:35 +00:00
|
|
|
site_description_length_check,
|
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2021-04-07 11:40:35 +00:00
|
|
|
diesel_option_overwrite,
|
|
|
|
diesel_option_overwrite_to_url,
|
2021-03-25 19:19:40 +00:00
|
|
|
naive_now,
|
|
|
|
source::site::{Site, SiteForm},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_views::site_view::SiteView;
|
2021-04-15 03:37:51 +00:00
|
|
|
use lemmy_utils::{utils::check_slurs_opt, ApiError, ConnectionId, LemmyError};
|
2021-03-25 19:30:15 +00:00
|
|
|
use lemmy_websocket::{messages::SendAllMessage, LemmyContext, UserOperationCrud};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for EditSite {
|
|
|
|
type Response = SiteResponse;
|
|
|
|
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
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
check_slurs_opt(&data.name, &context.settings().slur_regex())?;
|
|
|
|
check_slurs_opt(&data.description, &context.settings().slur_regex())?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Make sure user is an admin
|
|
|
|
is_admin(&local_user_view)?;
|
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
let found_site = blocking(context.pool(), Site::read_simple).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-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)?;
|
|
|
|
|
2021-04-07 11:40:35 +00:00
|
|
|
if let Some(Some(desc)) = &description {
|
|
|
|
site_description_length_check(desc)?;
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
let site_form = SiteForm {
|
2021-04-15 03:37:51 +00:00
|
|
|
creator_id: found_site.creator_id,
|
|
|
|
name: data.name.to_owned().unwrap_or(found_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-03-25 19:19:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let update_site = move |conn: &'_ _| Site::update(conn, 1, &site_form);
|
2021-10-13 19:50:21 +00:00
|
|
|
blocking(context.pool(), update_site)
|
|
|
|
.await?
|
|
|
|
.map_err(|e| ApiError::err("couldnt_update_site", e))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-10-12 16:46:26 +00:00
|
|
|
let site_view = blocking(context.pool(), SiteView::read).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)
|
|
|
|
}
|
|
|
|
}
|