]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/create.rs
Fixing missing forms, incorrect user discussion_languages (#2580)
[lemmy.git] / crates / api_crud / src / community / create.rs
1 use crate::PerformCrud;
2 use activitypub_federation::core::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     get_local_user_view_from_jwt,
13     is_admin,
14     local_site_to_slur_regex,
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::{check_slurs, check_slurs_opt, is_valid_actor_name},
38   ConnectionId,
39 };
40
41 #[async_trait::async_trait(?Send)]
42 impl PerformCrud for CreateCommunity {
43   type Response = CommunityResponse;
44
45   #[tracing::instrument(skip(context, _websocket_id))]
46   async fn perform(
47     &self,
48     context: &Data<LemmyContext>,
49     _websocket_id: Option<ConnectionId>,
50   ) -> Result<CommunityResponse, LemmyError> {
51     let data: &CreateCommunity = self;
52     let local_user_view =
53       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
54     let site_view = SiteView::read_local(context.pool()).await?;
55     let local_site = site_view.local_site;
56
57     if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
58       return Err(LemmyError::from_message(
59         "only_admins_can_create_communities",
60       ));
61     }
62
63     // Check to make sure the icon and banners are urls
64     let icon = diesel_option_overwrite_to_url_create(&data.icon)?;
65     let banner = diesel_option_overwrite_to_url_create(&data.banner)?;
66
67     let slur_regex = local_site_to_slur_regex(&local_site);
68     check_slurs(&data.name, &slur_regex)?;
69     check_slurs(&data.title, &slur_regex)?;
70     check_slurs_opt(&data.description, &slur_regex)?;
71
72     if !is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize) {
73       return Err(LemmyError::from_message("invalid_community_name"));
74     }
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 = Community::read_from_apub_id(context.pool(), &community_actor_id).await?;
83     if community_dupe.is_some() {
84       return Err(LemmyError::from_message("community_already_exists"));
85     }
86
87     // When you create a community, make sure the user becomes a moderator and a follower
88     let keypair = generate_actor_keypair()?;
89
90     let community_form = CommunityInsertForm::builder()
91       .name(data.name.clone())
92       .title(data.title.clone())
93       .description(data.description.clone())
94       .icon(icon)
95       .banner(banner)
96       .nsfw(data.nsfw)
97       .actor_id(Some(community_actor_id.clone()))
98       .private_key(Some(keypair.private_key))
99       .public_key(keypair.public_key)
100       .followers_url(Some(generate_followers_url(&community_actor_id)?))
101       .inbox_url(Some(generate_inbox_url(&community_actor_id)?))
102       .shared_inbox_url(Some(generate_shared_inbox_url(&community_actor_id)?))
103       .posting_restricted_to_mods(data.posting_restricted_to_mods)
104       .instance_id(site_view.site.instance_id)
105       .build();
106
107     let inserted_community = Community::create(context.pool(), &community_form)
108       .await
109       .map_err(|e| LemmyError::from_error_message(e, "community_already_exists"))?;
110
111     // The community creator becomes a moderator
112     let community_moderator_form = CommunityModeratorForm {
113       community_id: inserted_community.id,
114       person_id: local_user_view.person.id,
115     };
116
117     CommunityModerator::join(context.pool(), &community_moderator_form)
118       .await
119       .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
120
121     // Follow your own community
122     let community_follower_form = CommunityFollowerForm {
123       community_id: inserted_community.id,
124       person_id: local_user_view.person.id,
125       pending: false,
126     };
127
128     CommunityFollower::follow(context.pool(), &community_follower_form)
129       .await
130       .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
131
132     // Update the discussion_languages if that's provided
133     let community_id = inserted_community.id;
134     if let Some(languages) = data.discussion_languages.clone() {
135       let site_languages = SiteLanguage::read_local(context.pool()).await?;
136       // check that community languages are a subset of site languages
137       // https://stackoverflow.com/a/64227550
138       let is_subset = languages.iter().all(|item| site_languages.contains(item));
139       if !is_subset {
140         return Err(LemmyError::from_message("language_not_allowed"));
141       }
142       CommunityLanguage::update(context.pool(), languages, community_id).await?;
143     }
144
145     let person_id = local_user_view.person.id;
146     let community_view =
147       CommunityView::read(context.pool(), inserted_community.id, Some(person_id)).await?;
148     let discussion_languages =
149       CommunityLanguage::read(context.pool(), inserted_community.id).await?;
150
151     Ok(CommunityResponse {
152       community_view,
153       discussion_languages,
154     })
155   }
156 }