3 community::{announce::AnnouncableActivities, send_to_community},
6 verify_add_remove_moderator_target,
8 verify_person_in_community,
10 context::lemmy_context,
11 fetcher::object_id::ObjectId,
12 generate_moderators_url,
14 use activitystreams::{
15 activity::kind::AddType,
17 primitives::OneOrMany,
20 use lemmy_api_common::blocking;
23 traits::{ActivityFields, ActivityHandler, ActorType},
26 use lemmy_db_schema::{
28 community::{Community, CommunityModerator, CommunityModeratorForm},
33 use lemmy_utils::LemmyError;
34 use lemmy_websocket::LemmyContext;
35 use serde::{Deserialize, Serialize};
38 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
39 #[serde(rename_all = "camelCase")]
41 actor: ObjectId<Person>,
43 object: ObjectId<Person>,
45 cc: [ObjectId<Community>; 1],
46 #[serde(rename = "type")]
49 #[serde(rename = "@context")]
50 context: OneOrMany<AnyBase>,
57 community: &Community,
60 context: &LemmyContext,
61 ) -> Result<(), LemmyError> {
62 let id = generate_activity_id(
64 &context.settings().get_protocol_and_hostname(),
67 actor: ObjectId::new(actor.actor_id()),
68 to: [PublicUrl::Public],
69 object: ObjectId::new(added_mod.actor_id()),
70 target: generate_moderators_url(&community.actor_id)?.into(),
71 cc: [ObjectId::new(community.actor_id())],
74 context: lemmy_context(),
75 unparsed: Default::default(),
78 let activity = AnnouncableActivities::AddMod(add);
79 let inboxes = vec![added_mod.shared_inbox_or_inbox_url()];
80 send_to_community(activity, &id, actor, community, inboxes, context).await
84 #[async_trait::async_trait(?Send)]
85 impl ActivityHandler for AddMod {
86 type DataType = LemmyContext;
90 context: &Data<LemmyContext>,
91 request_counter: &mut i32,
92 ) -> Result<(), LemmyError> {
93 verify_activity(self, &context.settings())?;
94 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
95 verify_mod_action(&self.actor, self.cc[0].clone(), context, request_counter).await?;
96 verify_add_remove_moderator_target(&self.target, &self.cc[0])?;
102 context: &Data<LemmyContext>,
103 request_counter: &mut i32,
104 ) -> Result<(), LemmyError> {
105 let community = self.cc[0].dereference(context, request_counter).await?;
106 let new_mod = self.object.dereference(context, request_counter).await?;
108 // If we had to refetch the community while parsing the activity, then the new mod has already
109 // been added. Skip it here as it would result in a duplicate key error.
110 let new_mod_id = new_mod.id;
111 let moderated_communities = blocking(context.pool(), move |conn| {
112 CommunityModerator::get_person_moderated_communities(conn, new_mod_id)
115 if !moderated_communities.contains(&community.id) {
116 let form = CommunityModeratorForm {
117 community_id: community.id,
118 person_id: new_mod.id,
120 blocking(context.pool(), move |conn| {
121 CommunityModerator::join(conn, &form)
125 // TODO: send websocket notification about added mod