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_apub_lib::values::MediaTypeMarkdown;
16 use lemmy_db_queries::{ApubObject, Crud, DbPool};
17 use lemmy_db_schema::{CommunityId, DbUrl};
20 settings::structs::Settings,
21 utils::{convert_datetime, markdown_to_html},
24 use lemmy_websocket::LemmyContext;
27 pub(crate) mod comment;
28 pub(crate) mod community;
29 pub(crate) mod person;
31 pub(crate) mod private_message;
33 /// Trait for converting an object or actor into the respective ActivityPub type.
34 #[async_trait::async_trait(?Send)]
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)]
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. 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
51 apub: &Self::ApubType,
52 context: &LemmyContext,
54 request_counter: &mut i32,
55 mod_action_allowed: bool,
56 ) -> Result<Self, LemmyError>
61 #[async_trait::async_trait(?Send)]
62 pub trait FromApubToForm<ApubType> {
65 context: &LemmyContext,
67 request_counter: &mut i32,
68 mod_action_allowed: bool,
69 ) -> Result<Self, LemmyError>
74 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
75 #[serde(rename_all = "camelCase")]
78 media_type: MediaTypeMarkdown,
81 /// Updated is actually the deletion time
82 fn create_tombstone<T>(
85 updated: Option<NaiveDateTime>,
87 ) -> Result<Tombstone, LemmyError>
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));
99 Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
102 Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
106 pub(in crate::objects) fn check_object_domain<T, Kind>(
108 expected_domain: Url,
109 use_strict_allowlist: bool,
110 ) -> Result<DbUrl, LemmyError>
112 T: Base + AsBase<Kind>,
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())
120 pub(in crate::objects) fn set_content_and_source<T, Kind1, Kind2>(
123 ) -> Result<(), LemmyError>
125 T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
127 let mut source = Object::<()>::new_none_type();
129 .set_content(markdown_text)
130 .set_media_type(mime_markdown()?);
131 object.set_source(source.into_any_base()?);
133 object.set_content(markdown_to_html(markdown_text));
134 object.set_media_type(mime_html()?);
138 pub(in crate::objects) fn get_source_markdown_value<T, Kind1, Kind2>(
140 ) -> Result<Option<String>, LemmyError>
142 T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
146 .map(|s| s.as_single_xsd_string().map(|s2| s2.to_string()))
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
154 .map(|s| s.as_single_xsd_string().map(|s2| s2.to_string()))
156 .context(location_info!())?;
157 return Ok(Some(source_content));
162 fn mime_markdown() -> Result<Mime, FromStrError> {
163 "text/markdown".parse()
166 fn mime_html() -> Result<Mime, FromStrError> {
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"
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>(
186 context: &LemmyContext,
187 expected_domain: Url,
188 request_counter: &mut i32,
190 ) -> Result<To, LemmyError>
193 To: ApubObject<ToForm> + Crud<ToForm, IdType> + Send + 'static,
194 ToForm: FromApubToForm<From> + Send + 'static,
196 let object_id = from.id_unchecked().context(location_info!())?.to_owned();
197 let domain = object_id.domain().context(location_info!())?;
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())
207 // otherwise parse and insert, assuring that it comes from the right domain
209 let to_form = ToForm::from_apub(
218 let to = blocking(context.pool(), move |conn| To::upsert(conn, &to_form)).await??;
223 pub(in crate::objects) async fn check_object_for_community_or_site_ban<T, Kind>(
225 community_id: CommunityId,
226 context: &LemmyContext,
227 request_counter: &mut i32,
228 ) -> Result<(), LemmyError>
232 let person_id = object
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