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(),
44 #[tracing::instrument(skip_all)]
47 community: &ApubCommunity,
48 context: &LemmyContext,
49 ) -> Result<(), LemmyError> {
50 let community_follower_form = CommunityFollowerForm {
51 community_id: community.id,
55 CommunityFollower::follow(context.pool(), &community_follower_form)
59 let follow = FollowCommunity::new(actor, community, context)?;
60 let inbox = vec![community.shared_inbox_or_inbox()];
61 send_lemmy_activity(context, follow, actor, inbox, true).await
65 #[async_trait::async_trait(?Send)]
66 impl ActivityHandler for FollowCommunity {
67 type DataType = LemmyContext;
68 type Error = LemmyError;
70 fn id(&self) -> &Url {
74 fn actor(&self) -> &Url {
78 #[tracing::instrument(skip_all)]
81 context: &Data<LemmyContext>,
82 request_counter: &mut i32,
83 ) -> Result<(), LemmyError> {
84 verify_person(&self.actor, context, request_counter).await?;
87 .dereference(context, local_instance(context).await, request_counter)
89 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
93 #[tracing::instrument(skip_all)]
96 context: &Data<LemmyContext>,
97 request_counter: &mut i32,
98 ) -> Result<(), LemmyError> {
101 .dereference(context, local_instance(context).await, request_counter)
105 .dereference(context, local_instance(context).await, request_counter)
107 let community_follower_form = CommunityFollowerForm {
108 community_id: community.id,
109 person_id: person.id,
113 // This will fail if they're already a follower, but ignore the error.
114 CommunityFollower::follow(context.pool(), &community_follower_form)
118 AcceptFollowCommunity::send(self, context, request_counter).await