ExpressionMethods,
QueryDsl,
};
-use diesel_async::{
- pooled_connection::deadpool::Object as PooledConnection,
- AsyncPgConnection,
- RunQueryDsl,
-};
-use lemmy_utils::error::LemmyError;
+use diesel_async::{AsyncPgConnection, RunQueryDsl};
+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 mut lang_ids = convert_update_languages(conn, language_ids).await?;
// No need to update if languages are unchanged
- let current = LocalUserLanguage::read(pool, for_local_user_id).await?;
+ let current = LocalUserLanguage::read(&mut conn.into(), for_local_user_id).await?;
if current == lang_ids {
return Ok(());
}
}
impl SiteLanguage {
- pub async fn read_local_raw(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
}
- async fn read_raw(
- conn: &mut PooledConnection<AsyncPgConnection>,
- for_site_id: SiteId,
- ) -> Result<Vec<LanguageId>, Error> {
- site_language::table
+ 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
- }
-
- pub async fn read(pool: &DbPool, for_site_id: SiteId) -> Result<Vec<LanguageId>, Error> {
- let conn = &mut get_conn(pool).await?;
- let langs = Self::read_raw(conn, for_site_id).await?;
+ .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 lang_ids = convert_update_languages(conn, language_ids).await?;
// No need to update if languages are unchanged
- let current = SiteLanguage::read(pool, site.id).await?;
+ let current = SiteLanguage::read(&mut conn.into(), site.id).await?;
if current == lang_ids {
return Ok(());
}
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(())
Ok(())
}
- async fn read_raw(
- conn: &mut PooledConnection<AsyncPgConnection>,
+ pub async fn read(
+ pool: &mut DbPool<'_>,
for_community_id: CommunityId,
) -> Result<Vec<LanguageId>, Error> {
use crate::schema::community_language::dsl::{community_id, community_language, language_id};
- community_language
+ let conn = &mut get_conn(pool).await?;
+ let langs = community_language
.filter(community_id.eq(for_community_id))
.order(language_id)
.select(language_id)
.get_results(conn)
- .await
- }
-
- pub async fn read(
- pool: &DbPool,
- for_community_id: CommunityId,
- ) -> Result<Vec<LanguageId>, Error> {
- let conn = &mut get_conn(pool).await?;
- let langs = Self::read_raw(conn, for_community_id).await?;
+ .await?;
convert_read_languages(conn, langs).await
}
pub async fn update(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
mut language_ids: Vec<LanguageId>,
for_community_id: CommunityId,
) -> Result<(), Error> {
- let conn = &mut get_conn(pool).await?;
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?;
// No need to update if languages are unchanged
- let current = CommunityLanguage::read_raw(conn, for_community_id).await?;
+ let current = CommunityLanguage::read(&mut conn.into(), for_community_id).await?;
if current == lang_ids {
return Ok(());
}
}
pub async fn default_post_language(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
community_id: CommunityId,
local_user_id: LocalUserId,
) -> Result<Option<LanguageId>, Error> {
) -> 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)
};
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) {
+ 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();
#[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_raw(pool).await.unwrap();
#[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 mut test_langs = test_langs1(pool).await;
#[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)
#[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;