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