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