1 use activitypub_federation::{config::Data, http_signatures::generate_actor_keypair};
2 use actix_web::web::Json;
3 use lemmy_api_common::{
4 build_response::build_community_response,
5 community::{CommunityResponse, CreateCommunity},
8 generate_followers_url,
10 generate_local_apub_endpoint,
11 generate_shared_inbox_url,
13 local_site_to_slur_regex,
14 local_user_view_from_jwt,
20 use lemmy_db_schema::{
22 actor_language::{CommunityLanguage, SiteLanguage},
26 CommunityFollowerForm,
29 CommunityModeratorForm,
32 traits::{ApubActor, Crud, Followable, Joinable},
33 utils::diesel_option_overwrite_to_url_create,
35 use lemmy_db_views::structs::SiteView;
37 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
39 slurs::{check_slurs, check_slurs_opt},
40 validation::{is_valid_actor_name, is_valid_body_field},
44 #[tracing::instrument(skip(context))]
45 pub async fn create_community(
46 data: Json<CreateCommunity>,
47 context: Data<LemmyContext>,
48 ) -> Result<Json<CommunityResponse>, LemmyError> {
49 let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
50 let site_view = SiteView::read_local(&mut context.pool()).await?;
51 let local_site = site_view.local_site;
53 if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
54 return Err(LemmyErrorType::OnlyAdminsCanCreateCommunities)?;
57 // Check to make sure the icon and banners are urls
58 let icon = diesel_option_overwrite_to_url_create(&data.icon)?;
59 let banner = diesel_option_overwrite_to_url_create(&data.banner)?;
61 let name = sanitize_html(&data.name);
62 let title = sanitize_html(&data.title);
63 let description = sanitize_html_opt(&data.description);
65 let slur_regex = local_site_to_slur_regex(&local_site);
66 check_slurs(&name, &slur_regex)?;
67 check_slurs(&title, &slur_regex)?;
68 check_slurs_opt(&description, &slur_regex)?;
70 is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
71 is_valid_body_field(&data.description, false)?;
73 // Double check for duplicate community actor_ids
74 let community_actor_id = generate_local_apub_endpoint(
75 EndpointType::Community,
77 &context.settings().get_protocol_and_hostname(),
80 Community::read_from_apub_id(&mut context.pool(), &community_actor_id).await?;
81 if community_dupe.is_some() {
82 return Err(LemmyErrorType::CommunityAlreadyExists)?;
85 // When you create a community, make sure the user becomes a moderator and a follower
86 let keypair = generate_actor_keypair()?;
88 let community_form = CommunityInsertForm::builder()
91 .description(description)
95 .actor_id(Some(community_actor_id.clone()))
96 .private_key(Some(keypair.private_key))
97 .public_key(keypair.public_key)
98 .followers_url(Some(generate_followers_url(&community_actor_id)?))
99 .inbox_url(Some(generate_inbox_url(&community_actor_id)?))
100 .shared_inbox_url(Some(generate_shared_inbox_url(&community_actor_id)?))
101 .posting_restricted_to_mods(data.posting_restricted_to_mods)
102 .instance_id(site_view.site.instance_id)
105 let inserted_community = Community::create(&mut context.pool(), &community_form)
107 .with_lemmy_type(LemmyErrorType::CommunityAlreadyExists)?;
109 // The community creator becomes a moderator
110 let community_moderator_form = CommunityModeratorForm {
111 community_id: inserted_community.id,
112 person_id: local_user_view.person.id,
115 CommunityModerator::join(&mut context.pool(), &community_moderator_form)
117 .with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?;
119 // Follow your own community
120 let community_follower_form = CommunityFollowerForm {
121 community_id: inserted_community.id,
122 person_id: local_user_view.person.id,
126 CommunityFollower::follow(&mut context.pool(), &community_follower_form)
128 .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
130 // Update the discussion_languages if that's provided
131 let community_id = inserted_community.id;
132 if let Some(languages) = data.discussion_languages.clone() {
133 let site_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?;
134 // check that community languages are a subset of site languages
135 // https://stackoverflow.com/a/64227550
136 let is_subset = languages.iter().all(|item| site_languages.contains(item));
138 return Err(LemmyErrorType::LanguageNotAllowed)?;
140 CommunityLanguage::update(&mut context.pool(), languages, community_id).await?;
143 build_community_response(&context, local_user_view, community_id).await