]> Untitled Git - lemmy.git/blob - crates/apub/src/objects/mod.rs
Use Url type for ap_id fields in database (fixes #1364) (#1371)
[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 lemmy_db_queries::{ApubObject, Crud, DbPool};
15 use lemmy_structs::blocking;
16 use lemmy_utils::{location_info, settings::Settings, utils::convert_datetime, LemmyError};
17 use lemmy_websocket::LemmyContext;
18 use url::Url;
19
20 pub(crate) mod comment;
21 pub(crate) mod community;
22 pub(crate) mod post;
23 pub(crate) mod private_message;
24 pub(crate) mod user;
25
26 /// Trait for converting an object or actor into the respective ActivityPub type.
27 #[async_trait::async_trait(?Send)]
28 pub(crate) trait ToApub {
29   type ApubType;
30   async fn to_apub(&self, pool: &DbPool) -> Result<Self::ApubType, LemmyError>;
31   fn to_tombstone(&self) -> Result<Tombstone, LemmyError>;
32 }
33
34 #[async_trait::async_trait(?Send)]
35 pub(crate) trait FromApub {
36   type ApubType;
37   /// Converts an object from ActivityPub type to Lemmy internal type.
38   ///
39   /// * `apub` The object to read from
40   /// * `context` LemmyContext which holds DB pool, HTTP client etc
41   /// * `expected_domain` Domain where the object was received from
42   async fn from_apub(
43     apub: &Self::ApubType,
44     context: &LemmyContext,
45     expected_domain: Url,
46     request_counter: &mut i32,
47   ) -> Result<Self, LemmyError>
48   where
49     Self: Sized;
50 }
51
52 #[async_trait::async_trait(?Send)]
53 pub(in crate::objects) trait FromApubToForm<ApubType> {
54   async fn from_apub(
55     apub: &ApubType,
56     context: &LemmyContext,
57     expected_domain: Url,
58     request_counter: &mut i32,
59   ) -> Result<Self, LemmyError>
60   where
61     Self: Sized;
62 }
63
64 /// Updated is actually the deletion time
65 fn create_tombstone<T>(
66   deleted: bool,
67   object_id: Url,
68   updated: Option<NaiveDateTime>,
69   former_type: T,
70 ) -> Result<Tombstone, LemmyError>
71 where
72   T: ToString,
73 {
74   if deleted {
75     if let Some(updated) = updated {
76       let mut tombstone = Tombstone::new();
77       tombstone.set_id(object_id);
78       tombstone.set_former_type(former_type.to_string());
79       tombstone.set_deleted(convert_datetime(updated));
80       Ok(tombstone)
81     } else {
82       Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
83     }
84   } else {
85     Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
86   }
87 }
88
89 pub(in crate::objects) fn check_object_domain<T, Kind>(
90   apub: &T,
91   expected_domain: Url,
92 ) -> Result<lemmy_db_schema::Url, LemmyError>
93 where
94   T: Base + AsBase<Kind>,
95 {
96   let domain = expected_domain.domain().context(location_info!())?;
97   let object_id = apub.id(domain)?.context(location_info!())?;
98   check_is_apub_id_valid(object_id)?;
99   Ok(object_id.to_owned().into())
100 }
101
102 pub(in crate::objects) fn set_content_and_source<T, Kind1, Kind2>(
103   object: &mut T,
104   markdown_text: &str,
105 ) -> Result<(), LemmyError>
106 where
107   T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
108 {
109   let mut source = Object::<()>::new_none_type();
110   source
111     .set_content(markdown_text)
112     .set_media_type(mime_markdown()?);
113   object.set_source(source.into_any_base()?);
114
115   // set `content` to markdown for compatibility with older Lemmy versions
116   // TODO: change this to HTML in a while
117   object.set_content(markdown_text);
118   object.set_media_type(mime_markdown()?);
119   //object.set_content(markdown_to_html(markdown_text));
120   Ok(())
121 }
122
123 pub(in crate::objects) fn get_source_markdown_value<T, Kind1, Kind2>(
124   object: &T,
125 ) -> Result<Option<String>, LemmyError>
126 where
127   T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
128 {
129   let content = object
130     .content()
131     .map(|s| s.as_single_xsd_string())
132     .flatten()
133     .map(|s| s.to_string());
134   if content.is_some() {
135     let source = object.source();
136     // updated lemmy version, read markdown from `source.content`
137     if let Some(source) = source {
138       let source = Object::<()>::from_any_base(source.to_owned())?.context(location_info!())?;
139       check_is_markdown(source.media_type())?;
140       let source_content = source
141         .content()
142         .map(|s| s.as_single_xsd_string())
143         .flatten()
144         .context(location_info!())?
145         .to_string();
146       return Ok(Some(source_content));
147     }
148     // older lemmy version, read markdown from `content`
149     // TODO: remove this after a while
150     else {
151       return Ok(content);
152     }
153   }
154   Ok(None)
155 }
156
157 pub(in crate::objects) fn mime_markdown() -> Result<Mime, FromStrError> {
158   "text/markdown".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>(
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> + 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   context: &LemmyContext,
209   request_counter: &mut i32,
210 ) -> Result<(), LemmyError>
211 where
212   T: ObjectExt<Kind>,
213 {
214   let user_id = object
215     .attributed_to()
216     .context(location_info!())?
217     .as_single_xsd_any_uri()
218     .context(location_info!())?;
219   let user = get_or_fetch_and_upsert_user(user_id, context, request_counter).await?;
220   let community_id = object
221     .to()
222     .context(location_info!())?
223     .as_single_xsd_any_uri()
224     .context(location_info!())?;
225   let community = get_or_fetch_and_upsert_community(community_id, context, request_counter).await?;
226   check_community_or_site_ban(&user, &community, context.pool()).await
227 }