3 community::send_activity_in_community,
5 verify_add_remove_moderator_target,
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
12 objects::{community::ApubCommunity, person::ApubPerson},
13 protocol::{activities::community::add_mod::AddMod, InCommunity},
16 use activitypub_federation::{
17 core::object_id::ObjectId,
19 traits::{ActivityHandler, Actor},
21 use activitystreams_kinds::{activity::AddType, public};
22 use lemmy_api_common::{generate_moderators_url, LemmyContext};
23 use lemmy_db_schema::{
25 community::{CommunityModerator, CommunityModeratorForm},
26 moderator::{ModAddCommunity, ModAddCommunityForm},
28 traits::{Crud, Joinable},
30 use lemmy_utils::error::LemmyError;
34 #[tracing::instrument(skip_all)]
36 community: &ApubCommunity,
37 added_mod: &ApubPerson,
39 context: &LemmyContext,
40 ) -> Result<(), LemmyError> {
41 let id = generate_activity_id(
43 &context.settings().get_protocol_and_hostname(),
46 actor: ObjectId::new(actor.actor_id()),
48 object: ObjectId::new(added_mod.actor_id()),
49 target: generate_moderators_url(&community.actor_id)?.into(),
50 cc: vec![community.actor_id()],
53 audience: Some(ObjectId::new(community.actor_id())),
56 let activity = AnnouncableActivities::AddMod(add);
57 let inboxes = vec![added_mod.shared_inbox_or_inbox()];
58 send_activity_in_community(activity, actor, community, inboxes, true, context).await
62 #[async_trait::async_trait(?Send)]
63 impl ActivityHandler for AddMod {
64 type DataType = LemmyContext;
65 type Error = LemmyError;
67 fn id(&self) -> &Url {
71 fn actor(&self) -> &Url {
75 #[tracing::instrument(skip_all)]
78 context: &Data<LemmyContext>,
79 request_counter: &mut i32,
80 ) -> Result<(), LemmyError> {
81 verify_is_public(&self.to, &self.cc)?;
82 let community = self.community(context, request_counter).await?;
83 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
92 verify_add_remove_moderator_target(&self.target, &community)?;
96 #[tracing::instrument(skip_all)]
99 context: &Data<LemmyContext>,
100 request_counter: &mut i32,
101 ) -> Result<(), LemmyError> {
102 let community = self.community(context, request_counter).await?;
105 .dereference(context, local_instance(context).await, request_counter)
108 // If we had to refetch the community while parsing the activity, then the new mod has already
109 // been added. Skip it here as it would result in a duplicate key error.
110 let new_mod_id = new_mod.id;
111 let moderated_communities =
112 CommunityModerator::get_person_moderated_communities(context.pool(), new_mod_id).await?;
113 if !moderated_communities.contains(&community.id) {
114 let form = CommunityModeratorForm {
115 community_id: community.id,
116 person_id: new_mod.id,
118 CommunityModerator::join(context.pool(), &form).await?;
123 .dereference(context, local_instance(context).await, request_counter)
125 let form = ModAddCommunityForm {
126 mod_person_id: actor.id,
127 other_person_id: new_mod.id,
128 community_id: community.id,
129 removed: Some(false),
131 ModAddCommunity::create(context.pool(), &form).await?;
133 // TODO: send websocket notification about added mod