2 activities::{verify_is_public, verify_person_in_community},
3 check_is_apub_id_valid,
6 page::{Page, PageType},
13 use activitystreams_kinds::public;
14 use chrono::NaiveDateTime;
15 use lemmy_api_common::blocking;
19 values::{MediaTypeHtml, MediaTypeMarkdown},
20 verify::verify_domains_match,
22 use lemmy_db_schema::{
27 post::{Post, PostForm},
32 request::fetch_site_data,
33 utils::{check_slurs, convert_datetime, markdown_to_html, remove_slurs},
36 use lemmy_websocket::LemmyContext;
40 #[derive(Clone, Debug)]
41 pub struct ApubPost(Post);
43 impl Deref for ApubPost {
45 fn deref(&self) -> &Self::Target {
50 impl From<Post> for ApubPost {
51 fn from(p: Post) -> Self {
56 #[async_trait::async_trait(?Send)]
57 impl ApubObject for ApubPost {
58 type DataType = LemmyContext;
60 type TombstoneType = Tombstone;
62 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
66 #[tracing::instrument(skip_all)]
67 async fn read_from_apub_id(
69 context: &LemmyContext,
70 ) -> Result<Option<Self>, LemmyError> {
72 blocking(context.pool(), move |conn| {
73 Post::read_from_apub_id(conn, object_id)
80 #[tracing::instrument(skip_all)]
81 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
83 blocking(context.pool(), move |conn| {
84 Post::update_deleted(conn, self.id, true)
91 // Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
92 #[tracing::instrument(skip_all)]
93 async fn into_apub(self, context: &LemmyContext) -> Result<Page, LemmyError> {
94 let creator_id = self.creator_id;
95 let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
96 let community_id = self.community_id;
97 let community = blocking(context.pool(), move |conn| {
98 Community::read(conn, community_id)
102 let source = self.body.clone().map(|body| Source {
104 media_type: MediaTypeMarkdown::Markdown,
106 let image = self.thumbnail_url.clone().map(ImageObject::new);
109 r#type: PageType::Page,
110 id: ObjectId::new(self.ap_id.clone()),
111 attributed_to: ObjectId::new(creator.actor_id),
112 to: vec![community.actor_id.into(), public()],
114 name: self.name.clone(),
115 content: self.body.as_ref().map(|b| markdown_to_html(b)),
116 media_type: Some(MediaTypeHtml::Html),
118 url: self.url.clone().map(|u| u.into()),
120 comments_enabled: Some(!self.locked),
121 sensitive: Some(self.nsfw),
122 stickied: Some(self.stickied),
123 published: Some(convert_datetime(self.published)),
124 updated: self.updated.map(convert_datetime),
125 unparsed: Default::default(),
130 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
131 Ok(Tombstone::new(self.ap_id.clone().into()))
134 #[tracing::instrument(skip_all)]
137 expected_domain: &Url,
138 context: &LemmyContext,
139 request_counter: &mut i32,
140 ) -> Result<(), LemmyError> {
141 // We can't verify the domain in case of mod action, because the mod may be on a different
142 // instance from the post author.
143 if !page.is_mod_action(context).await? {
144 verify_domains_match(page.id.inner(), expected_domain)?;
147 let community = page.extract_community(context, request_counter).await?;
148 check_is_apub_id_valid(page.id.inner(), community.local, &context.settings())?;
149 verify_person_in_community(&page.attributed_to, &community, context, request_counter).await?;
150 check_slurs(&page.name, &context.settings().slur_regex())?;
151 verify_domains_match(page.attributed_to.inner(), page.id.inner())?;
152 verify_is_public(&page.to, &page.cc)?;
156 #[tracing::instrument(skip_all)]
159 context: &LemmyContext,
160 request_counter: &mut i32,
161 ) -> Result<ApubPost, LemmyError> {
164 .dereference(context, context.client(), request_counter)
166 let community = page.extract_community(context, request_counter).await?;
168 let thumbnail_url: Option<Url> = page.image.map(|i| i.url);
169 let (metadata_res, pictrs_thumbnail) = if let Some(url) = &page.url {
170 fetch_site_data(context.client(), &context.settings(), Some(url)).await
172 (None, thumbnail_url)
174 let (embed_title, embed_description, embed_html) = metadata_res
175 .map(|u| (u.title, u.description, u.html))
176 .unwrap_or((None, None, None));
178 let body_slurs_removed = page
181 .map(|s| remove_slurs(&s.content, &context.settings().slur_regex()));
182 let form = PostForm {
184 url: page.url.map(|u| u.into()),
185 body: body_slurs_removed,
186 creator_id: creator.id,
187 community_id: community.id,
189 locked: page.comments_enabled.map(|e| !e),
190 published: page.published.map(|u| u.naive_local()),
191 updated: page.updated.map(|u| u.naive_local()),
193 nsfw: page.sensitive,
194 stickied: page.stickied,
198 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
199 ap_id: Some(page.id.into()),
202 let post = blocking(context.pool(), move |conn| Post::upsert(conn, &form)).await??;
210 use crate::objects::{
211 community::tests::parse_lemmy_community,
212 person::tests::parse_lemmy_person,
214 tests::{file_to_json_object, init_context},
216 use lemmy_apub_lib::activity_queue::create_activity_queue;
217 use lemmy_db_schema::source::site::Site;
218 use serial_test::serial;
222 async fn test_parse_lemmy_post() {
223 let client = reqwest::Client::new().into();
224 let manager = create_activity_queue(client);
225 let context = init_context(manager.queue_handle().clone());
226 let (person, site) = parse_lemmy_person(&context).await;
227 let community = parse_lemmy_community(&context).await;
229 let json = file_to_json_object("assets/lemmy/objects/page.json").unwrap();
230 let url = Url::parse("https://enterprise.lemmy.ml/post/55143").unwrap();
231 let mut request_counter = 0;
232 ApubPost::verify(&json, &url, &context, &mut request_counter)
235 let post = ApubPost::from_apub(json, &context, &mut request_counter)
239 assert_eq!(post.ap_id, url.into());
240 assert_eq!(post.name, "Post title");
241 assert!(post.body.is_some());
242 assert_eq!(post.body.as_ref().unwrap().len(), 45);
243 assert!(!post.locked);
244 assert!(post.stickied);
245 assert_eq!(request_counter, 0);
247 Post::delete(&*context.pool().get().unwrap(), post.id).unwrap();
248 Person::delete(&*context.pool().get().unwrap(), person.id).unwrap();
249 Community::delete(&*context.pool().get().unwrap(), community.id).unwrap();
250 Site::delete(&*context.pool().get().unwrap(), site.id).unwrap();