]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/mod.rs
618c14f50ddca2a88158d62df5d8dffba540df15
[lemmy.git] / crates / apub / src / activities / mod.rs
1 use crate::{
2   check_community_or_site_ban,
3   check_is_apub_id_valid,
4   fetcher::object_id::ObjectId,
5   generate_moderators_url,
6 };
7 use anyhow::anyhow;
8 use lemmy_api_common::blocking;
9 use lemmy_apub_lib::{traits::ActivityFields, verify::verify_domains_match};
10 use lemmy_db_schema::source::{community::Community, person::Person};
11 use lemmy_db_views_actor::community_view::CommunityView;
12 use lemmy_utils::{settings::structs::Settings, LemmyError};
13 use lemmy_websocket::LemmyContext;
14 use serde::{Deserialize, Serialize};
15 use strum_macros::ToString;
16 use url::{ParseError, Url};
17 use uuid::Uuid;
18
19 pub mod comment;
20 pub mod community;
21 pub mod deletion;
22 pub mod following;
23 pub mod post;
24 pub mod private_message;
25 pub mod report;
26 pub mod undo_remove;
27 pub mod voting;
28
29 #[derive(Clone, Debug, ToString, Deserialize, Serialize)]
30 pub enum CreateOrUpdateType {
31   Create,
32   Update,
33 }
34
35 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
36 /// doesn't have a site ban.
37 async fn verify_person(
38   person_id: &ObjectId<Person>,
39   context: &LemmyContext,
40   request_counter: &mut i32,
41 ) -> Result<(), LemmyError> {
42   let person = person_id.dereference(context, request_counter).await?;
43   if person.banned {
44     return Err(anyhow!("Person {} is banned", person_id).into());
45   }
46   Ok(())
47 }
48
49 pub(crate) async fn extract_community(
50   cc: &[Url],
51   context: &LemmyContext,
52   request_counter: &mut i32,
53 ) -> Result<Community, LemmyError> {
54   let mut cc_iter = cc.iter();
55   loop {
56     if let Some(cid) = cc_iter.next() {
57       let cid = ObjectId::new(cid.clone());
58       if let Ok(c) = cid.dereference(context, request_counter).await {
59         break Ok(c);
60       }
61     } else {
62       return Err(anyhow!("No community found in cc").into());
63     }
64   }
65 }
66
67 /// Fetches the person and community to verify their type, then checks if person is banned from site
68 /// or community.
69 pub(crate) async fn verify_person_in_community(
70   person_id: &ObjectId<Person>,
71   community_id: &ObjectId<Community>,
72   context: &LemmyContext,
73   request_counter: &mut i32,
74 ) -> Result<(), LemmyError> {
75   let community = community_id.dereference(context, request_counter).await?;
76   let person = person_id.dereference(context, request_counter).await?;
77   check_community_or_site_ban(&person, community.id, context.pool()).await
78 }
79
80 /// Simply check that the url actually refers to a valid group.
81 async fn verify_community(
82   community_id: &ObjectId<Community>,
83   context: &LemmyContext,
84   request_counter: &mut i32,
85 ) -> Result<(), LemmyError> {
86   community_id.dereference(context, request_counter).await?;
87   Ok(())
88 }
89
90 fn verify_activity(activity: &dyn ActivityFields, settings: &Settings) -> Result<(), LemmyError> {
91   check_is_apub_id_valid(activity.actor(), false, settings)?;
92   verify_domains_match(activity.id_unchecked(), activity.actor())?;
93   Ok(())
94 }
95
96 /// Verify that the actor is a community mod. This check is only run if the community is local,
97 /// because in case of remote communities, admins can also perform mod actions. As admin status
98 /// is not federated, we cant verify their actions remotely.
99 pub(crate) async fn verify_mod_action(
100   actor_id: &ObjectId<Person>,
101   community_id: ObjectId<Community>,
102   context: &LemmyContext,
103   request_counter: &mut i32,
104 ) -> Result<(), LemmyError> {
105   let community = community_id.dereference_local(context).await?;
106
107   if community.local {
108     let actor = actor_id.dereference(context, request_counter).await?;
109
110     // Note: this will also return true for admins in addition to mods, but as we dont know about
111     //       remote admins, it doesnt make any difference.
112     let community_id = community.id;
113     let actor_id = actor.id;
114     let is_mod_or_admin = blocking(context.pool(), move |conn| {
115       CommunityView::is_mod_or_admin(conn, actor_id, community_id)
116     })
117     .await?;
118     if !is_mod_or_admin {
119       return Err(anyhow!("Not a mod").into());
120     }
121   }
122   Ok(())
123 }
124
125 /// For Add/Remove community moderator activities, check that the target field actually contains
126 /// /c/community/moderators. Any different values are unsupported.
127 fn verify_add_remove_moderator_target(
128   target: &Url,
129   community: &ObjectId<Community>,
130 ) -> Result<(), LemmyError> {
131   if target != &generate_moderators_url(&community.clone().into())?.into_inner() {
132     return Err(anyhow!("Unkown target url").into());
133   }
134   Ok(())
135 }
136
137 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
138   if community.deleted || community.removed {
139     Err(anyhow!("New post or comment cannot be created in deleted or removed community").into())
140   } else {
141     Ok(())
142   }
143 }
144
145 /// Generate a unique ID for an activity, in the format:
146 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
147 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
148 where
149   T: ToString,
150 {
151   let id = format!(
152     "{}/activities/{}/{}",
153     protocol_and_hostname,
154     kind.to_string().to_lowercase(),
155     Uuid::new_v4()
156   );
157   Url::parse(&id)
158 }