3 following::accept::AcceptFollowCommunity,
8 context::lemmy_context,
9 fetcher::object_id::ObjectId,
12 use activitystreams::{
13 activity::kind::FollowType,
15 primitives::OneOrMany,
18 use lemmy_api_common::blocking;
21 traits::{ActivityFields, ActivityHandler, ActorType},
22 verify::verify_urls_match,
24 use lemmy_db_schema::{
26 community::{Community, CommunityFollower, CommunityFollowerForm},
31 use lemmy_utils::LemmyError;
32 use lemmy_websocket::LemmyContext;
33 use serde::{Deserialize, Serialize};
36 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
37 #[serde(rename_all = "camelCase")]
38 pub struct FollowCommunity {
39 pub(in crate::activities::following) actor: ObjectId<Person>,
40 // TODO: is there any reason to put the same community id twice, in to and object?
41 pub(in crate::activities::following) to: ObjectId<Community>,
42 pub(in crate::activities::following) object: ObjectId<Community>,
43 #[serde(rename = "type")]
46 #[serde(rename = "@context")]
47 context: OneOrMany<AnyBase>,
52 impl FollowCommunity {
53 pub(in crate::activities::following) fn new(
55 community: &Community,
56 context: &LemmyContext,
57 ) -> Result<FollowCommunity, LemmyError> {
59 actor: ObjectId::new(actor.actor_id()),
60 to: ObjectId::new(community.actor_id()),
61 object: ObjectId::new(community.actor_id()),
62 kind: FollowType::Follow,
63 id: generate_activity_id(
65 &context.settings().get_protocol_and_hostname(),
67 context: lemmy_context(),
68 unparsed: Default::default(),
73 community: &Community,
74 context: &LemmyContext,
75 ) -> Result<(), LemmyError> {
76 let community_follower_form = CommunityFollowerForm {
77 community_id: community.id,
81 blocking(context.pool(), move |conn| {
82 CommunityFollower::follow(conn, &community_follower_form).ok()
86 let follow = FollowCommunity::new(actor, community, context)?;
87 let inbox = vec![community.inbox_url.clone().into()];
88 send_lemmy_activity(context, &follow, &follow.id, actor, inbox, true).await
92 #[async_trait::async_trait(?Send)]
93 impl ActivityHandler for FollowCommunity {
94 type DataType = LemmyContext;
97 context: &Data<LemmyContext>,
98 request_counter: &mut i32,
99 ) -> Result<(), LemmyError> {
100 verify_activity(self, &context.settings())?;
101 verify_urls_match(self.to.inner(), self.object.inner())?;
102 verify_person(&self.actor, context, request_counter).await?;
108 context: &Data<LemmyContext>,
109 request_counter: &mut i32,
110 ) -> Result<(), LemmyError> {
111 let actor = self.actor.dereference(context, request_counter).await?;
112 let community = self.object.dereference(context, request_counter).await?;
113 let community_follower_form = CommunityFollowerForm {
114 community_id: community.id,
119 // This will fail if they're already a follower, but ignore the error.
120 blocking(context.pool(), move |conn| {
121 CommunityFollower::follow(conn, &community_follower_form).ok()
125 AcceptFollowCommunity::send(self, context, request_counter).await