mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-01-11 04:25:55 +00:00
Ignore undetermined language when calculating post default language (#2769)
This will make it much more likely to successfully get a default, as most users and communities have undetermined selected.
This commit is contained in:
parent
209c8a9185
commit
f0e1627824
4 changed files with 24 additions and 17 deletions
|
@ -1,4 +1,9 @@
|
||||||
use lemmy_db_schema::{newtypes::LanguageId, source::language::Language, utils::DbPool};
|
use lemmy_db_schema::{
|
||||||
|
impls::actor_language::UNDETERMINED_ID,
|
||||||
|
newtypes::LanguageId,
|
||||||
|
source::language::Language,
|
||||||
|
utils::DbPool,
|
||||||
|
};
|
||||||
use lemmy_utils::error::LemmyError;
|
use lemmy_utils::error::LemmyError;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
@ -33,7 +38,7 @@ impl LanguageTag {
|
||||||
let lang = Language::read_from_id(pool, lang).await?;
|
let lang = Language::read_from_id(pool, lang).await?;
|
||||||
|
|
||||||
// undetermined
|
// undetermined
|
||||||
if lang.code == "und" {
|
if lang.id == UNDETERMINED_ID {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
} else {
|
} else {
|
||||||
Ok(Some(LanguageTag {
|
Ok(Some(LanguageTag {
|
||||||
|
|
|
@ -29,6 +29,8 @@ use diesel_async::{AsyncPgConnection, RunQueryDsl};
|
||||||
use lemmy_utils::error::LemmyError;
|
use lemmy_utils::error::LemmyError;
|
||||||
use tokio::sync::OnceCell;
|
use tokio::sync::OnceCell;
|
||||||
|
|
||||||
|
pub const UNDETERMINED_ID: LanguageId = LanguageId(0);
|
||||||
|
|
||||||
impl LocalUserLanguage {
|
impl LocalUserLanguage {
|
||||||
pub async fn read(
|
pub async fn read(
|
||||||
pool: &DbPool,
|
pool: &DbPool,
|
||||||
|
@ -280,7 +282,7 @@ pub async fn default_post_language(
|
||||||
) -> Result<Option<LanguageId>, Error> {
|
) -> Result<Option<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 intersection = ul::local_user_language
|
let mut intersection = ul::local_user_language
|
||||||
.inner_join(cl::community_language.on(ul::language_id.eq(cl::language_id)))
|
.inner_join(cl::community_language.on(ul::language_id.eq(cl::language_id)))
|
||||||
.filter(ul::local_user_id.eq(local_user_id))
|
.filter(ul::local_user_id.eq(local_user_id))
|
||||||
.filter(cl::community_id.eq(community_id))
|
.filter(cl::community_id.eq(community_id))
|
||||||
|
@ -288,8 +290,11 @@ pub async fn default_post_language(
|
||||||
.get_results::<LanguageId>(conn)
|
.get_results::<LanguageId>(conn)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if let Some(i) = intersection.get(0) {
|
if intersection.len() == 1 {
|
||||||
Ok(Some(*i))
|
Ok(intersection.pop())
|
||||||
|
} else if intersection.len() == 2 && intersection.contains(&UNDETERMINED_ID) {
|
||||||
|
intersection.retain(|i| i != &UNDETERMINED_ID);
|
||||||
|
Ok(intersection.pop())
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
@ -340,6 +345,7 @@ async fn convert_read_languages(
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::*;
|
||||||
use crate::{
|
use crate::{
|
||||||
impls::actor_language::{
|
impls::actor_language::{
|
||||||
convert_read_languages,
|
convert_read_languages,
|
||||||
|
@ -583,7 +589,8 @@ mod tests {
|
||||||
async fn test_default_post_language() {
|
async fn test_default_post_language() {
|
||||||
let pool = &build_db_pool_for_tests().await;
|
let pool = &build_db_pool_for_tests().await;
|
||||||
let (site, instance) = create_test_site(pool).await;
|
let (site, instance) = create_test_site(pool).await;
|
||||||
let test_langs = test_langs1(pool).await;
|
let mut test_langs = test_langs1(pool).await;
|
||||||
|
test_langs.push(UNDETERMINED_ID);
|
||||||
let test_langs2 = test_langs2(pool).await;
|
let test_langs2 = test_langs2(pool).await;
|
||||||
|
|
||||||
let community_form = CommunityInsertForm::builder()
|
let community_form = CommunityInsertForm::builder()
|
||||||
|
@ -632,6 +639,7 @@ mod tests {
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
UNDETERMINED_ID,
|
||||||
];
|
];
|
||||||
LocalUserLanguage::update(pool, test_langs3, local_user.id)
|
LocalUserLanguage::update(pool, test_langs3, local_user.id)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -418,6 +418,7 @@ mod tests {
|
||||||
};
|
};
|
||||||
use lemmy_db_schema::{
|
use lemmy_db_schema::{
|
||||||
aggregates::structs::CommentAggregates,
|
aggregates::structs::CommentAggregates,
|
||||||
|
impls::actor_language::UNDETERMINED_ID,
|
||||||
newtypes::LanguageId,
|
newtypes::LanguageId,
|
||||||
source::{
|
source::{
|
||||||
actor_language::LocalUserLanguage,
|
actor_language::LocalUserLanguage,
|
||||||
|
@ -775,11 +776,7 @@ mod tests {
|
||||||
assert_eq!(finnish_id, finnish_comment[0].comment.language_id);
|
assert_eq!(finnish_id, finnish_comment[0].comment.language_id);
|
||||||
|
|
||||||
// now show all comments with undetermined language (which is the default value)
|
// now show all comments with undetermined language (which is the default value)
|
||||||
let undetermined_id = Language::read_id_from_code(pool, Some("und"))
|
LocalUserLanguage::update(pool, vec![UNDETERMINED_ID], data.inserted_local_user.id)
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.unwrap();
|
|
||||||
LocalUserLanguage::update(pool, vec![undetermined_id], data.inserted_local_user.id)
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let undetermined_comment = CommentQuery::builder()
|
let undetermined_comment = CommentQuery::builder()
|
||||||
|
|
|
@ -467,6 +467,7 @@ mod tests {
|
||||||
use crate::post_view::{PostQuery, PostView};
|
use crate::post_view::{PostQuery, PostView};
|
||||||
use lemmy_db_schema::{
|
use lemmy_db_schema::{
|
||||||
aggregates::structs::PostAggregates,
|
aggregates::structs::PostAggregates,
|
||||||
|
impls::actor_language::UNDETERMINED_ID,
|
||||||
newtypes::LanguageId,
|
newtypes::LanguageId,
|
||||||
source::{
|
source::{
|
||||||
actor_language::LocalUserLanguage,
|
actor_language::LocalUserLanguage,
|
||||||
|
@ -800,13 +801,9 @@ mod tests {
|
||||||
assert_eq!(1, post_listing_french.len());
|
assert_eq!(1, post_listing_french.len());
|
||||||
assert_eq!(french_id, post_listing_french[0].post.language_id);
|
assert_eq!(french_id, post_listing_french[0].post.language_id);
|
||||||
|
|
||||||
let undetermined_id = Language::read_id_from_code(pool, Some("und"))
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.unwrap();
|
|
||||||
LocalUserLanguage::update(
|
LocalUserLanguage::update(
|
||||||
pool,
|
pool,
|
||||||
vec![french_id, undetermined_id],
|
vec![french_id, UNDETERMINED_ID],
|
||||||
data.inserted_local_user.id,
|
data.inserted_local_user.id,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
@ -823,7 +820,7 @@ mod tests {
|
||||||
// french post and undetermined language post should be returned
|
// french post and undetermined language post should be returned
|
||||||
assert_eq!(2, post_listings_french_und.len());
|
assert_eq!(2, post_listings_french_und.len());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
undetermined_id,
|
UNDETERMINED_ID,
|
||||||
post_listings_french_und[0].post.language_id
|
post_listings_french_und[0].post.language_id
|
||||||
);
|
);
|
||||||
assert_eq!(french_id, post_listings_french_und[1].post.language_id);
|
assert_eq!(french_id, post_listings_french_und[1].post.language_id);
|
||||||
|
|
Loading…
Reference in a new issue