1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 get_local_user_view_from_jwt,
8 site_description_length_check,
10 use lemmy_apub::generate_site_inbox_url;
11 use lemmy_db_schema::{
12 diesel_option_overwrite,
13 diesel_option_overwrite_to_url,
16 source::site::{Site, SiteForm},
19 use lemmy_db_views::site_view::SiteView;
21 apub::generate_actor_keypair,
22 settings::structs::Settings,
23 utils::{check_slurs, check_slurs_opt},
27 use lemmy_websocket::LemmyContext;
30 #[async_trait::async_trait(?Send)]
31 impl PerformCrud for CreateSite {
32 type Response = SiteResponse;
34 #[tracing::instrument(skip(context, _websocket_id))]
37 context: &Data<LemmyContext>,
38 _websocket_id: Option<ConnectionId>,
39 ) -> Result<SiteResponse, LemmyError> {
40 let data: &CreateSite = self;
42 let read_site = Site::read_local_site;
43 if blocking(context.pool(), read_site).await?.is_ok() {
44 return Err(LemmyError::from_message("site_already_exists"));
48 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
50 check_slurs(&data.name, &context.settings().slur_regex())?;
51 check_slurs_opt(&data.description, &context.settings().slur_regex())?;
53 // Make sure user is an admin
54 is_admin(&local_user_view)?;
56 let sidebar = diesel_option_overwrite(&data.sidebar);
57 let description = diesel_option_overwrite(&data.description);
58 let icon = diesel_option_overwrite_to_url(&data.icon)?;
59 let banner = diesel_option_overwrite_to_url(&data.banner)?;
61 if let Some(Some(desc)) = &description {
62 site_description_length_check(desc)?;
65 let actor_id: DbUrl = Url::parse(&Settings::get().get_protocol_and_hostname())?.into();
66 let inbox_url = Some(generate_site_inbox_url(&actor_id)?);
67 let keypair = generate_actor_keypair()?;
68 let site_form = SiteForm {
69 name: data.name.to_owned(),
74 enable_downvotes: data.enable_downvotes,
75 open_registration: data.open_registration,
76 enable_nsfw: data.enable_nsfw,
77 community_creation_admin_only: data.community_creation_admin_only,
78 actor_id: Some(actor_id),
79 last_refreshed_at: Some(naive_now()),
81 private_key: Some(Some(keypair.private_key)),
82 public_key: Some(keypair.public_key),
83 default_theme: data.default_theme.clone(),
87 let create_site = move |conn: &'_ _| Site::create(conn, &site_form);
88 if blocking(context.pool(), create_site).await?.is_err() {
89 return Err(LemmyError::from_message("site_already_exists"));
92 let site_view = blocking(context.pool(), SiteView::read_local).await??;
94 Ok(SiteResponse { site_view })