]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/add_mod.rs
Change activity.cc to Vec<Url> in public activities
[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   context::lemmy_context,
16   fetcher::object_id::ObjectId,
17   generate_moderators_url,
18   objects::{community::ApubCommunity, person::ApubPerson},
19 };
20 use activitystreams::{
21   activity::kind::AddType,
22   base::AnyBase,
23   primitives::OneOrMany,
24   public,
25   unparsed::Unparsed,
26 };
27 use lemmy_api_common::blocking;
28 use lemmy_apub_lib::{
29   data::Data,
30   traits::{ActivityFields, ActivityHandler, ActorType},
31 };
32 use lemmy_db_schema::{
33   source::community::{CommunityModerator, CommunityModeratorForm},
34   traits::Joinable,
35 };
36 use lemmy_utils::LemmyError;
37 use lemmy_websocket::LemmyContext;
38 use serde::{Deserialize, Serialize};
39 use url::Url;
40
41 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
42 #[serde(rename_all = "camelCase")]
43 pub struct AddMod {
44   actor: ObjectId<ApubPerson>,
45   to: Vec<Url>,
46   object: ObjectId<ApubPerson>,
47   target: Url,
48   cc: Vec<Url>,
49   #[serde(rename = "type")]
50   kind: AddType,
51   id: Url,
52   #[serde(rename = "@context")]
53   context: OneOrMany<AnyBase>,
54   #[serde(flatten)]
55   unparsed: Unparsed,
56 }
57
58 impl AddMod {
59   pub async fn send(
60     community: &ApubCommunity,
61     added_mod: &ApubPerson,
62     actor: &ApubPerson,
63     context: &LemmyContext,
64   ) -> Result<(), LemmyError> {
65     let id = generate_activity_id(
66       AddType::Add,
67       &context.settings().get_protocol_and_hostname(),
68     )?;
69     let add = AddMod {
70       actor: ObjectId::new(actor.actor_id()),
71       to: vec![public()],
72       object: ObjectId::new(added_mod.actor_id()),
73       target: generate_moderators_url(&community.actor_id)?.into(),
74       cc: vec![community.actor_id()],
75       kind: AddType::Add,
76       id: id.clone(),
77       context: lemmy_context(),
78       unparsed: Default::default(),
79     };
80
81     let activity = AnnouncableActivities::AddMod(add);
82     let inboxes = vec![added_mod.shared_inbox_or_inbox_url()];
83     send_to_community(activity, &id, actor, community, inboxes, context).await
84   }
85 }
86
87 #[async_trait::async_trait(?Send)]
88 impl ActivityHandler for AddMod {
89   type DataType = LemmyContext;
90
91   async fn verify(
92     &self,
93     context: &Data<LemmyContext>,
94     request_counter: &mut i32,
95   ) -> Result<(), LemmyError> {
96     verify_is_public(&self.to)?;
97     verify_activity(self, &context.settings())?;
98     let community = self.get_community(context, request_counter).await?;
99     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
100     verify_mod_action(&self.actor, &community, context, request_counter).await?;
101     verify_add_remove_moderator_target(&self.target, &community)?;
102     Ok(())
103   }
104
105   async fn receive(
106     self,
107     context: &Data<LemmyContext>,
108     request_counter: &mut i32,
109   ) -> Result<(), LemmyError> {
110     let community = self.get_community(context, request_counter).await?;
111     let new_mod = self.object.dereference(context, request_counter).await?;
112
113     // If we had to refetch the community while parsing the activity, then the new mod has already
114     // been added. Skip it here as it would result in a duplicate key error.
115     let new_mod_id = new_mod.id;
116     let moderated_communities = blocking(context.pool(), move |conn| {
117       CommunityModerator::get_person_moderated_communities(conn, new_mod_id)
118     })
119     .await??;
120     if !moderated_communities.contains(&community.id) {
121       let form = CommunityModeratorForm {
122         community_id: community.id,
123         person_id: new_mod.id,
124       };
125       blocking(context.pool(), move |conn| {
126         CommunityModerator::join(conn, &form)
127       })
128       .await??;
129     }
130     // TODO: send websocket notification about added mod
131     Ok(())
132   }
133 }
134
135 #[async_trait::async_trait(?Send)]
136 impl GetCommunity for AddMod {
137   async fn get_community(
138     &self,
139     context: &LemmyContext,
140     request_counter: &mut i32,
141   ) -> Result<ApubCommunity, LemmyError> {
142     get_community_from_moderators_url(&self.target, context, request_counter).await
143   }
144 }