Change logic for determining comment default language (fixes #3451) (#3672)

This commit is contained in:
Nutomic 2023-07-21 15:08:25 +02:00 committed by GitHub
parent ea7f83c4dc
commit 102124b6d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 11 deletions

View file

@ -16,6 +16,7 @@ use lemmy_api_common::{
}, },
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
impls::actor_language::default_post_language,
source::{ source::{
actor_language::CommunityLanguage, actor_language::CommunityLanguage,
comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm}, comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
@ -82,25 +83,31 @@ impl PerformCrud for CreateComment {
check_comment_depth(parent)?; 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( CommunityLanguage::is_allowed_community_language(
&mut context.pool(), &mut context.pool(),
Some(language_id), data.language_id,
community_id, community_id,
) )
.await?; .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() let comment_form = CommentInsertForm::builder()
.content(content_slurs_removed.clone()) .content(content_slurs_removed.clone())
.post_id(data.post_id) .post_id(data.post_id)
.creator_id(local_user_view.person.id) .creator_id(local_user_view.person.id)
.language_id(Some(language_id)) .language_id(language_id)
.build(); .build();
// Create the comment // Create the comment

View file

@ -91,6 +91,16 @@ pub async fn create_post(
.map(|u| (u.title, u.description, u.embed_video_url)) .map(|u| (u.title, u.description, u.embed_video_url))
.unwrap_or_default(); .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 { let language_id = match data.language_id {
Some(lid) => Some(lid), Some(lid) => Some(lid),
None => { None => {
@ -102,8 +112,6 @@ pub async fn create_post(
.await? .await?
} }
}; };
CommunityLanguage::is_allowed_community_language(&mut context.pool(), language_id, community_id)
.await?;
let post_form = PostInsertForm::builder() let post_form = PostInsertForm::builder()
.name(data.name.trim().to_owned()) .name(data.name.trim().to_owned())