Skip to content

Commit

Permalink
Return empty vec when reading all languages (fixes #2495)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic authored and dessalines committed Oct 14, 2022
1 parent 6c3e984 commit 77fb8ba
Showing 1 changed file with 57 additions and 15 deletions.
72 changes: 57 additions & 15 deletions crates/db_schema/src/impls/actor_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use diesel::{
RunQueryDsl,
};
use lemmy_utils::error::LemmyError;
use once_cell::sync::OnceCell;

impl LocalUserLanguage {
pub fn read(
Expand All @@ -23,10 +24,11 @@ impl LocalUserLanguage {
) -> Result<Vec<LanguageId>, Error> {
use crate::schema::local_user_language::dsl::*;

local_user_language
let langs = local_user_language
.filter(local_user_id.eq(for_local_user_id))
.select(language_id)
.get_results(conn)
.get_results(conn)?;
convert_read_languages(conn, langs)
}

/// Update the user's languages.
Expand All @@ -42,7 +44,7 @@ impl LocalUserLanguage {
// Clear the current user languages
delete(local_user_language.filter(local_user_id.eq(for_local_user_id))).execute(conn)?;

let lang_ids = update_languages(conn, language_ids)?;
let lang_ids = convert_update_languages(conn, language_ids)?;
for l in lang_ids {
let form = LocalUserLanguageForm {
local_user_id: for_local_user_id,
Expand Down Expand Up @@ -74,10 +76,11 @@ impl SiteLanguage {

pub fn read(conn: &mut PgConnection, for_site_id: SiteId) -> Result<Vec<LanguageId>, Error> {
use crate::schema::site_language::dsl::*;
site_language
let langs = site_language
.filter(site_id.eq(for_site_id))
.select(language_id)
.load(conn)
.load(conn)?;
convert_read_languages(conn, langs)
}

pub fn update(
Expand All @@ -90,7 +93,7 @@ impl SiteLanguage {
// Clear the current languages
delete(site_language.filter(site_id.eq(for_site_id))).execute(conn)?;

let lang_ids = update_languages(conn, language_ids)?;
let lang_ids = convert_update_languages(conn, language_ids)?;
for l in lang_ids {
let form = SiteLanguageForm {
site_id: for_site_id,
Expand Down Expand Up @@ -163,10 +166,11 @@ impl CommunityLanguage {
for_community_id: CommunityId,
) -> Result<Vec<LanguageId>, Error> {
use crate::schema::community_language::dsl::*;
community_language
let langs = community_language
.filter(community_id.eq(for_community_id))
.select(language_id)
.get_results(conn)
.get_results(conn)?;
convert_read_languages(conn, langs)
}

pub fn update(
Expand Down Expand Up @@ -216,8 +220,8 @@ pub fn default_post_language(
}
}

// If no language is given, set all languages
fn update_languages(
/// If no language is given, set all languages
fn convert_update_languages(
conn: &mut PgConnection,
language_ids: Vec<LanguageId>,
) -> Result<Vec<LanguageId>, Error> {
Expand All @@ -233,6 +237,28 @@ fn update_languages(
}
}

/// If all languages are returned, return empty vec instead
fn convert_read_languages(
conn: &mut PgConnection,
language_ids: Vec<LanguageId>,
) -> Result<Vec<LanguageId>, Error> {
static ALL_LANGUAGES_COUNT: OnceCell<usize> = OnceCell::new();
let count = ALL_LANGUAGES_COUNT.get_or_init(|| {
use crate::schema::language::dsl::*;
let count: i64 = language
.select(count(id))
.first(conn)
.expect("read number of languages");
count as usize
});

if &language_ids.len() == count {
Ok(vec![])
} else {
Ok(language_ids)
}
}

#[cfg(test)]
mod tests {
use crate::{
Expand Down Expand Up @@ -272,17 +298,33 @@ mod tests {

#[test]
#[serial]
fn test_update_languages() {
fn test_convert_update_languages() {
let conn = &mut establish_unpooled_connection();

// call with empty vec, returns all languages
let updated1 = update_languages(conn, vec![]).unwrap();
assert_eq!(184, updated1.len());
let converted1 = convert_update_languages(conn, vec![]).unwrap();
assert_eq!(184, converted1.len());

// call with nonempty vec, returns same vec
let test_langs = test_langs1(conn);
let converted2 = convert_update_languages(conn, test_langs.clone()).unwrap();
assert_eq!(test_langs, converted2);
}
#[test]
#[serial]
fn test_convert_read_languages() {
let conn = &mut establish_unpooled_connection();

// call with all languages, returns empty vec
use crate::schema::language::dsl::*;
let all_langs = language.select(id).get_results(conn).unwrap();
let converted1: Vec<LanguageId> = convert_read_languages(conn, all_langs).unwrap();
assert_eq!(0, converted1.len());

// call with nonempty vec, returns same vec
let test_langs = test_langs1(conn);
let updated2 = update_languages(conn, test_langs.clone()).unwrap();
assert_eq!(test_langs, updated2);
let converted2 = convert_read_languages(conn, test_langs.clone()).unwrap();
assert_eq!(test_langs, converted2);
}

#[test]
Expand Down

0 comments on commit 77fb8ba

Please sign in to comment.