2 extensions::{context::lemmy_context, page_extension::PageExtension},
3 fetcher::{community::get_or_fetch_and_upsert_community, user::get_or_fetch_and_upsert_user},
6 check_object_for_community_or_site_ban,
9 get_source_markdown_value,
10 set_content_and_source,
17 use activitystreams::{
18 object::{kind::PageType, ApObject, Image, Page, Tombstone},
21 use activitystreams_ext::Ext1;
23 use lemmy_db_queries::{Crud, DbPool};
24 use lemmy_db_schema::source::{
26 post::{Post, PostForm},
29 use lemmy_structs::blocking;
32 request::fetch_iframely_and_pictrs_data,
33 utils::{check_slurs, convert_datetime, remove_slurs},
36 use lemmy_websocket::LemmyContext;
39 #[async_trait::async_trait(?Send)]
40 impl ToApub for Post {
41 type ApubType = PageExt;
43 // Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
44 async fn to_apub(&self, pool: &DbPool) -> Result<PageExt, LemmyError> {
45 let mut page = ApObject::new(Page::new());
47 let creator_id = self.creator_id;
48 let creator = blocking(pool, move |conn| User_::read(conn, creator_id)).await??;
50 let community_id = self.community_id;
51 let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
54 // Not needed when the Post is embedded in a collection (like for community outbox)
55 // TODO: need to set proper context defining sensitive/commentsEnabled fields
56 // https://git.asonix.dog/Aardwolf/activitystreams/issues/5
57 .set_many_contexts(lemmy_context()?)
58 .set_id(self.ap_id.to_owned().into_inner())
59 // Use summary field to be consistent with mastodon content warning.
60 // https://mastodon.xyz/@Louisa/103987265222901387.json
61 .set_summary(self.name.to_owned())
62 .set_published(convert_datetime(self.published))
63 .set_to(community.actor_id.into_inner())
64 .set_attributed_to(creator.actor_id.into_inner());
66 if let Some(body) = &self.body {
67 set_content_and_source(&mut page, &body)?;
70 // TODO: hacky code because we get self.url == Some("")
71 // https://github.com/LemmyNet/lemmy/issues/602
72 let url = self.url.as_ref().filter(|u| !u.is_empty());
73 if let Some(u) = url {
74 page.set_url(Url::parse(u)?);
77 if let Some(thumbnail_url) = &self.thumbnail_url {
78 let mut image = Image::new();
79 image.set_url(Url::parse(thumbnail_url)?);
80 page.set_image(image.into_any_base()?);
83 if let Some(u) = self.updated {
84 page.set_updated(convert_datetime(u));
87 let ext = PageExtension {
88 comments_enabled: Some(!self.locked),
89 sensitive: Some(self.nsfw),
90 stickied: Some(self.stickied),
92 Ok(Ext1::new(page, ext))
95 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
98 self.ap_id.to_owned().into(),
105 #[async_trait::async_trait(?Send)]
106 impl FromApub for Post {
107 type ApubType = PageExt;
109 /// Converts a `PageExt` to `PostForm`.
111 /// If the post's community or creator are not known locally, these are also fetched.
114 context: &LemmyContext,
115 expected_domain: Url,
116 request_counter: &mut i32,
117 ) -> Result<Post, LemmyError> {
118 check_object_for_community_or_site_ban(page, context, request_counter).await?;
119 get_object_from_apub(page, context, expected_domain, request_counter).await
123 #[async_trait::async_trait(?Send)]
124 impl FromApubToForm<PageExt> for PostForm {
127 context: &LemmyContext,
128 expected_domain: Url,
129 request_counter: &mut i32,
130 ) -> Result<PostForm, LemmyError> {
131 let ext = &page.ext_one;
132 let creator_actor_id = page
136 .context(location_info!())?
137 .as_single_xsd_any_uri()
138 .context(location_info!())?;
140 let creator = get_or_fetch_and_upsert_user(creator_actor_id, context, request_counter).await?;
142 let community_actor_id = page
146 .context(location_info!())?
147 .as_single_xsd_any_uri()
148 .context(location_info!())?;
151 get_or_fetch_and_upsert_community(community_actor_id, context, request_counter).await?;
153 let thumbnail_url = match &page.inner.image() {
154 Some(any_image) => Image::from_any_base(
158 .context(location_info!())?
161 .context(location_info!())?
163 .context(location_info!())?
164 .as_single_xsd_any_uri()
165 .map(|u| u.to_string()),
171 .map(|u| u.as_single_xsd_any_uri())
173 .map(|s| s.to_string());
175 let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
176 if let Some(url) = &url {
177 fetch_iframely_and_pictrs_data(context.client(), Some(url.to_owned())).await
179 (None, None, None, thumbnail_url)
186 .context(location_info!())?
187 .as_single_xsd_string()
188 .context(location_info!())?
190 let body = get_source_markdown_value(page)?;
193 let body_slurs_removed = body.map(|b| remove_slurs(&b));
197 body: body_slurs_removed,
198 creator_id: creator.id,
199 community_id: community.id,
201 locked: ext.comments_enabled.map(|e| !e),
206 .map(|u| u.to_owned().naive_local()),
211 .map(|u| u.to_owned().naive_local()),
213 nsfw: ext.sensitive.unwrap_or(false),
214 stickied: ext.stickied.or(Some(false)),
215 embed_title: iframely_title,
216 embed_description: iframely_description,
217 embed_html: iframely_html,
218 thumbnail_url: pictrs_thumbnail,
219 ap_id: Some(check_object_domain(page, expected_domain)?),