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