]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/create.rs
Add restricted community field to CreateCommunity, UpdateCommunity (ref #2235) (...
[lemmy.git] / crates / api_crud / src / community / create.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   check_image_has_local_domain,
6   community::{CommunityResponse, CreateCommunity},
7   get_local_user_view_from_jwt,
8   is_admin,
9 };
10 use lemmy_apub::{
11   generate_followers_url,
12   generate_inbox_url,
13   generate_local_apub_endpoint,
14   generate_shared_inbox_url,
15   objects::community::ApubCommunity,
16   EndpointType,
17 };
18 use lemmy_apub_lib::object_id::ObjectId;
19 use lemmy_db_schema::{
20   diesel_option_overwrite_to_url,
21   source::{
22     community::{
23       Community,
24       CommunityFollower,
25       CommunityFollowerForm,
26       CommunityForm,
27       CommunityModerator,
28       CommunityModeratorForm,
29     },
30     site::Site,
31   },
32   traits::{Crud, Followable, Joinable},
33 };
34 use lemmy_db_views_actor::community_view::CommunityView;
35 use lemmy_utils::{
36   apub::generate_actor_keypair,
37   utils::{check_slurs, check_slurs_opt, is_valid_actor_name},
38   ConnectionId,
39   LemmyError,
40 };
41 use lemmy_websocket::LemmyContext;
42
43 #[async_trait::async_trait(?Send)]
44 impl PerformCrud for CreateCommunity {
45   type Response = CommunityResponse;
46
47   #[tracing::instrument(skip(context, _websocket_id))]
48   async fn perform(
49     &self,
50     context: &Data<LemmyContext>,
51     _websocket_id: Option<ConnectionId>,
52   ) -> Result<CommunityResponse, LemmyError> {
53     let data: &CreateCommunity = self;
54     let local_user_view =
55       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
56
57     let site = blocking(context.pool(), Site::read_local_site).await??;
58     if site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
59       return Err(LemmyError::from_message(
60         "only_admins_can_create_communities",
61       ));
62     }
63
64     // Check to make sure the icon and banners are urls
65     let icon = diesel_option_overwrite_to_url(&data.icon)?;
66     let banner = diesel_option_overwrite_to_url(&data.banner)?;
67
68     check_slurs(&data.name, &context.settings().slur_regex())?;
69     check_slurs(&data.title, &context.settings().slur_regex())?;
70     check_slurs_opt(&data.description, &context.settings().slur_regex())?;
71     check_image_has_local_domain(icon.as_ref().unwrap_or(&None))?;
72     check_image_has_local_domain(banner.as_ref().unwrap_or(&None))?;
73
74     if !is_valid_actor_name(&data.name, context.settings().actor_name_max_length) {
75       return Err(LemmyError::from_message("invalid_community_name"));
76     }
77
78     // Double check for duplicate community actor_ids
79     let community_actor_id = generate_local_apub_endpoint(
80       EndpointType::Community,
81       &data.name,
82       &context.settings().get_protocol_and_hostname(),
83     )?;
84     let community_actor_id_wrapped = ObjectId::<ApubCommunity>::new(community_actor_id.clone());
85     let community_dupe = community_actor_id_wrapped.dereference_local(context).await;
86     if community_dupe.is_ok() {
87       return Err(LemmyError::from_message("community_already_exists"));
88     }
89
90     // When you create a community, make sure the user becomes a moderator and a follower
91     let keypair = generate_actor_keypair()?;
92
93     let community_form = CommunityForm {
94       name: data.name.to_owned(),
95       title: data.title.to_owned(),
96       description: data.description.to_owned(),
97       icon,
98       banner,
99       nsfw: data.nsfw,
100       actor_id: Some(community_actor_id.to_owned()),
101       private_key: Some(Some(keypair.private_key)),
102       public_key: keypair.public_key,
103       followers_url: Some(generate_followers_url(&community_actor_id)?),
104       inbox_url: Some(generate_inbox_url(&community_actor_id)?),
105       shared_inbox_url: Some(Some(generate_shared_inbox_url(&community_actor_id)?)),
106       posting_restricted_to_mods: data.posting_restricted_to_mods,
107       ..CommunityForm::default()
108     };
109
110     let inserted_community = blocking(context.pool(), move |conn| {
111       Community::create(conn, &community_form)
112     })
113     .await?
114     .map_err(|e| LemmyError::from_error_message(e, "community_already_exists"))?;
115
116     // The community creator becomes a moderator
117     let community_moderator_form = CommunityModeratorForm {
118       community_id: inserted_community.id,
119       person_id: local_user_view.person.id,
120     };
121
122     let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
123     blocking(context.pool(), join)
124       .await?
125       .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
126
127     // Follow your own community
128     let community_follower_form = CommunityFollowerForm {
129       community_id: inserted_community.id,
130       person_id: local_user_view.person.id,
131       pending: false,
132     };
133
134     let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
135     blocking(context.pool(), follow)
136       .await?
137       .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
138
139     let person_id = local_user_view.person.id;
140     let community_view = blocking(context.pool(), move |conn| {
141       CommunityView::read(conn, inserted_community.id, Some(person_id))
142     })
143     .await??;
144
145     Ok(CommunityResponse { community_view })
146   }
147 }