2 check_community_or_site_ban,
3 check_is_apub_id_valid,
4 fetcher::person::get_or_fetch_and_upsert_person,
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 lemmy_api_common::blocking;
15 use lemmy_db_queries::{ApubObject, Crud, DbPool};
16 use lemmy_db_schema::{CommunityId, DbUrl};
19 settings::structs::Settings,
20 utils::{convert_datetime, markdown_to_html},
23 use lemmy_websocket::LemmyContext;
26 pub(crate) mod comment;
27 pub(crate) mod community;
28 pub(crate) mod person;
30 pub(crate) mod private_message;
32 /// Trait for converting an object or actor into the respective ActivityPub type.
33 #[async_trait::async_trait(?Send)]
36 async fn to_apub(&self, pool: &DbPool) -> Result<Self::ApubType, LemmyError>;
37 fn to_tombstone(&self) -> Result<Tombstone, LemmyError>;
40 #[async_trait::async_trait(?Send)]
43 /// Converts an object from ActivityPub type to Lemmy internal type.
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
50 apub: &Self::ApubType,
51 context: &LemmyContext,
53 request_counter: &mut i32,
54 mod_action_allowed: bool,
55 ) -> Result<Self, LemmyError>
60 #[async_trait::async_trait(?Send)]
61 pub trait FromApubToForm<ApubType> {
64 context: &LemmyContext,
66 request_counter: &mut i32,
67 mod_action_allowed: bool,
68 ) -> Result<Self, LemmyError>
73 /// Updated is actually the deletion time
74 fn create_tombstone<T>(
77 updated: Option<NaiveDateTime>,
79 ) -> Result<Tombstone, LemmyError>
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));
91 Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
94 Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
98 pub(in crate::objects) fn check_object_domain<T, Kind>(
100 expected_domain: Url,
101 use_strict_allowlist: bool,
102 ) -> Result<DbUrl, LemmyError>
104 T: Base + AsBase<Kind>,
106 let domain = expected_domain.domain().context(location_info!())?;
107 let object_id = apub.id(domain)?.context(location_info!())?;
108 check_is_apub_id_valid(object_id, use_strict_allowlist)?;
109 Ok(object_id.to_owned().into())
112 pub(in crate::objects) fn set_content_and_source<T, Kind1, Kind2>(
115 ) -> Result<(), LemmyError>
117 T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
119 let mut source = Object::<()>::new_none_type();
121 .set_content(markdown_text)
122 .set_media_type(mime_markdown()?);
123 object.set_source(source.into_any_base()?);
125 object.set_content(markdown_to_html(markdown_text));
126 object.set_media_type(mime_html()?);
130 pub(in crate::objects) fn get_source_markdown_value<T, Kind1, Kind2>(
132 ) -> Result<Option<String>, LemmyError>
134 T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
138 .map(|s| s.as_single_xsd_string().map(|s2| s2.to_string()))
140 if content.is_some() {
141 let source = object.source().context(location_info!())?;
142 let source = Object::<()>::from_any_base(source.to_owned())?.context(location_info!())?;
143 check_is_markdown(source.media_type())?;
144 let source_content = source
146 .map(|s| s.as_single_xsd_string().map(|s2| s2.to_string()))
148 .context(location_info!())?;
149 return Ok(Some(source_content));
154 fn mime_markdown() -> Result<Mime, FromStrError> {
155 "text/markdown".parse()
158 fn mime_html() -> Result<Mime, FromStrError> {
162 pub(in crate::objects) fn check_is_markdown(mime: Option<&Mime>) -> Result<(), LemmyError> {
163 let mime = mime.context(location_info!())?;
164 if !mime.eq(&mime_markdown()?) {
165 Err(LemmyError::from(anyhow!(
166 "Lemmy only supports markdown content"
173 /// Converts an ActivityPub object (eg `Note`) to a database object (eg `Comment`). If an object
174 /// with the same ActivityPub ID already exists in the database, it is returned directly. Otherwise
175 /// the apub object is parsed, inserted and returned.
176 pub async fn get_object_from_apub<From, Kind, To, ToForm, IdType>(
178 context: &LemmyContext,
179 expected_domain: Url,
180 request_counter: &mut i32,
182 ) -> Result<To, LemmyError>
185 To: ApubObject<ToForm> + Crud<ToForm, IdType> + Send + 'static,
186 ToForm: FromApubToForm<From> + Send + 'static,
188 let object_id = from.id_unchecked().context(location_info!())?.to_owned();
189 let domain = object_id.domain().context(location_info!())?;
191 // if its a local object, return it directly from the database
192 if Settings::get().hostname() == domain {
193 let object = blocking(context.pool(), move |conn| {
194 To::read_from_apub_id(conn, &object_id.into())
199 // otherwise parse and insert, assuring that it comes from the right domain
201 let to_form = ToForm::from_apub(
210 let to = blocking(context.pool(), move |conn| To::upsert(conn, &to_form)).await??;
215 pub(in crate::objects) async fn check_object_for_community_or_site_ban<T, Kind>(
217 community_id: CommunityId,
218 context: &LemmyContext,
219 request_counter: &mut i32,
220 ) -> Result<(), LemmyError>
224 let person_id = object
226 .context(location_info!())?
227 .as_single_xsd_any_uri()
228 .context(location_info!())?;
229 let person = get_or_fetch_and_upsert_person(person_id, context, request_counter).await?;
230 check_community_or_site_ban(&person, community_id, context.pool()).await