]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/create.rs
Add separate Post check for is_valid_body_field (#3263)
[lemmy.git] / crates / api_crud / src / community / create.rs
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},
7   context::LemmyContext,
8   utils::{
9     generate_followers_url,
10     generate_inbox_url,
11     generate_local_apub_endpoint,
12     generate_shared_inbox_url,
13     is_admin,
14     local_site_to_slur_regex,
15     local_user_view_from_jwt,
16     EndpointType,
17   },
18 };
19 use lemmy_db_schema::{
20   source::{
21     actor_language::{CommunityLanguage, SiteLanguage},
22     community::{
23       Community,
24       CommunityFollower,
25       CommunityFollowerForm,
26       CommunityInsertForm,
27       CommunityModerator,
28       CommunityModeratorForm,
29     },
30   },
31   traits::{ApubActor, Crud, Followable, Joinable},
32   utils::diesel_option_overwrite_to_url_create,
33 };
34 use lemmy_db_views::structs::SiteView;
35 use lemmy_utils::{
36   error::LemmyError,
37   utils::{
38     slurs::{check_slurs, check_slurs_opt},
39     validation::{is_valid_actor_name, is_valid_body_field},
40   },
41 };
42
43 #[async_trait::async_trait(?Send)]
44 impl PerformCrud for CreateCommunity {
45   type Response = CommunityResponse;
46
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;
53
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",
57       ));
58     }
59
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)?;
63
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)?;
68
69     is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
70     is_valid_body_field(&data.description, false)?;
71
72     // Double check for duplicate community actor_ids
73     let community_actor_id = generate_local_apub_endpoint(
74       EndpointType::Community,
75       &data.name,
76       &context.settings().get_protocol_and_hostname(),
77     )?;
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"));
81     }
82
83     // When you create a community, make sure the user becomes a moderator and a follower
84     let keypair = generate_actor_keypair()?;
85
86     let community_form = CommunityInsertForm::builder()
87       .name(data.name.clone())
88       .title(data.title.clone())
89       .description(data.description.clone())
90       .icon(icon)
91       .banner(banner)
92       .nsfw(data.nsfw)
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)
101       .build();
102
103     let inserted_community = Community::create(context.pool(), &community_form)
104       .await
105       .map_err(|e| LemmyError::from_error_message(e, "community_already_exists"))?;
106
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,
111     };
112
113     CommunityModerator::join(context.pool(), &community_moderator_form)
114       .await
115       .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
116
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,
121       pending: false,
122     };
123
124     CommunityFollower::follow(context.pool(), &community_follower_form)
125       .await
126       .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
127
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));
135       if !is_subset {
136         return Err(LemmyError::from_message("language_not_allowed"));
137       }
138       CommunityLanguage::update(context.pool(), languages, community_id).await?;
139     }
140
141     build_community_response(context, local_user_view, community_id).await
142   }
143 }