Change logic for determining comment default language (fixes #3451) (#3672)
authorNutomic <me@nutomic.com>
Fri, 21 Jul 2023 13:08:25 +0000 (15:08 +0200)
committerGitHub <noreply@github.com>
Fri, 21 Jul 2023 13:08:25 +0000 (09:08 -0400)
crates/api_crud/src/comment/create.rs
crates/api_crud/src/post/create.rs

index 92775772b36bd17dc3221be259a1b2b56e9e3510..098d1a66421886ac845e1f3a170239b6db70bf87 100644 (file)
@@ -16,6 +16,7 @@ use lemmy_api_common::{
   },
 };
 use lemmy_db_schema::{
+  impls::actor_language::default_post_language,
   source::{
     actor_language::CommunityLanguage,
     comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
@@ -82,25 +83,31 @@ impl PerformCrud for CreateComment {
       check_comment_depth(parent)?;
     }
 
-    // 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 = data.language_id.unwrap_or(parent_language);
-
     CommunityLanguage::is_allowed_community_language(
       &mut context.pool(),
-      Some(language_id),
+      data.language_id,
       community_id,
     )
     .await?;
 
+    // attempt to set default language if none was provided
+    let language_id = match data.language_id {
+      Some(lid) => Some(lid),
+      None => {
+        default_post_language(
+          &mut context.pool(),
+          community_id,
+          local_user_view.local_user.id,
+        )
+        .await?
+      }
+    };
+
     let comment_form = CommentInsertForm::builder()
       .content(content_slurs_removed.clone())
       .post_id(data.post_id)
       .creator_id(local_user_view.person.id)
-      .language_id(Some(language_id))
+      .language_id(language_id)
       .build();
 
     // Create the comment
index 16a6f0004389cebb54c8a4676de9abe72014b13e..458fdb24827d5e1d0711e63a3b7d72a367d2c653 100644 (file)
@@ -91,6 +91,16 @@ pub async fn create_post(
     .map(|u| (u.title, u.description, u.embed_video_url))
     .unwrap_or_default();
 
+  // Only need to check if language is allowed in case user set it explicitly. When using default
+  // language, it already only returns allowed languages.
+  CommunityLanguage::is_allowed_community_language(
+    &mut context.pool(),
+    data.language_id,
+    community_id,
+  )
+  .await?;
+
+  // attempt to set default language if none was provided
   let language_id = match data.language_id {
     Some(lid) => Some(lid),
     None => {
@@ -102,8 +112,6 @@ pub async fn create_post(
       .await?
     }
   };
-  CommunityLanguage::is_allowed_community_language(&mut context.pool(), language_id, community_id)
-    .await?;
 
   let post_form = PostInsertForm::builder()
     .name(data.name.trim().to_owned())