]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/add_mod.rs
Move @context out of object/activity definitions
[lemmy.git] / crates / apub / src / activities / community / add_mod.rs
1 use crate::{
2   activities::{
3     community::{
4       announce::{AnnouncableActivities, GetCommunity},
5       get_community_from_moderators_url,
6       send_to_community,
7     },
8     generate_activity_id,
9     verify_activity,
10     verify_add_remove_moderator_target,
11     verify_is_public,
12     verify_mod_action,
13     verify_person_in_community,
14   },
15   fetcher::object_id::ObjectId,
16   generate_moderators_url,
17   objects::{community::ApubCommunity, person::ApubPerson},
18 };
19 use activitystreams::{activity::kind::AddType, public, unparsed::Unparsed};
20 use lemmy_api_common::blocking;
21 use lemmy_apub_lib::{
22   data::Data,
23   traits::{ActivityFields, ActivityHandler, ActorType},
24 };
25 use lemmy_db_schema::{
26   source::community::{CommunityModerator, CommunityModeratorForm},
27   traits::Joinable,
28 };
29 use lemmy_utils::LemmyError;
30 use lemmy_websocket::LemmyContext;
31 use serde::{Deserialize, Serialize};
32 use url::Url;
33
34 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
35 #[serde(rename_all = "camelCase")]
36 pub struct AddMod {
37   actor: ObjectId<ApubPerson>,
38   to: Vec<Url>,
39   object: ObjectId<ApubPerson>,
40   target: Url,
41   cc: Vec<Url>,
42   #[serde(rename = "type")]
43   kind: AddType,
44   id: Url,
45   #[serde(flatten)]
46   unparsed: Unparsed,
47 }
48
49 impl AddMod {
50   pub async fn send(
51     community: &ApubCommunity,
52     added_mod: &ApubPerson,
53     actor: &ApubPerson,
54     context: &LemmyContext,
55   ) -> Result<(), LemmyError> {
56     let id = generate_activity_id(
57       AddType::Add,
58       &context.settings().get_protocol_and_hostname(),
59     )?;
60     let add = AddMod {
61       actor: ObjectId::new(actor.actor_id()),
62       to: vec![public()],
63       object: ObjectId::new(added_mod.actor_id()),
64       target: generate_moderators_url(&community.actor_id)?.into(),
65       cc: vec![community.actor_id()],
66       kind: AddType::Add,
67       id: id.clone(),
68       unparsed: Default::default(),
69     };
70
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
74   }
75 }
76
77 #[async_trait::async_trait(?Send)]
78 impl ActivityHandler for AddMod {
79   type DataType = LemmyContext;
80
81   async fn verify(
82     &self,
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)?;
92     Ok(())
93   }
94
95   async fn receive(
96     self,
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?;
102
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)
108     })
109     .await??;
110     if !moderated_communities.contains(&community.id) {
111       let form = CommunityModeratorForm {
112         community_id: community.id,
113         person_id: new_mod.id,
114       };
115       blocking(context.pool(), move |conn| {
116         CommunityModerator::join(conn, &form)
117       })
118       .await??;
119     }
120     // TODO: send websocket notification about added mod
121     Ok(())
122   }
123 }
124
125 #[async_trait::async_trait(?Send)]
126 impl GetCommunity for AddMod {
127   async fn get_community(
128     &self,
129     context: &LemmyContext,
130     request_counter: &mut i32,
131   ) -> Result<ApubCommunity, LemmyError> {
132     get_community_from_moderators_url(&self.target, context, request_counter).await
133   }
134 }