6 verify_person_in_community,
9 objects::{community::ApubCommunity, person::ApubPerson},
10 protocol::activities::following::{accept::AcceptFollowCommunity, follow::FollowCommunity},
13 use activitypub_federation::{
14 core::object_id::ObjectId,
16 traits::{ActivityHandler, Actor},
18 use activitystreams_kinds::activity::FollowType;
19 use lemmy_db_schema::{
20 source::community::{CommunityFollower, CommunityFollowerForm},
23 use lemmy_utils::error::LemmyError;
24 use lemmy_websocket::LemmyContext;
27 impl FollowCommunity {
28 pub(in crate::activities::following) fn new(
30 community: &ApubCommunity,
31 context: &LemmyContext,
32 ) -> Result<FollowCommunity, LemmyError> {
34 actor: ObjectId::new(actor.actor_id()),
35 object: ObjectId::new(community.actor_id()),
36 kind: FollowType::Follow,
37 id: generate_activity_id(
39 &context.settings().get_protocol_and_hostname(),
41 unparsed: Default::default(),
45 #[tracing::instrument(skip_all)]
48 community: &ApubCommunity,
49 context: &LemmyContext,
50 ) -> Result<(), LemmyError> {
51 let community_follower_form = CommunityFollowerForm {
52 community_id: community.id,
56 CommunityFollower::follow(context.pool(), &community_follower_form)
60 let follow = FollowCommunity::new(actor, community, context)?;
61 let inbox = vec![community.shared_inbox_or_inbox()];
62 send_lemmy_activity(context, follow, actor, inbox, true).await
66 #[async_trait::async_trait(?Send)]
67 impl ActivityHandler for FollowCommunity {
68 type DataType = LemmyContext;
69 type Error = LemmyError;
71 fn id(&self) -> &Url {
75 fn actor(&self) -> &Url {
79 #[tracing::instrument(skip_all)]
82 context: &Data<LemmyContext>,
83 request_counter: &mut i32,
84 ) -> Result<(), LemmyError> {
85 verify_person(&self.actor, context, request_counter).await?;
88 .dereference(context, local_instance(context).await, request_counter)
90 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
94 #[tracing::instrument(skip_all)]
97 context: &Data<LemmyContext>,
98 request_counter: &mut i32,
99 ) -> Result<(), LemmyError> {
102 .dereference(context, local_instance(context).await, request_counter)
106 .dereference(context, local_instance(context).await, request_counter)
108 let community_follower_form = CommunityFollowerForm {
109 community_id: community.id,
110 person_id: person.id,
114 // This will fail if they're already a follower, but ignore the error.
115 CommunityFollower::follow(context.pool(), &community_follower_form)
119 AcceptFollowCommunity::send(self, context, request_counter).await