]> Untitled Git - lemmy.git/blob - crates/api/src/community/follow.rs
Show deleted and removed posts for profile views. Fixes #2624 (#2729)
[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   context::LemmyContext,
6   utils::{check_community_ban, check_community_deleted_or_removed, get_local_user_view_from_jwt},
7 };
8 use lemmy_db_schema::{
9   source::{
10     actor_language::CommunityLanguage,
11     community::{Community, CommunityFollower, CommunityFollowerForm},
12   },
13   traits::{Crud, Followable},
14 };
15 use lemmy_db_views_actor::structs::CommunityView;
16 use lemmy_utils::{error::LemmyError, ConnectionId};
17
18 #[async_trait::async_trait(?Send)]
19 impl Perform for FollowCommunity {
20   type Response = CommunityResponse;
21
22   #[tracing::instrument(skip(context, _websocket_id))]
23   async fn perform(
24     &self,
25     context: &Data<LemmyContext>,
26     _websocket_id: Option<ConnectionId>,
27   ) -> Result<CommunityResponse, LemmyError> {
28     let data: &FollowCommunity = self;
29     let local_user_view =
30       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
31
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,
37       pending: false,
38     };
39
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?;
43
44       CommunityFollower::follow(context.pool(), &community_follower_form)
45         .await
46         .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
47     }
48     if !data.follow {
49       CommunityFollower::unfollow(context.pool(), &community_follower_form)
50         .await
51         .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
52     }
53
54     let community_id = data.community_id;
55     let person_id = local_user_view.person.id;
56     let community_view =
57       CommunityView::read(context.pool(), community_id, Some(person_id), None).await?;
58     let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?;
59
60     Ok(Self::Response {
61       community_view,
62       discussion_languages,
63     })
64   }
65 }