2 activities::{verify_is_public, verify_person_in_community},
3 check_is_apub_id_valid,
5 objects::{page::Page, tombstone::Tombstone},
10 use activitystreams_kinds::{object::PageType, public};
11 use chrono::NaiveDateTime;
12 use lemmy_api_common::blocking;
16 values::{MediaTypeHtml, MediaTypeMarkdown},
17 verify::verify_domains_match,
19 use lemmy_db_schema::{
24 post::{Post, PostForm},
29 request::fetch_site_data,
30 utils::{check_slurs, convert_datetime, markdown_to_html, remove_slurs},
33 use lemmy_websocket::LemmyContext;
37 #[derive(Clone, Debug)]
38 pub struct ApubPost(Post);
40 impl Deref for ApubPost {
42 fn deref(&self) -> &Self::Target {
47 impl From<Post> for ApubPost {
48 fn from(p: Post) -> Self {
53 #[async_trait::async_trait(?Send)]
54 impl ApubObject for ApubPost {
55 type DataType = LemmyContext;
57 type TombstoneType = Tombstone;
59 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
63 async fn read_from_apub_id(
65 context: &LemmyContext,
66 ) -> Result<Option<Self>, LemmyError> {
68 blocking(context.pool(), move |conn| {
69 Post::read_from_apub_id(conn, object_id)
76 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
78 blocking(context.pool(), move |conn| {
79 Post::update_deleted(conn, self.id, true)
86 // Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
87 async fn into_apub(self, context: &LemmyContext) -> Result<Page, LemmyError> {
88 let creator_id = self.creator_id;
89 let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
90 let community_id = self.community_id;
91 let community = blocking(context.pool(), move |conn| {
92 Community::read(conn, community_id)
96 let source = self.body.clone().map(|body| Source {
98 media_type: MediaTypeMarkdown::Markdown,
100 let image = self.thumbnail_url.clone().map(ImageObject::new);
103 r#type: PageType::Page,
104 id: ObjectId::new(self.ap_id.clone()),
105 attributed_to: ObjectId::new(creator.actor_id),
106 to: vec![community.actor_id.into(), public()],
108 name: self.name.clone(),
109 content: self.body.as_ref().map(|b| markdown_to_html(b)),
110 media_type: Some(MediaTypeHtml::Html),
112 url: self.url.clone().map(|u| u.into()),
114 comments_enabled: Some(!self.locked),
115 sensitive: Some(self.nsfw),
116 stickied: Some(self.stickied),
117 published: Some(convert_datetime(self.published)),
118 updated: self.updated.map(convert_datetime),
119 unparsed: Default::default(),
124 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
125 Ok(Tombstone::new(self.ap_id.clone().into()))
130 expected_domain: &Url,
131 context: &LemmyContext,
132 request_counter: &mut i32,
133 ) -> Result<(), LemmyError> {
134 // We can't verify the domain in case of mod action, because the mod may be on a different
135 // instance from the post author.
136 if !page.is_mod_action(context).await? {
137 verify_domains_match(page.id.inner(), expected_domain)?;
140 let community = page.extract_community(context, request_counter).await?;
141 check_is_apub_id_valid(page.id.inner(), community.local, &context.settings())?;
142 verify_person_in_community(&page.attributed_to, &community, context, request_counter).await?;
143 check_slurs(&page.name, &context.settings().slur_regex())?;
144 verify_domains_match(page.attributed_to.inner(), page.id.inner())?;
145 verify_is_public(&page.to, &page.cc)?;
151 context: &LemmyContext,
152 request_counter: &mut i32,
153 ) -> Result<ApubPost, LemmyError> {
156 .dereference(context, request_counter)
158 let community = page.extract_community(context, request_counter).await?;
160 let thumbnail_url: Option<Url> = page.image.map(|i| i.url);
161 let (metadata_res, pictrs_thumbnail) = if let Some(url) = &page.url {
162 fetch_site_data(context.client(), &context.settings(), Some(url)).await
164 (None, thumbnail_url)
166 let (embed_title, embed_description, embed_html) = metadata_res
167 .map(|u| (u.title, u.description, u.html))
168 .unwrap_or((None, None, None));
170 let body_slurs_removed = page
173 .map(|s| remove_slurs(&s.content, &context.settings().slur_regex()));
174 let form = PostForm {
176 url: page.url.map(|u| u.into()),
177 body: body_slurs_removed,
178 creator_id: creator.id,
179 community_id: community.id,
181 locked: page.comments_enabled.map(|e| !e),
182 published: page.published.map(|u| u.naive_local()),
183 updated: page.updated.map(|u| u.naive_local()),
185 nsfw: page.sensitive,
186 stickied: page.stickied,
190 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
191 ap_id: Some(page.id.into()),
194 let post = blocking(context.pool(), move |conn| Post::upsert(conn, &form)).await??;
202 use crate::objects::{
203 community::tests::parse_lemmy_community,
204 person::tests::parse_lemmy_person,
206 tests::{file_to_json_object, init_context},
208 use serial_test::serial;
212 async fn test_parse_lemmy_post() {
213 let context = init_context();
214 let community = parse_lemmy_community(&context).await;
215 let person = parse_lemmy_person(&context).await;
217 let json = file_to_json_object("assets/lemmy/objects/page.json");
218 let url = Url::parse("https://enterprise.lemmy.ml/post/55143").unwrap();
219 let mut request_counter = 0;
220 ApubPost::verify(&json, &url, &context, &mut request_counter)
223 let post = ApubPost::from_apub(json, &context, &mut request_counter)
227 assert_eq!(post.ap_id, url.into());
228 assert_eq!(post.name, "Post title");
229 assert!(post.body.is_some());
230 assert_eq!(post.body.as_ref().unwrap().len(), 45);
231 assert!(!post.locked);
232 assert!(post.stickied);
233 assert_eq!(request_counter, 0);
235 Post::delete(&*context.pool().get().unwrap(), post.id).unwrap();
236 Person::delete(&*context.pool().get().unwrap(), person.id).unwrap();
237 Community::delete(&*context.pool().get().unwrap(), community.id).unwrap();