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