2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 community::{CommunityResponse, FollowCommunity},
5 utils::{check_community_ban, check_community_deleted_or_removed, get_local_user_view_from_jwt},
8 objects::community::ApubCommunity,
9 protocol::activities::following::{
10 follow::Follow as FollowCommunityApub,
11 undo_follow::UndoFollow,
14 use lemmy_db_schema::{
15 source::community::{Community, CommunityFollower, CommunityFollowerForm},
16 traits::{Crud, Followable},
18 use lemmy_db_views_actor::structs::CommunityView;
19 use lemmy_utils::{error::LemmyError, ConnectionId};
20 use lemmy_websocket::LemmyContext;
22 #[async_trait::async_trait(?Send)]
23 impl Perform for FollowCommunity {
24 type Response = CommunityResponse;
26 #[tracing::instrument(skip(context, _websocket_id))]
29 context: &Data<LemmyContext>,
30 _websocket_id: Option<ConnectionId>,
31 ) -> Result<CommunityResponse, LemmyError> {
32 let data: &FollowCommunity = self;
34 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
36 let community_id = data.community_id;
37 let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
38 let community_follower_form = CommunityFollowerForm {
39 community_id: data.community_id,
40 person_id: local_user_view.person.id,
46 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
47 check_community_deleted_or_removed(community_id, context.pool()).await?;
49 CommunityFollower::follow(context.pool(), &community_follower_form)
51 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
53 CommunityFollower::unfollow(context.pool(), &community_follower_form)
55 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
57 } else if data.follow {
58 // Dont actually add to the community followers here, because you need
59 // to wait for the accept
60 FollowCommunityApub::send(&local_user_view.person.clone().into(), &community, context)
63 UndoFollow::send(&local_user_view.person.clone().into(), &community, context).await?;
64 CommunityFollower::unfollow(context.pool(), &community_follower_form)
66 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
69 let community_id = data.community_id;
70 let person_id = local_user_view.person.id;
71 let community_view = CommunityView::read(context.pool(), community_id, Some(person_id)).await?;
73 Ok(Self::Response { community_view })