3 community::{announce::AnnouncableActivities, send_to_community},
6 verify_add_remove_moderator_target,
9 verify_person_in_community,
11 context::lemmy_context,
12 fetcher::object_id::ObjectId,
13 generate_moderators_url,
14 objects::{community::ApubCommunity, person::ApubPerson},
16 use activitystreams::{
17 activity::kind::AddType,
19 primitives::OneOrMany,
23 use lemmy_api_common::blocking;
26 traits::{ActivityFields, ActivityHandler, ActorType},
28 use lemmy_db_schema::{
29 source::community::{CommunityModerator, CommunityModeratorForm},
32 use lemmy_utils::LemmyError;
33 use lemmy_websocket::LemmyContext;
34 use serde::{Deserialize, Serialize};
37 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
38 #[serde(rename_all = "camelCase")]
40 actor: ObjectId<ApubPerson>,
42 object: ObjectId<ApubPerson>,
44 cc: [ObjectId<ApubCommunity>; 1],
45 #[serde(rename = "type")]
48 #[serde(rename = "@context")]
49 context: OneOrMany<AnyBase>,
56 community: &ApubCommunity,
57 added_mod: &ApubPerson,
59 context: &LemmyContext,
60 ) -> Result<(), LemmyError> {
61 let id = generate_activity_id(
63 &context.settings().get_protocol_and_hostname(),
66 actor: ObjectId::new(actor.actor_id()),
68 object: ObjectId::new(added_mod.actor_id()),
69 target: generate_moderators_url(&community.actor_id)?.into(),
70 cc: [ObjectId::new(community.actor_id())],
73 context: lemmy_context(),
74 unparsed: Default::default(),
77 let activity = AnnouncableActivities::AddMod(add);
78 let inboxes = vec![added_mod.shared_inbox_or_inbox_url()];
79 send_to_community(activity, &id, actor, community, inboxes, context).await
83 #[async_trait::async_trait(?Send)]
84 impl ActivityHandler for AddMod {
85 type DataType = LemmyContext;
89 context: &Data<LemmyContext>,
90 request_counter: &mut i32,
91 ) -> Result<(), LemmyError> {
92 verify_is_public(&self.to)?;
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], 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