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_api_common::utils::blocking;
20 use lemmy_db_schema::{
21 source::community::{CommunityFollower, CommunityFollowerForm},
24 use lemmy_utils::error::LemmyError;
25 use lemmy_websocket::LemmyContext;
28 impl FollowCommunity {
29 pub(in crate::activities::following) fn new(
31 community: &ApubCommunity,
32 context: &LemmyContext,
33 ) -> Result<FollowCommunity, LemmyError> {
35 actor: ObjectId::new(actor.actor_id()),
36 object: ObjectId::new(community.actor_id()),
37 kind: FollowType::Follow,
38 id: generate_activity_id(
40 &context.settings().get_protocol_and_hostname(),
42 unparsed: Default::default(),
46 #[tracing::instrument(skip_all)]
49 community: &ApubCommunity,
50 context: &LemmyContext,
51 ) -> Result<(), LemmyError> {
52 let community_follower_form = CommunityFollowerForm {
53 community_id: community.id,
57 blocking(context.pool(), move |conn| {
58 CommunityFollower::follow(conn, &community_follower_form).ok()
62 let follow = FollowCommunity::new(actor, community, context)?;
63 let inbox = vec![community.shared_inbox_or_inbox()];
64 send_lemmy_activity(context, follow, actor, inbox, true).await
68 #[async_trait::async_trait(?Send)]
69 impl ActivityHandler for FollowCommunity {
70 type DataType = LemmyContext;
71 type Error = LemmyError;
73 fn id(&self) -> &Url {
77 fn actor(&self) -> &Url {
81 #[tracing::instrument(skip_all)]
84 context: &Data<LemmyContext>,
85 request_counter: &mut i32,
86 ) -> Result<(), LemmyError> {
87 verify_person(&self.actor, context, request_counter).await?;
90 .dereference(context, local_instance(context), request_counter)
92 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
96 #[tracing::instrument(skip_all)]
99 context: &Data<LemmyContext>,
100 request_counter: &mut i32,
101 ) -> Result<(), LemmyError> {
104 .dereference(context, local_instance(context), request_counter)
108 .dereference(context, local_instance(context), request_counter)
110 let community_follower_form = CommunityFollowerForm {
111 community_id: community.id,
112 person_id: person.id,
116 // This will fail if they're already a follower, but ignore the error.
117 blocking(context.pool(), move |conn| {
118 CommunityFollower::follow(conn, &community_follower_form).ok()
122 AcceptFollowCommunity::send(self, context, request_counter).await