QueryDsl,
};
use diesel_async::{AsyncPgConnection, RunQueryDsl};
-use lemmy_utils::error::LemmyError;
+use lemmy_utils::error::{LemmyError, LemmyErrorType};
use tokio::sync::OnceCell;
+pub const UNDETERMINED_ID: LanguageId = LanguageId(0);
+
impl LocalUserLanguage {
pub async fn read(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
for_local_user_id: LocalUserId,
) -> Result<Vec<LanguageId>, Error> {
use crate::schema::local_user_language::dsl::{
///
/// If no language_id vector is given, it will show all languages
pub async fn update(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
language_ids: Vec<LanguageId>,
for_local_user_id: LocalUserId,
) -> Result<(), Error> {
let conn = &mut get_conn(pool).await?;
+ let mut lang_ids = convert_update_languages(conn, language_ids).await?;
+
+ // No need to update if languages are unchanged
+ let current = LocalUserLanguage::read(&mut conn.into(), for_local_user_id).await?;
+ if current == lang_ids {
+ return Ok(());
+ }
+
+ // TODO: Force enable undetermined language for all users. This is necessary because many posts
+ // don't have a language tag (e.g. those from other federated platforms), so Lemmy users
+ // won't see them if undetermined language is disabled.
+ // This hack can be removed once a majority of posts have language tags, or when it is
+ // clearer for new users that they need to enable undetermined language.
+ // See https://github.com/LemmyNet/lemmy-ui/issues/999
+ if !lang_ids.contains(&UNDETERMINED_ID) {
+ lang_ids.push(UNDETERMINED_ID);
+ }
conn
.build_transaction()
.execute(conn)
.await?;
- let lang_ids = convert_update_languages(conn, language_ids).await?;
for l in lang_ids {
let form = LocalUserLanguageForm {
local_user_id: for_local_user_id,
}
impl SiteLanguage {
- pub async fn read_local(pool: &DbPool) -> Result<Vec<LanguageId>, Error> {
+ pub async fn read_local_raw(pool: &mut DbPool<'_>) -> Result<Vec<LanguageId>, Error> {
let conn = &mut get_conn(pool).await?;
site::table
.inner_join(local_site::table)
.await
}
- pub async fn read(pool: &DbPool, for_site_id: SiteId) -> Result<Vec<LanguageId>, Error> {
+ pub async fn read(pool: &mut DbPool<'_>, for_site_id: SiteId) -> Result<Vec<LanguageId>, Error> {
let conn = &mut get_conn(pool).await?;
-
let langs = site_language::table
.filter(site_language::site_id.eq(for_site_id))
.order(site_language::language_id)
.select(site_language::language_id)
.load(conn)
.await?;
+
convert_read_languages(conn, langs).await
}
pub async fn update(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
language_ids: Vec<LanguageId>,
site: &Site,
) -> Result<(), Error> {
let conn = &mut get_conn(pool).await?;
let for_site_id = site.id;
let instance_id = site.instance_id;
+ let lang_ids = convert_update_languages(conn, language_ids).await?;
+
+ // No need to update if languages are unchanged
+ let current = SiteLanguage::read(&mut conn.into(), site.id).await?;
+ if current == lang_ids {
+ return Ok(());
+ }
conn
.build_transaction()
.execute(conn)
.await?;
- let lang_ids = convert_update_languages(conn, language_ids).await?;
for l in lang_ids {
let form = SiteLanguageForm {
site_id: for_site_id,
impl CommunityLanguage {
/// Returns true if the given language is one of configured languages for given community
pub async fn is_allowed_community_language(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
for_language_id: Option<LanguageId>,
for_community_id: CommunityId,
) -> Result<(), LemmyError> {
if is_allowed {
Ok(())
} else {
- Err(LemmyError::from_message("language_not_allowed"))
+ Err(LemmyErrorType::LanguageNotAllowed)?
}
} else {
Ok(())
}
pub async fn read(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
for_community_id: CommunityId,
) -> Result<Vec<LanguageId>, Error> {
use crate::schema::community_language::dsl::{community_id, community_language, language_id};
let conn = &mut get_conn(pool).await?;
-
let langs = community_language
.filter(community_id.eq(for_community_id))
.order(language_id)
}
pub async fn update(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
mut language_ids: Vec<LanguageId>,
for_community_id: CommunityId,
) -> Result<(), Error> {
+ if language_ids.is_empty() {
+ language_ids = SiteLanguage::read_local_raw(pool).await?;
+ }
let conn = &mut get_conn(pool).await?;
+ let lang_ids = convert_update_languages(conn, language_ids).await?;
- if language_ids.is_empty() {
- language_ids = SiteLanguage::read_local(pool).await?;
+ // No need to update if languages are unchanged
+ let current = CommunityLanguage::read(&mut conn.into(), for_community_id).await?;
+ if current == lang_ids {
+ return Ok(());
}
conn
.execute(conn)
.await?;
- for l in language_ids {
+ for l in lang_ids {
let form = CommunityLanguageForm {
community_id: for_community_id,
language_id: l,
}
pub async fn default_post_language(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
community_id: CommunityId,
local_user_id: LocalUserId,
) -> Result<Option<LanguageId>, Error> {
use crate::schema::{community_language::dsl as cl, local_user_language::dsl as ul};
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)))
.filter(ul::local_user_id.eq(local_user_id))
.filter(cl::community_id.eq(community_id))
.get_results::<LanguageId>(conn)
.await?;
- if let Some(i) = intersection.get(0) {
- Ok(Some(*i))
+ if intersection.len() == 1 {
+ Ok(intersection.pop())
+ } else if intersection.len() == 2 && intersection.contains(&UNDETERMINED_ID) {
+ intersection.retain(|i| i != &UNDETERMINED_ID);
+ Ok(intersection.pop())
} else {
Ok(None)
}
) -> Result<Vec<LanguageId>, Error> {
if language_ids.is_empty() {
Ok(
- Language::read_all_conn(conn)
+ Language::read_all(&mut conn.into())
.await?
.into_iter()
.map(|l| l.id)
#[cfg(test)]
mod tests {
+ use super::*;
use crate::{
impls::actor_language::{
convert_read_languages,
};
use serial_test::serial;
- async fn test_langs1(pool: &DbPool) -> Vec<LanguageId> {
+ async fn test_langs1(pool: &mut DbPool<'_>) -> Vec<LanguageId> {
vec![
Language::read_id_from_code(pool, Some("en"))
.await
.unwrap(),
]
}
- async fn test_langs2(pool: &DbPool) -> Vec<LanguageId> {
+ async fn test_langs2(pool: &mut DbPool<'_>) -> Vec<LanguageId> {
vec![
Language::read_id_from_code(pool, Some("fi"))
.await
]
}
- async fn create_test_site(pool: &DbPool) -> (Site, Instance) {
- let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
+ async fn create_test_site(pool: &mut DbPool<'_>) -> (Site, Instance) {
+ let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
+ .await
+ .unwrap();
let site_form = SiteInsertForm::builder()
.name("test site".to_string())
#[serial]
async fn test_convert_update_languages() {
let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
// call with empty vec, returns all languages
let conn = &mut get_conn(pool).await.unwrap();
assert_eq!(184, converted1.len());
// call with nonempty vec, returns same vec
- let test_langs = test_langs1(pool).await;
+ let test_langs = test_langs1(&mut conn.into()).await;
let converted2 = convert_update_languages(conn, test_langs.clone())
.await
.unwrap();
async fn test_convert_read_languages() {
use crate::schema::language::dsl::{id, language};
let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
// call with all languages, returns empty vec
let conn = &mut get_conn(pool).await.unwrap();
assert_eq!(0, converted1.len());
// call with nonempty vec, returns same vec
- let test_langs = test_langs1(pool).await;
+ let test_langs = test_langs1(&mut conn.into()).await;
let converted2 = convert_read_languages(conn, test_langs.clone())
.await
.unwrap();
#[serial]
async fn test_site_languages() {
let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
let (site, instance) = create_test_site(pool).await;
- let site_languages1 = SiteLanguage::read_local(pool).await.unwrap();
+ let site_languages1 = SiteLanguage::read_local_raw(pool).await.unwrap();
// site is created with all languages
assert_eq!(184, site_languages1.len());
.await
.unwrap();
- let site_languages2 = SiteLanguage::read_local(pool).await.unwrap();
+ let site_languages2 = SiteLanguage::read_local_raw(pool).await.unwrap();
// after update, site only has new languages
assert_eq!(test_langs, site_languages2);
#[serial]
async fn test_user_languages() {
let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
let (site, instance) = create_test_site(pool).await;
- let test_langs = test_langs1(pool).await;
+ let mut test_langs = test_langs1(pool).await;
SiteLanguage::update(pool, test_langs.clone(), &site)
.await
.unwrap();
let local_user = LocalUser::create(pool, &local_user_form).await.unwrap();
let local_user_langs1 = LocalUserLanguage::read(pool, local_user.id).await.unwrap();
- // new user should be initialized with site languages
+ // new user should be initialized with site languages and undetermined
+ //test_langs.push(UNDETERMINED_ID);
+ //test_langs.sort();
+ test_langs.insert(0, UNDETERMINED_ID);
assert_eq!(test_langs, local_user_langs1);
// update user languages
.await
.unwrap();
let local_user_langs2 = LocalUserLanguage::read(pool, local_user.id).await.unwrap();
- assert_eq!(2, local_user_langs2.len());
+ assert_eq!(3, local_user_langs2.len());
Person::delete(pool, person.id).await.unwrap();
LocalUser::delete(pool, local_user.id).await.unwrap();
#[serial]
async fn test_community_languages() {
let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
let (site, instance) = create_test_site(pool).await;
let test_langs = test_langs1(pool).await;
SiteLanguage::update(pool, test_langs.clone(), &site)
assert_eq!(test_langs, read_site_langs);
// Test the local ones are the same
- let read_local_site_langs = SiteLanguage::read_local(pool).await.unwrap();
+ let read_local_site_langs = SiteLanguage::read_local_raw(pool).await.unwrap();
assert_eq!(test_langs, read_local_site_langs);
let community_form = CommunityInsertForm::builder()
#[serial]
async fn test_default_post_language() {
let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
let (site, instance) = create_test_site(pool).await;
let test_langs = test_langs1(pool).await;
let test_langs2 = test_langs2(pool).await;
.await
.unwrap();
- // no overlap in user/community languages, so no default language for post
+ // no overlap in user/community languages, so defaults to undetermined
let def1 = default_post_language(pool, community.id, local_user.id)
.await
.unwrap();
.await
.unwrap()
.unwrap(),
+ UNDETERMINED_ID,
];
LocalUserLanguage::update(pool, test_langs3, local_user.id)
.await