]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/following/undo.rs
Apub inbox rewrite (#1652)
[lemmy.git] / crates / apub / src / activities / following / undo.rs
1 use crate::{
2   activities::{following::follow::FollowCommunity, verify_activity, verify_person},
3   fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
4 };
5 use activitystreams::activity::kind::UndoType;
6 use lemmy_api_common::blocking;
7 use lemmy_apub_lib::{verify_urls_match, ActivityCommonFields, ActivityHandler};
8 use lemmy_db_queries::Followable;
9 use lemmy_db_schema::source::community::{CommunityFollower, CommunityFollowerForm};
10 use lemmy_utils::LemmyError;
11 use lemmy_websocket::LemmyContext;
12 use url::Url;
13
14 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
15 #[serde(rename_all = "camelCase")]
16 pub struct UndoFollowCommunity {
17   to: Url,
18   object: FollowCommunity,
19   #[serde(rename = "type")]
20   kind: UndoType,
21   #[serde(flatten)]
22   common: ActivityCommonFields,
23 }
24
25 #[async_trait::async_trait(?Send)]
26 impl ActivityHandler for UndoFollowCommunity {
27   async fn verify(
28     &self,
29     context: &LemmyContext,
30     request_counter: &mut i32,
31   ) -> Result<(), LemmyError> {
32     verify_activity(self.common())?;
33     verify_urls_match(&self.to, &self.object.object)?;
34     verify_urls_match(&self.common.actor, &self.object.common.actor)?;
35     verify_person(&self.common.actor, context, request_counter).await?;
36     self.object.verify(context, request_counter).await?;
37     Ok(())
38   }
39
40   async fn receive(
41     &self,
42     context: &LemmyContext,
43     request_counter: &mut i32,
44   ) -> Result<(), LemmyError> {
45     let actor =
46       get_or_fetch_and_upsert_person(&self.common.actor, context, request_counter).await?;
47     let community = get_or_fetch_and_upsert_community(&self.to, context, request_counter).await?;
48
49     let community_follower_form = CommunityFollowerForm {
50       community_id: community.id,
51       person_id: actor.id,
52       pending: false,
53     };
54
55     // This will fail if they aren't a follower, but ignore the error.
56     blocking(context.pool(), move |conn| {
57       CommunityFollower::unfollow(conn, &community_follower_form).ok()
58     })
59     .await?;
60     Ok(())
61   }
62
63   fn common(&self) -> &ActivityCommonFields {
64     &self.common
65   }
66 }