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, get_local_user_view_from_jwt},
9 source::community::{Community, CommunityFollower, CommunityFollowerForm},
10 traits::{Crud, Followable},
12 use lemmy_db_views_actor::structs::CommunityView;
13 use lemmy_utils::{error::LemmyError, ConnectionId};
15 #[async_trait::async_trait(?Send)]
16 impl Perform for FollowCommunity {
17 type Response = CommunityResponse;
19 #[tracing::instrument(skip(context, _websocket_id))]
22 context: &Data<LemmyContext>,
23 _websocket_id: Option<ConnectionId>,
24 ) -> Result<CommunityResponse, LemmyError> {
25 let data: &FollowCommunity = self;
27 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
29 let community_id = data.community_id;
30 let community = Community::read(context.pool(), community_id).await?;
31 let community_follower_form = CommunityFollowerForm {
32 community_id: data.community_id,
33 person_id: local_user_view.person.id,
37 if community.local && data.follow {
38 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
39 check_community_deleted_or_removed(community_id, context.pool()).await?;
41 CommunityFollower::follow(context.pool(), &community_follower_form)
43 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
46 CommunityFollower::unfollow(context.pool(), &community_follower_form)
48 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
51 let community_id = data.community_id;
52 let person_id = local_user_view.person.id;
53 let community_view = CommunityView::read(context.pool(), community_id, Some(person_id)).await?;
55 Ok(Self::Response { community_view })