]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/add_mod.rs
Fix case in activitypub context (ref #2734) (#2743)
[lemmy.git] / crates / apub / src / activities / community / add_mod.rs
1 use crate::{
2   activities::{
3     community::send_activity_in_community,
4     generate_activity_id,
5     verify_add_remove_moderator_target,
6     verify_is_public,
7     verify_mod_action,
8     verify_person_in_community,
9   },
10   activity_lists::AnnouncableActivities,
11   local_instance,
12   objects::{community::ApubCommunity, person::ApubPerson},
13   protocol::{
14     activities::community::{add_mod::AddMod, remove_mod::RemoveMod},
15     InCommunity,
16   },
17   ActorType,
18   SendActivity,
19 };
20 use activitypub_federation::{
21   core::object_id::ObjectId,
22   data::Data,
23   traits::{ActivityHandler, Actor},
24 };
25 use activitystreams_kinds::{activity::AddType, public};
26 use lemmy_api_common::{
27   community::{AddModToCommunity, AddModToCommunityResponse},
28   context::LemmyContext,
29   utils::{generate_moderators_url, get_local_user_view_from_jwt},
30 };
31 use lemmy_db_schema::{
32   source::{
33     community::{Community, CommunityModerator, CommunityModeratorForm},
34     moderator::{ModAddCommunity, ModAddCommunityForm},
35     person::Person,
36   },
37   traits::{Crud, Joinable},
38 };
39 use lemmy_utils::error::LemmyError;
40 use url::Url;
41
42 impl AddMod {
43   #[tracing::instrument(skip_all)]
44   pub async fn send(
45     community: &ApubCommunity,
46     added_mod: &ApubPerson,
47     actor: &ApubPerson,
48     context: &LemmyContext,
49   ) -> Result<(), LemmyError> {
50     let id = generate_activity_id(
51       AddType::Add,
52       &context.settings().get_protocol_and_hostname(),
53     )?;
54     let add = AddMod {
55       actor: ObjectId::new(actor.actor_id()),
56       to: vec![public()],
57       object: ObjectId::new(added_mod.actor_id()),
58       target: generate_moderators_url(&community.actor_id)?.into(),
59       cc: vec![community.actor_id()],
60       kind: AddType::Add,
61       id: id.clone(),
62       audience: Some(ObjectId::new(community.actor_id())),
63     };
64
65     let activity = AnnouncableActivities::AddMod(add);
66     let inboxes = vec![added_mod.shared_inbox_or_inbox()];
67     send_activity_in_community(activity, actor, community, inboxes, true, context).await
68   }
69 }
70
71 #[async_trait::async_trait(?Send)]
72 impl ActivityHandler for AddMod {
73   type DataType = LemmyContext;
74   type Error = LemmyError;
75
76   fn id(&self) -> &Url {
77     &self.id
78   }
79
80   fn actor(&self) -> &Url {
81     self.actor.inner()
82   }
83
84   #[tracing::instrument(skip_all)]
85   async fn verify(
86     &self,
87     context: &Data<LemmyContext>,
88     request_counter: &mut i32,
89   ) -> Result<(), LemmyError> {
90     verify_is_public(&self.to, &self.cc)?;
91     let community = self.community(context, request_counter).await?;
92     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
93     verify_mod_action(
94       &self.actor,
95       self.object.inner(),
96       community.id,
97       context,
98       request_counter,
99     )
100     .await?;
101     verify_add_remove_moderator_target(&self.target, &community)?;
102     Ok(())
103   }
104
105   #[tracing::instrument(skip_all)]
106   async fn receive(
107     self,
108     context: &Data<LemmyContext>,
109     request_counter: &mut i32,
110   ) -> Result<(), LemmyError> {
111     let community = self.community(context, request_counter).await?;
112     let new_mod = self
113       .object
114       .dereference(context, local_instance(context).await, request_counter)
115       .await?;
116
117     // If we had to refetch the community while parsing the activity, then the new mod has already
118     // been added. Skip it here as it would result in a duplicate key error.
119     let new_mod_id = new_mod.id;
120     let moderated_communities =
121       CommunityModerator::get_person_moderated_communities(context.pool(), new_mod_id).await?;
122     if !moderated_communities.contains(&community.id) {
123       let form = CommunityModeratorForm {
124         community_id: community.id,
125         person_id: new_mod.id,
126       };
127       CommunityModerator::join(context.pool(), &form).await?;
128
129       // write mod log
130       let actor = self
131         .actor
132         .dereference(context, local_instance(context).await, request_counter)
133         .await?;
134       let form = ModAddCommunityForm {
135         mod_person_id: actor.id,
136         other_person_id: new_mod.id,
137         community_id: community.id,
138         removed: Some(false),
139       };
140       ModAddCommunity::create(context.pool(), &form).await?;
141     }
142     // TODO: send websocket notification about added mod
143     Ok(())
144   }
145 }
146
147 #[async_trait::async_trait(?Send)]
148 impl SendActivity for AddModToCommunity {
149   type Response = AddModToCommunityResponse;
150
151   async fn send_activity(
152     request: &Self,
153     _response: &Self::Response,
154     context: &LemmyContext,
155   ) -> Result<(), LemmyError> {
156     let local_user_view =
157       get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
158     let community: ApubCommunity = Community::read(context.pool(), request.community_id)
159       .await?
160       .into();
161     let updated_mod: ApubPerson = Person::read(context.pool(), request.person_id)
162       .await?
163       .into();
164     if request.added {
165       AddMod::send(
166         &community,
167         &updated_mod,
168         &local_user_view.person.into(),
169         context,
170       )
171       .await
172     } else {
173       RemoveMod::send(
174         &community,
175         &updated_mod,
176         &local_user_view.person.into(),
177         context,
178       )
179       .await
180     }
181   }
182 }