]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/activities/community/mod.rs
Implement separate mod activities for feature, lock post (#2716)
[lemmy.git] / crates / apub / src / activities / community / mod.rs
index 81152d926e4d19f12e4b93358992bca7ea44c1c8..226d5d64310b927652f5d95a46c1e0f5f5b08085 100644 (file)
@@ -1,35 +1,64 @@
-use lemmy_api_common::{blocking, community::CommunityResponse};
-use lemmy_db_schema::CommunityId;
-use lemmy_db_views_actor::community_view::CommunityView;
-use lemmy_utils::LemmyError;
-use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext};
+use crate::{
+  activities::send_lemmy_activity,
+  activity_lists::AnnouncableActivities,
+  objects::{community::ApubCommunity, person::ApubPerson},
+  protocol::activities::community::announce::AnnounceActivity,
+};
+use activitypub_federation::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 undo_block_user;
+pub mod collection_add;
+pub mod collection_remove;
+pub mod lock_page;
+pub mod report;
 pub mod update;
 
-pub(crate) async fn send_websocket_message<
-  OP: ToString + Send + lemmy_websocket::OperationType + 'static,
->(
-  community_id: CommunityId,
-  op: OP,
+/// 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,
+  actor: &ApubPerson,
+  community: &ApubCommunity,
+  extra_inboxes: Vec<Url>,
+  is_mod_action: bool,
   context: &LemmyContext,
 ) -> Result<(), LemmyError> {
-  let community_view = blocking(context.pool(), move |conn| {
-    CommunityView::read(conn, community_id, None)
-  })
-  .await??;
+  // send to extra_inboxes
+  send_lemmy_activity(context, activity.clone(), actor, extra_inboxes, false).await?;
 
-  let res = CommunityResponse { community_view };
+  if community.local {
+    // 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
+    let inbox = vec![community.shared_inbox_or_inbox()];
+    send_lemmy_activity(context, activity.clone(), actor, inbox, false).await?;
+  }
 
-  context.chat_server().do_send(SendCommunityRoomMessage {
-    op,
-    response: res,
-    community_id,
-    websocket_id: None,
-  });
+  // send to those who follow `actor`
+  if !is_mod_action {
+    let inboxes = PersonFollower::list_followers(context.pool(), actor.id)
+      .await?
+      .into_iter()
+      .map(|p| ApubPerson(p).shared_inbox_or_inbox())
+      .collect();
+    send_lemmy_activity(context, activity, actor, inboxes, false).await?;
+  }
 
   Ok(())
 }