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,
7 base::{AsBase, BaseExt, ExtendsExt},
9 mime::{FromStrError, Mime},
10 object::{ApObjectExt, Object, ObjectExt, Tombstone, TombstoneExt},
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;
21 utils::{convert_datetime, markdown_to_html},
24 use lemmy_websocket::LemmyContext;
27 pub(crate) mod comment;
28 pub(crate) mod community;
30 pub(crate) mod private_message;
33 /// Trait for converting an object or actor into the respective ActivityPub type.
34 #[async_trait::async_trait(?Send)]
35 pub(crate) trait ToApub {
37 async fn to_apub(&self, pool: &DbPool) -> Result<Self::ApubType, LemmyError>;
38 fn to_tombstone(&self) -> Result<Tombstone, LemmyError>;
41 #[async_trait::async_trait(?Send)]
42 pub(crate) trait FromApub {
44 /// Converts an object from ActivityPub type to Lemmy internal type.
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
50 apub: &Self::ApubType,
51 context: &LemmyContext,
53 request_counter: &mut i32,
54 ) -> Result<Self, LemmyError>
59 #[async_trait::async_trait(?Send)]
60 pub(in crate::objects) trait FromApubToForm<ApubType> {
63 context: &LemmyContext,
65 request_counter: &mut i32,
66 ) -> Result<Self, LemmyError>
71 /// Updated is actually the deletion time
72 fn create_tombstone<T>(
75 updated: Option<NaiveDateTime>,
77 ) -> Result<Tombstone, LemmyError>
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));
89 Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
92 Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
96 pub(in crate::objects) fn check_object_domain<T, Kind>(
99 ) -> Result<lemmy_db_schema::Url, LemmyError>
101 T: Base + AsBase<Kind>,
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())
109 pub(in crate::objects) fn set_content_and_source<T, Kind1, Kind2>(
112 ) -> Result<(), LemmyError>
114 T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
116 let mut source = Object::<()>::new_none_type();
118 .set_content(markdown_text)
119 .set_media_type(mime_markdown()?);
120 object.set_source(source.into_any_base()?);
122 object.set_content(markdown_to_html(markdown_text));
123 object.set_media_type(mime_html()?);
127 pub(in crate::objects) fn get_source_markdown_value<T, Kind1, Kind2>(
129 ) -> Result<Option<String>, LemmyError>
131 T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
135 .map(|s| s.as_single_xsd_string())
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
144 .map(|s| s.as_single_xsd_string())
146 .context(location_info!())?
148 return Ok(Some(source_content));
153 fn mime_markdown() -> Result<Mime, FromStrError> {
154 "text/markdown".parse()
157 fn mime_html() -> Result<Mime, FromStrError> {
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"
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>(
177 context: &LemmyContext,
178 expected_domain: Url,
179 request_counter: &mut i32,
180 ) -> Result<To, LemmyError>
183 To: ApubObject<ToForm> + Crud<ToForm> + Send + 'static,
184 ToForm: FromApubToForm<From> + Send + 'static,
186 let object_id = from.id_unchecked().context(location_info!())?.to_owned();
187 let domain = object_id.domain().context(location_info!())?;
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())
197 // otherwise parse and insert, assuring that it comes from the right domain
199 let to_form = ToForm::from_apub(&from, context, expected_domain, request_counter).await?;
201 let to = blocking(context.pool(), move |conn| To::upsert(conn, &to_form)).await??;
206 pub(in crate::objects) async fn check_object_for_community_or_site_ban<T, Kind>(
209 context: &LemmyContext,
210 request_counter: &mut i32,
211 ) -> Result<(), LemmyError>
217 .context(location_info!())?
218 .as_single_xsd_any_uri()
219 .context(location_info!())?;
220 let user = get_or_fetch_and_upsert_user(user_id, context, request_counter).await?;
221 check_community_or_site_ban(&user, community_id, context.pool()).await
224 pub(in crate::objects) async fn get_to_community<T, Kind>(
226 context: &LemmyContext,
227 request_counter: &mut i32,
228 ) -> Result<Community, LemmyError>
232 let community_ids = object
234 .context(location_info!())?
236 .context(location_info!())?
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() {