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 generate_apub_endpoint,
11 generate_followers_url,
13 generate_shared_inbox_url,
16 use lemmy_db_queries::{diesel_option_overwrite_to_url, ApubObject, Crud, Followable, Joinable};
17 use lemmy_db_schema::source::{
21 CommunityFollowerForm,
24 CommunityModeratorForm,
28 use lemmy_db_views_actor::community_view::CommunityView;
30 apub::generate_actor_keypair,
31 utils::{check_slurs, check_slurs_opt, is_valid_community_name},
36 use lemmy_websocket::LemmyContext;
38 #[async_trait::async_trait(?Send)]
39 impl PerformCrud for CreateCommunity {
40 type Response = CommunityResponse;
44 context: &Data<LemmyContext>,
45 _websocket_id: Option<ConnectionId>,
46 ) -> Result<CommunityResponse, LemmyError> {
47 let data: &CreateCommunity = &self;
48 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
50 let site = blocking(context.pool(), move |conn| Site::read(conn, 0)).await??;
51 if site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
52 return Err(ApiError::err("only_admins_can_create_communities").into());
55 check_slurs(&data.name)?;
56 check_slurs(&data.title)?;
57 check_slurs_opt(&data.description)?;
59 if !is_valid_community_name(&data.name) {
60 return Err(ApiError::err("invalid_community_name").into());
63 // Double check for duplicate community actor_ids
64 let community_actor_id = generate_apub_endpoint(EndpointType::Community, &data.name)?;
65 let actor_id_cloned = community_actor_id.to_owned();
66 let community_dupe = blocking(context.pool(), move |conn| {
67 Community::read_from_apub_id(conn, &actor_id_cloned)
70 if community_dupe.is_ok() {
71 return Err(ApiError::err("community_already_exists").into());
74 // Check to make sure the icon and banners are urls
75 let icon = diesel_option_overwrite_to_url(&data.icon)?;
76 let banner = diesel_option_overwrite_to_url(&data.banner)?;
78 // When you create a community, make sure the user becomes a moderator and a follower
79 let keypair = generate_actor_keypair()?;
81 let community_form = CommunityForm {
82 name: data.name.to_owned(),
83 title: data.title.to_owned(),
84 description: data.description.to_owned(),
88 actor_id: Some(community_actor_id.to_owned()),
89 private_key: Some(keypair.private_key),
90 public_key: Some(keypair.public_key),
91 followers_url: Some(generate_followers_url(&community_actor_id)?),
92 inbox_url: Some(generate_inbox_url(&community_actor_id)?),
93 shared_inbox_url: Some(Some(generate_shared_inbox_url(&community_actor_id)?)),
94 ..CommunityForm::default()
97 let inserted_community = blocking(context.pool(), move |conn| {
98 Community::create(conn, &community_form)
101 .map_err(|_| ApiError::err("community_already_exists"))?;
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,
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());
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,
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());
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))
132 Ok(CommunityResponse { community_view })