3 community::{announce::GetCommunity, get_community_from_moderators_url, send_to_community},
6 verify_add_remove_moderator_target,
9 verify_person_in_community,
11 activity_lists::AnnouncableActivities,
12 generate_moderators_url,
13 objects::{community::ApubCommunity, person::ApubPerson},
14 protocol::activities::community::add_mod::AddMod,
16 use activitystreams::{activity::kind::AddType, public};
17 use lemmy_api_common::blocking;
21 traits::{ActivityHandler, ActorType},
23 use lemmy_db_schema::{
24 source::community::{CommunityModerator, CommunityModeratorForm},
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::LemmyContext;
32 community: &ApubCommunity,
33 added_mod: &ApubPerson,
35 context: &LemmyContext,
36 ) -> Result<(), LemmyError> {
37 let id = generate_activity_id(
39 &context.settings().get_protocol_and_hostname(),
42 actor: ObjectId::new(actor.actor_id()),
44 object: ObjectId::new(added_mod.actor_id()),
45 target: generate_moderators_url(&community.actor_id)?.into(),
46 cc: vec![community.actor_id()],
49 unparsed: Default::default(),
52 let activity = AnnouncableActivities::AddMod(add);
53 let inboxes = vec![added_mod.shared_inbox_or_inbox_url()];
54 send_to_community(activity, &id, actor, community, inboxes, context).await
58 #[async_trait::async_trait(?Send)]
59 impl ActivityHandler for AddMod {
60 type DataType = LemmyContext;
64 context: &Data<LemmyContext>,
65 request_counter: &mut i32,
66 ) -> Result<(), LemmyError> {
67 verify_is_public(&self.to, &self.cc)?;
68 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
69 let community = self.get_community(context, request_counter).await?;
70 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
71 verify_mod_action(&self.actor, &community, context, request_counter).await?;
72 verify_add_remove_moderator_target(&self.target, &community)?;
78 context: &Data<LemmyContext>,
79 request_counter: &mut i32,
80 ) -> Result<(), LemmyError> {
81 let community = self.get_community(context, request_counter).await?;
82 let new_mod = self.object.dereference(context, request_counter).await?;
84 // If we had to refetch the community while parsing the activity, then the new mod has already
85 // been added. Skip it here as it would result in a duplicate key error.
86 let new_mod_id = new_mod.id;
87 let moderated_communities = blocking(context.pool(), move |conn| {
88 CommunityModerator::get_person_moderated_communities(conn, new_mod_id)
91 if !moderated_communities.contains(&community.id) {
92 let form = CommunityModeratorForm {
93 community_id: community.id,
94 person_id: new_mod.id,
96 blocking(context.pool(), move |conn| {
97 CommunityModerator::join(conn, &form)
101 // TODO: send websocket notification about added mod
106 #[async_trait::async_trait(?Send)]
107 impl GetCommunity for AddMod {
108 async fn get_community(
110 context: &LemmyContext,
111 request_counter: &mut i32,
112 ) -> Result<ApubCommunity, LemmyError> {
113 get_community_from_moderators_url(&self.target, context, request_counter).await