]> Untitled Git - lemmy.git/blob - crates/api/src/community/follow.rs
15a56b17b0ba50832beaefbea9bf8e0dd6340fa1
[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::{FollowCommunity, FollowCommunityResponse},
5   utils::{
6     blocking,
7     check_community_ban,
8     check_community_deleted_or_removed,
9     get_local_user_view_from_jwt,
10   },
11 };
12 use lemmy_apub::{
13   objects::community::ApubCommunity,
14   protocol::activities::following::{
15     follow::FollowCommunity as FollowCommunityApub,
16     undo_follow::UndoFollowCommunity,
17   },
18 };
19 use lemmy_db_schema::{
20   source::community::{Community, CommunityFollower, CommunityFollowerForm},
21   traits::{Crud, Followable},
22 };
23 use lemmy_db_views_actor::structs::CommunityFollowerView;
24 use lemmy_utils::{ConnectionId, LemmyError};
25 use lemmy_websocket::LemmyContext;
26
27 #[async_trait::async_trait(?Send)]
28 impl Perform for FollowCommunity {
29   type Response = FollowCommunityResponse;
30
31   #[tracing::instrument(skip(context, _websocket_id))]
32   async fn perform(
33     &self,
34     context: &Data<LemmyContext>,
35     _websocket_id: Option<ConnectionId>,
36   ) -> Result<Self::Response, LemmyError> {
37     let data: &FollowCommunity = self;
38     let local_user_view =
39       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
40
41     let community_id = data.community_id;
42     let community: ApubCommunity = blocking(context.pool(), move |conn| {
43       Community::read(conn, community_id)
44     })
45     .await??
46     .into();
47     let community_follower_form = CommunityFollowerForm {
48       community_id: data.community_id,
49       person_id: local_user_view.person.id,
50       pending: Some(false), // Don't worry, this form isn't used for remote follows
51     };
52
53     if community.local {
54       if data.follow {
55         check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
56         check_community_deleted_or_removed(community_id, context.pool()).await?;
57
58         let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
59         blocking(context.pool(), follow)
60           .await?
61           .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
62       } else {
63         let unfollow =
64           move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
65         blocking(context.pool(), unfollow)
66           .await?
67           .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
68       }
69     } else if data.follow {
70       // Dont actually add to the community followers here, because you need
71       // to wait for the accept
72       FollowCommunityApub::send(&local_user_view.person.clone().into(), &community, context)
73         .await?;
74     } else {
75       UndoFollowCommunity::send(&local_user_view.person.clone().into(), &community, context)
76         .await?;
77       let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
78       blocking(context.pool(), unfollow)
79         .await?
80         .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
81     }
82
83     let community_id = data.community_id;
84     let person_id = local_user_view.person.id;
85     let community_follower_view = blocking(context.pool(), move |conn| {
86       CommunityFollowerView::read(conn, community_id, person_id)
87     })
88     .await?
89     .ok();
90
91     Ok(Self::Response {
92       community_follower_view,
93     })
94   }
95 }