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