2 check_is_apub_id_valid,
3 extensions::{context::lemmy_context, page_extension::PageExtension},
4 fetcher::person::get_or_fetch_and_upsert_person,
5 get_community_from_to_or_cc,
8 check_object_for_community_or_site_ban,
11 get_source_markdown_value,
12 set_content_and_source,
19 use activitystreams::{
20 object::{kind::PageType, ApObject, Image, Page, Tombstone},
24 use activitystreams_ext::Ext1;
26 use lemmy_api_common::blocking;
27 use lemmy_db_queries::{Crud, DbPool};
28 use lemmy_db_schema::{
33 post::{Post, PostForm},
38 request::fetch_iframely_and_pictrs_data,
39 utils::{check_slurs, convert_datetime, remove_slurs},
42 use lemmy_websocket::LemmyContext;
45 #[async_trait::async_trait(?Send)]
46 impl ToApub for Post {
47 type ApubType = PageExt;
49 // Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
50 async fn to_apub(&self, pool: &DbPool) -> Result<PageExt, LemmyError> {
51 let mut page = ApObject::new(Page::new());
53 let creator_id = self.creator_id;
54 let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
56 let community_id = self.community_id;
57 let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
60 // Not needed when the Post is embedded in a collection (like for community outbox)
61 // TODO: need to set proper context defining sensitive/commentsEnabled fields
62 // https://git.asonix.dog/Aardwolf/activitystreams/issues/5
63 .set_many_contexts(lemmy_context()?)
64 .set_id(self.ap_id.to_owned().into_inner())
65 .set_name(self.name.to_owned())
66 // `summary` field for compatibility with lemmy v0.9.9 and older,
67 // TODO: remove this after some time
68 .set_summary(self.name.to_owned())
69 .set_published(convert_datetime(self.published))
70 .set_many_tos(vec![community.actor_id.into_inner(), public()])
71 .set_attributed_to(creator.actor_id.into_inner());
73 if let Some(body) = &self.body {
74 set_content_and_source(&mut page, &body)?;
77 if let Some(url) = &self.url {
78 page.set_url::<Url>(url.to_owned().into());
81 if let Some(thumbnail_url) = &self.thumbnail_url {
82 let mut image = Image::new();
83 image.set_url::<Url>(thumbnail_url.to_owned().into());
84 page.set_image(image.into_any_base()?);
87 if let Some(u) = self.updated {
88 page.set_updated(convert_datetime(u));
91 let ext = PageExtension {
92 comments_enabled: Some(!self.locked),
93 sensitive: Some(self.nsfw),
94 stickied: Some(self.stickied),
96 Ok(Ext1::new(page, ext))
99 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
102 self.ap_id.to_owned().into(),
109 #[async_trait::async_trait(?Send)]
110 impl FromApub for Post {
111 type ApubType = PageExt;
113 /// Converts a `PageExt` to `PostForm`.
115 /// If the post's community or creator are not known locally, these are also fetched.
118 context: &LemmyContext,
119 expected_domain: Url,
120 request_counter: &mut i32,
121 mod_action_allowed: bool,
122 ) -> Result<Post, LemmyError> {
123 let post: Post = get_object_from_apub(
131 check_object_for_community_or_site_ban(page, post.community_id, context, request_counter)
137 #[async_trait::async_trait(?Send)]
138 impl FromApubToForm<PageExt> for PostForm {
141 context: &LemmyContext,
142 expected_domain: Url,
143 request_counter: &mut i32,
144 mod_action_allowed: bool,
145 ) -> Result<PostForm, LemmyError> {
146 let community = get_community_from_to_or_cc(page, context, request_counter).await?;
147 let ap_id = if mod_action_allowed {
148 let id = page.id_unchecked().context(location_info!())?;
149 check_is_apub_id_valid(id, community.local)?;
152 check_object_domain(page, expected_domain, community.local)?
154 let ext = &page.ext_one;
155 let creator_actor_id = page
159 .context(location_info!())?
160 .as_single_xsd_any_uri()
161 .context(location_info!())?;
164 get_or_fetch_and_upsert_person(creator_actor_id, context, request_counter).await?;
166 let thumbnail_url: Option<Url> = match &page.inner.image() {
167 Some(any_image) => Image::from_any_base(
171 .context(location_info!())?
174 .context(location_info!())?
176 .context(location_info!())?
177 .as_single_xsd_any_uri()
178 .map(|url| url.to_owned()),
184 .map(|u| u.as_single_xsd_any_uri())
186 .map(|u| u.to_owned());
188 let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
189 if let Some(url) = &url {
190 fetch_iframely_and_pictrs_data(context.client(), Some(url)).await
192 (None, None, None, thumbnail_url)
198 // The following is for compatibility with lemmy v0.9.9 and older
199 // TODO: remove it after some time (along with the map above)
200 .or_else(|| page.inner.summary())
201 .context(location_info!())?
202 .as_single_xsd_string()
203 .context(location_info!())?
205 let body = get_source_markdown_value(page)?;
207 // TODO: expected_domain is wrong in this case, because it simply takes the domain of the actor
208 // maybe we need to take id_unchecked() if the activity is from community to user?
209 // why did this work before? -> i dont think it did?
210 // -> try to make expected_domain optional and set it null if it is a mod action
213 let body_slurs_removed = body.map(|b| remove_slurs(&b));
216 url: url.map(|u| u.into()),
217 body: body_slurs_removed,
218 creator_id: creator.id,
219 community_id: community.id,
221 locked: ext.comments_enabled.map(|e| !e),
226 .map(|u| u.to_owned().naive_local()),
231 .map(|u| u.to_owned().naive_local()),
234 stickied: ext.stickied,
235 embed_title: iframely_title,
236 embed_description: iframely_description,
237 embed_html: iframely_html,
238 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),