3 following::accept::AcceptFollowCommunity,
8 activity_queue::send_activity_new,
9 extensions::context::lemmy_context,
10 fetcher::object_id::ObjectId,
13 use activitystreams::{
14 activity::kind::FollowType,
16 primitives::OneOrMany,
19 use lemmy_api_common::blocking;
20 use lemmy_apub_lib::{verify_urls_match, ActivityFields, ActivityHandler};
21 use lemmy_db_queries::Followable;
22 use lemmy_db_schema::source::{
23 community::{Community, CommunityFollower, CommunityFollowerForm},
26 use lemmy_utils::LemmyError;
27 use lemmy_websocket::LemmyContext;
28 use serde::{Deserialize, Serialize};
31 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
32 #[serde(rename_all = "camelCase")]
33 pub struct FollowCommunity {
34 actor: ObjectId<Person>,
35 // TODO: is there any reason to put the same community id twice, in to and object?
36 pub(in crate::activities::following) to: ObjectId<Community>,
37 pub(in crate::activities::following) object: ObjectId<Community>,
38 #[serde(rename = "type")]
41 #[serde(rename = "@context")]
42 context: OneOrMany<AnyBase>,
47 impl FollowCommunity {
48 pub(in crate::activities::following) fn new(
50 community: &Community,
51 ) -> Result<FollowCommunity, LemmyError> {
53 actor: ObjectId::new(actor.actor_id()),
54 to: ObjectId::new(community.actor_id()),
55 object: ObjectId::new(community.actor_id()),
56 kind: FollowType::Follow,
57 id: generate_activity_id(FollowType::Follow)?,
58 context: lemmy_context(),
59 unparsed: Default::default(),
64 community: &Community,
65 context: &LemmyContext,
66 ) -> Result<(), LemmyError> {
67 let community_follower_form = CommunityFollowerForm {
68 community_id: community.id,
72 blocking(context.pool(), move |conn| {
73 CommunityFollower::follow(conn, &community_follower_form).ok()
77 let follow = FollowCommunity::new(actor, community)?;
78 let inbox = vec![community.inbox_url.clone().into()];
79 send_activity_new(context, &follow, &follow.id, actor, inbox, true).await
83 #[async_trait::async_trait(?Send)]
84 impl ActivityHandler for FollowCommunity {
87 context: &LemmyContext,
88 request_counter: &mut i32,
89 ) -> Result<(), LemmyError> {
90 verify_activity(self)?;
91 verify_urls_match(self.to.inner(), self.object.inner())?;
92 verify_person(&self.actor, context, request_counter).await?;
98 context: &LemmyContext,
99 request_counter: &mut i32,
100 ) -> Result<(), LemmyError> {
101 let actor = self.actor.dereference(context, request_counter).await?;
102 let community = self.object.dereference(context, request_counter).await?;
103 let community_follower_form = CommunityFollowerForm {
104 community_id: community.id,
109 // This will fail if they're already a follower, but ignore the error.
110 blocking(context.pool(), move |conn| {
111 CommunityFollower::follow(conn, &community_follower_form).ok()
115 AcceptFollowCommunity::send(self, context).await