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