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},
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, ConnectionId};
18 #[async_trait::async_trait(?Send)]
19 impl Perform for FollowCommunity {
20 type Response = CommunityResponse;
22 #[tracing::instrument(skip(context, _websocket_id))]
25 context: &Data<LemmyContext>,
26 _websocket_id: Option<ConnectionId>,
27 ) -> Result<CommunityResponse, LemmyError> {
28 let data: &FollowCommunity = self;
30 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
32 let community_id = data.community_id;
33 let community = Community::read(context.pool(), community_id).await?;
34 let community_follower_form = CommunityFollowerForm {
35 community_id: data.community_id,
36 person_id: local_user_view.person.id,
40 if community.local && data.follow {
41 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
42 check_community_deleted_or_removed(community_id, context.pool()).await?;
44 CommunityFollower::follow(context.pool(), &community_follower_form)
46 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
49 CommunityFollower::unfollow(context.pool(), &community_follower_form)
51 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
54 let community_id = data.community_id;
55 let person_id = local_user_view.person.id;
57 CommunityView::read(context.pool(), community_id, Some(person_id), None).await?;
58 let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?;