2 activities::{generate_activity_id, send_lemmy_activity, verify_person},
3 fetcher::user_or_community::UserOrCommunity,
5 objects::{community::ApubCommunity, person::ApubPerson},
6 protocol::activities::following::{follow::Follow, undo_follow::UndoFollow},
8 use activitypub_federation::{
10 kinds::activity::UndoType,
11 protocol::verification::verify_urls_match,
12 traits::{ActivityHandler, Actor},
14 use lemmy_api_common::context::LemmyContext;
15 use lemmy_db_schema::{
17 community::{CommunityFollower, CommunityFollowerForm},
18 person::{PersonFollower, PersonFollowerForm},
22 use lemmy_utils::error::LemmyError;
26 #[tracing::instrument(skip_all)]
29 community: &ApubCommunity,
30 context: &Data<LemmyContext>,
31 ) -> Result<(), LemmyError> {
32 let object = Follow::new(actor, community, context)?;
33 let undo = UndoFollow {
34 actor: actor.id().into(),
37 id: generate_activity_id(
39 &context.settings().get_protocol_and_hostname(),
42 let inbox = vec![community.shared_inbox_or_inbox()];
43 send_lemmy_activity(context, undo, actor, inbox, true).await
47 #[async_trait::async_trait]
48 impl ActivityHandler for UndoFollow {
49 type DataType = LemmyContext;
50 type Error = LemmyError;
52 fn id(&self) -> &Url {
56 fn actor(&self) -> &Url {
60 #[tracing::instrument(skip_all)]
61 async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
62 verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
63 verify_person(&self.actor, context).await?;
64 self.object.verify(context).await?;
68 #[tracing::instrument(skip_all)]
69 async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
70 insert_activity(&self.id, &self, false, true, context).await?;
71 let person = self.actor.dereference(context).await?;
72 let object = self.object.object.dereference(context).await?;
75 UserOrCommunity::User(u) => {
76 let form = PersonFollowerForm {
78 follower_id: person.id,
81 PersonFollower::unfollow(context.pool(), &form).await?;
83 UserOrCommunity::Community(c) => {
84 let form = CommunityFollowerForm {
89 CommunityFollower::unfollow(context.pool(), &form).await?;