1 use crate::PerformCrud;
2 use activitypub_federation::http_signatures::generate_actor_keypair;
3 use actix_web::web::Data;
4 use lemmy_api_common::{
5 build_response::build_community_response,
6 community::{CommunityResponse, CreateCommunity},
9 generate_followers_url,
11 generate_local_apub_endpoint,
12 generate_shared_inbox_url,
14 local_site_to_slur_regex,
15 local_user_view_from_jwt,
21 use lemmy_db_schema::{
23 actor_language::{CommunityLanguage, SiteLanguage},
27 CommunityFollowerForm,
30 CommunityModeratorForm,
33 traits::{ApubActor, Crud, Followable, Joinable},
34 utils::diesel_option_overwrite_to_url_create,
36 use lemmy_db_views::structs::SiteView;
38 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
40 slurs::{check_slurs, check_slurs_opt},
41 validation::{is_valid_actor_name, is_valid_body_field},
45 #[async_trait::async_trait(?Send)]
46 impl PerformCrud for CreateCommunity {
47 type Response = CommunityResponse;
49 #[tracing::instrument(skip(context))]
50 async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommunityResponse, LemmyError> {
51 let data: &CreateCommunity = self;
52 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
53 let site_view = SiteView::read_local(&mut context.pool()).await?;
54 let local_site = site_view.local_site;
56 if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
57 return Err(LemmyErrorType::OnlyAdminsCanCreateCommunities)?;
60 // Check to make sure the icon and banners are urls
61 let icon = diesel_option_overwrite_to_url_create(&data.icon)?;
62 let banner = diesel_option_overwrite_to_url_create(&data.banner)?;
64 let name = sanitize_html(&data.name);
65 let title = sanitize_html(&data.title);
66 let description = sanitize_html_opt(&data.description);
68 let slur_regex = local_site_to_slur_regex(&local_site);
69 check_slurs(&name, &slur_regex)?;
70 check_slurs(&title, &slur_regex)?;
71 check_slurs_opt(&description, &slur_regex)?;
73 is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
74 is_valid_body_field(&data.description, false)?;
76 // Double check for duplicate community actor_ids
77 let community_actor_id = generate_local_apub_endpoint(
78 EndpointType::Community,
80 &context.settings().get_protocol_and_hostname(),
83 Community::read_from_apub_id(&mut context.pool(), &community_actor_id).await?;
84 if community_dupe.is_some() {
85 return Err(LemmyErrorType::CommunityAlreadyExists)?;
88 // When you create a community, make sure the user becomes a moderator and a follower
89 let keypair = generate_actor_keypair()?;
91 let community_form = CommunityInsertForm::builder()
94 .description(description)
98 .actor_id(Some(community_actor_id.clone()))
99 .private_key(Some(keypair.private_key))
100 .public_key(keypair.public_key)
101 .followers_url(Some(generate_followers_url(&community_actor_id)?))
102 .inbox_url(Some(generate_inbox_url(&community_actor_id)?))
103 .shared_inbox_url(Some(generate_shared_inbox_url(&community_actor_id)?))
104 .posting_restricted_to_mods(data.posting_restricted_to_mods)
105 .instance_id(site_view.site.instance_id)
108 let inserted_community = Community::create(&mut context.pool(), &community_form)
110 .with_lemmy_type(LemmyErrorType::CommunityAlreadyExists)?;
112 // The community creator becomes a moderator
113 let community_moderator_form = CommunityModeratorForm {
114 community_id: inserted_community.id,
115 person_id: local_user_view.person.id,
118 CommunityModerator::join(&mut context.pool(), &community_moderator_form)
120 .with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?;
122 // Follow your own community
123 let community_follower_form = CommunityFollowerForm {
124 community_id: inserted_community.id,
125 person_id: local_user_view.person.id,
129 CommunityFollower::follow(&mut context.pool(), &community_follower_form)
131 .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
133 // Update the discussion_languages if that's provided
134 let community_id = inserted_community.id;
135 if let Some(languages) = data.discussion_languages.clone() {
136 let site_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?;
137 // check that community languages are a subset of site languages
138 // https://stackoverflow.com/a/64227550
139 let is_subset = languages.iter().all(|item| site_languages.contains(item));
141 return Err(LemmyErrorType::LanguageNotAllowed)?;
143 CommunityLanguage::update(&mut context.pool(), languages, community_id).await?;
146 build_community_response(context, local_user_view, community_id).await