]> Untitled Git - lemmy.git/blobdiff - crates/api_crud/src/comment/create.rs
Moving settings to Database. (#2492)
[lemmy.git] / crates / api_crud / src / comment / create.rs
index 7ab0f20b6d0b0e96a904be242185cb52763428cd..ef13e54d9421b2224dd93f6da1da3dd9ec1c68f0 100644 (file)
@@ -9,6 +9,7 @@ use lemmy_api_common::{
     check_post_deleted_or_removed,
     get_local_user_view_from_jwt,
     get_post,
+    local_site_to_slur_regex,
   },
 };
 use lemmy_apub::{
@@ -19,9 +20,11 @@ use lemmy_apub::{
 };
 use lemmy_db_schema::{
   source::{
-    comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
-    comment_reply::CommentReply,
-    person_mention::PersonMention,
+    actor_language::CommunityLanguage,
+    comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
+    comment_reply::{CommentReply, CommentReplyUpdateForm},
+    local_site::LocalSite,
+    person_mention::{PersonMention, PersonMentionUpdateForm},
   },
   traits::{Crud, Likeable},
 };
@@ -49,9 +52,12 @@ impl PerformCrud for CreateComment {
     let data: &CreateComment = self;
     let local_user_view =
       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
+    let local_site = blocking(context.pool(), LocalSite::read).await??;
 
-    let content_slurs_removed =
-      remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
+    let content_slurs_removed = remove_slurs(
+      &data.content.to_owned(),
+      &local_site_to_slur_regex(&local_site),
+    );
 
     // Check for a community ban
     let post_id = data.post_id;
@@ -89,15 +95,19 @@ impl PerformCrud for CreateComment {
       .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,
-      post_id: data.post_id,
-      creator_id: local_user_view.person.id,
-      language_id,
-      ..CommentForm::default()
-    };
+    let language_id = data.language_id.unwrap_or(parent_language);
+
+    blocking(context.pool(), move |conn| {
+      CommunityLanguage::is_allowed_community_language(conn, Some(language_id), community_id)
+    })
+    .await??;
+
+    let comment_form = CommentInsertForm::builder()
+      .content(content_slurs_removed.to_owned())
+      .post_id(data.post_id)
+      .creator_id(local_user_view.person.id)
+      .language_id(Some(language_id))
+      .build();
 
     // Create the comment
     let comment_form2 = comment_form.clone();
@@ -119,14 +129,18 @@ impl PerformCrud for CreateComment {
           &inserted_comment_id.to_string(),
           &protocol_and_hostname,
         )?;
-        Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
+        Ok(Comment::update(
+          conn,
+          inserted_comment_id,
+          &CommentUpdateForm::builder().ap_id(Some(apub_id)).build(),
+        )?)
       })
       .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 mentions = scrape_text_for_mentions(&content_slurs_removed);
     let recipient_ids = send_local_notifs(
       mentions,
       &updated_comment,
@@ -169,7 +183,7 @@ impl PerformCrud for CreateComment {
       .await?;
       if let Ok(reply) = comment_reply {
         blocking(context.pool(), move |conn| {
-          CommentReply::update_read(conn, reply.id, true)
+          CommentReply::update(conn, reply.id, &CommentReplyUpdateForm { read: Some(true) })
         })
         .await?
         .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_replies"))?;
@@ -183,7 +197,11 @@ impl PerformCrud for CreateComment {
       .await?;
       if let Ok(mention) = person_mention {
         blocking(context.pool(), move |conn| {
-          PersonMention::update_read(conn, mention.id, true)
+          PersonMention::update(
+            conn,
+            mention.id,
+            &PersonMentionUpdateForm { read: Some(true) },
+          )
         })
         .await?
         .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_person_mentions"))?;