6 verify_person_in_community,
9 objects::{community::ApubCommunity, person::ApubPerson},
10 protocol::activities::following::{accept::AcceptFollowCommunity, follow::FollowCommunity},
13 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
14 use activitystreams_kinds::activity::FollowType;
15 use lemmy_api_common::utils::blocking;
16 use lemmy_db_schema::{
17 source::community::{CommunityFollower, CommunityFollowerForm},
20 use lemmy_utils::error::LemmyError;
21 use lemmy_websocket::LemmyContext;
24 impl FollowCommunity {
25 pub(in crate::activities::following) fn new(
27 community: &ApubCommunity,
28 context: &LemmyContext,
29 ) -> Result<FollowCommunity, LemmyError> {
31 actor: ObjectId::new(actor.actor_id()),
32 object: ObjectId::new(community.actor_id()),
33 kind: FollowType::Follow,
34 id: generate_activity_id(
36 &context.settings().get_protocol_and_hostname(),
38 unparsed: Default::default(),
42 #[tracing::instrument(skip_all)]
45 community: &ApubCommunity,
46 context: &LemmyContext,
47 ) -> Result<(), LemmyError> {
48 let community_follower_form = CommunityFollowerForm {
49 community_id: community.id,
53 blocking(context.pool(), move |conn| {
54 CommunityFollower::follow(conn, &community_follower_form).ok()
58 let follow = FollowCommunity::new(actor, community, context)?;
59 let inbox = vec![community.inbox_url.clone().into()];
60 send_lemmy_activity(context, &follow, &follow.id, actor, inbox, true).await
64 #[async_trait::async_trait(?Send)]
65 impl ActivityHandler for FollowCommunity {
66 type DataType = LemmyContext;
67 type Error = LemmyError;
69 fn id(&self) -> &Url {
73 fn actor(&self) -> &Url {
77 #[tracing::instrument(skip_all)]
80 context: &Data<LemmyContext>,
81 request_counter: &mut i32,
82 ) -> Result<(), LemmyError> {
83 verify_person(&self.actor, context, request_counter).await?;
86 .dereference::<LemmyError>(context, local_instance(context), request_counter)
88 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
92 #[tracing::instrument(skip_all)]
95 context: &Data<LemmyContext>,
96 request_counter: &mut i32,
97 ) -> Result<(), LemmyError> {
100 .dereference::<LemmyError>(context, local_instance(context), request_counter)
104 .dereference::<LemmyError>(context, local_instance(context), request_counter)
106 let community_follower_form = CommunityFollowerForm {
107 community_id: community.id,
108 person_id: person.id,
112 // This will fail if they're already a follower, but ignore the error.
113 blocking(context.pool(), move |conn| {
114 CommunityFollower::follow(conn, &community_follower_form).ok()
118 AcceptFollowCommunity::send(self, context, request_counter).await