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