1 use activitypub_federation::config::Data;
2 use actix_web::web::Json;
3 use lemmy_api_common::{
4 community::{CommunityResponse, FollowCommunity},
6 send_activity::{ActivityChannel, SendActivityData},
7 utils::{check_community_ban, check_community_deleted_or_removed, local_user_view_from_jwt},
11 actor_language::CommunityLanguage,
12 community::{Community, CommunityFollower, CommunityFollowerForm},
14 traits::{Crud, Followable},
16 use lemmy_db_views_actor::structs::CommunityView;
17 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
19 #[tracing::instrument(skip(context))]
20 pub async fn follow_community(
21 data: Json<FollowCommunity>,
22 context: Data<LemmyContext>,
23 ) -> Result<Json<CommunityResponse>, LemmyError> {
24 let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
26 let community = Community::read(&mut context.pool(), data.community_id).await?;
27 let mut community_follower_form = CommunityFollowerForm {
28 community_id: community.id,
29 person_id: local_user_view.person.id,
35 check_community_ban(local_user_view.person.id, community.id, &mut context.pool()).await?;
36 check_community_deleted_or_removed(community.id, &mut context.pool()).await?;
38 CommunityFollower::follow(&mut context.pool(), &community_follower_form)
40 .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
42 // Mark as pending, the actual federation activity is sent via `SendActivity` handler
43 community_follower_form.pending = true;
44 CommunityFollower::follow(&mut context.pool(), &community_follower_form)
46 .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
50 CommunityFollower::unfollow(&mut context.pool(), &community_follower_form)
52 .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
55 ActivityChannel::submit_activity(
56 SendActivityData::FollowCommunity(community, local_user_view.person.clone(), data.follow),
61 let community_id = data.community_id;
62 let person_id = local_user_view.person.id;
64 CommunityView::read(&mut context.pool(), community_id, Some(person_id), false).await?;
65 let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?;
67 Ok(Json(CommunityResponse {