]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/following/mod.rs
06f95e3f41672fb678a6ce1942df53c1680f2094
[lemmy.git] / crates / apub / src / activities / following / mod.rs
1 use crate::{
2   objects::community::ApubCommunity,
3   protocol::activities::following::{follow::Follow, undo_follow::UndoFollow},
4   SendActivity,
5 };
6 use activitypub_federation::config::Data;
7 use lemmy_api_common::{
8   community::{CommunityResponse, FollowCommunity},
9   context::LemmyContext,
10   utils::local_user_view_from_jwt,
11 };
12 use lemmy_db_schema::{source::community::Community, traits::Crud};
13 use lemmy_utils::error::LemmyError;
14
15 pub mod accept;
16 pub mod follow;
17 pub mod undo_follow;
18
19 #[async_trait::async_trait]
20 impl SendActivity for FollowCommunity {
21   type Response = CommunityResponse;
22
23   async fn send_activity(
24     request: &Self,
25     _response: &Self::Response,
26     context: &Data<LemmyContext>,
27   ) -> Result<(), LemmyError> {
28     let local_user_view = local_user_view_from_jwt(&request.auth, context).await?;
29     let person = local_user_view.person.clone().into();
30     let community: ApubCommunity = Community::read(&mut context.pool(), request.community_id)
31       .await?
32       .into();
33     if community.local {
34       Ok(())
35     } else if request.follow {
36       Follow::send(&person, &community, context).await
37     } else {
38       UndoFollow::send(&person, &community, context).await
39     }
40   }
41 }