]> Untitled Git - lemmy.git/blob - crates/apub_receive/src/activities/mod.rs
Apub inbox rewrite (#1652)
[lemmy.git] / crates / apub_receive / src / activities / mod.rs
1 use anyhow::anyhow;
2 use lemmy_api_common::blocking;
3 use lemmy_apub::{
4   check_community_or_site_ban,
5   check_is_apub_id_valid,
6   fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
7   generate_moderators_url,
8 };
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::LemmyError;
17 use lemmy_websocket::LemmyContext;
18 use url::Url;
19
20 pub mod comment;
21 pub mod community;
22 pub mod deletion;
23 pub mod following;
24 pub mod post;
25 pub mod private_message;
26 pub mod removal;
27 pub mod voting;
28
29 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
30 /// doesn't have a site ban.
31 async fn verify_person(
32   person_id: &Url,
33   context: &LemmyContext,
34   request_counter: &mut i32,
35 ) -> Result<(), LemmyError> {
36   let person = get_or_fetch_and_upsert_person(person_id, context, request_counter).await?;
37   if person.banned {
38     return Err(anyhow!("Person {} is banned", person_id).into());
39   }
40   Ok(())
41 }
42
43 /// Fetches the person and community to verify their type, then checks if person is banned from site
44 /// or community.
45 async fn verify_person_in_community(
46   person_id: &Url,
47   cc: &[Url],
48   context: &LemmyContext,
49   request_counter: &mut i32,
50 ) -> Result<Community, LemmyError> {
51   let person = get_or_fetch_and_upsert_person(person_id, context, request_counter).await?;
52   let mut cc_iter = cc.iter();
53   let community: Community = loop {
54     if let Some(cid) = cc_iter.next() {
55       if let Ok(c) = get_or_fetch_and_upsert_community(cid, context, request_counter).await {
56         break c;
57       }
58     } else {
59       return Err(anyhow!("No community found in cc").into());
60     }
61   };
62   check_community_or_site_ban(&person, community.id, context.pool()).await?;
63   Ok(community)
64 }
65
66 /// Simply check that the url actually refers to a valid group.
67 async fn verify_community(
68   community_id: &Url,
69   context: &LemmyContext,
70   request_counter: &mut i32,
71 ) -> Result<(), LemmyError> {
72   get_or_fetch_and_upsert_community(community_id, context, request_counter).await?;
73   Ok(())
74 }
75
76 fn verify_activity(common: &ActivityCommonFields) -> Result<(), LemmyError> {
77   check_is_apub_id_valid(&common.actor, false)?;
78   verify_domains_match(common.id_unchecked(), &common.actor)?;
79   Ok(())
80 }
81
82 async fn verify_mod_action(
83   actor_id: &Url,
84   activity_cc: Url,
85   context: &LemmyContext,
86 ) -> Result<(), LemmyError> {
87   let community = blocking(context.pool(), move |conn| {
88     Community::read_from_apub_id(conn, &activity_cc.into())
89   })
90   .await??;
91
92   if community.local {
93     let actor_id: DbUrl = actor_id.clone().into();
94     let actor = blocking(context.pool(), move |conn| {
95       Person::read_from_apub_id(conn, &actor_id)
96     })
97     .await??;
98
99     // Note: this will also return true for admins in addition to mods, but as we dont know about
100     //       remote admins, it doesnt make any difference.
101     let community_id = community.id;
102     let actor_id = actor.id;
103     let is_mod_or_admin = blocking(context.pool(), move |conn| {
104       CommunityView::is_mod_or_admin(conn, actor_id, community_id)
105     })
106     .await?;
107     if !is_mod_or_admin {
108       return Err(anyhow!("Not a mod").into());
109     }
110   }
111   Ok(())
112 }
113
114 /// For Add/Remove community moderator activities, check that the target field actually contains
115 /// /c/community/moderators. Any different values are unsupported.
116 fn verify_add_remove_moderator_target(target: &Url, community: Url) -> Result<(), LemmyError> {
117   if target != &generate_moderators_url(&community.into())?.into_inner() {
118     return Err(anyhow!("Unkown target url").into());
119   }
120   Ok(())
121 }