6 verify_person_in_community,
8 fetcher::user_or_community::UserOrCommunity,
10 objects::{community::ApubCommunity, person::ApubPerson},
11 protocol::activities::following::{accept::AcceptFollow, follow::Follow},
14 use activitypub_federation::{
15 core::object_id::ObjectId,
17 traits::{ActivityHandler, Actor},
19 use activitystreams_kinds::activity::FollowType;
20 use lemmy_api_common::LemmyContext;
21 use lemmy_db_schema::{
23 community::{CommunityFollower, CommunityFollowerForm},
24 person::{PersonFollower, PersonFollowerForm},
28 use lemmy_utils::error::LemmyError;
32 pub(in crate::activities::following) fn new(
34 community: &ApubCommunity,
35 context: &LemmyContext,
36 ) -> Result<Follow, LemmyError> {
38 actor: ObjectId::new(actor.actor_id()),
39 object: ObjectId::new(community.actor_id()),
40 kind: FollowType::Follow,
41 id: generate_activity_id(
43 &context.settings().get_protocol_and_hostname(),
48 #[tracing::instrument(skip_all)]
51 community: &ApubCommunity,
52 context: &LemmyContext,
53 ) -> Result<(), LemmyError> {
54 let community_follower_form = CommunityFollowerForm {
55 community_id: community.id,
59 CommunityFollower::follow(context.pool(), &community_follower_form)
63 let follow = Follow::new(actor, community, context)?;
64 let inbox = vec![community.shared_inbox_or_inbox()];
65 send_lemmy_activity(context, follow, actor, inbox, true).await
69 #[async_trait::async_trait(?Send)]
70 impl ActivityHandler for Follow {
71 type DataType = LemmyContext;
72 type Error = LemmyError;
74 fn id(&self) -> &Url {
78 fn actor(&self) -> &Url {
82 #[tracing::instrument(skip_all)]
85 context: &Data<LemmyContext>,
86 request_counter: &mut i32,
87 ) -> Result<(), LemmyError> {
88 verify_person(&self.actor, context, request_counter).await?;
91 .dereference(context, local_instance(context).await, request_counter)
93 if let UserOrCommunity::Community(c) = object {
94 verify_person_in_community(&self.actor, &c, context, request_counter).await?;
99 #[tracing::instrument(skip_all)]
102 context: &Data<LemmyContext>,
103 request_counter: &mut i32,
104 ) -> Result<(), LemmyError> {
107 .dereference(context, local_instance(context).await, request_counter)
111 .dereference(context, local_instance(context).await, request_counter)
114 UserOrCommunity::User(u) => {
115 let form = PersonFollowerForm {
117 follower_id: actor.id,
120 PersonFollower::follow(context.pool(), &form).await?;
122 UserOrCommunity::Community(c) => {
123 let form = CommunityFollowerForm {
128 CommunityFollower::follow(context.pool(), &form).await?;
132 AcceptFollow::send(self, context, request_counter).await