3 following::accept::AcceptFollowCommunity,
8 activity_queue::send_activity_new,
9 extensions::context::lemmy_context,
10 fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
13 use activitystreams::activity::kind::FollowType;
14 use lemmy_api_common::blocking;
15 use lemmy_apub_lib::{verify_urls_match, ActivityCommonFields, ActivityHandler};
16 use lemmy_db_queries::Followable;
17 use lemmy_db_schema::source::{
18 community::{Community, CommunityFollower, CommunityFollowerForm},
21 use lemmy_utils::LemmyError;
22 use lemmy_websocket::LemmyContext;
25 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
26 #[serde(rename_all = "camelCase")]
27 pub struct FollowCommunity {
28 pub(in crate::activities::following) to: Url,
29 pub(in crate::activities::following) object: Url,
30 #[serde(rename = "type")]
31 pub(in crate::activities::following) kind: FollowType,
33 pub(in crate::activities::following) common: ActivityCommonFields,
36 impl FollowCommunity {
39 community: &Community,
40 context: &LemmyContext,
41 ) -> Result<(), LemmyError> {
42 let community_follower_form = CommunityFollowerForm {
43 community_id: community.id,
47 blocking(context.pool(), move |conn| {
48 CommunityFollower::follow(conn, &community_follower_form).ok()
52 let id = generate_activity_id(FollowType::Follow)?;
53 let follow = FollowCommunity {
54 to: community.actor_id(),
55 object: community.actor_id(),
56 kind: FollowType::Follow,
57 common: ActivityCommonFields {
58 context: lemmy_context(),
60 actor: actor.actor_id(),
61 unparsed: Default::default(),
64 let inbox = vec![community.inbox_url.clone().into()];
65 send_activity_new(context, &follow, &id, actor, inbox, true).await
69 #[async_trait::async_trait(?Send)]
70 impl ActivityHandler for FollowCommunity {
73 context: &LemmyContext,
74 request_counter: &mut i32,
75 ) -> Result<(), LemmyError> {
76 verify_activity(self.common())?;
77 verify_urls_match(&self.to, &self.object)?;
78 verify_person(&self.common.actor, context, request_counter).await?;
84 context: &LemmyContext,
85 request_counter: &mut i32,
86 ) -> Result<(), LemmyError> {
88 get_or_fetch_and_upsert_person(&self.common.actor, context, request_counter).await?;
90 get_or_fetch_and_upsert_community(&self.object, context, request_counter).await?;
91 let community_follower_form = CommunityFollowerForm {
92 community_id: community.id,
97 // This will fail if they're already a follower, but ignore the error.
98 blocking(context.pool(), move |conn| {
99 CommunityFollower::follow(conn, &community_follower_form).ok()
103 AcceptFollowCommunity::send(self, context).await
106 fn common(&self) -> &ActivityCommonFields {