4 announce::{AnnouncableActivities, GetCommunity},
5 get_community_from_moderators_url,
10 verify_add_remove_moderator_target,
13 verify_person_in_community,
15 fetcher::object_id::ObjectId,
16 generate_moderators_url,
17 objects::{community::ApubCommunity, person::ApubPerson},
19 use activitystreams::{activity::kind::AddType, public, unparsed::Unparsed};
20 use lemmy_api_common::blocking;
23 traits::{ActivityFields, ActivityHandler, ActorType},
25 use lemmy_db_schema::{
26 source::community::{CommunityModerator, CommunityModeratorForm},
29 use lemmy_utils::LemmyError;
30 use lemmy_websocket::LemmyContext;
31 use serde::{Deserialize, Serialize};
34 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
35 #[serde(rename_all = "camelCase")]
37 actor: ObjectId<ApubPerson>,
39 object: ObjectId<ApubPerson>,
42 #[serde(rename = "type")]
51 community: &ApubCommunity,
52 added_mod: &ApubPerson,
54 context: &LemmyContext,
55 ) -> Result<(), LemmyError> {
56 let id = generate_activity_id(
58 &context.settings().get_protocol_and_hostname(),
61 actor: ObjectId::new(actor.actor_id()),
63 object: ObjectId::new(added_mod.actor_id()),
64 target: generate_moderators_url(&community.actor_id)?.into(),
65 cc: vec![community.actor_id()],
68 unparsed: Default::default(),
71 let activity = AnnouncableActivities::AddMod(add);
72 let inboxes = vec![added_mod.shared_inbox_or_inbox_url()];
73 send_to_community(activity, &id, actor, community, inboxes, context).await
77 #[async_trait::async_trait(?Send)]
78 impl ActivityHandler for AddMod {
79 type DataType = LemmyContext;
83 context: &Data<LemmyContext>,
84 request_counter: &mut i32,
85 ) -> Result<(), LemmyError> {
86 verify_is_public(&self.to)?;
87 verify_activity(self, &context.settings())?;
88 let community = self.get_community(context, request_counter).await?;
89 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
90 verify_mod_action(&self.actor, &community, context, request_counter).await?;
91 verify_add_remove_moderator_target(&self.target, &community)?;
97 context: &Data<LemmyContext>,
98 request_counter: &mut i32,
99 ) -> Result<(), LemmyError> {
100 let community = self.get_community(context, request_counter).await?;
101 let new_mod = self.object.dereference(context, request_counter).await?;
103 // If we had to refetch the community while parsing the activity, then the new mod has already
104 // been added. Skip it here as it would result in a duplicate key error.
105 let new_mod_id = new_mod.id;
106 let moderated_communities = blocking(context.pool(), move |conn| {
107 CommunityModerator::get_person_moderated_communities(conn, new_mod_id)
110 if !moderated_communities.contains(&community.id) {
111 let form = CommunityModeratorForm {
112 community_id: community.id,
113 person_id: new_mod.id,
115 blocking(context.pool(), move |conn| {
116 CommunityModerator::join(conn, &form)
120 // TODO: send websocket notification about added mod
125 #[async_trait::async_trait(?Send)]
126 impl GetCommunity for AddMod {
127 async fn get_community(
129 context: &LemmyContext,
130 request_counter: &mut i32,
131 ) -> Result<ApubCommunity, LemmyError> {
132 get_community_from_moderators_url(&self.target, context, request_counter).await