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