]> Untitled Git - lemmy.git/blob - crates/apub/src/objects/mod.rs
Merge branch 'Mart-Bogdan-1462-jwt-revocation-on-pwd-change' into jwt_revocation_dess
[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,
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, CommunityId, 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 person;
30 pub(crate) mod post;
31 pub(crate) mod private_message;
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
49   async fn from_apub(
50     apub: &Self::ApubType,
51     context: &LemmyContext,
52     expected_domain: Url,
53     request_counter: &mut i32,
54   ) -> Result<Self, LemmyError>
55   where
56     Self: Sized;
57 }
58
59 #[async_trait::async_trait(?Send)]
60 pub(in crate::objects) trait FromApubToForm<ApubType> {
61   async fn from_apub(
62     apub: &ApubType,
63     context: &LemmyContext,
64     expected_domain: Url,
65     request_counter: &mut i32,
66   ) -> Result<Self, LemmyError>
67   where
68     Self: Sized;
69 }
70
71 /// Updated is actually the deletion time
72 fn create_tombstone<T>(
73   deleted: bool,
74   object_id: Url,
75   updated: Option<NaiveDateTime>,
76   former_type: T,
77 ) -> Result<Tombstone, LemmyError>
78 where
79   T: ToString,
80 {
81   if deleted {
82     if let Some(updated) = updated {
83       let mut tombstone = Tombstone::new();
84       tombstone.set_id(object_id);
85       tombstone.set_former_type(former_type.to_string());
86       tombstone.set_deleted(convert_datetime(updated));
87       Ok(tombstone)
88     } else {
89       Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
90     }
91   } else {
92     Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
93   }
94 }
95
96 pub(in crate::objects) fn check_object_domain<T, Kind>(
97   apub: &T,
98   expected_domain: Url,
99 ) -> Result<DbUrl, LemmyError>
100 where
101   T: Base + AsBase<Kind>,
102 {
103   let domain = expected_domain.domain().context(location_info!())?;
104   let object_id = apub.id(domain)?.context(location_info!())?;
105   check_is_apub_id_valid(object_id)?;
106   Ok(object_id.to_owned().into())
107 }
108
109 pub(in crate::objects) fn set_content_and_source<T, Kind1, Kind2>(
110   object: &mut T,
111   markdown_text: &str,
112 ) -> Result<(), LemmyError>
113 where
114   T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
115 {
116   let mut source = Object::<()>::new_none_type();
117   source
118     .set_content(markdown_text)
119     .set_media_type(mime_markdown()?);
120   object.set_source(source.into_any_base()?);
121
122   object.set_content(markdown_to_html(markdown_text));
123   object.set_media_type(mime_html()?);
124   Ok(())
125 }
126
127 pub(in crate::objects) fn get_source_markdown_value<T, Kind1, Kind2>(
128   object: &T,
129 ) -> Result<Option<String>, LemmyError>
130 where
131   T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
132 {
133   let content = object
134     .content()
135     .map(|s| s.as_single_xsd_string())
136     .flatten()
137     .map(|s| s.to_string());
138   if content.is_some() {
139     let source = object.source().context(location_info!())?;
140     let source = Object::<()>::from_any_base(source.to_owned())?.context(location_info!())?;
141     check_is_markdown(source.media_type())?;
142     let source_content = source
143       .content()
144       .map(|s| s.as_single_xsd_string())
145       .flatten()
146       .context(location_info!())?
147       .to_string();
148     return Ok(Some(source_content));
149   }
150   Ok(None)
151 }
152
153 fn mime_markdown() -> Result<Mime, FromStrError> {
154   "text/markdown".parse()
155 }
156
157 fn mime_html() -> Result<Mime, FromStrError> {
158   "text/html".parse()
159 }
160
161 pub(in crate::objects) fn check_is_markdown(mime: Option<&Mime>) -> Result<(), LemmyError> {
162   let mime = mime.context(location_info!())?;
163   if !mime.eq(&mime_markdown()?) {
164     Err(LemmyError::from(anyhow!(
165       "Lemmy only supports markdown content"
166     )))
167   } else {
168     Ok(())
169   }
170 }
171
172 /// Converts an ActivityPub object (eg `Note`) to a database object (eg `Comment`). If an object
173 /// with the same ActivityPub ID already exists in the database, it is returned directly. Otherwise
174 /// the apub object is parsed, inserted and returned.
175 pub(in crate::objects) async fn get_object_from_apub<From, Kind, To, ToForm, IdType>(
176   from: &From,
177   context: &LemmyContext,
178   expected_domain: Url,
179   request_counter: &mut i32,
180 ) -> Result<To, LemmyError>
181 where
182   From: BaseExt<Kind>,
183   To: ApubObject<ToForm> + Crud<ToForm, IdType> + Send + 'static,
184   ToForm: FromApubToForm<From> + Send + 'static,
185 {
186   let object_id = from.id_unchecked().context(location_info!())?.to_owned();
187   let domain = object_id.domain().context(location_info!())?;
188
189   // if its a local object, return it directly from the database
190   if Settings::get().hostname() == domain {
191     let object = blocking(context.pool(), move |conn| {
192       To::read_from_apub_id(conn, &object_id.into())
193     })
194     .await??;
195     Ok(object)
196   }
197   // otherwise parse and insert, assuring that it comes from the right domain
198   else {
199     let to_form = ToForm::from_apub(&from, context, expected_domain, request_counter).await?;
200
201     let to = blocking(context.pool(), move |conn| To::upsert(conn, &to_form)).await??;
202     Ok(to)
203   }
204 }
205
206 pub(in crate::objects) async fn check_object_for_community_or_site_ban<T, Kind>(
207   object: &T,
208   community_id: CommunityId,
209   context: &LemmyContext,
210   request_counter: &mut i32,
211 ) -> Result<(), LemmyError>
212 where
213   T: ObjectExt<Kind>,
214 {
215   let person_id = object
216     .attributed_to()
217     .context(location_info!())?
218     .as_single_xsd_any_uri()
219     .context(location_info!())?;
220   let person = get_or_fetch_and_upsert_person(person_id, context, request_counter).await?;
221   check_community_or_site_ban(&person, community_id, context.pool()).await
222 }
223
224 pub(in crate::objects) async fn get_to_community<T, Kind>(
225   object: &T,
226   context: &LemmyContext,
227   request_counter: &mut i32,
228 ) -> Result<Community, LemmyError>
229 where
230   T: ObjectExt<Kind>,
231 {
232   let community_ids = object
233     .to()
234     .context(location_info!())?
235     .as_many()
236     .context(location_info!())?
237     .iter()
238     .map(|a| a.as_xsd_any_uri().context(location_info!()))
239     .collect::<Result<Vec<&Url>, anyhow::Error>>()?;
240   for cid in community_ids {
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 }