Fix limit_languages to operate on correct instance (fixes #2496) (#2518)

* Fix limit_languages to operate on correct instance (fixes #2496)

* cargo fmt
This commit is contained in:
Nutomic 2022-10-28 14:42:05 +00:00 committed by GitHub
parent 3b0be52e67
commit 6aa9bdebae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 16 deletions

View File

@ -76,7 +76,8 @@ impl PerformCrud for EditSite {
let site_id = local_site.site_id; let site_id = local_site.site_id;
if let Some(discussion_languages) = data.discussion_languages.clone() { if let Some(discussion_languages) = data.discussion_languages.clone() {
blocking(context.pool(), move |conn| { blocking(context.pool(), move |conn| {
SiteLanguage::update(conn, discussion_languages.clone(), site_id) let site = Site::read(conn, site_id)?;
SiteLanguage::update(conn, discussion_languages, &site)
}) })
.await??; .await??;
} }
@ -92,7 +93,7 @@ impl PerformCrud for EditSite {
.build(); .build();
blocking(context.pool(), move |conn| { blocking(context.pool(), move |conn| {
Site::update(conn, site_id, &site_form) Site::update(conn, local_site.site_id, &site_form)
}) })
.await .await
// Ignore errors for all these, so as to not throw errors if no update occurs // Ignore errors for all these, so as to not throw errors if no update occurs

View File

@ -159,7 +159,7 @@ impl ApubObject for ApubSite {
let site = blocking(data.pool(), move |conn| { let site = blocking(data.pool(), move |conn| {
let site = Site::create(conn, &site_form)?; let site = Site::create(conn, &site_form)?;
SiteLanguage::update(conn, languages, site.id)?; SiteLanguage::update(conn, languages, &site)?;
Ok::<Site, diesel::result::Error>(site) Ok::<Site, diesel::result::Error>(site)
}) })
.await??; .await??;

View File

@ -1,7 +1,7 @@
use crate::{ use crate::{
diesel::JoinOnDsl, diesel::JoinOnDsl,
newtypes::{CommunityId, LanguageId, LocalUserId, SiteId}, newtypes::{CommunityId, InstanceId, LanguageId, LocalUserId, SiteId},
source::{actor_language::*, language::Language}, source::{actor_language::*, language::Language, site::Site},
}; };
use diesel::{ use diesel::{
delete, delete,
@ -81,17 +81,17 @@ impl SiteLanguage {
pub fn update( pub fn update(
conn: &mut PgConnection, conn: &mut PgConnection,
language_ids: Vec<LanguageId>, language_ids: Vec<LanguageId>,
for_site_id: SiteId, site: &Site,
) -> Result<(), Error> { ) -> Result<(), Error> {
conn.build_transaction().read_write().run(|conn| { conn.build_transaction().read_write().run(|conn| {
use crate::schema::site_language::dsl::*; use crate::schema::site_language::dsl::*;
// Clear the current languages // Clear the current languages
delete(site_language.filter(site_id.eq(for_site_id))).execute(conn)?; delete(site_language.filter(site_id.eq(site.id))).execute(conn)?;
let lang_ids = convert_update_languages(conn, language_ids)?; let lang_ids = convert_update_languages(conn, language_ids)?;
for l in lang_ids { for l in lang_ids {
let form = SiteLanguageForm { let form = SiteLanguageForm {
site_id: for_site_id, site_id: site.id,
language_id: l, language_id: l,
}; };
insert_into(site_language) insert_into(site_language)
@ -99,7 +99,7 @@ impl SiteLanguage {
.get_result::<Self>(conn)?; .get_result::<Self>(conn)?;
} }
CommunityLanguage::limit_languages(conn)?; CommunityLanguage::limit_languages(conn, site.instance_id)?;
Ok(()) Ok(())
}) })
@ -136,7 +136,7 @@ impl CommunityLanguage {
/// also part of site languages. This is because post/comment language is only checked against /// also part of site languages. This is because post/comment language is only checked against
/// community language, and it shouldnt be possible to post content in languages which are not /// community language, and it shouldnt be possible to post content in languages which are not
/// allowed by local site. /// allowed by local site.
fn limit_languages(conn: &mut PgConnection) -> Result<(), Error> { fn limit_languages(conn: &mut PgConnection, for_instance_id: InstanceId) -> Result<(), Error> {
use crate::schema::{ use crate::schema::{
community::dsl as c, community::dsl as c,
community_language::dsl as cl, community_language::dsl as cl,
@ -145,7 +145,7 @@ impl CommunityLanguage {
let community_languages: Vec<LanguageId> = cl::community_language let community_languages: Vec<LanguageId> = cl::community_language
.left_outer_join(sl::site_language.on(cl::language_id.eq(sl::language_id))) .left_outer_join(sl::site_language.on(cl::language_id.eq(sl::language_id)))
.inner_join(c::community) .inner_join(c::community)
.filter(c::local) .filter(c::instance_id.eq(for_instance_id))
.filter(sl::language_id.is_null()) .filter(sl::language_id.is_null())
.select(cl::language_id) .select(cl::language_id)
.get_results(conn)?; .get_results(conn)?;
@ -343,7 +343,7 @@ mod tests {
assert_eq!(184, site_languages1.len()); assert_eq!(184, site_languages1.len());
let test_langs = test_langs1(conn); let test_langs = test_langs1(conn);
SiteLanguage::update(conn, test_langs.clone(), site.id).unwrap(); SiteLanguage::update(conn, test_langs.clone(), &site).unwrap();
let site_languages2 = SiteLanguage::read_local(conn).unwrap(); let site_languages2 = SiteLanguage::read_local(conn).unwrap();
// after update, site only has new languages // after update, site only has new languages
@ -361,7 +361,7 @@ mod tests {
let (site, instance) = create_test_site(conn); let (site, instance) = create_test_site(conn);
let test_langs = test_langs1(conn); let test_langs = test_langs1(conn);
SiteLanguage::update(conn, test_langs.clone(), site.id).unwrap(); SiteLanguage::update(conn, test_langs.clone(), &site).unwrap();
let person_form = PersonInsertForm::builder() let person_form = PersonInsertForm::builder()
.name("my test person".to_string()) .name("my test person".to_string())
@ -399,7 +399,7 @@ mod tests {
let conn = &mut establish_unpooled_connection(); let conn = &mut establish_unpooled_connection();
let (site, instance) = create_test_site(conn); let (site, instance) = create_test_site(conn);
let test_langs = test_langs1(conn); let test_langs = test_langs1(conn);
SiteLanguage::update(conn, test_langs.clone(), site.id).unwrap(); SiteLanguage::update(conn, test_langs.clone(), &site).unwrap();
let read_site_langs = SiteLanguage::read(conn, site.id).unwrap(); let read_site_langs = SiteLanguage::read(conn, site.id).unwrap();
assert_eq!(test_langs, read_site_langs); assert_eq!(test_langs, read_site_langs);
@ -431,7 +431,7 @@ mod tests {
// limit site languages to en, fi. after this, community languages should be updated to // limit site languages to en, fi. after this, community languages should be updated to
// intersection of old languages (en, fr, ru) and (en, fi), which is only fi. // intersection of old languages (en, fr, ru) and (en, fi), which is only fi.
SiteLanguage::update(conn, vec![test_langs[0], test_langs2[0]], site.id).unwrap(); SiteLanguage::update(conn, vec![test_langs[0], test_langs2[0]], &site).unwrap();
let community_langs2 = CommunityLanguage::read(conn, community.id).unwrap(); let community_langs2 = CommunityLanguage::read(conn, community.id).unwrap();
assert_eq!(vec![test_langs[0]], community_langs2); assert_eq!(vec![test_langs[0]], community_langs2);

View File

@ -26,7 +26,7 @@ impl Crud for Site {
.get_result::<Self>(conn)?; .get_result::<Self>(conn)?;
// initialize with all languages // initialize with all languages
SiteLanguage::update(conn, vec![], site_.id)?; SiteLanguage::update(conn, vec![], &site_)?;
Ok(site_) Ok(site_)
} }