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;
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 community_follower_form = CommunityFollowerForm {
30 community_id: data.community_id,
31 person_id: local_user_view.person.id,
35 if community.local && data.follow {
36 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
37 check_community_deleted_or_removed(community_id, context.pool()).await?;
39 CommunityFollower::follow(context.pool(), &community_follower_form)
41 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
44 CommunityFollower::unfollow(context.pool(), &community_follower_form)
46 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
49 let community_id = data.community_id;
50 let person_id = local_user_view.person.id;
52 CommunityView::read(context.pool(), community_id, Some(person_id), None).await?;
53 let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?;