Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Mar 27, 2023
1 parent a9f8e44 commit cbe41b1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
48 changes: 33 additions & 15 deletions crates/db_schema/src/impls/actor_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ use diesel::{
ExpressionMethods,
QueryDsl,
};
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use diesel_async::{
pooled_connection::deadpool::Object as PooledConnection,
AsyncPgConnection,
RunQueryDsl,
};
use lemmy_utils::error::LemmyError;
use tokio::sync::OnceCell;

Expand Down Expand Up @@ -115,15 +119,22 @@ impl SiteLanguage {
.await
}

pub async fn read(pool: &DbPool, for_site_id: SiteId) -> Result<Vec<LanguageId>, Error> {
let conn = &mut get_conn(pool).await?;

let langs = site_language::table
async fn read_internal(
conn: &mut PooledConnection<AsyncPgConnection>,
for_site_id: SiteId,
) -> Result<Vec<LanguageId>, Error> {
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?;
.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_internal(conn, for_site_id).await?;

convert_read_languages(conn, langs).await
}

Expand Down Expand Up @@ -233,19 +244,25 @@ impl CommunityLanguage {
Ok(())
}

pub async fn read(
pool: &DbPool,
async fn read_internal(
conn: &mut PooledConnection<AsyncPgConnection>,
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
community_language
.filter(community_id.eq(for_community_id))
.order(language_id)
.select(language_id)
.get_results(conn)
.await?;
.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_internal(conn, for_community_id).await?;
convert_read_languages(conn, langs).await
}

Expand All @@ -258,10 +275,11 @@ impl CommunityLanguage {
if language_ids.is_empty() {
language_ids = SiteLanguage::read_local(pool).await?;
}
let lang_ids = convert_update_languages(conn, language_ids).await?;

// No need to update if languages are unchanged
let current = CommunityLanguage::read(pool, for_community_id).await?;
if current == language_ids {
let current = CommunityLanguage::read_internal(conn, for_community_id).await?;
if current == lang_ids {
return Ok(());
}

Expand All @@ -275,7 +293,7 @@ impl CommunityLanguage {
.execute(conn)
.await?;

for l in language_ids {
for l in lang_ids {
let form = CommunityLanguageForm {
community_id: for_community_id,
language_id: l,
Expand Down
26 changes: 8 additions & 18 deletions crates/db_schema/src/impls/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
newtypes::{CommunityId, DbUrl, PersonId},
schema::community::dsl::{actor_id, community, deleted, local, name, removed},
source::{
actor_language::{CommunityLanguage, SiteLanguage},
actor_language::CommunityLanguage,
community::{
Community,
CommunityFollower,
Expand All @@ -21,7 +21,6 @@ use crate::{
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
use diesel_async::RunQueryDsl;
use futures::future::OptionFuture;

#[async_trait]
impl Crud for Community {
Expand All @@ -42,13 +41,11 @@ impl Crud for Community {

async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
let existing = OptionFuture::from(
form
.actor_id
.as_ref()
.map(|id| Community::read_from_apub_id(pool, id)),
)
.await;
let is_new_community = match &form.actor_id {
Some(id) => Community::read_from_apub_id(pool, id).await?.is_none(),
None => true,
};

// Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible
let community_ = insert_into(community)
.values(form)
Expand All @@ -59,15 +56,8 @@ impl Crud for Community {
.await?;

// Initialize languages for new community
if existing.is_none() {
let site_languages = SiteLanguage::read_local(pool).await;
if let Ok(langs) = site_languages {
// if site exists, init community with site languages
CommunityLanguage::update(pool, langs, community_.id).await?;
} else {
// otherwise, init with all languages (this only happens during tests)
CommunityLanguage::update(pool, vec![], community_.id).await?;
}
if is_new_community {
CommunityLanguage::update(pool, vec![], community_.id).await?;
}

Ok(community_)
Expand Down
15 changes: 6 additions & 9 deletions crates/db_schema/src/impls/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use futures::future::OptionFuture;
use url::Url;

#[async_trait]
Expand All @@ -26,13 +25,11 @@ impl Crud for Site {

async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
let existing = OptionFuture::from(
form
.actor_id
.as_ref()
.map(|id_| Site::read_from_apub_id(pool, id_)),
)
.await;
let is_new_site = match &form.actor_id {
Some(id_) => Site::read_from_apub_id(pool, id_).await?.is_none(),
None => true,
};

// Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible
let site_ = insert_into(site)
.values(form)
Expand All @@ -43,7 +40,7 @@ impl Crud for Site {
.await?;

// initialize languages if site is newly created
if existing.is_none() {
if !is_new_site {
// initialize with all languages
SiteLanguage::update(pool, vec![], &site_).await?;
}
Expand Down

0 comments on commit cbe41b1

Please sign in to comment.