]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/block_user.rs
Fix changelog links
[lemmy.git] / crates / apub / src / activities / community / block_user.rs
1 use crate::{
2   activities::{verify_activity, verify_mod_action, verify_person_in_community},
3   fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
4 };
5 use activitystreams::activity::kind::BlockType;
6 use lemmy_api_common::blocking;
7 use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
8 use lemmy_db_queries::{Bannable, Followable};
9 use lemmy_db_schema::source::community::{
10   CommunityFollower,
11   CommunityFollowerForm,
12   CommunityPersonBan,
13   CommunityPersonBanForm,
14 };
15 use lemmy_utils::LemmyError;
16 use lemmy_websocket::LemmyContext;
17 use url::Url;
18
19 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
20 #[serde(rename_all = "camelCase")]
21 pub struct BlockUserFromCommunity {
22   to: PublicUrl,
23   pub(in crate::activities::community) object: Url,
24   cc: [Url; 1],
25   #[serde(rename = "type")]
26   kind: BlockType,
27   #[serde(flatten)]
28   common: ActivityCommonFields,
29 }
30
31 #[async_trait::async_trait(?Send)]
32 impl ActivityHandler for BlockUserFromCommunity {
33   async fn verify(
34     &self,
35     context: &LemmyContext,
36     request_counter: &mut i32,
37   ) -> Result<(), LemmyError> {
38     verify_activity(self.common())?;
39     verify_person_in_community(&self.common.actor, &self.cc, context, request_counter).await?;
40     verify_mod_action(&self.common.actor, self.cc[0].clone(), context).await?;
41     Ok(())
42   }
43
44   async fn receive(
45     &self,
46     context: &LemmyContext,
47     request_counter: &mut i32,
48   ) -> Result<(), LemmyError> {
49     let community =
50       get_or_fetch_and_upsert_community(&self.cc[0], context, request_counter).await?;
51     let blocked_user =
52       get_or_fetch_and_upsert_person(&self.object, context, request_counter).await?;
53
54     let community_user_ban_form = CommunityPersonBanForm {
55       community_id: community.id,
56       person_id: blocked_user.id,
57     };
58
59     blocking(context.pool(), move |conn: &'_ _| {
60       CommunityPersonBan::ban(conn, &community_user_ban_form)
61     })
62     .await??;
63
64     // Also unsubscribe them from the community, if they are subscribed
65     let community_follower_form = CommunityFollowerForm {
66       community_id: community.id,
67       person_id: blocked_user.id,
68       pending: false,
69     };
70     blocking(context.pool(), move |conn: &'_ _| {
71       CommunityFollower::unfollow(conn, &community_follower_form)
72     })
73     .await?
74     .ok();
75
76     Ok(())
77   }
78
79   fn common(&self) -> &ActivityCommonFields {
80     &self.common
81   }
82 }