3 community::send_activity_in_community,
5 verify_add_remove_moderator_target,
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
11 generate_moderators_url,
13 objects::{community::ApubCommunity, person::ApubPerson},
14 protocol::{activities::community::add_mod::AddMod, InCommunity},
17 use activitypub_federation::{
18 core::object_id::ObjectId,
20 traits::{ActivityHandler, Actor},
22 use activitystreams_kinds::{activity::AddType, public};
23 use lemmy_db_schema::{
25 community::{CommunityModerator, CommunityModeratorForm},
26 moderator::{ModAddCommunity, ModAddCommunityForm},
28 traits::{Crud, Joinable},
30 use lemmy_utils::error::LemmyError;
31 use lemmy_websocket::LemmyContext;
35 #[tracing::instrument(skip_all)]
37 community: &ApubCommunity,
38 added_mod: &ApubPerson,
40 context: &LemmyContext,
41 ) -> Result<(), LemmyError> {
42 let id = generate_activity_id(
44 &context.settings().get_protocol_and_hostname(),
47 actor: ObjectId::new(actor.actor_id()),
49 object: ObjectId::new(added_mod.actor_id()),
50 target: generate_moderators_url(&community.actor_id)?.into(),
51 cc: vec![community.actor_id()],
54 audience: Some(ObjectId::new(community.actor_id())),
57 let activity = AnnouncableActivities::AddMod(add);
58 let inboxes = vec![added_mod.shared_inbox_or_inbox()];
59 send_activity_in_community(activity, actor, community, inboxes, true, context).await
63 #[async_trait::async_trait(?Send)]
64 impl ActivityHandler for AddMod {
65 type DataType = LemmyContext;
66 type Error = LemmyError;
68 fn id(&self) -> &Url {
72 fn actor(&self) -> &Url {
76 #[tracing::instrument(skip_all)]
79 context: &Data<LemmyContext>,
80 request_counter: &mut i32,
81 ) -> Result<(), LemmyError> {
82 verify_is_public(&self.to, &self.cc)?;
83 let community = self.community(context, request_counter).await?;
84 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
93 verify_add_remove_moderator_target(&self.target, &community)?;
97 #[tracing::instrument(skip_all)]
100 context: &Data<LemmyContext>,
101 request_counter: &mut i32,
102 ) -> Result<(), LemmyError> {
103 let community = self.community(context, request_counter).await?;
106 .dereference(context, local_instance(context).await, request_counter)
109 // If we had to refetch the community while parsing the activity, then the new mod has already
110 // been added. Skip it here as it would result in a duplicate key error.
111 let new_mod_id = new_mod.id;
112 let moderated_communities =
113 CommunityModerator::get_person_moderated_communities(context.pool(), new_mod_id).await?;
114 if !moderated_communities.contains(&community.id) {
115 let form = CommunityModeratorForm {
116 community_id: community.id,
117 person_id: new_mod.id,
119 CommunityModerator::join(context.pool(), &form).await?;
124 .dereference(context, local_instance(context).await, request_counter)
126 let form = ModAddCommunityForm {
127 mod_person_id: actor.id,
128 other_person_id: new_mod.id,
129 community_id: community.id,
130 removed: Some(false),
132 ModAddCommunity::create(context.pool(), &form).await?;
134 // TODO: send websocket notification about added mod