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::{
29 community::{CommunityModerator, CommunityModeratorForm},
30 moderator::{ModAddCommunity, ModAddCommunityForm},
32 traits::{Crud, Joinable},
34 use lemmy_utils::LemmyError;
35 use lemmy_websocket::LemmyContext;
38 #[tracing::instrument(skip_all)]
40 community: &ApubCommunity,
41 added_mod: &ApubPerson,
43 context: &LemmyContext,
44 ) -> Result<(), LemmyError> {
45 let id = generate_activity_id(
47 &context.settings().get_protocol_and_hostname(),
50 actor: ObjectId::new(actor.actor_id()),
52 object: ObjectId::new(added_mod.actor_id()),
53 target: generate_moderators_url(&community.actor_id)?.into(),
54 cc: vec![community.actor_id()],
57 unparsed: Default::default(),
60 let activity = AnnouncableActivities::AddMod(add);
61 let inboxes = vec![added_mod.shared_inbox_or_inbox_url()];
62 send_activity_in_community(activity, &id, actor, community, inboxes, context).await
66 #[async_trait::async_trait(?Send)]
67 impl ActivityHandler for AddMod {
68 type DataType = LemmyContext;
70 #[tracing::instrument(skip_all)]
73 context: &Data<LemmyContext>,
74 request_counter: &mut i32,
75 ) -> Result<(), LemmyError> {
76 verify_is_public(&self.to, &self.cc)?;
77 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
78 let community = self.get_community(context, request_counter).await?;
79 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
88 verify_add_remove_moderator_target(&self.target, &community)?;
92 #[tracing::instrument(skip_all)]
95 context: &Data<LemmyContext>,
96 request_counter: &mut i32,
97 ) -> Result<(), LemmyError> {
98 let community = self.get_community(context, request_counter).await?;
101 .dereference(context, context.client(), request_counter)
104 // If we had to refetch the community while parsing the activity, then the new mod has already
105 // been added. Skip it here as it would result in a duplicate key error.
106 let new_mod_id = new_mod.id;
107 let moderated_communities = blocking(context.pool(), move |conn| {
108 CommunityModerator::get_person_moderated_communities(conn, new_mod_id)
111 if !moderated_communities.contains(&community.id) {
112 let form = CommunityModeratorForm {
113 community_id: community.id,
114 person_id: new_mod.id,
116 blocking(context.pool(), move |conn| {
117 CommunityModerator::join(conn, &form)
124 .dereference(context, context.client(), 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 blocking(context.pool(), move |conn| {
133 ModAddCommunity::create(conn, &form)
137 // TODO: send websocket notification about added mod
142 #[async_trait::async_trait(?Send)]
143 impl GetCommunity for AddMod {
144 #[tracing::instrument(skip_all)]
145 async fn get_community(
147 context: &LemmyContext,
148 request_counter: &mut i32,
149 ) -> Result<ApubCommunity, LemmyError> {
150 get_community_from_moderators_url(&self.target, context, request_counter).await