]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/mod.rs
63f79caa0d8e3c695062df8583f69d7d42288fff
[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: &ApubCommunity,
87   context: &LemmyContext,
88   request_counter: &mut i32,
89 ) -> Result<(), LemmyError> {
90   if community.local {
91     let actor = actor_id.dereference(context, request_counter).await?;
92
93     // Note: this will also return true for admins in addition to mods, but as we dont know about
94     //       remote admins, it doesnt make any difference.
95     let community_id = community.id;
96     let actor_id = actor.id;
97     let is_mod_or_admin = blocking(context.pool(), move |conn| {
98       CommunityView::is_mod_or_admin(conn, actor_id, community_id)
99     })
100     .await?;
101     if !is_mod_or_admin {
102       return Err(anyhow!("Not a mod").into());
103     }
104   }
105   Ok(())
106 }
107
108 /// For Add/Remove community moderator activities, check that the target field actually contains
109 /// /c/community/moderators. Any different values are unsupported.
110 fn verify_add_remove_moderator_target(
111   target: &Url,
112   community: &ApubCommunity,
113 ) -> Result<(), LemmyError> {
114   if target != &generate_moderators_url(&community.actor_id)?.into_inner() {
115     return Err(anyhow!("Unkown target url").into());
116   }
117   Ok(())
118 }
119
120 pub(crate) fn verify_is_public(to: &[Url]) -> Result<(), LemmyError> {
121   if !to.contains(&public()) {
122     return Err(anyhow!("Object is not public").into());
123   }
124   Ok(())
125 }
126
127 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
128   if community.deleted || community.removed {
129     Err(anyhow!("New post or comment cannot be created in deleted or removed community").into())
130   } else {
131     Ok(())
132   }
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, protocol_and_hostname: &str) -> Result<Url, ParseError>
138 where
139   T: ToString,
140 {
141   let id = format!(
142     "{}/activities/{}/{}",
143     protocol_and_hostname,
144     kind.to_string().to_lowercase(),
145     Uuid::new_v4()
146   );
147   Url::parse(&id)
148 }