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 community::{CommunityResponse, CreateCommunity},
8 generate_followers_url,
10 generate_local_apub_endpoint,
11 generate_shared_inbox_url,
12 get_local_user_view_from_jwt,
14 local_site_to_slur_regex,
18 use lemmy_db_schema::{
20 actor_language::{CommunityLanguage, SiteLanguage},
24 CommunityFollowerForm,
27 CommunityModeratorForm,
30 traits::{ApubActor, Crud, Followable, Joinable},
31 utils::diesel_option_overwrite_to_url_create,
33 use lemmy_db_views::structs::SiteView;
34 use lemmy_db_views_actor::structs::CommunityView;
38 slurs::{check_slurs, check_slurs_opt},
39 validation::is_valid_actor_name,
44 #[async_trait::async_trait(?Send)]
45 impl PerformCrud for CreateCommunity {
46 type Response = CommunityResponse;
48 #[tracing::instrument(skip(context, _websocket_id))]
51 context: &Data<LemmyContext>,
52 _websocket_id: Option<ConnectionId>,
53 ) -> Result<CommunityResponse, LemmyError> {
54 let data: &CreateCommunity = self;
56 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
57 let site_view = SiteView::read_local(context.pool()).await?;
58 let local_site = site_view.local_site;
60 if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
61 return Err(LemmyError::from_message(
62 "only_admins_can_create_communities",
66 // Check to make sure the icon and banners are urls
67 let icon = diesel_option_overwrite_to_url_create(&data.icon)?;
68 let banner = diesel_option_overwrite_to_url_create(&data.banner)?;
70 let slur_regex = local_site_to_slur_regex(&local_site);
71 check_slurs(&data.name, &slur_regex)?;
72 check_slurs(&data.title, &slur_regex)?;
73 check_slurs_opt(&data.description, &slur_regex)?;
75 if !is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize) {
76 return Err(LemmyError::from_message("invalid_community_name"));
79 // Double check for duplicate community actor_ids
80 let community_actor_id = generate_local_apub_endpoint(
81 EndpointType::Community,
83 &context.settings().get_protocol_and_hostname(),
85 let community_dupe = Community::read_from_apub_id(context.pool(), &community_actor_id).await?;
86 if community_dupe.is_some() {
87 return Err(LemmyError::from_message("community_already_exists"));
90 // When you create a community, make sure the user becomes a moderator and a follower
91 let keypair = generate_actor_keypair()?;
93 let community_form = CommunityInsertForm::builder()
94 .name(data.name.clone())
95 .title(data.title.clone())
96 .description(data.description.clone())
100 .actor_id(Some(community_actor_id.clone()))
101 .private_key(Some(keypair.private_key))
102 .public_key(keypair.public_key)
103 .followers_url(Some(generate_followers_url(&community_actor_id)?))
104 .inbox_url(Some(generate_inbox_url(&community_actor_id)?))
105 .shared_inbox_url(Some(generate_shared_inbox_url(&community_actor_id)?))
106 .posting_restricted_to_mods(data.posting_restricted_to_mods)
107 .instance_id(site_view.site.instance_id)
110 let inserted_community = Community::create(context.pool(), &community_form)
112 .map_err(|e| LemmyError::from_error_message(e, "community_already_exists"))?;
114 // The community creator becomes a moderator
115 let community_moderator_form = CommunityModeratorForm {
116 community_id: inserted_community.id,
117 person_id: local_user_view.person.id,
120 CommunityModerator::join(context.pool(), &community_moderator_form)
122 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
124 // Follow your own community
125 let community_follower_form = CommunityFollowerForm {
126 community_id: inserted_community.id,
127 person_id: local_user_view.person.id,
131 CommunityFollower::follow(context.pool(), &community_follower_form)
133 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
135 // Update the discussion_languages if that's provided
136 let community_id = inserted_community.id;
137 if let Some(languages) = data.discussion_languages.clone() {
138 let site_languages = SiteLanguage::read_local_raw(context.pool()).await?;
139 // check that community languages are a subset of site languages
140 // https://stackoverflow.com/a/64227550
141 let is_subset = languages.iter().all(|item| site_languages.contains(item));
143 return Err(LemmyError::from_message("language_not_allowed"));
145 CommunityLanguage::update(context.pool(), languages, community_id).await?;
148 let person_id = local_user_view.person.id;
150 CommunityView::read(context.pool(), inserted_community.id, Some(person_id), None).await?;
151 let discussion_languages =
152 CommunityLanguage::read(context.pool(), inserted_community.id).await?;
154 Ok(CommunityResponse {
156 discussion_languages,