2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::core::signatures::generate_actor_keypair;
|
2021-03-25 19:19:40 +00:00
|
|
|
use actix_web::web::Data;
|
2021-04-07 11:40:35 +00:00
|
|
|
use lemmy_api_common::{
|
2022-05-03 17:44:13 +00:00
|
|
|
site::{CreateSite, 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
|
|
|
};
|
2022-02-07 19:23:12 +00:00
|
|
|
use lemmy_apub::generate_site_inbox_url;
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-02-07 19:23:12 +00:00
|
|
|
newtypes::DbUrl,
|
2021-10-16 13:33:38 +00:00
|
|
|
source::site::{Site, SiteForm},
|
|
|
|
traits::Crud,
|
2022-05-03 17:44:13 +00:00
|
|
|
utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
|
2021-04-07 11:40:35 +00:00
|
|
|
};
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views::structs::SiteView;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_utils::{
|
2022-06-02 14:33:41 +00:00
|
|
|
error::LemmyError,
|
2021-03-25 19:19:40 +00:00
|
|
|
utils::{check_slurs, check_slurs_opt},
|
|
|
|
ConnectionId,
|
|
|
|
};
|
|
|
|
use lemmy_websocket::LemmyContext;
|
2022-02-07 19:23:12 +00:00
|
|
|
use url::Url;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for CreateSite {
|
|
|
|
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: &CreateSite = self;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2022-10-06 18:27:58 +00:00
|
|
|
let read_site = Site::read_local;
|
2021-03-25 19:19:40 +00:00
|
|
|
if blocking(context.pool(), read_site).await?.is_ok() {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("site_already_exists"));
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2022-04-04 20:23:18 +00:00
|
|
|
let sidebar = diesel_option_overwrite(&data.sidebar);
|
|
|
|
let description = diesel_option_overwrite(&data.description);
|
|
|
|
let icon = diesel_option_overwrite_to_url(&data.icon)?;
|
|
|
|
let banner = diesel_option_overwrite_to_url(&data.banner)?;
|
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
check_slurs(&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-04-07 11:40:35 +00:00
|
|
|
if let Some(Some(desc)) = &description {
|
|
|
|
site_description_length_check(desc)?;
|
|
|
|
}
|
|
|
|
|
2022-06-22 20:24:54 +00:00
|
|
|
let actor_id: DbUrl = Url::parse(&context.settings().get_protocol_and_hostname())?.into();
|
2022-02-07 19:23:12 +00:00
|
|
|
let inbox_url = Some(generate_site_inbox_url(&actor_id)?);
|
|
|
|
let keypair = generate_actor_keypair()?;
|
2021-03-25 19:19:40 +00:00
|
|
|
let site_form = SiteForm {
|
|
|
|
name: data.name.to_owned(),
|
2021-04-07 11:40:35 +00:00
|
|
|
sidebar,
|
|
|
|
description,
|
|
|
|
icon,
|
|
|
|
banner,
|
2021-03-25 19:19:40 +00:00
|
|
|
enable_downvotes: data.enable_downvotes,
|
|
|
|
open_registration: data.open_registration,
|
|
|
|
enable_nsfw: data.enable_nsfw,
|
2021-04-23 06:40:10 +00:00
|
|
|
community_creation_admin_only: data.community_creation_admin_only,
|
2022-02-07 19:23:12 +00:00
|
|
|
actor_id: Some(actor_id),
|
|
|
|
last_refreshed_at: Some(naive_now()),
|
|
|
|
inbox_url,
|
|
|
|
private_key: Some(Some(keypair.private_key)),
|
|
|
|
public_key: Some(keypair.public_key),
|
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-09-27 16:48:44 +00:00
|
|
|
application_email_admins: data.application_email_admins,
|
2022-08-16 11:52:04 +00:00
|
|
|
hide_modlog_mod_names: data.hide_modlog_mod_names,
|
2021-12-15 19:49:59 +00:00
|
|
|
..SiteForm::default()
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
|
|
|
|
2022-09-26 14:09:32 +00:00
|
|
|
let create_site = move |conn: &mut _| Site::create(conn, &site_form);
|
2022-03-16 20:11:49 +00:00
|
|
|
blocking(context.pool(), create_site)
|
|
|
|
.await?
|
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "site_already_exists"))?;
|
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
|
|
|
|
|
|
|
Ok(SiteResponse { site_view })
|
|
|
|
}
|
|
|
|
}
|