]> Untitled Git - lemmy.git/blobdiff - crates/api_crud/src/comment/create.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / api_crud / src / comment / create.rs
index 37ecb2f05dff4c0771a440f0f798b1b0b3b57c21..7ab0f20b6d0b0e96a904be242185cb52763428cd 100644 (file)
@@ -1,45 +1,46 @@
 use crate::PerformCrud;
 use actix_web::web::Data;
 use lemmy_api_common::{
-  blocking,
-  check_community_ban,
-  check_community_deleted_or_removed,
-  check_person_block,
-  check_post_deleted_or_removed,
-  comment::*,
-  get_local_user_view_from_jwt,
-  get_post,
-  send_local_notifs,
+  comment::{CommentResponse, CreateComment},
+  utils::{
+    blocking,
+    check_community_ban,
+    check_community_deleted_or_removed,
+    check_post_deleted_or_removed,
+    get_local_user_view_from_jwt,
+    get_post,
+  },
 };
 use lemmy_apub::{
-  activities::{
-    comment::create_or_update::CreateOrUpdateComment,
-    voting::vote::{Vote, VoteType},
-    CreateOrUpdateType,
-  },
-  fetcher::post_or_comment::PostOrComment,
-  generate_apub_endpoint,
+  generate_local_apub_endpoint,
+  objects::comment::ApubComment,
+  protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
   EndpointType,
 };
-use lemmy_db_queries::{
-  source::{comment::Comment_, person_mention::PersonMention_},
-  Crud,
-  Likeable,
+use lemmy_db_schema::{
+  source::{
+    comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
+    comment_reply::CommentReply,
+    person_mention::PersonMention,
+  },
+  traits::{Crud, Likeable},
 };
-use lemmy_db_schema::source::{comment::*, person_mention::PersonMention};
-use lemmy_db_views::comment_view::CommentView;
 use lemmy_utils::{
+  error::LemmyError,
   utils::{remove_slurs, scrape_text_for_mentions},
-  ApiError,
   ConnectionId,
-  LemmyError,
 };
-use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
+use lemmy_websocket::{
+  send::{send_comment_ws_message, send_local_notifs},
+  LemmyContext,
+  UserOperationCrud,
+};
 
 #[async_trait::async_trait(?Send)]
 impl PerformCrud for CreateComment {
   type Response = CommentResponse;
 
+  #[tracing::instrument(skip(context, websocket_id))]
   async fn perform(
     &self,
     context: &Data<LemmyContext>,
@@ -61,43 +62,51 @@ impl PerformCrud for CreateComment {
     check_community_deleted_or_removed(community_id, context.pool()).await?;
     check_post_deleted_or_removed(&post)?;
 
-    check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
-
     // Check if post is locked, no new comments
     if post.locked {
-      return Err(ApiError::err_plain("locked").into());
+      return Err(LemmyError::from_message("locked"));
     }
 
-    // If there's a parent_id, check to make sure that comment is in that post
-    if let Some(parent_id) = data.parent_id {
-      // Make sure the parent comment exists
-      let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
+    // Fetch the parent, if it exists
+    let parent_opt = if let Some(parent_id) = data.parent_id {
+      blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
         .await?
-        .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
-
-      check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
+        .ok()
+    } else {
+      None
+    };
 
-      // Strange issue where sometimes the post ID is incorrect
+    // If there's a parent_id, check to make sure that comment is in that post
+    // Strange issue where sometimes the post ID of the parent comment is incorrect
+    if let Some(parent) = parent_opt.as_ref() {
       if parent.post_id != post_id {
-        return Err(ApiError::err_plain("couldnt_create_comment").into());
+        return Err(LemmyError::from_message("couldnt_create_comment"));
       }
     }
 
+    // if no language is set, copy language from parent post/comment
+    let parent_language = parent_opt
+      .as_ref()
+      .map(|p| p.language_id)
+      .unwrap_or(post.language_id);
+    let language_id = Some(data.language_id.unwrap_or(parent_language));
+
     let comment_form = CommentForm {
       content: content_slurs_removed,
-      parent_id: data.parent_id.to_owned(),
       post_id: data.post_id,
       creator_id: local_user_view.person.id,
+      language_id,
       ..CommentForm::default()
     };
 
     // Create the comment
     let comment_form2 = comment_form.clone();
+    let parent_path = parent_opt.to_owned().map(|t| t.path);
     let inserted_comment = blocking(context.pool(), move |conn| {
-      Comment::create(conn, &comment_form2)
+      Comment::create(conn, &comment_form2, parent_path.as_ref())
     })
     .await?
-    .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?;
 
     // Necessary to update the ap_id
     let inserted_comment_id = inserted_comment.id;
@@ -105,7 +114,7 @@ impl PerformCrud for CreateComment {
 
     let updated_comment: Comment =
       blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
-        let apub_id = generate_apub_endpoint(
+        let apub_id = generate_local_apub_endpoint(
           EndpointType::Comment,
           &inserted_comment_id.to_string(),
           &protocol_and_hostname,
@@ -113,27 +122,18 @@ impl PerformCrud for CreateComment {
         Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
       })
       .await?
-      .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
-
-    CreateOrUpdateComment::send(
-      &updated_comment,
-      &local_user_view.person,
-      CreateOrUpdateType::Create,
-      context,
-    )
-    .await?;
+      .map_err(|e| e.with_message("couldnt_create_comment"))?;
 
     // Scan the comment for user mentions, add those rows
     let post_id = post.id;
     let mentions = scrape_text_for_mentions(&comment_form.content);
     let recipient_ids = send_local_notifs(
       mentions,
-      updated_comment.clone(),
-      local_user_view.person.clone(),
-      post,
-      context.pool(),
+      &updated_comment,
+      &local_user_view.person,
+      &post,
       true,
-      &context.settings(),
+      context,
     )
     .await?;
 
@@ -145,50 +145,36 @@ impl PerformCrud for CreateComment {
       score: 1,
     };
 
-    let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
+    let like = move |conn: &mut _| CommentLike::like(conn, &like_form);
     blocking(context.pool(), like)
       .await?
-      .map_err(|e| ApiError::err("couldnt_like_comment", e))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
 
-    let object = PostOrComment::Comment(updated_comment);
-    Vote::send(
-      &object,
-      &local_user_view.person,
-      community_id,
-      VoteType::Like,
+    let apub_comment: ApubComment = updated_comment.into();
+    CreateOrUpdateComment::send(
+      apub_comment.clone(),
+      &local_user_view.person.clone().into(),
+      CreateOrUpdateType::Create,
       context,
+      &mut 0,
     )
     .await?;
 
-    let person_id = local_user_view.person.id;
-    let comment_id = inserted_comment.id;
-    let comment_view = blocking(context.pool(), move |conn| {
-      CommentView::read(conn, comment_id, Some(person_id))
-    })
-    .await??;
-
-    // If its a comment to yourself, mark it as read
-    if local_user_view.person.id == comment_view.get_recipient_id() {
-      let comment_id = inserted_comment.id;
-      blocking(context.pool(), move |conn| {
-        Comment::update_read(conn, comment_id, true)
-      })
-      .await?
-      .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
-    }
     // If its a reply, mark the parent as read
-    if let Some(parent_id) = data.parent_id {
-      let parent_comment = blocking(context.pool(), move |conn| {
-        CommentView::read(conn, parent_id, Some(person_id))
+    if let Some(parent) = parent_opt {
+      let parent_id = parent.id;
+      let comment_reply = blocking(context.pool(), move |conn| {
+        CommentReply::read_by_comment(conn, parent_id)
       })
-      .await??;
-      if local_user_view.person.id == parent_comment.get_recipient_id() {
+      .await?;
+      if let Ok(reply) = comment_reply {
         blocking(context.pool(), move |conn| {
-          Comment::update_read(conn, parent_id, true)
+          CommentReply::update_read(conn, reply.id, true)
         })
         .await?
-        .map_err(|e| ApiError::err("couldnt_update_parent_comment", e))?;
+        .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_replies"))?;
       }
+
       // If the parent has PersonMentions mark them as read too
       let person_id = local_user_view.person.id;
       let person_mention = blocking(context.pool(), move |conn| {
@@ -200,7 +186,7 @@ impl PerformCrud for CreateComment {
           PersonMention::update_read(conn, mention.id, true)
         })
         .await?
-        .map_err(|e| ApiError::err("couldnt_update_person_mentions", e))?;
+        .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_person_mentions"))?;
       }
     }