-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 serde::{Deserialize, Serialize};
use url::Url;
let lang = Language::read_from_id(pool, lang).await?;
// undetermined
- if lang.code == "und" {
+ if lang.id == UNDETERMINED_ID {
Ok(None)
} else {
Ok(Some(LanguageTag {
use lemmy_utils::error::LemmyError;
use tokio::sync::OnceCell;
+pub const UNDETERMINED_ID: LanguageId = LanguageId(0);
+
impl LocalUserLanguage {
pub async fn read(
pool: &DbPool,
) -> 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)
}
#[cfg(test)]
mod tests {
+ use super::*;
use crate::{
impls::actor_language::{
convert_read_languages,
async fn test_default_post_language() {
let pool = &build_db_pool_for_tests().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 community_form = CommunityInsertForm::builder()
.await
.unwrap()
.unwrap(),
+ UNDETERMINED_ID,
];
LocalUserLanguage::update(pool, test_langs3, local_user.id)
.await
};
use lemmy_db_schema::{
aggregates::structs::CommentAggregates,
+ impls::actor_language::UNDETERMINED_ID,
newtypes::LanguageId,
source::{
actor_language::LocalUserLanguage,
assert_eq!(finnish_id, finnish_comment[0].comment.language_id);
// now show all comments with undetermined language (which is the default value)
- let undetermined_id = Language::read_id_from_code(pool, Some("und"))
- .await
- .unwrap()
- .unwrap();
- LocalUserLanguage::update(pool, vec![undetermined_id], data.inserted_local_user.id)
+ LocalUserLanguage::update(pool, vec![UNDETERMINED_ID], data.inserted_local_user.id)
.await
.unwrap();
let undetermined_comment = CommentQuery::builder()
use crate::post_view::{PostQuery, PostView};
use lemmy_db_schema::{
aggregates::structs::PostAggregates,
+ impls::actor_language::UNDETERMINED_ID,
newtypes::LanguageId,
source::{
actor_language::LocalUserLanguage,
assert_eq!(1, post_listing_french.len());
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(
pool,
- vec![french_id, undetermined_id],
+ vec![french_id, UNDETERMINED_ID],
data.inserted_local_user.id,
)
.await
// french post and undetermined language post should be returned
assert_eq!(2, post_listings_french_und.len());
assert_eq!(
- undetermined_id,
+ UNDETERMINED_ID,
post_listings_french_und[0].post.language_id
);
assert_eq!(french_id, post_listings_french_und[1].post.language_id);