]> Untitled Git - lemmy.git/blob - crates/apub/src/objects/mod.rs
Merge pull request #1570 from guland2000/patch-2
[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_db_queries::{ApubObject, Crud, DbPool};
16 use lemmy_db_schema::{CommunityId, DbUrl};
17 use lemmy_utils::{
18   location_info,
19   settings::structs::Settings,
20   utils::{convert_datetime, markdown_to_html},
21   LemmyError,
22 };
23 use lemmy_websocket::LemmyContext;
24 use url::Url;
25
26 pub(crate) mod comment;
27 pub(crate) mod community;
28 pub(crate) mod person;
29 pub(crate) mod post;
30 pub(crate) mod private_message;
31
32 /// Trait for converting an object or actor into the respective ActivityPub type.
33 #[async_trait::async_trait(?Send)]
34 pub trait ToApub {
35   type ApubType;
36   async fn to_apub(&self, pool: &DbPool) -> Result<Self::ApubType, LemmyError>;
37   fn to_tombstone(&self) -> Result<Tombstone, LemmyError>;
38 }
39
40 #[async_trait::async_trait(?Send)]
41 pub trait FromApub {
42   type ApubType;
43   /// Converts an object from ActivityPub type to Lemmy internal type.
44   ///
45   /// * `apub` The object to read from
46   /// * `context` LemmyContext which holds DB pool, HTTP client etc
47   /// * `expected_domain` Domain where the object was received from. None in case of mod action.
48   /// * `mod_action_allowed` True if the object can be a mod activity, ignore `expected_domain` in this case
49   async fn from_apub(
50     apub: &Self::ApubType,
51     context: &LemmyContext,
52     expected_domain: Url,
53     request_counter: &mut i32,
54     mod_action_allowed: bool,
55   ) -> Result<Self, LemmyError>
56   where
57     Self: Sized;
58 }
59
60 #[async_trait::async_trait(?Send)]
61 pub trait FromApubToForm<ApubType> {
62   async fn from_apub(
63     apub: &ApubType,
64     context: &LemmyContext,
65     expected_domain: Url,
66     request_counter: &mut i32,
67     mod_action_allowed: bool,
68   ) -> Result<Self, LemmyError>
69   where
70     Self: Sized;
71 }
72
73 /// Updated is actually the deletion time
74 fn create_tombstone<T>(
75   deleted: bool,
76   object_id: Url,
77   updated: Option<NaiveDateTime>,
78   former_type: T,
79 ) -> Result<Tombstone, LemmyError>
80 where
81   T: ToString,
82 {
83   if deleted {
84     if let Some(updated) = updated {
85       let mut tombstone = Tombstone::new();
86       tombstone.set_id(object_id);
87       tombstone.set_former_type(former_type.to_string());
88       tombstone.set_deleted(convert_datetime(updated));
89       Ok(tombstone)
90     } else {
91       Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
92     }
93   } else {
94     Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
95   }
96 }
97
98 pub(in crate::objects) fn check_object_domain<T, Kind>(
99   apub: &T,
100   expected_domain: Url,
101 ) -> Result<DbUrl, LemmyError>
102 where
103   T: Base + AsBase<Kind>,
104 {
105   let domain = expected_domain.domain().context(location_info!())?;
106   let object_id = apub.id(domain)?.context(location_info!())?;
107   check_is_apub_id_valid(object_id)?;
108   Ok(object_id.to_owned().into())
109 }
110
111 pub(in crate::objects) fn set_content_and_source<T, Kind1, Kind2>(
112   object: &mut T,
113   markdown_text: &str,
114 ) -> Result<(), LemmyError>
115 where
116   T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
117 {
118   let mut source = Object::<()>::new_none_type();
119   source
120     .set_content(markdown_text)
121     .set_media_type(mime_markdown()?);
122   object.set_source(source.into_any_base()?);
123
124   object.set_content(markdown_to_html(markdown_text));
125   object.set_media_type(mime_html()?);
126   Ok(())
127 }
128
129 pub(in crate::objects) fn get_source_markdown_value<T, Kind1, Kind2>(
130   object: &T,
131 ) -> Result<Option<String>, LemmyError>
132 where
133   T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
134 {
135   let content = object
136     .content()
137     .map(|s| s.as_single_xsd_string().map(|s2| s2.to_string()))
138     .flatten();
139   if content.is_some() {
140     let source = object.source().context(location_info!())?;
141     let source = Object::<()>::from_any_base(source.to_owned())?.context(location_info!())?;
142     check_is_markdown(source.media_type())?;
143     let source_content = source
144       .content()
145       .map(|s| s.as_single_xsd_string().map(|s2| s2.to_string()))
146       .flatten()
147       .context(location_info!())?;
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 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   is_mod_action: bool,
181 ) -> Result<To, LemmyError>
182 where
183   From: BaseExt<Kind>,
184   To: ApubObject<ToForm> + Crud<ToForm, IdType> + Send + 'static,
185   ToForm: FromApubToForm<From> + Send + 'static,
186 {
187   let object_id = from.id_unchecked().context(location_info!())?.to_owned();
188   let domain = object_id.domain().context(location_info!())?;
189
190   // if its a local object, return it directly from the database
191   if Settings::get().hostname() == domain {
192     let object = blocking(context.pool(), move |conn| {
193       To::read_from_apub_id(conn, &object_id.into())
194     })
195     .await??;
196     Ok(object)
197   }
198   // otherwise parse and insert, assuring that it comes from the right domain
199   else {
200     let to_form = ToForm::from_apub(
201       &from,
202       context,
203       expected_domain,
204       request_counter,
205       is_mod_action,
206     )
207     .await?;
208
209     let to = blocking(context.pool(), move |conn| To::upsert(conn, &to_form)).await??;
210     Ok(to)
211   }
212 }
213
214 pub(in crate::objects) async fn check_object_for_community_or_site_ban<T, Kind>(
215   object: &T,
216   community_id: CommunityId,
217   context: &LemmyContext,
218   request_counter: &mut i32,
219 ) -> Result<(), LemmyError>
220 where
221   T: ObjectExt<Kind>,
222 {
223   let person_id = object
224     .attributed_to()
225     .context(location_info!())?
226     .as_single_xsd_any_uri()
227     .context(location_info!())?;
228   let person = get_or_fetch_and_upsert_person(person_id, context, request_counter).await?;
229   check_community_or_site_ban(&person, community_id, context.pool()).await
230 }