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