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;
36 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
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(LemmyErrorType::OnlyAdminsCanCreateCommunities)?;
58 // Check to make sure the icon and banners are urls
59 let icon = diesel_option_overwrite_to_url_create(&data.icon)?;
60 let banner = diesel_option_overwrite_to_url_create(&data.banner)?;
62 let slur_regex = local_site_to_slur_regex(&local_site);
63 check_slurs(&data.name, &slur_regex)?;
64 check_slurs(&data.title, &slur_regex)?;
65 check_slurs_opt(&data.description, &slur_regex)?;
67 is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
68 is_valid_body_field(&data.description, false)?;
70 // Double check for duplicate community actor_ids
71 let community_actor_id = generate_local_apub_endpoint(
72 EndpointType::Community,
74 &context.settings().get_protocol_and_hostname(),
76 let community_dupe = Community::read_from_apub_id(context.pool(), &community_actor_id).await?;
77 if community_dupe.is_some() {
78 return Err(LemmyErrorType::CommunityAlreadyExists)?;
81 // When you create a community, make sure the user becomes a moderator and a follower
82 let keypair = generate_actor_keypair()?;
84 let community_form = CommunityInsertForm::builder()
85 .name(data.name.clone())
86 .title(data.title.clone())
87 .description(data.description.clone())
91 .actor_id(Some(community_actor_id.clone()))
92 .private_key(Some(keypair.private_key))
93 .public_key(keypair.public_key)
94 .followers_url(Some(generate_followers_url(&community_actor_id)?))
95 .inbox_url(Some(generate_inbox_url(&community_actor_id)?))
96 .shared_inbox_url(Some(generate_shared_inbox_url(&community_actor_id)?))
97 .posting_restricted_to_mods(data.posting_restricted_to_mods)
98 .instance_id(site_view.site.instance_id)
101 let inserted_community = Community::create(context.pool(), &community_form)
103 .with_lemmy_type(LemmyErrorType::CommunityAlreadyExists)?;
105 // The community creator becomes a moderator
106 let community_moderator_form = CommunityModeratorForm {
107 community_id: inserted_community.id,
108 person_id: local_user_view.person.id,
111 CommunityModerator::join(context.pool(), &community_moderator_form)
113 .with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?;
115 // Follow your own community
116 let community_follower_form = CommunityFollowerForm {
117 community_id: inserted_community.id,
118 person_id: local_user_view.person.id,
122 CommunityFollower::follow(context.pool(), &community_follower_form)
124 .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
126 // Update the discussion_languages if that's provided
127 let community_id = inserted_community.id;
128 if let Some(languages) = data.discussion_languages.clone() {
129 let site_languages = SiteLanguage::read_local_raw(context.pool()).await?;
130 // check that community languages are a subset of site languages
131 // https://stackoverflow.com/a/64227550
132 let is_subset = languages.iter().all(|item| site_languages.contains(item));
134 return Err(LemmyErrorType::LanguageNotAllowed)?;
136 CommunityLanguage::update(context.pool(), languages, community_id).await?;
139 build_community_response(context, local_user_view, community_id).await