]> Untitled Git - lemmy.git/blob - crates/apub/src/objects/mod.rs
Merge remote-tracking branch 'yerba/federated-moderation'
[lemmy.git] / crates / apub / src / objects / mod.rs
1 use crate::{
2   check_is_apub_id_valid,
3   fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
4   inbox::{community_inbox::check_community_or_site_ban, get_activity_to_and_cc},
5   PageExt,
6 };
7 use activitystreams::{
8   base::{AsBase, BaseExt, ExtendsExt},
9   markers::Base,
10   mime::{FromStrError, Mime},
11   object::{ApObjectExt, Object, ObjectExt, Tombstone, TombstoneExt},
12 };
13 use anyhow::{anyhow, Context};
14 use chrono::NaiveDateTime;
15 use diesel::result::Error::NotFound;
16 use lemmy_api_structs::blocking;
17 use lemmy_db_queries::{ApubObject, Crud, DbPool};
18 use lemmy_db_schema::{source::community::Community, CommunityId, DbUrl};
19 use lemmy_utils::{
20   location_info,
21   settings::structs::Settings,
22   utils::{convert_datetime, markdown_to_html},
23   LemmyError,
24 };
25 use lemmy_websocket::LemmyContext;
26 use url::Url;
27
28 pub(crate) mod comment;
29 pub(crate) mod community;
30 pub(crate) mod person;
31 pub(crate) mod post;
32 pub(crate) mod private_message;
33
34 /// Trait for converting an object or actor into the respective ActivityPub type.
35 #[async_trait::async_trait(?Send)]
36 pub(crate) trait ToApub {
37   type ApubType;
38   async fn to_apub(&self, pool: &DbPool) -> Result<Self::ApubType, LemmyError>;
39   fn to_tombstone(&self) -> Result<Tombstone, LemmyError>;
40 }
41
42 #[async_trait::async_trait(?Send)]
43 pub(crate) trait FromApub {
44   type ApubType;
45   /// Converts an object from ActivityPub type to Lemmy internal type.
46   ///
47   /// * `apub` The object to read from
48   /// * `context` LemmyContext which holds DB pool, HTTP client etc
49   /// * `expected_domain` Domain where the object was received from. None in case of mod action.
50   /// * `mod_action_allowed` True if the object can be a mod activity, ignore `expected_domain` in this case
51   async fn from_apub(
52     apub: &Self::ApubType,
53     context: &LemmyContext,
54     expected_domain: Url,
55     request_counter: &mut i32,
56     mod_action_allowed: bool,
57   ) -> Result<Self, LemmyError>
58   where
59     Self: Sized;
60 }
61
62 #[async_trait::async_trait(?Send)]
63 pub(in crate::objects) trait FromApubToForm<ApubType> {
64   async fn from_apub(
65     apub: &ApubType,
66     context: &LemmyContext,
67     expected_domain: Url,
68     request_counter: &mut i32,
69     mod_action_allowed: bool,
70   ) -> Result<Self, LemmyError>
71   where
72     Self: Sized;
73 }
74
75 /// Updated is actually the deletion time
76 fn create_tombstone<T>(
77   deleted: bool,
78   object_id: Url,
79   updated: Option<NaiveDateTime>,
80   former_type: T,
81 ) -> Result<Tombstone, LemmyError>
82 where
83   T: ToString,
84 {
85   if deleted {
86     if let Some(updated) = updated {
87       let mut tombstone = Tombstone::new();
88       tombstone.set_id(object_id);
89       tombstone.set_former_type(former_type.to_string());
90       tombstone.set_deleted(convert_datetime(updated));
91       Ok(tombstone)
92     } else {
93       Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
94     }
95   } else {
96     Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
97   }
98 }
99
100 pub(in crate::objects) fn check_object_domain<T, Kind>(
101   apub: &T,
102   expected_domain: Url,
103 ) -> Result<DbUrl, LemmyError>
104 where
105   T: Base + AsBase<Kind>,
106 {
107   let domain = expected_domain.domain().context(location_info!())?;
108   let object_id = apub.id(domain)?.context(location_info!())?;
109   check_is_apub_id_valid(object_id)?;
110   Ok(object_id.to_owned().into())
111 }
112
113 pub(in crate::objects) fn set_content_and_source<T, Kind1, Kind2>(
114   object: &mut T,
115   markdown_text: &str,
116 ) -> Result<(), LemmyError>
117 where
118   T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
119 {
120   let mut source = Object::<()>::new_none_type();
121   source
122     .set_content(markdown_text)
123     .set_media_type(mime_markdown()?);
124   object.set_source(source.into_any_base()?);
125
126   object.set_content(markdown_to_html(markdown_text));
127   object.set_media_type(mime_html()?);
128   Ok(())
129 }
130
131 pub(in crate::objects) fn get_source_markdown_value<T, Kind1, Kind2>(
132   object: &T,
133 ) -> Result<Option<String>, LemmyError>
134 where
135   T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
136 {
137   let content = object
138     .content()
139     .map(|s| s.as_single_xsd_string().map(|s2| s2.to_string()))
140     .flatten();
141   if content.is_some() {
142     let source = object.source().context(location_info!())?;
143     let source = Object::<()>::from_any_base(source.to_owned())?.context(location_info!())?;
144     check_is_markdown(source.media_type())?;
145     let source_content = source
146       .content()
147       .map(|s| s.as_single_xsd_string().map(|s2| s2.to_string()))
148       .flatten()
149       .context(location_info!())?;
150     return Ok(Some(source_content));
151   }
152   Ok(None)
153 }
154
155 fn mime_markdown() -> Result<Mime, FromStrError> {
156   "text/markdown".parse()
157 }
158
159 fn mime_html() -> Result<Mime, FromStrError> {
160   "text/html".parse()
161 }
162
163 pub(in crate::objects) fn check_is_markdown(mime: Option<&Mime>) -> Result<(), LemmyError> {
164   let mime = mime.context(location_info!())?;
165   if !mime.eq(&mime_markdown()?) {
166     Err(LemmyError::from(anyhow!(
167       "Lemmy only supports markdown content"
168     )))
169   } else {
170     Ok(())
171   }
172 }
173
174 /// Converts an ActivityPub object (eg `Note`) to a database object (eg `Comment`). If an object
175 /// with the same ActivityPub ID already exists in the database, it is returned directly. Otherwise
176 /// the apub object is parsed, inserted and returned.
177 pub(in crate::objects) async fn get_object_from_apub<From, Kind, To, ToForm, IdType>(
178   from: &From,
179   context: &LemmyContext,
180   expected_domain: Url,
181   request_counter: &mut i32,
182   is_mod_action: bool,
183 ) -> Result<To, LemmyError>
184 where
185   From: BaseExt<Kind>,
186   To: ApubObject<ToForm> + Crud<ToForm, IdType> + Send + 'static,
187   ToForm: FromApubToForm<From> + Send + 'static,
188 {
189   let object_id = from.id_unchecked().context(location_info!())?.to_owned();
190   let domain = object_id.domain().context(location_info!())?;
191
192   // if its a local object, return it directly from the database
193   if Settings::get().hostname() == domain {
194     let object = blocking(context.pool(), move |conn| {
195       To::read_from_apub_id(conn, &object_id.into())
196     })
197     .await??;
198     Ok(object)
199   }
200   // otherwise parse and insert, assuring that it comes from the right domain
201   else {
202     let to_form = ToForm::from_apub(
203       &from,
204       context,
205       expected_domain,
206       request_counter,
207       is_mod_action,
208     )
209     .await?;
210
211     let to = blocking(context.pool(), move |conn| To::upsert(conn, &to_form)).await??;
212     Ok(to)
213   }
214 }
215
216 pub(in crate::objects) async fn check_object_for_community_or_site_ban<T, Kind>(
217   object: &T,
218   community_id: CommunityId,
219   context: &LemmyContext,
220   request_counter: &mut i32,
221 ) -> Result<(), LemmyError>
222 where
223   T: ObjectExt<Kind>,
224 {
225   let person_id = object
226     .attributed_to()
227     .context(location_info!())?
228     .as_single_xsd_any_uri()
229     .context(location_info!())?;
230   let person = get_or_fetch_and_upsert_person(person_id, context, request_counter).await?;
231   check_community_or_site_ban(&person, community_id, context.pool()).await
232 }
233
234 pub(in crate::objects) async fn get_community_from_to_or_cc(
235   page: &PageExt,
236   context: &LemmyContext,
237   request_counter: &mut i32,
238 ) -> Result<Community, LemmyError> {
239   for cid in get_activity_to_and_cc(page) {
240     let community = get_or_fetch_and_upsert_community(&cid, context, request_counter).await;
241     if community.is_ok() {
242       return community;
243     }
244   }
245   Err(NotFound.into())
246 }