]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/objects/comment.rs
First pass at adding comment trees. (#2362)
[lemmy.git] / crates / apub / src / objects / comment.rs
index c987ff8bdbfcd43d05fc80a440f3557c4985eaec..88b08746c15ea6507ccbd7067f38118900364328 100644 (file)
@@ -1,23 +1,21 @@
 use crate::{
   activities::{verify_is_public, verify_person_in_community},
-  check_is_apub_id_valid,
+  check_apub_id_valid_with_strictness,
+  local_instance,
   mentions::collect_non_local_mentions,
   objects::{read_from_string_or_source, verify_is_remote_object},
-  protocol::{
-    objects::{note::Note, tombstone::Tombstone},
-    Source,
-  },
+  protocol::{objects::note::Note, Source},
   PostOrComment,
 };
+use activitypub_federation::{
+  core::object_id::ObjectId,
+  deser::values::MediaTypeMarkdownOrHtml,
+  traits::ApubObject,
+  utils::verify_domains_match,
+};
 use activitystreams_kinds::{object::NoteType, public};
 use chrono::NaiveDateTime;
 use lemmy_api_common::utils::blocking;
-use lemmy_apub_lib::{
-  object_id::ObjectId,
-  traits::ApubObject,
-  values::MediaTypeHtml,
-  verify::verify_domains_match,
-};
 use lemmy_db_schema::{
   source::{
     comment::{Comment, CommentForm},
@@ -28,8 +26,8 @@ use lemmy_db_schema::{
   traits::Crud,
 };
 use lemmy_utils::{
+  error::LemmyError,
   utils::{convert_datetime, markdown_to_html, remove_slurs},
-  LemmyError,
 };
 use lemmy_websocket::LemmyContext;
 use std::ops::Deref;
@@ -56,7 +54,7 @@ impl ApubObject for ApubComment {
   type DataType = LemmyContext;
   type ApubType = Note;
   type DbType = Comment;
-  type TombstoneType = Tombstone;
+  type Error = LemmyError;
 
   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
     None
@@ -100,7 +98,7 @@ impl ApubObject for ApubComment {
     })
     .await??;
 
-    let in_reply_to = if let Some(comment_id) = self.parent_id {
+    let in_reply_to = if let Some(comment_id) = self.parent_comment_id() {
       let parent_comment =
         blocking(context.pool(), move |conn| Comment::read(conn, comment_id)).await??;
       ObjectId::<PostOrComment>::new(parent_comment.ap_id)
@@ -117,7 +115,7 @@ impl ApubObject for ApubComment {
       to: vec![public()],
       cc: maa.ccs,
       content: markdown_to_html(&self.content),
-      media_type: Some(MediaTypeHtml::Html),
+      media_type: Some(MediaTypeMarkdownOrHtml::Html),
       source: Some(Source::new(self.content.clone())),
       in_reply_to,
       published: Some(convert_datetime(self.published)),
@@ -128,10 +126,6 @@ impl ApubObject for ApubComment {
     Ok(note)
   }
 
-  fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
-    Ok(Tombstone::new(self.ap_id.clone().into()))
-  }
-
   #[tracing::instrument(skip_all)]
   async fn verify(
     note: &Note,
@@ -148,8 +142,8 @@ impl ApubObject for ApubComment {
       Community::read(conn, community_id)
     })
     .await??;
-    check_is_apub_id_valid(note.id.inner(), community.local, &context.settings())?;
-    verify_is_remote_object(note.id.inner())?;
+    check_apub_id_valid_with_strictness(note.id.inner(), community.local, context.settings())?;
+    verify_is_remote_object(note.id.inner(), context.settings())?;
     verify_person_in_community(
       &note.attributed_to,
       &community.into(),
@@ -174,27 +168,29 @@ impl ApubObject for ApubComment {
   ) -> Result<ApubComment, LemmyError> {
     let creator = note
       .attributed_to
-      .dereference(context, context.client(), request_counter)
+      .dereference(context, local_instance(context), request_counter)
       .await?;
-    let (post, parent_comment_id) = note.get_parents(context, request_counter).await?;
+    let (post, parent_comment) = note.get_parents(context, request_counter).await?;
 
-    let content = read_from_string_or_source(&note.content, &note.source);
+    let content = read_from_string_or_source(&note.content, &note.media_type, &note.source);
     let content_slurs_removed = remove_slurs(&content, &context.settings().slur_regex());
 
     let form = CommentForm {
       creator_id: creator.id,
       post_id: post.id,
-      parent_id: parent_comment_id,
       content: content_slurs_removed,
       removed: None,
-      read: None,
       published: note.published.map(|u| u.naive_local()),
       updated: note.updated.map(|u| u.naive_local()),
       deleted: None,
       ap_id: Some(note.id.into()),
       local: Some(false),
     };
-    let comment = blocking(context.pool(), move |conn| Comment::upsert(conn, &form)).await??;
+    let parent_comment_path = parent_comment.map(|t| t.0.path);
+    let comment = blocking(context.pool(), move |conn| {
+      Comment::create(conn, &form, parent_comment_path.as_ref())
+    })
+    .await??;
     Ok(comment.into())
   }
 }