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