2 activities::verify_person_in_community,
3 fetcher::object_id::ObjectId,
5 objects::{page::Page, tombstone::Tombstone},
10 use activitystreams::{
11 object::kind::{ImageType, PageType},
14 use chrono::NaiveDateTime;
15 use lemmy_api_common::blocking;
18 values::{MediaTypeHtml, MediaTypeMarkdown},
20 use lemmy_db_schema::{
25 post::{Post, PostForm},
30 request::fetch_site_data,
31 utils::{convert_datetime, markdown_to_html, remove_slurs},
34 use lemmy_websocket::LemmyContext;
38 #[derive(Clone, Debug)]
39 pub struct ApubPost(Post);
41 impl Deref for ApubPost {
43 fn deref(&self) -> &Self::Target {
48 impl From<Post> for ApubPost {
49 fn from(p: Post) -> Self {
54 #[async_trait::async_trait(?Send)]
55 impl ApubObject for ApubPost {
56 type DataType = LemmyContext;
58 type TombstoneType = Tombstone;
60 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
64 async fn read_from_apub_id(
66 context: &LemmyContext,
67 ) -> Result<Option<Self>, LemmyError> {
69 blocking(context.pool(), move |conn| {
70 Post::read_from_apub_id(conn, object_id)
77 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
78 blocking(context.pool(), move |conn| {
79 Post::update_deleted(conn, self.id, true)
85 // Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
86 async fn to_apub(&self, context: &LemmyContext) -> Result<Page, LemmyError> {
87 let creator_id = self.creator_id;
88 let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
89 let community_id = self.community_id;
90 let community = blocking(context.pool(), move |conn| {
91 Community::read(conn, community_id)
95 let source = self.body.clone().map(|body| Source {
97 media_type: MediaTypeMarkdown::Markdown,
99 let image = self.thumbnail_url.clone().map(|thumb| ImageObject {
100 kind: ImageType::Image,
105 r#type: PageType::Page,
106 id: self.ap_id.clone().into(),
107 attributed_to: ObjectId::new(creator.actor_id),
108 to: vec![community.actor_id.into(), public()],
109 name: self.name.clone(),
110 content: self.body.as_ref().map(|b| markdown_to_html(b)),
111 media_type: Some(MediaTypeHtml::Html),
113 url: self.url.clone().map(|u| u.into()),
115 comments_enabled: Some(!self.locked),
116 sensitive: Some(self.nsfw),
117 stickied: Some(self.stickied),
118 published: Some(convert_datetime(self.published)),
119 updated: self.updated.map(convert_datetime),
120 unparsed: Default::default(),
125 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
128 self.updated.unwrap_or(self.published),
134 context: &LemmyContext,
135 expected_domain: &Url,
136 request_counter: &mut i32,
137 ) -> Result<ApubPost, LemmyError> {
138 // We can't verify the domain in case of mod action, because the mod may be on a different
139 // instance from the post author.
140 let ap_id = if page.is_mod_action(context).await? {
143 page.id(expected_domain)?
145 let ap_id = Some(ap_id.clone().into());
148 .dereference(context, request_counter)
150 let community = page.extract_community(context, request_counter).await?;
151 verify_person_in_community(&page.attributed_to, &community, context, request_counter).await?;
153 let thumbnail_url: Option<Url> = page.image.clone().map(|i| i.url);
154 let (metadata_res, pictrs_thumbnail) = if let Some(url) = &page.url {
155 fetch_site_data(context.client(), &context.settings(), Some(url)).await
157 (None, thumbnail_url)
159 let (embed_title, embed_description, embed_html) = metadata_res
160 .map(|u| (u.title, u.description, u.html))
161 .unwrap_or((None, None, None));
163 let body_slurs_removed = page
166 .map(|s| remove_slurs(&s.content, &context.settings().slur_regex()));
167 let form = PostForm {
168 name: page.name.clone(),
169 url: page.url.clone().map(|u| u.into()),
170 body: body_slurs_removed,
171 creator_id: creator.id,
172 community_id: community.id,
174 locked: page.comments_enabled.map(|e| !e),
175 published: page.published.map(|u| u.naive_local()),
176 updated: page.updated.map(|u| u.naive_local()),
178 nsfw: page.sensitive,
179 stickied: page.stickied,
183 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
187 let post = blocking(context.pool(), move |conn| Post::upsert(conn, &form)).await??;
195 use crate::objects::{
196 community::ApubCommunity,
199 tests::{file_to_json_object, init_context},
201 use assert_json_diff::assert_json_include;
202 use serial_test::serial;
206 async fn test_parse_lemmy_post() {
207 let context = init_context();
208 let url = Url::parse("https://enterprise.lemmy.ml/post/55143").unwrap();
209 let community_json = file_to_json_object("assets/lemmy/objects/group.json");
210 let community = ApubCommunity::from_apub(&community_json, &context, &url, &mut 0)
213 let person_json = file_to_json_object("assets/lemmy/objects/person.json");
214 let person = ApubPerson::from_apub(&person_json, &context, &url, &mut 0)
217 let json = file_to_json_object("assets/lemmy/objects/page.json");
218 let mut request_counter = 0;
219 let post = ApubPost::from_apub(&json, &context, &url, &mut request_counter)
223 assert_eq!(post.ap_id.clone().into_inner(), url);
224 assert_eq!(post.name, "Post title");
225 assert!(post.body.is_some());
226 assert_eq!(post.body.as_ref().unwrap().len(), 45);
227 assert!(!post.locked);
228 assert!(post.stickied);
229 assert_eq!(request_counter, 0);
231 let to_apub = post.to_apub(&context).await.unwrap();
232 assert_json_include!(actual: json, expected: to_apub);
234 Post::delete(&*context.pool().get().unwrap(), post.id).unwrap();
235 Person::delete(&*context.pool().get().unwrap(), person.id).unwrap();
236 Community::delete(&*context.pool().get().unwrap(), community.id).unwrap();