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,
19 use lemmy_db_schema::{
21 actor_language::{CommunityLanguage, SiteLanguage},
25 CommunityFollowerForm,
28 CommunityModeratorForm,
31 traits::{ApubActor, Crud, Followable, Joinable},
32 utils::diesel_option_overwrite_to_url_create,
34 use lemmy_db_views::structs::SiteView;
38 slurs::{check_slurs, check_slurs_opt},
39 validation::{is_valid_actor_name, is_valid_body_field},
43 #[async_trait::async_trait(?Send)]
44 impl PerformCrud for CreateCommunity {
45 type Response = CommunityResponse;
47 #[tracing::instrument(skip(context))]
48 async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommunityResponse, LemmyError> {
49 let data: &CreateCommunity = self;
50 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
51 let site_view = SiteView::read_local(context.pool()).await?;
52 let local_site = site_view.local_site;
54 if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
55 return Err(LemmyError::from_message(
56 "only_admins_can_create_communities",
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 slur_regex = local_site_to_slur_regex(&local_site);
65 check_slurs(&data.name, &slur_regex)?;
66 check_slurs(&data.title, &slur_regex)?;
67 check_slurs_opt(&data.description, &slur_regex)?;
69 is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
70 is_valid_body_field(&data.description, false)?;
72 // Double check for duplicate community actor_ids
73 let community_actor_id = generate_local_apub_endpoint(
74 EndpointType::Community,
76 &context.settings().get_protocol_and_hostname(),
78 let community_dupe = Community::read_from_apub_id(context.pool(), &community_actor_id).await?;
79 if community_dupe.is_some() {
80 return Err(LemmyError::from_message("community_already_exists"));
83 // When you create a community, make sure the user becomes a moderator and a follower
84 let keypair = generate_actor_keypair()?;
86 let community_form = CommunityInsertForm::builder()
87 .name(data.name.clone())
88 .title(data.title.clone())
89 .description(data.description.clone())
93 .actor_id(Some(community_actor_id.clone()))
94 .private_key(Some(keypair.private_key))
95 .public_key(keypair.public_key)
96 .followers_url(Some(generate_followers_url(&community_actor_id)?))
97 .inbox_url(Some(generate_inbox_url(&community_actor_id)?))
98 .shared_inbox_url(Some(generate_shared_inbox_url(&community_actor_id)?))
99 .posting_restricted_to_mods(data.posting_restricted_to_mods)
100 .instance_id(site_view.site.instance_id)
103 let inserted_community = Community::create(context.pool(), &community_form)
105 .map_err(|e| LemmyError::from_error_message(e, "community_already_exists"))?;
107 // The community creator becomes a moderator
108 let community_moderator_form = CommunityModeratorForm {
109 community_id: inserted_community.id,
110 person_id: local_user_view.person.id,
113 CommunityModerator::join(context.pool(), &community_moderator_form)
115 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
117 // Follow your own community
118 let community_follower_form = CommunityFollowerForm {
119 community_id: inserted_community.id,
120 person_id: local_user_view.person.id,
124 CommunityFollower::follow(context.pool(), &community_follower_form)
126 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
128 // Update the discussion_languages if that's provided
129 let community_id = inserted_community.id;
130 if let Some(languages) = data.discussion_languages.clone() {
131 let site_languages = SiteLanguage::read_local_raw(context.pool()).await?;
132 // check that community languages are a subset of site languages
133 // https://stackoverflow.com/a/64227550
134 let is_subset = languages.iter().all(|item| site_languages.contains(item));
136 return Err(LemmyError::from_message("language_not_allowed"));
138 CommunityLanguage::update(context.pool(), languages, community_id).await?;
141 build_community_response(context, local_user_view, community_id).await