]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/create.rs
Sanitize html (#3708)
[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     sanitize_html,
17     sanitize_html_opt,
18     EndpointType,
19   },
20 };
21 use lemmy_db_schema::{
22   source::{
23     actor_language::{CommunityLanguage, SiteLanguage},
24     community::{
25       Community,
26       CommunityFollower,
27       CommunityFollowerForm,
28       CommunityInsertForm,
29       CommunityModerator,
30       CommunityModeratorForm,
31     },
32   },
33   traits::{ApubActor, Crud, Followable, Joinable},
34   utils::diesel_option_overwrite_to_url_create,
35 };
36 use lemmy_db_views::structs::SiteView;
37 use lemmy_utils::{
38   error::{LemmyError, LemmyErrorExt, LemmyErrorType},
39   utils::{
40     slurs::{check_slurs, check_slurs_opt},
41     validation::{is_valid_actor_name, is_valid_body_field},
42   },
43 };
44
45 #[async_trait::async_trait(?Send)]
46 impl PerformCrud for CreateCommunity {
47   type Response = CommunityResponse;
48
49   #[tracing::instrument(skip(context))]
50   async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommunityResponse, LemmyError> {
51     let data: &CreateCommunity = self;
52     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
53     let site_view = SiteView::read_local(&mut context.pool()).await?;
54     let local_site = site_view.local_site;
55
56     if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
57       return Err(LemmyErrorType::OnlyAdminsCanCreateCommunities)?;
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 name = sanitize_html(&data.name);
65     let title = sanitize_html(&data.title);
66     let description = sanitize_html_opt(&data.description);
67
68     let slur_regex = local_site_to_slur_regex(&local_site);
69     check_slurs(&name, &slur_regex)?;
70     check_slurs(&title, &slur_regex)?;
71     check_slurs_opt(&description, &slur_regex)?;
72
73     is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
74     is_valid_body_field(&data.description, false)?;
75
76     // Double check for duplicate community actor_ids
77     let community_actor_id = generate_local_apub_endpoint(
78       EndpointType::Community,
79       &data.name,
80       &context.settings().get_protocol_and_hostname(),
81     )?;
82     let community_dupe =
83       Community::read_from_apub_id(&mut context.pool(), &community_actor_id).await?;
84     if community_dupe.is_some() {
85       return Err(LemmyErrorType::CommunityAlreadyExists)?;
86     }
87
88     // When you create a community, make sure the user becomes a moderator and a follower
89     let keypair = generate_actor_keypair()?;
90
91     let community_form = CommunityInsertForm::builder()
92       .name(name)
93       .title(title)
94       .description(description)
95       .icon(icon)
96       .banner(banner)
97       .nsfw(data.nsfw)
98       .actor_id(Some(community_actor_id.clone()))
99       .private_key(Some(keypair.private_key))
100       .public_key(keypair.public_key)
101       .followers_url(Some(generate_followers_url(&community_actor_id)?))
102       .inbox_url(Some(generate_inbox_url(&community_actor_id)?))
103       .shared_inbox_url(Some(generate_shared_inbox_url(&community_actor_id)?))
104       .posting_restricted_to_mods(data.posting_restricted_to_mods)
105       .instance_id(site_view.site.instance_id)
106       .build();
107
108     let inserted_community = Community::create(&mut context.pool(), &community_form)
109       .await
110       .with_lemmy_type(LemmyErrorType::CommunityAlreadyExists)?;
111
112     // The community creator becomes a moderator
113     let community_moderator_form = CommunityModeratorForm {
114       community_id: inserted_community.id,
115       person_id: local_user_view.person.id,
116     };
117
118     CommunityModerator::join(&mut context.pool(), &community_moderator_form)
119       .await
120       .with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?;
121
122     // Follow your own community
123     let community_follower_form = CommunityFollowerForm {
124       community_id: inserted_community.id,
125       person_id: local_user_view.person.id,
126       pending: false,
127     };
128
129     CommunityFollower::follow(&mut context.pool(), &community_follower_form)
130       .await
131       .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
132
133     // Update the discussion_languages if that's provided
134     let community_id = inserted_community.id;
135     if let Some(languages) = data.discussion_languages.clone() {
136       let site_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?;
137       // check that community languages are a subset of site languages
138       // https://stackoverflow.com/a/64227550
139       let is_subset = languages.iter().all(|item| site_languages.contains(item));
140       if !is_subset {
141         return Err(LemmyErrorType::LanguageNotAllowed)?;
142       }
143       CommunityLanguage::update(&mut context.pool(), languages, community_id).await?;
144     }
145
146     build_community_response(context, local_user_view, community_id).await
147   }
148 }