4 announce::GetCommunity,
5 get_community_from_moderators_url,
6 send_activity_in_community,
10 verify_add_remove_moderator_target,
13 verify_person_in_community,
15 activity_lists::AnnouncableActivities,
16 generate_moderators_url,
17 objects::{community::ApubCommunity, person::ApubPerson},
18 protocol::activities::community::add_mod::AddMod,
20 use activitystreams_kinds::{activity::AddType, public};
21 use lemmy_api_common::blocking;
25 traits::{ActivityHandler, ActorType},
27 use lemmy_db_schema::{
28 source::community::{CommunityModerator, CommunityModeratorForm},
31 use lemmy_utils::LemmyError;
32 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 unparsed: Default::default(),
57 let activity = AnnouncableActivities::AddMod(add);
58 let inboxes = vec![added_mod.shared_inbox_or_inbox_url()];
59 send_activity_in_community(activity, &id, actor, community, inboxes, context).await
63 #[async_trait::async_trait(?Send)]
64 impl ActivityHandler for AddMod {
65 type DataType = LemmyContext;
67 #[tracing::instrument(skip_all)]
70 context: &Data<LemmyContext>,
71 request_counter: &mut i32,
72 ) -> Result<(), LemmyError> {
73 verify_is_public(&self.to, &self.cc)?;
74 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
75 let community = self.get_community(context, request_counter).await?;
76 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
77 verify_mod_action(&self.actor, &community, context, request_counter).await?;
78 verify_add_remove_moderator_target(&self.target, &community)?;
82 #[tracing::instrument(skip_all)]
85 context: &Data<LemmyContext>,
86 request_counter: &mut i32,
87 ) -> Result<(), LemmyError> {
88 let community = self.get_community(context, request_counter).await?;
89 let new_mod = self.object.dereference(context, request_counter).await?;
91 // If we had to refetch the community while parsing the activity, then the new mod has already
92 // been added. Skip it here as it would result in a duplicate key error.
93 let new_mod_id = new_mod.id;
94 let moderated_communities = blocking(context.pool(), move |conn| {
95 CommunityModerator::get_person_moderated_communities(conn, new_mod_id)
98 if !moderated_communities.contains(&community.id) {
99 let form = CommunityModeratorForm {
100 community_id: community.id,
101 person_id: new_mod.id,
103 blocking(context.pool(), move |conn| {
104 CommunityModerator::join(conn, &form)
108 // TODO: send websocket notification about added mod
113 #[async_trait::async_trait(?Send)]
114 impl GetCommunity for AddMod {
115 #[tracing::instrument(skip_all)]
116 async fn get_community(
118 context: &LemmyContext,
119 request_counter: &mut i32,
120 ) -> Result<ApubCommunity, LemmyError> {
121 get_community_from_moderators_url(&self.target, context, request_counter).await