Skip to content

Commit

Permalink
Change logic for determining comment default language (fixes #3451) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Jul 21, 2023
1 parent dc6dd59 commit 1af94e1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/api_common/src/build_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub async fn build_post_response_deleted_allowed(
person_id: PersonId,
post_id: PostId,
) -> Result<PostResponse, LemmyError> {
let post_view = PostView::read(&mut context.pool(), post_id, Some(person_id), Some(true)).await?;
let post_view = PostView::read(context.pool(), post_id, Some(person_id), Some(true)).await?;
Ok(PostResponse { post_view })
}

Expand Down
20 changes: 11 additions & 9 deletions crates/api_crud/src/comment/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -81,25 +82,26 @@ 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(
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(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
Expand Down
12 changes: 10 additions & 2 deletions crates/api_crud/src/post/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,22 @@ impl PerformCrud for CreatePost {
.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(
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 => {
default_post_language(context.pool(), community_id, local_user_view.local_user.id).await?
}
};
CommunityLanguage::is_allowed_community_language(context.pool(), language_id, community_id)
.await?;

let post_form = PostInsertForm::builder()
.name(data.name.trim().to_owned())
Expand Down

0 comments on commit 1af94e1

Please sign in to comment.