]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/mod.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / apub / src / activities / community / mod.rs
1 use crate::{
2   activities::send_lemmy_activity,
3   activity_lists::AnnouncableActivities,
4   objects::{community::ApubCommunity, person::ApubPerson},
5   protocol::activities::community::announce::AnnounceActivity,
6 };
7 use activitypub_federation::{config::Data, traits::Actor};
8 use lemmy_api_common::context::LemmyContext;
9 use lemmy_db_schema::source::person::PersonFollower;
10 use lemmy_utils::error::LemmyError;
11 use url::Url;
12
13 pub mod announce;
14 pub mod collection_add;
15 pub mod collection_remove;
16 pub mod lock_page;
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: &Data<LemmyContext>,
40 ) -> Result<(), LemmyError> {
41   // send to any users which are mentioned or affected directly
42   let mut inboxes = extra_inboxes;
43
44   // send to user followers
45   if !is_mod_action {
46     inboxes.extend(
47       &mut PersonFollower::list_followers(&mut context.pool(), actor.id)
48         .await?
49         .into_iter()
50         .map(|p| ApubPerson(p).shared_inbox_or_inbox()),
51     );
52   }
53
54   if community.local {
55     // send directly to community followers
56     AnnounceActivity::send(activity.clone().try_into()?, community, context).await?;
57   } else {
58     // send to the community, which will then forward to followers
59     inboxes.push(community.shared_inbox_or_inbox());
60   }
61
62   send_lemmy_activity(context, activity.clone(), actor, inboxes, false).await?;
63   Ok(())
64 }