Optimize actor_language.rs (#4612)
* Remove useless transaction in actor_language.rs * Update actor_language.rs * site * community * Update actor_language.rs * Update actor_language.rs * Update actor_language.rs * Update actor_language.rs * Update actor_language.rs
This commit is contained in:
parent
b459949f57
commit
d3737d4453
1 changed files with 59 additions and 52 deletions
|
@ -43,10 +43,6 @@ impl LocalUserLanguage {
|
||||||
};
|
};
|
||||||
let conn = &mut get_conn(pool).await?;
|
let conn = &mut get_conn(pool).await?;
|
||||||
|
|
||||||
conn
|
|
||||||
.build_transaction()
|
|
||||||
.run(|conn| {
|
|
||||||
Box::pin(async move {
|
|
||||||
let langs = local_user_language
|
let langs = local_user_language
|
||||||
.filter(local_user_id.eq(for_local_user_id))
|
.filter(local_user_id.eq(for_local_user_id))
|
||||||
.order(language_id)
|
.order(language_id)
|
||||||
|
@ -54,9 +50,6 @@ impl LocalUserLanguage {
|
||||||
.get_results(conn)
|
.get_results(conn)
|
||||||
.await?;
|
.await?;
|
||||||
convert_read_languages(conn, langs).await
|
convert_read_languages(conn, langs).await
|
||||||
}) as _
|
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update the user's languages.
|
/// Update the user's languages.
|
||||||
|
@ -90,24 +83,33 @@ impl LocalUserLanguage {
|
||||||
.build_transaction()
|
.build_transaction()
|
||||||
.run(|conn| {
|
.run(|conn| {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
use crate::schema::local_user_language::dsl::{local_user_id, local_user_language};
|
use crate::schema::local_user_language::dsl::{
|
||||||
// Clear the current user languages
|
language_id,
|
||||||
delete(local_user_language.filter(local_user_id.eq(for_local_user_id)))
|
local_user_id,
|
||||||
.execute(conn)
|
local_user_language,
|
||||||
.await?;
|
};
|
||||||
|
// Delete old languages, not including new languages
|
||||||
|
let delete_old = delete(local_user_language)
|
||||||
|
.filter(local_user_id.eq(for_local_user_id))
|
||||||
|
.filter(language_id.ne_all(&lang_ids))
|
||||||
|
.execute(conn);
|
||||||
|
|
||||||
let forms = lang_ids
|
let forms = lang_ids
|
||||||
.into_iter()
|
.iter()
|
||||||
.map(|l| LocalUserLanguageForm {
|
.map(|&l| LocalUserLanguageForm {
|
||||||
local_user_id: for_local_user_id,
|
local_user_id: for_local_user_id,
|
||||||
language_id: l,
|
language_id: l,
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
insert_into(local_user_language)
|
// Insert new languages
|
||||||
|
let insert_new = insert_into(local_user_language)
|
||||||
.values(forms)
|
.values(forms)
|
||||||
.execute(conn)
|
.on_conflict((language_id, local_user_id))
|
||||||
.await?;
|
.do_nothing()
|
||||||
|
.execute(conn);
|
||||||
|
|
||||||
|
tokio::try_join!(delete_old, insert_new)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}) as _
|
}) as _
|
||||||
})
|
})
|
||||||
|
@ -159,25 +161,30 @@ impl SiteLanguage {
|
||||||
.build_transaction()
|
.build_transaction()
|
||||||
.run(|conn| {
|
.run(|conn| {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
use crate::schema::site_language::dsl::{site_id, site_language};
|
use crate::schema::site_language::dsl::{language_id, site_id, site_language};
|
||||||
|
|
||||||
// Clear the current languages
|
// Delete old languages, not including new languages
|
||||||
delete(site_language.filter(site_id.eq(for_site_id)))
|
let delete_old = delete(site_language)
|
||||||
.execute(conn)
|
.filter(site_id.eq(for_site_id))
|
||||||
.await?;
|
.filter(language_id.ne_all(&lang_ids))
|
||||||
|
.execute(conn);
|
||||||
|
|
||||||
let forms = lang_ids
|
let forms = lang_ids
|
||||||
.into_iter()
|
.iter()
|
||||||
.map(|l| SiteLanguageForm {
|
.map(|&l| SiteLanguageForm {
|
||||||
site_id: for_site_id,
|
site_id: for_site_id,
|
||||||
language_id: l,
|
language_id: l,
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
insert_into(site_language)
|
// Insert new languages
|
||||||
|
let insert_new = insert_into(site_language)
|
||||||
.values(forms)
|
.values(forms)
|
||||||
.get_result::<Self>(conn)
|
.on_conflict((site_id, language_id))
|
||||||
.await?;
|
.do_nothing()
|
||||||
|
.execute(conn);
|
||||||
|
|
||||||
|
tokio::try_join!(delete_old, insert_new)?;
|
||||||
|
|
||||||
CommunityLanguage::limit_languages(conn, instance_id).await?;
|
CommunityLanguage::limit_languages(conn, instance_id).await?;
|
||||||
|
|
||||||
|
@ -278,8 +285,8 @@ impl CommunityLanguage {
|
||||||
}
|
}
|
||||||
|
|
||||||
let form = lang_ids
|
let form = lang_ids
|
||||||
.into_iter()
|
.iter()
|
||||||
.map(|language_id| CommunityLanguageForm {
|
.map(|&language_id| CommunityLanguageForm {
|
||||||
community_id: for_community_id,
|
community_id: for_community_id,
|
||||||
language_id,
|
language_id,
|
||||||
})
|
})
|
||||||
|
@ -289,25 +296,25 @@ impl CommunityLanguage {
|
||||||
.build_transaction()
|
.build_transaction()
|
||||||
.run(|conn| {
|
.run(|conn| {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
use crate::schema::community_language::dsl::{community_id, community_language};
|
use crate::schema::community_language::dsl::{
|
||||||
use diesel::result::DatabaseErrorKind::UniqueViolation;
|
community_id,
|
||||||
// Clear the current languages
|
community_language,
|
||||||
delete(community_language.filter(community_id.eq(for_community_id)))
|
language_id,
|
||||||
.execute(conn)
|
};
|
||||||
.await?;
|
// Delete old languages, not including new languages
|
||||||
|
let delete_old = delete(community_language)
|
||||||
|
.filter(community_id.eq(for_community_id))
|
||||||
|
.filter(language_id.ne_all(&lang_ids))
|
||||||
|
.execute(conn);
|
||||||
|
|
||||||
let insert_res = insert_into(community_language)
|
// Insert new languages
|
||||||
|
let insert_new = insert_into(community_language)
|
||||||
.values(form)
|
.values(form)
|
||||||
.get_result::<Self>(conn)
|
.on_conflict((community_id, language_id))
|
||||||
.await;
|
.do_nothing()
|
||||||
|
.execute(conn);
|
||||||
|
|
||||||
if let Err(Error::DatabaseError(UniqueViolation, _info)) = insert_res {
|
tokio::try_join!(delete_old, insert_new)?;
|
||||||
// race condition: this function was probably called simultaneously from another caller. ignore error
|
|
||||||
// tracing::warn!("unique error: {_info:#?}");
|
|
||||||
// _info.constraint_name() should be = "community_language_community_id_language_id_key"
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
insert_res?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}) as _
|
}) as _
|
||||||
|
|
Loading…
Reference in a new issue