]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/create.rs
77ab833b9116a2ac37f4e8a5629e76c1c7b2ceed
[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, LemmyErrorExt, LemmyErrorType},
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(&mut 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(LemmyErrorType::OnlyAdminsCanCreateCommunities)?;
56     }
57
58     // Check to make sure the icon and banners are urls
59     let icon = diesel_option_overwrite_to_url_create(&data.icon)?;
60     let banner = diesel_option_overwrite_to_url_create(&data.banner)?;
61
62     let slur_regex = local_site_to_slur_regex(&local_site);
63     check_slurs(&data.name, &slur_regex)?;
64     check_slurs(&data.title, &slur_regex)?;
65     check_slurs_opt(&data.description, &slur_regex)?;
66
67     is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
68     is_valid_body_field(&data.description, false)?;
69
70     // Double check for duplicate community actor_ids
71     let community_actor_id = generate_local_apub_endpoint(
72       EndpointType::Community,
73       &data.name,
74       &context.settings().get_protocol_and_hostname(),
75     )?;
76     let community_dupe =
77       Community::read_from_apub_id(&mut context.pool(), &community_actor_id).await?;
78     if community_dupe.is_some() {
79       return Err(LemmyErrorType::CommunityAlreadyExists)?;
80     }
81
82     // When you create a community, make sure the user becomes a moderator and a follower
83     let keypair = generate_actor_keypair()?;
84
85     let community_form = CommunityInsertForm::builder()
86       .name(data.name.clone())
87       .title(data.title.clone())
88       .description(data.description.clone())
89       .icon(icon)
90       .banner(banner)
91       .nsfw(data.nsfw)
92       .actor_id(Some(community_actor_id.clone()))
93       .private_key(Some(keypair.private_key))
94       .public_key(keypair.public_key)
95       .followers_url(Some(generate_followers_url(&community_actor_id)?))
96       .inbox_url(Some(generate_inbox_url(&community_actor_id)?))
97       .shared_inbox_url(Some(generate_shared_inbox_url(&community_actor_id)?))
98       .posting_restricted_to_mods(data.posting_restricted_to_mods)
99       .instance_id(site_view.site.instance_id)
100       .build();
101
102     let inserted_community = Community::create(&mut context.pool(), &community_form)
103       .await
104       .with_lemmy_type(LemmyErrorType::CommunityAlreadyExists)?;
105
106     // The community creator becomes a moderator
107     let community_moderator_form = CommunityModeratorForm {
108       community_id: inserted_community.id,
109       person_id: local_user_view.person.id,
110     };
111
112     CommunityModerator::join(&mut context.pool(), &community_moderator_form)
113       .await
114       .with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?;
115
116     // Follow your own community
117     let community_follower_form = CommunityFollowerForm {
118       community_id: inserted_community.id,
119       person_id: local_user_view.person.id,
120       pending: false,
121     };
122
123     CommunityFollower::follow(&mut context.pool(), &community_follower_form)
124       .await
125       .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
126
127     // Update the discussion_languages if that's provided
128     let community_id = inserted_community.id;
129     if let Some(languages) = data.discussion_languages.clone() {
130       let site_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?;
131       // check that community languages are a subset of site languages
132       // https://stackoverflow.com/a/64227550
133       let is_subset = languages.iter().all(|item| site_languages.contains(item));
134       if !is_subset {
135         return Err(LemmyErrorType::LanguageNotAllowed)?;
136       }
137       CommunityLanguage::update(&mut context.pool(), languages, community_id).await?;
138     }
139
140     build_community_response(context, local_user_view, community_id).await
141   }
142 }