2 activities::{extract_community, verify_is_public, verify_person_in_community},
3 context::lemmy_context,
4 fetcher::object_id::ObjectId,
5 objects::{person::ApubPerson, tombstone::Tombstone, ImageObject, Source},
9 object::kind::{ImageType, PageType},
10 primitives::OneOrMany,
14 use chrono::{DateTime, FixedOffset, NaiveDateTime};
15 use lemmy_api_common::blocking;
17 traits::{ActorType, ApubObject},
18 values::{MediaTypeHtml, MediaTypeMarkdown},
19 verify::verify_domains_match,
21 use lemmy_db_schema::{
26 post::{Post, PostForm},
31 request::fetch_site_data,
32 utils::{check_slurs, convert_datetime, markdown_to_html, remove_slurs},
35 use lemmy_websocket::LemmyContext;
36 use serde::{Deserialize, Serialize};
37 use serde_with::skip_serializing_none;
41 #[skip_serializing_none]
42 #[derive(Clone, Debug, Deserialize, Serialize)]
43 #[serde(rename_all = "camelCase")]
45 #[serde(rename = "@context")]
46 context: OneOrMany<AnyBase>,
49 pub(crate) attributed_to: ObjectId<ApubPerson>,
52 content: Option<String>,
53 media_type: Option<MediaTypeHtml>,
54 source: Option<Source>,
56 image: Option<ImageObject>,
57 pub(crate) comments_enabled: Option<bool>,
58 sensitive: Option<bool>,
59 pub(crate) stickied: Option<bool>,
60 published: Option<DateTime<FixedOffset>>,
61 updated: Option<DateTime<FixedOffset>>,
67 pub(crate) fn id_unchecked(&self) -> &Url {
70 pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
71 verify_domains_match(&self.id, expected_domain)?;
75 /// Only mods can change the post's stickied/locked status. So if either of these is changed from
76 /// the current value, it is a mod action and needs to be verified as such.
78 /// Both stickied and locked need to be false on a newly created post (verified in [[CreatePost]].
79 pub(crate) async fn is_mod_action(&self, context: &LemmyContext) -> Result<bool, LemmyError> {
80 let old_post = ObjectId::<ApubPost>::new(self.id.clone())
81 .dereference_local(context)
84 let is_mod_action = if let Ok(old_post) = old_post {
85 self.stickied != Some(old_post.stickied) || self.comments_enabled != Some(!old_post.locked)
92 pub(crate) async fn verify(
94 context: &LemmyContext,
95 request_counter: &mut i32,
96 ) -> Result<(), LemmyError> {
97 let community = extract_community(&self.to, context, request_counter).await?;
99 check_slurs(&self.name, &context.settings().slur_regex())?;
100 verify_domains_match(self.attributed_to.inner(), &self.id.clone())?;
101 verify_person_in_community(
103 &ObjectId::new(community.actor_id()),
108 verify_is_public(&self.to.clone())?;
113 #[derive(Clone, Debug)]
114 pub struct ApubPost(Post);
116 impl Deref for ApubPost {
118 fn deref(&self) -> &Self::Target {
123 impl From<Post> for ApubPost {
124 fn from(p: Post) -> Self {
129 #[async_trait::async_trait(?Send)]
130 impl ApubObject for ApubPost {
131 type DataType = LemmyContext;
132 type ApubType = Page;
133 type TombstoneType = Tombstone;
135 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
139 async fn read_from_apub_id(
141 context: &LemmyContext,
142 ) -> Result<Option<Self>, LemmyError> {
144 blocking(context.pool(), move |conn| {
145 Post::read_from_apub_id(conn, object_id)
152 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
153 blocking(context.pool(), move |conn| {
154 Post::update_deleted(conn, self.id, true)
160 // Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
161 async fn to_apub(&self, context: &LemmyContext) -> Result<Page, LemmyError> {
162 let creator_id = self.creator_id;
163 let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
164 let community_id = self.community_id;
165 let community = blocking(context.pool(), move |conn| {
166 Community::read(conn, community_id)
170 let source = self.body.clone().map(|body| Source {
172 media_type: MediaTypeMarkdown::Markdown,
174 let image = self.thumbnail_url.clone().map(|thumb| ImageObject {
175 kind: ImageType::Image,
180 context: lemmy_context(),
181 r#type: PageType::Page,
182 id: self.ap_id.clone().into(),
183 attributed_to: ObjectId::new(creator.actor_id),
184 to: vec![community.actor_id.into(), public()],
185 name: self.name.clone(),
186 content: self.body.as_ref().map(|b| markdown_to_html(b)),
187 media_type: Some(MediaTypeHtml::Html),
189 url: self.url.clone().map(|u| u.into()),
191 comments_enabled: Some(!self.locked),
192 sensitive: Some(self.nsfw),
193 stickied: Some(self.stickied),
194 published: Some(convert_datetime(self.published)),
195 updated: self.updated.map(convert_datetime),
196 unparsed: Default::default(),
201 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
204 self.updated.unwrap_or(self.published),
210 context: &LemmyContext,
211 expected_domain: &Url,
212 request_counter: &mut i32,
213 ) -> Result<ApubPost, LemmyError> {
214 // We can't verify the domain in case of mod action, because the mod may be on a different
215 // instance from the post author.
216 let ap_id = if page.is_mod_action(context).await? {
219 page.id(expected_domain)?
221 let ap_id = Some(ap_id.clone().into());
224 .dereference(context, request_counter)
226 let community = extract_community(&page.to, context, request_counter).await?;
228 let thumbnail_url: Option<Url> = page.image.clone().map(|i| i.url);
229 let (metadata_res, pictrs_thumbnail) = if let Some(url) = &page.url {
230 fetch_site_data(context.client(), &context.settings(), Some(url)).await
232 (None, thumbnail_url)
234 let (embed_title, embed_description, embed_html) = metadata_res
235 .map(|u| (u.title, u.description, u.html))
236 .unwrap_or((None, None, None));
238 let body_slurs_removed = page
241 .map(|s| remove_slurs(&s.content, &context.settings().slur_regex()));
242 let form = PostForm {
243 name: page.name.clone(),
244 url: page.url.clone().map(|u| u.into()),
245 body: body_slurs_removed,
246 creator_id: creator.id,
247 community_id: community.id,
249 locked: page.comments_enabled.map(|e| !e),
250 published: page.published.map(|u| u.naive_local()),
251 updated: page.updated.map(|u| u.naive_local()),
253 nsfw: page.sensitive,
254 stickied: page.stickied,
258 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
262 let post = blocking(context.pool(), move |conn| Post::upsert(conn, &form)).await??;
270 use crate::objects::{
271 community::ApubCommunity,
272 tests::{file_to_json_object, init_context},
274 use assert_json_diff::assert_json_include;
275 use serial_test::serial;
279 async fn test_parse_lemmy_post() {
280 let context = init_context();
281 let url = Url::parse("https://enterprise.lemmy.ml/post/55143").unwrap();
282 let community_json = file_to_json_object("assets/lemmy-community.json");
283 let community = ApubCommunity::from_apub(&community_json, &context, &url, &mut 0)
286 let person_json = file_to_json_object("assets/lemmy-person.json");
287 let person = ApubPerson::from_apub(&person_json, &context, &url, &mut 0)
290 let json = file_to_json_object("assets/lemmy-post.json");
291 let mut request_counter = 0;
292 let post = ApubPost::from_apub(&json, &context, &url, &mut request_counter)
296 assert_eq!(post.ap_id.clone().into_inner(), url);
297 assert_eq!(post.name, "Post title");
298 assert!(post.body.is_some());
299 assert_eq!(post.body.as_ref().unwrap().len(), 45);
300 assert!(!post.locked);
301 assert!(post.stickied);
302 assert_eq!(request_counter, 0);
304 let to_apub = post.to_apub(&context).await.unwrap();
305 assert_json_include!(actual: json, expected: to_apub);
307 Post::delete(&*context.pool().get().unwrap(), post.id).unwrap();
308 Person::delete(&*context.pool().get().unwrap(), person.id).unwrap();
309 Community::delete(&*context.pool().get().unwrap(), community.id).unwrap();