]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/create.rs
add enable_federated_downvotes site option
[lemmy.git] / crates / api_crud / src / community / create.rs
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},
6   context::LemmyContext,
7   utils::{
8     generate_followers_url,
9     generate_inbox_url,
10     generate_local_apub_endpoint,
11     generate_shared_inbox_url,
12     is_admin,
13     local_site_to_slur_regex,
14     local_user_view_from_jwt,
15     sanitize_html,
16     sanitize_html_opt,
17     EndpointType,
18   },
19 };
20 use lemmy_db_schema::{
21   source::{
22     actor_language::{CommunityLanguage, SiteLanguage},
23     community::{
24       Community,
25       CommunityFollower,
26       CommunityFollowerForm,
27       CommunityInsertForm,
28       CommunityModerator,
29       CommunityModeratorForm,
30     },
31   },
32   traits::{ApubActor, Crud, Followable, Joinable},
33   utils::diesel_option_overwrite_to_url_create,
34 };
35 use lemmy_db_views::structs::SiteView;
36 use lemmy_utils::{
37   error::{LemmyError, LemmyErrorExt, LemmyErrorType},
38   utils::{
39     slurs::{check_slurs, check_slurs_opt},
40     validation::{is_valid_actor_name, is_valid_body_field},
41   },
42 };
43
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;
52
53   if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
54     return Err(LemmyErrorType::OnlyAdminsCanCreateCommunities)?;
55   }
56
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)?;
60
61   let name = sanitize_html(&data.name);
62   let title = sanitize_html(&data.title);
63   let description = sanitize_html_opt(&data.description);
64
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)?;
69
70   is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
71   is_valid_body_field(&data.description, false)?;
72
73   // Double check for duplicate community actor_ids
74   let community_actor_id = generate_local_apub_endpoint(
75     EndpointType::Community,
76     &data.name,
77     &context.settings().get_protocol_and_hostname(),
78   )?;
79   let community_dupe =
80     Community::read_from_apub_id(&mut context.pool(), &community_actor_id).await?;
81   if community_dupe.is_some() {
82     return Err(LemmyErrorType::CommunityAlreadyExists)?;
83   }
84
85   // When you create a community, make sure the user becomes a moderator and a follower
86   let keypair = generate_actor_keypair()?;
87
88   let community_form = CommunityInsertForm::builder()
89     .name(name)
90     .title(title)
91     .description(description)
92     .icon(icon)
93     .banner(banner)
94     .nsfw(data.nsfw)
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)
103     .build();
104
105   let inserted_community = Community::create(&mut context.pool(), &community_form)
106     .await
107     .with_lemmy_type(LemmyErrorType::CommunityAlreadyExists)?;
108
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,
113   };
114
115   CommunityModerator::join(&mut context.pool(), &community_moderator_form)
116     .await
117     .with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?;
118
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,
123     pending: false,
124   };
125
126   CommunityFollower::follow(&mut context.pool(), &community_follower_form)
127     .await
128     .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
129
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));
137     if !is_subset {
138       return Err(LemmyErrorType::LanguageNotAllowed)?;
139     }
140     CommunityLanguage::update(&mut context.pool(), languages, community_id).await?;
141   }
142
143   build_community_response(&context, local_user_view, community_id).await
144 }