]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/create.rs
Dont return error in case optional auth is invalid (#2879)
[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   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     EndpointType,
16   },
17 };
18 use lemmy_db_schema::{
19   source::{
20     actor_language::{CommunityLanguage, SiteLanguage},
21     community::{
22       Community,
23       CommunityFollower,
24       CommunityFollowerForm,
25       CommunityInsertForm,
26       CommunityModerator,
27       CommunityModeratorForm,
28     },
29   },
30   traits::{ApubActor, Crud, Followable, Joinable},
31   utils::diesel_option_overwrite_to_url_create,
32 };
33 use lemmy_db_views::structs::SiteView;
34 use lemmy_db_views_actor::structs::CommunityView;
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   ConnectionId,
42 };
43
44 #[async_trait::async_trait(?Send)]
45 impl PerformCrud for CreateCommunity {
46   type Response = CommunityResponse;
47
48   #[tracing::instrument(skip(context, _websocket_id))]
49   async fn perform(
50     &self,
51     context: &Data<LemmyContext>,
52     _websocket_id: Option<ConnectionId>,
53   ) -> Result<CommunityResponse, LemmyError> {
54     let data: &CreateCommunity = self;
55     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
56     let site_view = SiteView::read_local(context.pool()).await?;
57     let local_site = site_view.local_site;
58
59     if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
60       return Err(LemmyError::from_message(
61         "only_admins_can_create_communities",
62       ));
63     }
64
65     // Check to make sure the icon and banners are urls
66     let icon = diesel_option_overwrite_to_url_create(&data.icon)?;
67     let banner = diesel_option_overwrite_to_url_create(&data.banner)?;
68
69     let slur_regex = local_site_to_slur_regex(&local_site);
70     check_slurs(&data.name, &slur_regex)?;
71     check_slurs(&data.title, &slur_regex)?;
72     check_slurs_opt(&data.description, &slur_regex)?;
73
74     is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
75     is_valid_body_field(&data.description)?;
76
77     // Double check for duplicate community actor_ids
78     let community_actor_id = generate_local_apub_endpoint(
79       EndpointType::Community,
80       &data.name,
81       &context.settings().get_protocol_and_hostname(),
82     )?;
83     let community_dupe = Community::read_from_apub_id(context.pool(), &community_actor_id).await?;
84     if community_dupe.is_some() {
85       return Err(LemmyError::from_message("community_already_exists"));
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(data.name.clone())
93       .title(data.title.clone())
94       .description(data.description.clone())
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(context.pool(), &community_form)
109       .await
110       .map_err(|e| LemmyError::from_error_message(e, "community_already_exists"))?;
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(context.pool(), &community_moderator_form)
119       .await
120       .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
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(context.pool(), &community_follower_form)
130       .await
131       .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
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(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(LemmyError::from_message("language_not_allowed"));
142       }
143       CommunityLanguage::update(context.pool(), languages, community_id).await?;
144     }
145
146     let person_id = local_user_view.person.id;
147     let community_view =
148       CommunityView::read(context.pool(), inserted_community.id, Some(person_id), None).await?;
149     let discussion_languages =
150       CommunityLanguage::read(context.pool(), inserted_community.id).await?;
151
152     Ok(CommunityResponse {
153       community_view,
154       discussion_languages,
155     })
156   }
157 }