]> Untitled Git - lemmy.git/blob - crates/api/src/community/follow.rs
8a68646e89a29c5f1f543b56f859d27d4c36eff9
[lemmy.git] / crates / api / src / community / follow.rs
1 use crate::Perform;
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},
6 };
7 use lemmy_apub::{
8   objects::community::ApubCommunity,
9   protocol::activities::following::{
10     follow::FollowCommunity as FollowCommunityApub,
11     undo_follow::UndoFollowCommunity,
12   },
13 };
14 use lemmy_db_schema::{
15   source::community::{Community, CommunityFollower, CommunityFollowerForm},
16   traits::{Crud, Followable},
17 };
18 use lemmy_db_views_actor::structs::CommunityView;
19 use lemmy_utils::{error::LemmyError, ConnectionId};
20 use lemmy_websocket::LemmyContext;
21
22 #[async_trait::async_trait(?Send)]
23 impl Perform for FollowCommunity {
24   type Response = CommunityResponse;
25
26   #[tracing::instrument(skip(context, _websocket_id))]
27   async fn perform(
28     &self,
29     context: &Data<LemmyContext>,
30     _websocket_id: Option<ConnectionId>,
31   ) -> Result<CommunityResponse, LemmyError> {
32     let data: &FollowCommunity = self;
33     let local_user_view =
34       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
35
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,
41       pending: false,
42     };
43
44     if community.local {
45       if data.follow {
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?;
48
49         CommunityFollower::follow(context.pool(), &community_follower_form)
50           .await
51           .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
52       } else {
53         CommunityFollower::unfollow(context.pool(), &community_follower_form)
54           .await
55           .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
56       }
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)
61         .await?;
62     } else {
63       UndoFollowCommunity::send(&local_user_view.person.clone().into(), &community, context)
64         .await?;
65       CommunityFollower::unfollow(context.pool(), &community_follower_form)
66         .await
67         .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
68     }
69
70     let community_id = data.community_id;
71     let person_id = local_user_view.person.id;
72     let community_view = CommunityView::read(context.pool(), community_id, Some(person_id)).await?;
73
74     Ok(Self::Response { community_view })
75   }
76 }