1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 community::{CommunityResponse, CreateCommunity},
6 get_local_user_view_from_jwt,
10 fetcher::object_id::ObjectId,
11 generate_apub_endpoint,
12 generate_followers_url,
14 generate_shared_inbox_url,
15 objects::community::ApubCommunity,
18 use lemmy_db_schema::{
19 diesel_option_overwrite_to_url,
24 CommunityFollowerForm,
27 CommunityModeratorForm,
31 traits::{Crud, Followable, Joinable},
33 use lemmy_db_views_actor::community_view::CommunityView;
35 apub::generate_actor_keypair,
36 utils::{check_slurs, check_slurs_opt, is_valid_actor_name},
41 use lemmy_websocket::LemmyContext;
43 #[async_trait::async_trait(?Send)]
44 impl PerformCrud for CreateCommunity {
45 type Response = CommunityResponse;
49 context: &Data<LemmyContext>,
50 _websocket_id: Option<ConnectionId>,
51 ) -> Result<CommunityResponse, LemmyError> {
52 let data: &CreateCommunity = self;
54 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
56 let site = blocking(context.pool(), move |conn| Site::read(conn, 0)).await??;
57 if site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
58 return Err(ApiError::err_plain("only_admins_can_create_communities").into());
61 check_slurs(&data.name, &context.settings().slur_regex())?;
62 check_slurs(&data.title, &context.settings().slur_regex())?;
63 check_slurs_opt(&data.description, &context.settings().slur_regex())?;
65 if !is_valid_actor_name(&data.name, context.settings().actor_name_max_length) {
66 return Err(ApiError::err_plain("invalid_community_name").into());
69 // Double check for duplicate community actor_ids
70 let community_actor_id = generate_apub_endpoint(
71 EndpointType::Community,
73 &context.settings().get_protocol_and_hostname(),
75 let community_actor_id_wrapped = ObjectId::<ApubCommunity>::new(community_actor_id.clone());
76 let community_dupe = community_actor_id_wrapped.dereference_local(context).await;
77 if community_dupe.is_ok() {
78 return Err(ApiError::err_plain("community_already_exists").into());
81 // Check to make sure the icon and banners are urls
82 let icon = diesel_option_overwrite_to_url(&data.icon)?;
83 let banner = diesel_option_overwrite_to_url(&data.banner)?;
85 // When you create a community, make sure the user becomes a moderator and a follower
86 let keypair = generate_actor_keypair()?;
88 let community_form = CommunityForm {
89 name: data.name.to_owned(),
90 title: data.title.to_owned(),
91 description: data.description.to_owned(),
95 actor_id: Some(community_actor_id.to_owned()),
96 private_key: Some(keypair.private_key),
97 public_key: Some(keypair.public_key),
98 followers_url: Some(generate_followers_url(&community_actor_id)?),
99 inbox_url: Some(generate_inbox_url(&community_actor_id)?),
100 shared_inbox_url: Some(Some(generate_shared_inbox_url(&community_actor_id)?)),
101 ..CommunityForm::default()
104 let inserted_community = blocking(context.pool(), move |conn| {
105 Community::create(conn, &community_form)
108 .map_err(|e| ApiError::err("community_already_exists", e))?;
110 // The community creator becomes a moderator
111 let community_moderator_form = CommunityModeratorForm {
112 community_id: inserted_community.id,
113 person_id: local_user_view.person.id,
116 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
117 if blocking(context.pool(), join).await?.is_err() {
118 return Err(ApiError::err_plain("community_moderator_already_exists").into());
121 // Follow your own community
122 let community_follower_form = CommunityFollowerForm {
123 community_id: inserted_community.id,
124 person_id: local_user_view.person.id,
128 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
129 if blocking(context.pool(), follow).await?.is_err() {
130 return Err(ApiError::err_plain("community_follower_already_exists").into());
133 let person_id = local_user_view.person.id;
134 let community_view = blocking(context.pool(), move |conn| {
135 CommunityView::read(conn, inserted_community.id, Some(person_id))
139 Ok(CommunityResponse { community_view })