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::{activity::kind::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;
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 unparsed: Default::default(),
56 let activity = AnnouncableActivities::AddMod(add);
57 let inboxes = vec![added_mod.shared_inbox_or_inbox_url()];
58 send_activity_in_community(activity, &id, actor, community, inboxes, context).await
62 #[async_trait::async_trait(?Send)]
63 impl ActivityHandler for AddMod {
64 type DataType = LemmyContext;
68 context: &Data<LemmyContext>,
69 request_counter: &mut i32,
70 ) -> Result<(), LemmyError> {
71 verify_is_public(&self.to, &self.cc)?;
72 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
73 let community = self.get_community(context, request_counter).await?;
74 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
75 verify_mod_action(&self.actor, &community, context, request_counter).await?;
76 verify_add_remove_moderator_target(&self.target, &community)?;
82 context: &Data<LemmyContext>,
83 request_counter: &mut i32,
84 ) -> Result<(), LemmyError> {
85 let community = self.get_community(context, request_counter).await?;
86 let new_mod = self.object.dereference(context, request_counter).await?;
88 // If we had to refetch the community while parsing the activity, then the new mod has already
89 // been added. Skip it here as it would result in a duplicate key error.
90 let new_mod_id = new_mod.id;
91 let moderated_communities = blocking(context.pool(), move |conn| {
92 CommunityModerator::get_person_moderated_communities(conn, new_mod_id)
95 if !moderated_communities.contains(&community.id) {
96 let form = CommunityModeratorForm {
97 community_id: community.id,
98 person_id: new_mod.id,
100 blocking(context.pool(), move |conn| {
101 CommunityModerator::join(conn, &form)
105 // TODO: send websocket notification about added mod
110 #[async_trait::async_trait(?Send)]
111 impl GetCommunity for AddMod {
112 async fn get_community(
114 context: &LemmyContext,
115 request_counter: &mut i32,
116 ) -> Result<ApubCommunity, LemmyError> {
117 get_community_from_moderators_url(&self.target, context, request_counter).await