2 check_is_apub_id_valid,
3 fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
4 inbox::{community_inbox::check_community_or_site_ban, get_activity_to_and_cc},
8 base::{AsBase, BaseExt, ExtendsExt},
10 mime::{FromStrError, Mime},
11 object::{ApObjectExt, Object, ObjectExt, Tombstone, TombstoneExt},
13 use anyhow::{anyhow, Context};
14 use chrono::NaiveDateTime;
15 use diesel::result::Error::NotFound;
16 use lemmy_api_common::blocking;
17 use lemmy_db_queries::{ApubObject, Crud, DbPool};
18 use lemmy_db_schema::{source::community::Community, CommunityId, DbUrl};
21 settings::structs::Settings,
22 utils::{convert_datetime, markdown_to_html},
25 use lemmy_websocket::LemmyContext;
28 pub(crate) mod comment;
29 pub(crate) mod community;
30 pub(crate) mod person;
32 pub(crate) mod private_message;
34 /// Trait for converting an object or actor into the respective ActivityPub type.
35 #[async_trait::async_trait(?Send)]
36 pub(crate) trait ToApub {
38 async fn to_apub(&self, pool: &DbPool) -> Result<Self::ApubType, LemmyError>;
39 fn to_tombstone(&self) -> Result<Tombstone, LemmyError>;
42 #[async_trait::async_trait(?Send)]
43 pub(crate) trait FromApub {
45 /// Converts an object from ActivityPub type to Lemmy internal type.
47 /// * `apub` The object to read from
48 /// * `context` LemmyContext which holds DB pool, HTTP client etc
49 /// * `expected_domain` Domain where the object was received from. None in case of mod action.
50 /// * `mod_action_allowed` True if the object can be a mod activity, ignore `expected_domain` in this case
52 apub: &Self::ApubType,
53 context: &LemmyContext,
55 request_counter: &mut i32,
56 mod_action_allowed: bool,
57 ) -> Result<Self, LemmyError>
62 #[async_trait::async_trait(?Send)]
63 pub(in crate::objects) trait FromApubToForm<ApubType> {
66 context: &LemmyContext,
68 request_counter: &mut i32,
69 mod_action_allowed: bool,
70 ) -> Result<Self, LemmyError>
75 /// Updated is actually the deletion time
76 fn create_tombstone<T>(
79 updated: Option<NaiveDateTime>,
81 ) -> Result<Tombstone, LemmyError>
86 if let Some(updated) = updated {
87 let mut tombstone = Tombstone::new();
88 tombstone.set_id(object_id);
89 tombstone.set_former_type(former_type.to_string());
90 tombstone.set_deleted(convert_datetime(updated));
93 Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
96 Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
100 pub(in crate::objects) fn check_object_domain<T, Kind>(
102 expected_domain: Url,
103 ) -> Result<DbUrl, LemmyError>
105 T: Base + AsBase<Kind>,
107 let domain = expected_domain.domain().context(location_info!())?;
108 let object_id = apub.id(domain)?.context(location_info!())?;
109 check_is_apub_id_valid(object_id)?;
110 Ok(object_id.to_owned().into())
113 pub(in crate::objects) fn set_content_and_source<T, Kind1, Kind2>(
116 ) -> Result<(), LemmyError>
118 T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
120 let mut source = Object::<()>::new_none_type();
122 .set_content(markdown_text)
123 .set_media_type(mime_markdown()?);
124 object.set_source(source.into_any_base()?);
126 object.set_content(markdown_to_html(markdown_text));
127 object.set_media_type(mime_html()?);
131 pub(in crate::objects) fn get_source_markdown_value<T, Kind1, Kind2>(
133 ) -> Result<Option<String>, LemmyError>
135 T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
139 .map(|s| s.as_single_xsd_string().map(|s2| s2.to_string()))
141 if content.is_some() {
142 let source = object.source().context(location_info!())?;
143 let source = Object::<()>::from_any_base(source.to_owned())?.context(location_info!())?;
144 check_is_markdown(source.media_type())?;
145 let source_content = source
147 .map(|s| s.as_single_xsd_string().map(|s2| s2.to_string()))
149 .context(location_info!())?;
150 return Ok(Some(source_content));
155 fn mime_markdown() -> Result<Mime, FromStrError> {
156 "text/markdown".parse()
159 fn mime_html() -> Result<Mime, FromStrError> {
163 pub(in crate::objects) fn check_is_markdown(mime: Option<&Mime>) -> Result<(), LemmyError> {
164 let mime = mime.context(location_info!())?;
165 if !mime.eq(&mime_markdown()?) {
166 Err(LemmyError::from(anyhow!(
167 "Lemmy only supports markdown content"
174 /// Converts an ActivityPub object (eg `Note`) to a database object (eg `Comment`). If an object
175 /// with the same ActivityPub ID already exists in the database, it is returned directly. Otherwise
176 /// the apub object is parsed, inserted and returned.
177 pub(in crate::objects) async fn get_object_from_apub<From, Kind, To, ToForm, IdType>(
179 context: &LemmyContext,
180 expected_domain: Url,
181 request_counter: &mut i32,
183 ) -> Result<To, LemmyError>
186 To: ApubObject<ToForm> + Crud<ToForm, IdType> + Send + 'static,
187 ToForm: FromApubToForm<From> + Send + 'static,
189 let object_id = from.id_unchecked().context(location_info!())?.to_owned();
190 let domain = object_id.domain().context(location_info!())?;
192 // if its a local object, return it directly from the database
193 if Settings::get().hostname() == domain {
194 let object = blocking(context.pool(), move |conn| {
195 To::read_from_apub_id(conn, &object_id.into())
200 // otherwise parse and insert, assuring that it comes from the right domain
202 let to_form = ToForm::from_apub(
211 let to = blocking(context.pool(), move |conn| To::upsert(conn, &to_form)).await??;
216 pub(in crate::objects) async fn check_object_for_community_or_site_ban<T, Kind>(
218 community_id: CommunityId,
219 context: &LemmyContext,
220 request_counter: &mut i32,
221 ) -> Result<(), LemmyError>
225 let person_id = object
227 .context(location_info!())?
228 .as_single_xsd_any_uri()
229 .context(location_info!())?;
230 let person = get_or_fetch_and_upsert_person(person_id, context, request_counter).await?;
231 check_community_or_site_ban(&person, community_id, context.pool()).await
234 pub(in crate::objects) async fn get_community_from_to_or_cc(
236 context: &LemmyContext,
237 request_counter: &mut i32,
238 ) -> Result<Community, LemmyError> {
239 for cid in get_activity_to_and_cc(page) {
240 let community = get_or_fetch_and_upsert_community(&cid, context, request_counter).await;
241 if community.is_ok() {