Always assign default language before checking if language is allowed

This commit is contained in:
Felix Ableitner 2024-10-22 14:49:04 +02:00
parent 859dfb3f81
commit 5a6685f5bb
5 changed files with 48 additions and 40 deletions

View file

@ -89,17 +89,13 @@ pub async fn create_comment(
} }
// attempt to set default language if none was provided // attempt to set default language if none was provided
let language_id = match data.language_id { let language_id = default_post_language(
Some(lid) => lid, &mut context.pool(),
None => { data.language_id,
default_post_language( community_id,
&mut context.pool(), local_user_view.local_user.id,
community_id, )
local_user_view.local_user.id, .await?;
)
.await?
}
};
CommunityLanguage::is_allowed_community_language(&mut context.pool(), language_id, community_id) CommunityLanguage::is_allowed_community_language(&mut context.pool(), language_id, community_id)
.await?; .await?;

View file

@ -55,14 +55,19 @@ pub async fn update_comment(
Err(LemmyErrorType::NoCommentEditAllowed)? Err(LemmyErrorType::NoCommentEditAllowed)?
} }
if let Some(language_id) = data.language_id { let language_id = default_post_language(
&mut context.pool(),
data.language_id,
community_id,
local_user_view.local_user.id,
)
.await?;
CommunityLanguage::is_allowed_community_language( CommunityLanguage::is_allowed_community_language(
&mut context.pool(), &mut context.pool(),
language_id, language_id,
orig_comment.community.id, orig_comment.community.id,
) )
.await?; .await?;
}
let slur_regex = local_site_to_slur_regex(&local_site); let slur_regex = local_site_to_slur_regex(&local_site);
let url_blocklist = get_url_blocklist(&context).await?; let url_blocklist = get_url_blocklist(&context).await?;

View file

@ -105,17 +105,13 @@ pub async fn create_post(
} }
// attempt to set default language if none was provided // attempt to set default language if none was provided
let language_id = match data.language_id { let language_id = default_post_language(
Some(lid) => lid, &mut context.pool(),
None => { data.language_id,
default_post_language( community_id,
&mut context.pool(), local_user_view.local_user.id,
community_id, )
local_user_view.local_user.id, .await?;
)
.await?
}
};
// Only need to check if language is allowed in case user set it explicitly. When using 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. // language, it already only returns allowed languages.

View file

@ -101,14 +101,19 @@ pub async fn update_post(
Err(LemmyErrorType::NoPostEditAllowed)? Err(LemmyErrorType::NoPostEditAllowed)?
} }
if let Some(language_id) = data.language_id { let language_id = default_post_language(
&mut context.pool(),
data.language_id,
community_id,
local_user_view.local_user.id,
)
.await?;
CommunityLanguage::is_allowed_community_language( CommunityLanguage::is_allowed_community_language(
&mut context.pool(), &mut context.pool(),
language_id, language_id,
orig_post.community_id, orig_post.community_id,
) )
.await?; .await?;
}
// handle changes to scheduled_publish_time // handle changes to scheduled_publish_time
let scheduled_publish_time = match ( let scheduled_publish_time = match (

View file

@ -321,26 +321,32 @@ impl CommunityLanguage {
pub async fn default_post_language( pub async fn default_post_language(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
language_id: Option<LanguageId>,
community_id: CommunityId, community_id: CommunityId,
local_user_id: LocalUserId, local_user_id: LocalUserId,
) -> Result<LanguageId, Error> { ) -> Result<LanguageId, Error> {
use crate::schema::{community_language::dsl as cl, local_user_language::dsl as ul}; use crate::schema::{community_language::dsl as cl, local_user_language::dsl as ul};
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
let mut intersection = ul::local_user_language match language_id {
.inner_join(cl::community_language.on(ul::language_id.eq(cl::language_id))) None | Some(LanguageId(0)) => {
.filter(ul::local_user_id.eq(local_user_id)) let mut intersection = ul::local_user_language
.filter(cl::community_id.eq(community_id)) .inner_join(cl::community_language.on(ul::language_id.eq(cl::language_id)))
.select(cl::language_id) .filter(ul::local_user_id.eq(local_user_id))
.get_results::<LanguageId>(conn) .filter(cl::community_id.eq(community_id))
.await?; .select(cl::language_id)
.get_results::<LanguageId>(conn)
.await?;
if intersection.len() == 1 { if intersection.len() == 1 {
Ok(intersection.pop().unwrap_or(UNDETERMINED_ID)) Ok(intersection.pop().unwrap_or(UNDETERMINED_ID))
} else if intersection.len() == 2 && intersection.contains(&UNDETERMINED_ID) { } else if intersection.len() == 2 && intersection.contains(&UNDETERMINED_ID) {
intersection.retain(|i| i != &UNDETERMINED_ID); intersection.retain(|i| i != &UNDETERMINED_ID);
Ok(intersection.pop().unwrap_or(UNDETERMINED_ID)) Ok(intersection.pop().unwrap_or(UNDETERMINED_ID))
} else { } else {
Ok(UNDETERMINED_ID) Ok(UNDETERMINED_ID)
}
}
Some(lid) => Ok(lid),
} }
} }