2 activities::{verify_is_public, verify_person_in_community},
3 check_is_apub_id_valid,
4 mentions::collect_non_local_mentions,
7 note::{Note, SourceCompat},
14 use activitystreams_kinds::{object::NoteType, public};
15 use chrono::NaiveDateTime;
16 use html2md::parse_html;
17 use lemmy_api_common::blocking;
21 values::MediaTypeHtml,
22 verify::verify_domains_match,
24 use lemmy_db_schema::{
26 comment::{Comment, CommentForm},
34 utils::{convert_datetime, markdown_to_html, remove_slurs},
37 use lemmy_websocket::LemmyContext;
41 #[derive(Clone, Debug)]
42 pub struct ApubComment(Comment);
44 impl Deref for ApubComment {
45 type Target = Comment;
46 fn deref(&self) -> &Self::Target {
51 impl From<Comment> for ApubComment {
52 fn from(c: Comment) -> Self {
57 #[async_trait::async_trait(?Send)]
58 impl ApubObject for ApubComment {
59 type DataType = LemmyContext;
61 type TombstoneType = Tombstone;
63 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
67 #[tracing::instrument(skip_all)]
68 async fn read_from_apub_id(
70 context: &LemmyContext,
71 ) -> Result<Option<Self>, LemmyError> {
73 blocking(context.pool(), move |conn| {
74 Comment::read_from_apub_id(conn, object_id)
81 #[tracing::instrument(skip_all)]
82 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
84 blocking(context.pool(), move |conn| {
85 Comment::update_deleted(conn, self.id, true)
92 #[tracing::instrument(skip_all)]
93 async fn into_apub(self, context: &LemmyContext) -> Result<Note, LemmyError> {
94 let creator_id = self.creator_id;
95 let creator = blocking(context.pool(), move |conn| Person::read(conn, creator_id)).await??;
97 let post_id = self.post_id;
98 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
99 let community_id = post.community_id;
100 let community = blocking(context.pool(), move |conn| {
101 Community::read(conn, community_id)
105 let in_reply_to = if let Some(comment_id) = self.parent_id {
107 blocking(context.pool(), move |conn| Comment::read(conn, comment_id)).await??;
108 ObjectId::<PostOrComment>::new(parent_comment.ap_id)
110 ObjectId::<PostOrComment>::new(post.ap_id)
113 collect_non_local_mentions(&self, ObjectId::new(community.actor_id), context, &mut 0).await?;
116 r#type: NoteType::Note,
117 id: ObjectId::new(self.ap_id.clone()),
118 attributed_to: ObjectId::new(creator.actor_id),
121 content: markdown_to_html(&self.content),
122 media_type: Some(MediaTypeHtml::Html),
123 source: SourceCompat::Lemmy(Source::new(self.content.clone())),
125 published: Some(convert_datetime(self.published)),
126 updated: self.updated.map(convert_datetime),
133 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
134 Ok(Tombstone::new(self.ap_id.clone().into()))
137 #[tracing::instrument(skip_all)]
140 expected_domain: &Url,
141 context: &LemmyContext,
142 request_counter: &mut i32,
143 ) -> Result<(), LemmyError> {
144 verify_domains_match(note.id.inner(), expected_domain)?;
145 verify_domains_match(note.attributed_to.inner(), note.id.inner())?;
146 verify_is_public(¬e.to, ¬e.cc)?;
147 let (post, _) = note.get_parents(context, request_counter).await?;
148 let community_id = post.community_id;
149 let community = blocking(context.pool(), move |conn| {
150 Community::read(conn, community_id)
153 check_is_apub_id_valid(note.id.inner(), community.local, &context.settings())?;
154 verify_person_in_community(
162 return Err(LemmyError::from_message("Post is locked"));
167 /// Converts a `Note` to `Comment`.
169 /// If the parent community, post and comment(s) are not known locally, these are also fetched.
170 #[tracing::instrument(skip_all)]
173 context: &LemmyContext,
174 request_counter: &mut i32,
175 ) -> Result<ApubComment, LemmyError> {
178 .dereference(context, context.client(), request_counter)
180 let (post, parent_comment_id) = note.get_parents(context, request_counter).await?;
182 let content = if let SourceCompat::Lemmy(source) = ¬e.source {
183 source.content.clone()
185 parse_html(¬e.content)
187 let content_slurs_removed = remove_slurs(&content, &context.settings().slur_regex());
189 let form = CommentForm {
190 creator_id: creator.id,
192 parent_id: parent_comment_id,
193 content: content_slurs_removed,
196 published: note.published.map(|u| u.naive_local()),
197 updated: note.updated.map(|u| u.naive_local()),
199 ap_id: Some(note.id.into()),
202 let comment = blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??;
208 pub(crate) mod tests {
212 community::{tests::parse_lemmy_community, ApubCommunity},
214 person::{tests::parse_lemmy_person, ApubPerson},
218 protocol::tests::file_to_json_object,
220 use assert_json_diff::assert_json_include;
221 use lemmy_db_schema::source::site::Site;
222 use serial_test::serial;
224 async fn prepare_comment_test(
226 context: &LemmyContext,
227 ) -> (ApubPerson, ApubCommunity, ApubPost, ApubSite) {
228 let (person, site) = parse_lemmy_person(context).await;
229 let community = parse_lemmy_community(context).await;
230 let post_json = file_to_json_object("assets/lemmy/objects/page.json").unwrap();
231 ApubPost::verify(&post_json, url, context, &mut 0)
234 let post = ApubPost::from_apub(post_json, context, &mut 0)
237 (person, community, post, site)
240 fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost, ApubSite), context: &LemmyContext) {
241 Post::delete(&*context.pool().get().unwrap(), data.2.id).unwrap();
242 Community::delete(&*context.pool().get().unwrap(), data.1.id).unwrap();
243 Person::delete(&*context.pool().get().unwrap(), data.0.id).unwrap();
244 Site::delete(&*context.pool().get().unwrap(), data.3.id).unwrap();
249 pub(crate) async fn test_parse_lemmy_comment() {
250 let context = init_context();
251 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
252 let data = prepare_comment_test(&url, &context).await;
254 let json: Note = file_to_json_object("assets/lemmy/objects/note.json").unwrap();
255 let mut request_counter = 0;
256 ApubComment::verify(&json, &url, &context, &mut request_counter)
259 let comment = ApubComment::from_apub(json.clone(), &context, &mut request_counter)
263 assert_eq!(comment.ap_id, url.into());
264 assert_eq!(comment.content.len(), 14);
265 assert!(!comment.local);
266 assert_eq!(request_counter, 0);
268 let comment_id = comment.id;
269 let to_apub = comment.into_apub(&context).await.unwrap();
270 assert_json_include!(actual: json, expected: to_apub);
272 Comment::delete(&*context.pool().get().unwrap(), comment_id).unwrap();
273 cleanup(data, &context);
278 async fn test_parse_pleroma_comment() {
279 let context = init_context();
280 let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
281 let data = prepare_comment_test(&url, &context).await;
284 Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
286 let person_json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
287 ApubPerson::verify(&person_json, &pleroma_url, &context, &mut 0)
290 ApubPerson::from_apub(person_json, &context, &mut 0)
293 let json = file_to_json_object("assets/pleroma/objects/note.json").unwrap();
294 let mut request_counter = 0;
295 ApubComment::verify(&json, &pleroma_url, &context, &mut request_counter)
298 let comment = ApubComment::from_apub(json, &context, &mut request_counter)
302 assert_eq!(comment.ap_id, pleroma_url.into());
303 assert_eq!(comment.content.len(), 64);
304 assert!(!comment.local);
305 assert_eq!(request_counter, 0);
307 Comment::delete(&*context.pool().get().unwrap(), comment.id).unwrap();
308 cleanup(data, &context);
313 async fn test_html_to_markdown_sanitize() {
314 let parsed = parse_html("<script></script><b>hello</b>");
315 assert_eq!(parsed, "**hello**");