7 verify_person_in_community,
9 objects::{community::ApubCommunity, person::ApubPerson},
10 protocol::activities::following::{accept::AcceptFollowCommunity, follow::FollowCommunity},
12 use activitystreams_kinds::activity::FollowType;
13 use lemmy_api_common::utils::blocking;
17 traits::{ActivityHandler, ActorType},
19 use lemmy_db_schema::{
20 source::community::{CommunityFollower, CommunityFollowerForm},
23 use lemmy_utils::LemmyError;
24 use lemmy_websocket::LemmyContext;
26 impl FollowCommunity {
27 pub(in crate::activities::following) fn new(
29 community: &ApubCommunity,
30 context: &LemmyContext,
31 ) -> Result<FollowCommunity, LemmyError> {
33 actor: ObjectId::new(actor.actor_id()),
34 object: ObjectId::new(community.actor_id()),
35 kind: FollowType::Follow,
36 id: generate_activity_id(
38 &context.settings().get_protocol_and_hostname(),
40 unparsed: Default::default(),
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 blocking(context.pool(), move |conn| {
56 CommunityFollower::follow(conn, &community_follower_form).ok()
60 let follow = FollowCommunity::new(actor, community, context)?;
61 let inbox = vec![community.inbox_url.clone().into()];
62 send_lemmy_activity(context, &follow, &follow.id, actor, inbox, true).await
66 #[async_trait::async_trait(?Send)]
67 impl ActivityHandler for FollowCommunity {
68 type DataType = LemmyContext;
70 #[tracing::instrument(skip_all)]
73 context: &Data<LemmyContext>,
74 request_counter: &mut i32,
75 ) -> Result<(), LemmyError> {
76 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
77 verify_person(&self.actor, context, request_counter).await?;
80 .dereference(context, context.client(), request_counter)
82 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
86 #[tracing::instrument(skip_all)]
89 context: &Data<LemmyContext>,
90 request_counter: &mut i32,
91 ) -> Result<(), LemmyError> {
94 .dereference(context, context.client(), request_counter)
98 .dereference(context, context.client(), request_counter)
100 let community_follower_form = CommunityFollowerForm {
101 community_id: community.id,
102 person_id: person.id,
103 pending: Some(false),
106 // This will fail if they're already a follower, but ignore the error.
107 blocking(context.pool(), move |conn| {
108 CommunityFollower::follow(conn, &community_follower_form).ok()
112 AcceptFollowCommunity::send(self, context, request_counter).await