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