use crate::{
activities::send_lemmy_activity,
activity_lists::AnnouncableActivities,
- objects::community::ApubCommunity,
+ objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::community::announce::AnnounceActivity,
};
-use lemmy_apub_lib::{object_id::ObjectId, traits::ActorType};
-use lemmy_utils::LemmyError;
-use lemmy_websocket::LemmyContext;
+use activitypub_federation::{config::Data, traits::Actor};
+use lemmy_api_common::context::LemmyContext;
+use lemmy_db_schema::source::person::PersonFollower;
+use lemmy_utils::error::LemmyError;
use url::Url;
-pub mod add_mod;
pub mod announce;
-pub mod block_user;
-pub mod remove_mod;
+pub mod collection_add;
+pub mod collection_remove;
+pub mod lock_page;
pub mod report;
-pub mod undo_block_user;
pub mod update;
-#[tracing::instrument(skip_all)]
-pub(crate) async fn send_activity_in_community<T: ActorType>(
+/// This function sends all activities which are happening in a community to the right inboxes.
+/// For example Create/Page, Add/Mod etc, but not private messages.
+///
+/// Activities are sent to the community itself if it lives on another instance. If the community
+/// is local, the activity is directly wrapped into Announce and sent to community followers.
+/// Activities are also sent to those who follow the actor (with exception of moderation activities).
+///
+/// * `activity` - The activity which is being sent
+/// * `actor` - The user who is sending the activity
+/// * `community` - Community inside which the activity is sent
+/// * `inboxes` - Any additional inboxes the activity should be sent to (for example,
+/// to the user who is being promoted to moderator)
+/// * `is_mod_activity` - True for things like Add/Mod, these are not sent to user followers
+pub(crate) async fn send_activity_in_community(
activity: AnnouncableActivities,
- activity_id: &Url,
- actor: &T,
+ actor: &ApubPerson,
community: &ApubCommunity,
- mut inboxes: Vec<Url>,
- context: &LemmyContext,
+ extra_inboxes: Vec<Url>,
+ is_mod_action: bool,
+ context: &Data<LemmyContext>,
) -> Result<(), LemmyError> {
- inboxes.push(community.shared_inbox_or_inbox_url());
- send_lemmy_activity(context, &activity, activity_id, actor, inboxes, false).await?;
+ // send to any users which are mentioned or affected directly
+ let mut inboxes = extra_inboxes;
+
+ // send to user followers
+ if !is_mod_action {
+ inboxes.extend(
+ &mut PersonFollower::list_followers(context.pool(), actor.id)
+ .await?
+ .into_iter()
+ .map(|p| ApubPerson(p).shared_inbox_or_inbox()),
+ );
+ }
if community.local {
- AnnounceActivity::send(activity, community, context).await?;
+ // send directly to community followers
+ AnnounceActivity::send(activity.clone().try_into()?, community, context).await?;
+ } else {
+ // send to the community, which will then forward to followers
+ inboxes.push(community.shared_inbox_or_inbox());
}
+ send_lemmy_activity(context, activity.clone(), actor, inboxes, false).await?;
Ok(())
}
-
-#[tracing::instrument(skip_all)]
-async fn get_community_from_moderators_url(
- moderators: &Url,
- context: &LemmyContext,
- request_counter: &mut i32,
-) -> Result<ApubCommunity, LemmyError> {
- let community_id = Url::parse(&moderators.to_string().replace("/moderators", ""))?;
- ObjectId::new(community_id)
- .dereference(context, request_counter)
- .await
-}