4 announce::GetCommunity,
5 get_community_from_moderators_url,
6 send_activity_in_community,
9 verify_add_remove_moderator_target,
12 verify_person_in_community,
14 activity_lists::AnnouncableActivities,
15 generate_moderators_url,
17 objects::{community::ApubCommunity, person::ApubPerson},
18 protocol::activities::community::add_mod::AddMod,
21 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
22 use activitystreams_kinds::{activity::AddType, public};
23 use lemmy_api_common::utils::blocking;
24 use lemmy_db_schema::{
26 community::{CommunityModerator, CommunityModeratorForm},
27 moderator::{ModAddCommunity, ModAddCommunityForm},
29 traits::{Crud, Joinable},
31 use lemmy_utils::error::LemmyError;
32 use lemmy_websocket::LemmyContext;
36 #[tracing::instrument(skip_all)]
38 community: &ApubCommunity,
39 added_mod: &ApubPerson,
41 context: &LemmyContext,
42 ) -> Result<(), LemmyError> {
43 let id = generate_activity_id(
45 &context.settings().get_protocol_and_hostname(),
48 actor: ObjectId::new(actor.actor_id()),
50 object: ObjectId::new(added_mod.actor_id()),
51 target: generate_moderators_url(&community.actor_id)?.into(),
52 cc: vec![community.actor_id()],
55 unparsed: Default::default(),
58 let activity = AnnouncableActivities::AddMod(add);
59 let inboxes = vec![added_mod.shared_inbox_or_inbox_url()];
60 send_activity_in_community(activity, &id, actor, community, inboxes, context).await
64 #[async_trait::async_trait(?Send)]
65 impl ActivityHandler for AddMod {
66 type DataType = LemmyContext;
67 type Error = LemmyError;
69 fn id(&self) -> &Url {
73 fn actor(&self) -> &Url {
77 #[tracing::instrument(skip_all)]
80 context: &Data<LemmyContext>,
81 request_counter: &mut i32,
82 ) -> Result<(), LemmyError> {
83 verify_is_public(&self.to, &self.cc)?;
84 let community = self.get_community(context, request_counter).await?;
85 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
94 verify_add_remove_moderator_target(&self.target, &community)?;
98 #[tracing::instrument(skip_all)]
101 context: &Data<LemmyContext>,
102 request_counter: &mut i32,
103 ) -> Result<(), LemmyError> {
104 let community = self.get_community(context, request_counter).await?;
107 .dereference::<LemmyError>(context, local_instance(context), request_counter)
110 // If we had to refetch the community while parsing the activity, then the new mod has already
111 // been added. Skip it here as it would result in a duplicate key error.
112 let new_mod_id = new_mod.id;
113 let moderated_communities = blocking(context.pool(), move |conn| {
114 CommunityModerator::get_person_moderated_communities(conn, new_mod_id)
117 if !moderated_communities.contains(&community.id) {
118 let form = CommunityModeratorForm {
119 community_id: community.id,
120 person_id: new_mod.id,
122 blocking(context.pool(), move |conn| {
123 CommunityModerator::join(conn, &form)
130 .dereference::<LemmyError>(context, local_instance(context), request_counter)
132 let form = ModAddCommunityForm {
133 mod_person_id: actor.id,
134 other_person_id: new_mod.id,
135 community_id: community.id,
136 removed: Some(false),
138 blocking(context.pool(), move |conn| {
139 ModAddCommunity::create(conn, &form)
143 // TODO: send websocket notification about added mod
148 #[async_trait::async_trait(?Send)]
149 impl GetCommunity for AddMod {
150 #[tracing::instrument(skip_all)]
151 async fn get_community(
153 context: &LemmyContext,
154 request_counter: &mut i32,
155 ) -> Result<ApubCommunity, LemmyError> {
156 get_community_from_moderators_url(&self.target, context, request_counter).await