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_queries::{source::community::CommunityModerator_, Joinable};
27 use lemmy_db_schema::source::{
28 community::{Community, CommunityModerator, CommunityModeratorForm},
31 use lemmy_utils::LemmyError;
32 use lemmy_websocket::LemmyContext;
33 use serde::{Deserialize, Serialize};
36 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
37 #[serde(rename_all = "camelCase")]
39 actor: ObjectId<Person>,
41 object: ObjectId<Person>,
43 cc: [ObjectId<Community>; 1],
44 #[serde(rename = "type")]
47 #[serde(rename = "@context")]
48 context: OneOrMany<AnyBase>,
55 community: &Community,
58 context: &LemmyContext,
59 ) -> Result<(), LemmyError> {
60 let id = generate_activity_id(
62 &context.settings().get_protocol_and_hostname(),
65 actor: ObjectId::new(actor.actor_id()),
66 to: [PublicUrl::Public],
67 object: ObjectId::new(added_mod.actor_id()),
68 target: generate_moderators_url(&community.actor_id)?.into(),
69 cc: [ObjectId::new(community.actor_id())],
72 context: lemmy_context(),
73 unparsed: Default::default(),
76 let activity = AnnouncableActivities::AddMod(add);
77 let inboxes = vec![added_mod.shared_inbox_or_inbox_url()];
78 send_to_community(activity, &id, actor, community, inboxes, context).await
82 #[async_trait::async_trait(?Send)]
83 impl ActivityHandler for AddMod {
84 type DataType = LemmyContext;
88 context: &Data<LemmyContext>,
89 request_counter: &mut i32,
90 ) -> Result<(), LemmyError> {
91 verify_activity(self, &context.settings())?;
92 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
93 verify_mod_action(&self.actor, self.cc[0].clone(), context, request_counter).await?;
94 verify_add_remove_moderator_target(&self.target, &self.cc[0])?;
100 context: &Data<LemmyContext>,
101 request_counter: &mut i32,
102 ) -> Result<(), LemmyError> {
103 let community = self.cc[0].dereference(context, request_counter).await?;
104 let new_mod = self.object.dereference(context, request_counter).await?;
106 // If we had to refetch the community while parsing the activity, then the new mod has already
107 // been added. Skip it here as it would result in a duplicate key error.
108 let new_mod_id = new_mod.id;
109 let moderated_communities = blocking(context.pool(), move |conn| {
110 CommunityModerator::get_person_moderated_communities(conn, new_mod_id)
113 if !moderated_communities.contains(&community.id) {
114 let form = CommunityModeratorForm {
115 community_id: community.id,
116 person_id: new_mod.id,
118 blocking(context.pool(), move |conn| {
119 CommunityModerator::join(conn, &form)
123 // TODO: send websocket notification about added mod