]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/create.rs
Merge pull request #2593 from LemmyNet/refactor-notifications
[lemmy.git] / crates / api_crud / src / community / create.rs
1 use crate::PerformCrud;
2 use activitypub_federation::core::signatures::generate_actor_keypair;
3 use actix_web::web::Data;
4 use lemmy_api_common::{
5   community::{CommunityResponse, CreateCommunity},
6   context::LemmyContext,
7   utils::{
8     generate_followers_url,
9     generate_inbox_url,
10     generate_local_apub_endpoint,
11     generate_shared_inbox_url,
12     get_local_user_view_from_jwt,
13     is_admin,
14     local_site_to_slur_regex,
15     EndpointType,
16   },
17 };
18 use lemmy_db_schema::{
19   source::community::{
20     Community,
21     CommunityFollower,
22     CommunityFollowerForm,
23     CommunityInsertForm,
24     CommunityModerator,
25     CommunityModeratorForm,
26   },
27   traits::{ApubActor, Crud, Followable, Joinable},
28   utils::diesel_option_overwrite_to_url_create,
29 };
30 use lemmy_db_views::structs::SiteView;
31 use lemmy_db_views_actor::structs::CommunityView;
32 use lemmy_utils::{
33   error::LemmyError,
34   utils::{check_slurs, check_slurs_opt, is_valid_actor_name},
35   ConnectionId,
36 };
37
38 #[async_trait::async_trait(?Send)]
39 impl PerformCrud for CreateCommunity {
40   type Response = CommunityResponse;
41
42   #[tracing::instrument(skip(context, _websocket_id))]
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     let site_view = SiteView::read_local(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(LemmyError::from_message(
56         "only_admins_can_create_communities",
57       ));
58     }
59
60     // Check to make sure the icon and banners are urls
61     let icon = diesel_option_overwrite_to_url_create(&data.icon)?;
62     let banner = diesel_option_overwrite_to_url_create(&data.banner)?;
63
64     let slur_regex = local_site_to_slur_regex(&local_site);
65     check_slurs(&data.name, &slur_regex)?;
66     check_slurs(&data.title, &slur_regex)?;
67     check_slurs_opt(&data.description, &slur_regex)?;
68
69     if !is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize) {
70       return Err(LemmyError::from_message("invalid_community_name"));
71     }
72
73     // Double check for duplicate community actor_ids
74     let community_actor_id = generate_local_apub_endpoint(
75       EndpointType::Community,
76       &data.name,
77       &context.settings().get_protocol_and_hostname(),
78     )?;
79     let community_dupe = Community::read_from_apub_id(context.pool(), &community_actor_id).await?;
80     if community_dupe.is_some() {
81       return Err(LemmyError::from_message("community_already_exists"));
82     }
83
84     // When you create a community, make sure the user becomes a moderator and a follower
85     let keypair = generate_actor_keypair()?;
86
87     let community_form = CommunityInsertForm::builder()
88       .name(data.name.clone())
89       .title(data.title.clone())
90       .description(data.description.clone())
91       .icon(icon)
92       .banner(banner)
93       .nsfw(data.nsfw)
94       .actor_id(Some(community_actor_id.clone()))
95       .private_key(Some(keypair.private_key))
96       .public_key(keypair.public_key)
97       .followers_url(Some(generate_followers_url(&community_actor_id)?))
98       .inbox_url(Some(generate_inbox_url(&community_actor_id)?))
99       .shared_inbox_url(Some(generate_shared_inbox_url(&community_actor_id)?))
100       .posting_restricted_to_mods(data.posting_restricted_to_mods)
101       .instance_id(site_view.site.instance_id)
102       .build();
103
104     let inserted_community = Community::create(context.pool(), &community_form)
105       .await
106       .map_err(|e| LemmyError::from_error_message(e, "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     CommunityModerator::join(context.pool(), &community_moderator_form)
115       .await
116       .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
117
118     // Follow your own community
119     let community_follower_form = CommunityFollowerForm {
120       community_id: inserted_community.id,
121       person_id: local_user_view.person.id,
122       pending: false,
123     };
124
125     CommunityFollower::follow(context.pool(), &community_follower_form)
126       .await
127       .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
128
129     let person_id = local_user_view.person.id;
130     let community_view =
131       CommunityView::read(context.pool(), inserted_community.id, Some(person_id)).await?;
132
133     Ok(CommunityResponse { community_view })
134   }
135 }