2 activities::{verify_is_public, verify_person_in_community},
3 check_is_apub_id_valid,
4 objects::read_from_string_or_source_opt,
7 page::{Page, PageType},
14 use activitystreams_kinds::public;
15 use chrono::NaiveDateTime;
16 use lemmy_api_common::blocking;
20 values::MediaTypeHtml,
21 verify::verify_domains_match,
23 use lemmy_db_schema::{
28 post::{Post, PostForm},
33 request::fetch_site_data,
34 utils::{check_slurs, convert_datetime, markdown_to_html, remove_slurs},
37 use lemmy_websocket::LemmyContext;
41 #[derive(Clone, Debug)]
42 pub struct ApubPost(Post);
44 impl Deref for ApubPost {
46 fn deref(&self) -> &Self::Target {
51 impl From<Post> for ApubPost {
52 fn from(p: Post) -> Self {
57 #[async_trait::async_trait(?Send)]
58 impl ApubObject for ApubPost {
59 type DataType = LemmyContext;
62 type TombstoneType = Tombstone;
64 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
68 #[tracing::instrument(skip_all)]
69 async fn read_from_apub_id(
71 context: &LemmyContext,
72 ) -> Result<Option<Self>, LemmyError> {
74 blocking(context.pool(), move |conn| {
75 Post::read_from_apub_id(conn, object_id)
82 #[tracing::instrument(skip_all)]
83 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
85 blocking(context.pool(), move |conn| {
86 Post::update_deleted(conn, self.id, true)
93 // Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
94 #[tracing::instrument(skip_all)]
95 async fn into_apub(self, context: &LemmyContext) -> Result<Page, LemmyError> {
96 let creator_id = self.creator_id;
97 let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
98 let community_id = self.community_id;
99 let community = blocking(context.pool(), move |conn| {
100 Community::read(conn, community_id)
105 r#type: PageType::Page,
106 id: ObjectId::new(self.ap_id.clone()),
107 attributed_to: ObjectId::new(creator.actor_id),
108 to: vec![community.actor_id.into(), public()],
110 name: self.name.clone(),
111 content: self.body.as_ref().map(|b| markdown_to_html(b)),
112 media_type: Some(MediaTypeHtml::Html),
113 source: self.body.clone().map(SourceCompat::new),
114 url: self.url.clone().map(|u| u.into()),
115 image: self.thumbnail_url.clone().map(ImageObject::new),
116 comments_enabled: Some(!self.locked),
117 sensitive: Some(self.nsfw),
118 stickied: Some(self.stickied),
119 published: Some(convert_datetime(self.published)),
120 updated: self.updated.map(convert_datetime),
125 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
126 Ok(Tombstone::new(self.ap_id.clone().into()))
129 #[tracing::instrument(skip_all)]
132 expected_domain: &Url,
133 context: &LemmyContext,
134 request_counter: &mut i32,
135 ) -> Result<(), LemmyError> {
136 // We can't verify the domain in case of mod action, because the mod may be on a different
137 // instance from the post author.
138 if !page.is_mod_action(context).await? {
139 verify_domains_match(page.id.inner(), expected_domain)?;
142 let community = page.extract_community(context, request_counter).await?;
143 check_is_apub_id_valid(page.id.inner(), community.local, &context.settings())?;
144 verify_person_in_community(&page.attributed_to, &community, context, request_counter).await?;
145 check_slurs(&page.name, &context.settings().slur_regex())?;
146 verify_domains_match(page.attributed_to.inner(), page.id.inner())?;
147 verify_is_public(&page.to, &page.cc)?;
151 #[tracing::instrument(skip_all)]
154 context: &LemmyContext,
155 request_counter: &mut i32,
156 ) -> Result<ApubPost, LemmyError> {
159 .dereference(context, context.client(), request_counter)
161 let community = page.extract_community(context, request_counter).await?;
163 let thumbnail_url: Option<Url> = page.image.map(|i| i.url);
164 let (metadata_res, pictrs_thumbnail) = if let Some(url) = &page.url {
165 fetch_site_data(context.client(), &context.settings(), Some(url)).await
167 (None, thumbnail_url)
169 let (embed_title, embed_description, embed_html) = metadata_res
170 .map(|u| (u.title, u.description, u.html))
171 .unwrap_or((None, None, None));
173 let body_slurs_removed = read_from_string_or_source_opt(&page.content, &page.source)
174 .map(|s| remove_slurs(&s, &context.settings().slur_regex()));
175 let form = PostForm {
177 url: page.url.map(|u| u.into()),
178 body: body_slurs_removed,
179 creator_id: creator.id,
180 community_id: community.id,
182 locked: page.comments_enabled.map(|e| !e),
183 published: page.published.map(|u| u.naive_local()),
184 updated: page.updated.map(|u| u.naive_local()),
186 nsfw: page.sensitive,
187 stickied: page.stickied,
191 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
192 ap_id: Some(page.id.into()),
195 let post = blocking(context.pool(), move |conn| Post::upsert(conn, &form)).await??;
205 community::tests::parse_lemmy_community,
206 person::tests::parse_lemmy_person,
210 protocol::tests::file_to_json_object,
212 use lemmy_db_schema::source::site::Site;
213 use serial_test::serial;
217 async fn test_parse_lemmy_post() {
218 let context = init_context();
219 let (person, site) = parse_lemmy_person(&context).await;
220 let community = parse_lemmy_community(&context).await;
222 let json = file_to_json_object("assets/lemmy/objects/page.json").unwrap();
223 let url = Url::parse("https://enterprise.lemmy.ml/post/55143").unwrap();
224 let mut request_counter = 0;
225 ApubPost::verify(&json, &url, &context, &mut request_counter)
228 let post = ApubPost::from_apub(json, &context, &mut request_counter)
232 assert_eq!(post.ap_id, url.into());
233 assert_eq!(post.name, "Post title");
234 assert!(post.body.is_some());
235 assert_eq!(post.body.as_ref().unwrap().len(), 45);
236 assert!(!post.locked);
237 assert!(post.stickied);
238 assert_eq!(request_counter, 0);
240 Post::delete(&*context.pool().get().unwrap(), post.id).unwrap();
241 Person::delete(&*context.pool().get().unwrap(), person.id).unwrap();
242 Community::delete(&*context.pool().get().unwrap(), community.id).unwrap();
243 Site::delete(&*context.pool().get().unwrap(), site.id).unwrap();