2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 community::{CommunityResponse, FollowCommunity},
6 utils::{check_community_ban, check_community_deleted_or_removed, local_user_view_from_jwt},
10 actor_language::CommunityLanguage,
11 community::{Community, CommunityFollower, CommunityFollowerForm},
13 traits::{Crud, Followable},
15 use lemmy_db_views_actor::structs::CommunityView;
16 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
18 #[async_trait::async_trait(?Send)]
19 impl Perform for FollowCommunity {
20 type Response = CommunityResponse;
22 #[tracing::instrument(skip(context))]
23 async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommunityResponse, LemmyError> {
24 let data: &FollowCommunity = self;
25 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
27 let community_id = data.community_id;
28 let community = Community::read(context.pool(), community_id).await?;
29 let mut community_follower_form = CommunityFollowerForm {
30 community_id: data.community_id,
31 person_id: local_user_view.person.id,
37 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
38 check_community_deleted_or_removed(community_id, context.pool()).await?;
40 CommunityFollower::follow(context.pool(), &community_follower_form)
42 .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
44 // Mark as pending, the actual federation activity is sent via `SendActivity` handler
45 community_follower_form.pending = true;
46 CommunityFollower::follow(context.pool(), &community_follower_form)
48 .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
52 CommunityFollower::unfollow(context.pool(), &community_follower_form)
54 .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
57 let community_id = data.community_id;
58 let person_id = local_user_view.person.id;
60 CommunityView::read(context.pool(), community_id, Some(person_id), None).await?;
61 let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?;