]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/undo_block_user.rs
Rewrite activitypub following, person, community, pm (#1692)
[lemmy.git] / crates / apub / src / activities / community / undo_block_user.rs
1 use crate::{
2   activities::{
3     community::block_user::BlockUserFromCommunity,
4     verify_activity,
5     verify_mod_action,
6     verify_person_in_community,
7   },
8   fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
9 };
10 use activitystreams::activity::kind::UndoType;
11 use lemmy_api_common::blocking;
12 use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
13 use lemmy_db_queries::Bannable;
14 use lemmy_db_schema::source::community::{CommunityPersonBan, CommunityPersonBanForm};
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 UndoBlockUserFromCommunity {
22   to: PublicUrl,
23   object: BlockUserFromCommunity,
24   cc: [Url; 1],
25   #[serde(rename = "type")]
26   kind: UndoType,
27   #[serde(flatten)]
28   common: ActivityCommonFields,
29 }
30
31 #[async_trait::async_trait(?Send)]
32 impl ActivityHandler for UndoBlockUserFromCommunity {
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[0], context, request_counter).await?;
40     verify_mod_action(&self.common.actor, self.cc[0].clone(), context).await?;
41     self.object.verify(context, request_counter).await?;
42     Ok(())
43   }
44
45   async fn receive(
46     self,
47     context: &LemmyContext,
48     request_counter: &mut i32,
49   ) -> Result<(), LemmyError> {
50     let community =
51       get_or_fetch_and_upsert_community(&self.cc[0], context, request_counter).await?;
52     let blocked_user =
53       get_or_fetch_and_upsert_person(&self.object.object, context, request_counter).await?;
54
55     let community_user_ban_form = CommunityPersonBanForm {
56       community_id: community.id,
57       person_id: blocked_user.id,
58     };
59
60     blocking(context.pool(), move |conn: &'_ _| {
61       CommunityPersonBan::unban(conn, &community_user_ban_form)
62     })
63     .await??;
64
65     Ok(())
66   }
67
68   fn common(&self) -> &ActivityCommonFields {
69     &self.common
70   }
71 }