From 6aa9bdebaecaeb3f17008c6137b421d2dbf05949 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Fri, 28 Oct 2022 14:42:05 +0000 Subject: [PATCH] Fix limit_languages to operate on correct instance (fixes #2496) (#2518) * Fix limit_languages to operate on correct instance (fixes #2496) * cargo fmt --- crates/api_crud/src/site/update.rs | 5 ++-- crates/apub/src/objects/instance.rs | 2 +- crates/db_schema/src/impls/actor_language.rs | 24 ++++++++++---------- crates/db_schema/src/impls/site.rs | 2 +- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index 47915e43..2f050395 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -76,7 +76,8 @@ impl PerformCrud for EditSite { let site_id = local_site.site_id; if let Some(discussion_languages) = data.discussion_languages.clone() { 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??; } @@ -92,7 +93,7 @@ impl PerformCrud for EditSite { .build(); blocking(context.pool(), move |conn| { - Site::update(conn, site_id, &site_form) + Site::update(conn, local_site.site_id, &site_form) }) .await // Ignore errors for all these, so as to not throw errors if no update occurs diff --git a/crates/apub/src/objects/instance.rs b/crates/apub/src/objects/instance.rs index 521de64f..de4839ad 100644 --- a/crates/apub/src/objects/instance.rs +++ b/crates/apub/src/objects/instance.rs @@ -159,7 +159,7 @@ impl ApubObject for ApubSite { let site = blocking(data.pool(), move |conn| { let site = Site::create(conn, &site_form)?; - SiteLanguage::update(conn, languages, site.id)?; + SiteLanguage::update(conn, languages, &site)?; Ok::(site) }) .await??; diff --git a/crates/db_schema/src/impls/actor_language.rs b/crates/db_schema/src/impls/actor_language.rs index 1de5c6aa..6525a82d 100644 --- a/crates/db_schema/src/impls/actor_language.rs +++ b/crates/db_schema/src/impls/actor_language.rs @@ -1,7 +1,7 @@ use crate::{ diesel::JoinOnDsl, - newtypes::{CommunityId, LanguageId, LocalUserId, SiteId}, - source::{actor_language::*, language::Language}, + newtypes::{CommunityId, InstanceId, LanguageId, LocalUserId, SiteId}, + source::{actor_language::*, language::Language, site::Site}, }; use diesel::{ delete, @@ -81,17 +81,17 @@ impl SiteLanguage { pub fn update( conn: &mut PgConnection, language_ids: Vec, - for_site_id: SiteId, + site: &Site, ) -> Result<(), Error> { conn.build_transaction().read_write().run(|conn| { use crate::schema::site_language::dsl::*; // 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)?; for l in lang_ids { let form = SiteLanguageForm { - site_id: for_site_id, + site_id: site.id, language_id: l, }; insert_into(site_language) @@ -99,7 +99,7 @@ impl SiteLanguage { .get_result::(conn)?; } - CommunityLanguage::limit_languages(conn)?; + CommunityLanguage::limit_languages(conn, site.instance_id)?; Ok(()) }) @@ -136,7 +136,7 @@ impl CommunityLanguage { /// 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 /// 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::{ community::dsl as c, community_language::dsl as cl, @@ -145,7 +145,7 @@ impl CommunityLanguage { let community_languages: Vec = cl::community_language .left_outer_join(sl::site_language.on(cl::language_id.eq(sl::language_id))) .inner_join(c::community) - .filter(c::local) + .filter(c::instance_id.eq(for_instance_id)) .filter(sl::language_id.is_null()) .select(cl::language_id) .get_results(conn)?; @@ -343,7 +343,7 @@ mod tests { assert_eq!(184, site_languages1.len()); 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(); // after update, site only has new languages @@ -361,7 +361,7 @@ mod tests { let (site, instance) = create_test_site(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() .name("my test person".to_string()) @@ -399,7 +399,7 @@ mod tests { let conn = &mut establish_unpooled_connection(); let (site, instance) = create_test_site(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(); 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 // 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(); assert_eq!(vec![test_langs[0]], community_langs2); diff --git a/crates/db_schema/src/impls/site.rs b/crates/db_schema/src/impls/site.rs index bea9a21c..de53c19d 100644 --- a/crates/db_schema/src/impls/site.rs +++ b/crates/db_schema/src/impls/site.rs @@ -26,7 +26,7 @@ impl Crud for Site { .get_result::(conn)?; // initialize with all languages - SiteLanguage::update(conn, vec![], site_.id)?; + SiteLanguage::update(conn, vec![], &site_)?; Ok(site_) } -- 2.44.1