]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/mod.rs
Implement federated user following (fixes #752) (#2577)
[lemmy.git] / crates / apub / src / activities / community / mod.rs
1 use crate::{
2   activities::send_lemmy_activity,
3   activity_lists::AnnouncableActivities,
4   local_instance,
5   objects::{community::ApubCommunity, person::ApubPerson},
6   protocol::activities::community::announce::AnnounceActivity,
7 };
8 use activitypub_federation::{core::object_id::ObjectId, traits::Actor};
9 use lemmy_db_schema::source::person::PersonFollower;
10 use lemmy_utils::error::LemmyError;
11 use lemmy_websocket::LemmyContext;
12 use url::Url;
13
14 pub mod add_mod;
15 pub mod announce;
16 pub mod remove_mod;
17 pub mod report;
18 pub mod update;
19
20 /// This function sends all activities which are happening in a community to the right inboxes.
21 /// For example Create/Page, Add/Mod etc, but not private messages.
22 ///
23 /// Activities are sent to the community itself if it lives on another instance. If the community
24 /// is local, the activity is directly wrapped into Announce and sent to community followers.
25 /// Activities are also sent to those who follow the actor (with exception of moderation activities).
26 ///
27 /// * `activity` - The activity which is being sent
28 /// * `actor` - The user who is sending the activity
29 /// * `community` - Community inside which the activity is sent
30 /// * `inboxes` - Any additional inboxes the activity should be sent to (for example,
31 ///               to the user who is being promoted to moderator)
32 /// * `is_mod_activity` - True for things like Add/Mod, these are not sent to user followers
33 pub(crate) async fn send_activity_in_community(
34   activity: AnnouncableActivities,
35   actor: &ApubPerson,
36   community: &ApubCommunity,
37   extra_inboxes: Vec<Url>,
38   is_mod_action: bool,
39   context: &LemmyContext,
40 ) -> Result<(), LemmyError> {
41   // send to extra_inboxes
42   send_lemmy_activity(context, activity.clone(), actor, extra_inboxes, false).await?;
43
44   if community.local {
45     // send directly to community followers
46     AnnounceActivity::send(activity.clone().try_into()?, community, context).await?;
47   } else {
48     // send to the community, which will then forward to followers
49     let inbox = vec![community.shared_inbox_or_inbox()];
50     send_lemmy_activity(context, activity.clone(), actor, inbox, false).await?;
51   }
52
53   // send to those who follow `actor`
54   if !is_mod_action {
55     let inboxes = PersonFollower::list_followers(context.pool(), actor.id)
56       .await?
57       .into_iter()
58       .map(|p| ApubPerson(p).shared_inbox_or_inbox())
59       .collect();
60     send_lemmy_activity(context, activity, actor, inboxes, false).await?;
61   }
62
63   Ok(())
64 }
65
66 #[tracing::instrument(skip_all)]
67 async fn get_community_from_moderators_url(
68   moderators: &Url,
69   context: &LemmyContext,
70   request_counter: &mut i32,
71 ) -> Result<ApubCommunity, LemmyError> {
72   let community_id = Url::parse(&moderators.to_string().replace("/moderators", ""))?;
73   ObjectId::new(community_id)
74     .dereference(context, local_instance(context).await, request_counter)
75     .await
76 }