From d1866cbd04da7a3784097760cf4ff8d76ca1ce83 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 10 Sep 2024 14:32:12 -0400 Subject: [PATCH] Remove pointless block_views. (#4841) - Fixes #4793 --- crates/api_common/src/site.rs | 11 ++--- crates/api_crud/src/site/read.rs | 18 +++---- crates/db_schema/src/impls/community_block.rs | 32 ++++++++++--- crates/db_schema/src/impls/instance_block.rs | 30 +++++++++--- crates/db_schema/src/impls/person_block.rs | 47 +++++++++++++++---- .../src/community_block_view.rs | 24 ---------- .../db_views_actor/src/instance_block_view.rs | 27 ----------- crates/db_views_actor/src/lib.rs | 6 --- .../db_views_actor/src/person_block_view.rs | 30 ------------ crates/db_views_actor/src/structs.rs | 34 -------------- 10 files changed, 100 insertions(+), 159 deletions(-) delete mode 100644 crates/db_views_actor/src/community_block_view.rs delete mode 100644 crates/db_views_actor/src/instance_block_view.rs delete mode 100644 crates/db_views_actor/src/person_block_view.rs diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index 3850de1c6..5311eca9c 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -11,10 +11,12 @@ use lemmy_db_schema::{ RegistrationApplicationId, }, source::{ + community::Community, federation_queue_state::FederationQueueState, instance::Instance, language::Language, local_site_url_blocklist::LocalSiteUrlBlocklist, + person::Person, tagline::Tagline, }, ListingType, @@ -33,12 +35,9 @@ use lemmy_db_views::structs::{ SiteView, }; use lemmy_db_views_actor::structs::{ - CommunityBlockView, CommunityFollowerView, CommunityModeratorView, CommunityView, - InstanceBlockView, - PersonBlockView, PersonView, }; use lemmy_db_views_moderator::structs::{ @@ -337,9 +336,9 @@ pub struct MyUserInfo { pub local_user_view: LocalUserView, pub follows: Vec, pub moderates: Vec, - pub community_blocks: Vec, - pub instance_blocks: Vec, - pub person_blocks: Vec, + pub community_blocks: Vec, + pub instance_blocks: Vec, + pub person_blocks: Vec, pub discussion_languages: Vec, } diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index c633bebde..d2a13ac5e 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -5,19 +5,15 @@ use lemmy_api_common::{ }; use lemmy_db_schema::source::{ actor_language::{LocalUserLanguage, SiteLanguage}, + community_block::CommunityBlock, + instance_block::InstanceBlock, language::Language, local_site_url_blocklist::LocalSiteUrlBlocklist, + person_block::PersonBlock, tagline::Tagline, }; use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView}; -use lemmy_db_views_actor::structs::{ - CommunityBlockView, - CommunityFollowerView, - CommunityModeratorView, - InstanceBlockView, - PersonBlockView, - PersonView, -}; +use lemmy_db_views_actor::structs::{CommunityFollowerView, CommunityModeratorView, PersonView}; use lemmy_utils::{ error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult}, CACHE_DURATION_API, @@ -81,9 +77,9 @@ pub async fn get_site( discussion_languages, ) = lemmy_db_schema::try_join_with_pool!(pool => ( |pool| CommunityFollowerView::for_person(pool, person_id), - |pool| CommunityBlockView::for_person(pool, person_id), - |pool| InstanceBlockView::for_person(pool, person_id), - |pool| PersonBlockView::for_person(pool, person_id), + |pool| CommunityBlock::for_person(pool, person_id), + |pool| InstanceBlock::for_person(pool, person_id), + |pool| PersonBlock::for_person(pool, person_id), |pool| CommunityModeratorView::for_person(pool, person_id, Some(&local_user_view.local_user)), |pool| LocalUserLanguage::read(pool, local_user_id) )) diff --git a/crates/db_schema/src/impls/community_block.rs b/crates/db_schema/src/impls/community_block.rs index 1393f49d3..944a53ae3 100644 --- a/crates/db_schema/src/impls/community_block.rs +++ b/crates/db_schema/src/impls/community_block.rs @@ -1,7 +1,10 @@ use crate::{ newtypes::{CommunityId, PersonId}, - schema::community_block::dsl::{community_block, community_id, person_id}, - source::community_block::{CommunityBlock, CommunityBlockForm}, + schema::{community, community_block}, + source::{ + community::Community, + community_block::{CommunityBlock, CommunityBlockForm}, + }, traits::Blockable, utils::{get_conn, DbPool}, }; @@ -9,6 +12,7 @@ use diesel::{ dsl::{exists, insert_into}, result::Error, select, + ExpressionMethods, QueryDsl, }; use diesel_async::RunQueryDsl; @@ -21,11 +25,27 @@ impl CommunityBlock { ) -> Result { let conn = &mut get_conn(pool).await?; select(exists( - community_block.find((for_person_id, for_community_id)), + community_block::table.find((for_person_id, for_community_id)), )) .get_result(conn) .await } + + pub async fn for_person( + pool: &mut DbPool<'_>, + person_id: PersonId, + ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; + community_block::table + .inner_join(community::table) + .select(community::all_columns) + .filter(community_block::person_id.eq(person_id)) + .filter(community::deleted.eq(false)) + .filter(community::removed.eq(false)) + .order_by(community_block::published) + .load::(conn) + .await + } } #[async_trait] @@ -33,9 +53,9 @@ impl Blockable for CommunityBlock { type Form = CommunityBlockForm; async fn block(pool: &mut DbPool<'_>, community_block_form: &Self::Form) -> Result { let conn = &mut get_conn(pool).await?; - insert_into(community_block) + insert_into(community_block::table) .values(community_block_form) - .on_conflict((person_id, community_id)) + .on_conflict((community_block::person_id, community_block::community_id)) .do_update() .set(community_block_form) .get_result::(conn) @@ -46,7 +66,7 @@ impl Blockable for CommunityBlock { community_block_form: &Self::Form, ) -> Result { let conn = &mut get_conn(pool).await?; - diesel::delete(community_block.find(( + diesel::delete(community_block::table.find(( community_block_form.person_id, community_block_form.community_id, ))) diff --git a/crates/db_schema/src/impls/instance_block.rs b/crates/db_schema/src/impls/instance_block.rs index e32688411..15fcd1443 100644 --- a/crates/db_schema/src/impls/instance_block.rs +++ b/crates/db_schema/src/impls/instance_block.rs @@ -1,7 +1,10 @@ use crate::{ newtypes::{InstanceId, PersonId}, - schema::instance_block::dsl::{instance_block, instance_id, person_id}, - source::instance_block::{InstanceBlock, InstanceBlockForm}, + schema::{instance, instance_block}, + source::{ + instance::Instance, + instance_block::{InstanceBlock, InstanceBlockForm}, + }, traits::Blockable, utils::{get_conn, DbPool}, }; @@ -9,6 +12,7 @@ use diesel::{ dsl::{exists, insert_into}, result::Error, select, + ExpressionMethods, QueryDsl, }; use diesel_async::RunQueryDsl; @@ -21,11 +25,25 @@ impl InstanceBlock { ) -> Result { let conn = &mut get_conn(pool).await?; select(exists( - instance_block.find((for_person_id, for_instance_id)), + instance_block::table.find((for_person_id, for_instance_id)), )) .get_result(conn) .await } + + pub async fn for_person( + pool: &mut DbPool<'_>, + person_id: PersonId, + ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; + instance_block::table + .inner_join(instance::table) + .select(instance::all_columns) + .filter(instance_block::person_id.eq(person_id)) + .order_by(instance_block::published) + .load::(conn) + .await + } } #[async_trait] @@ -33,9 +51,9 @@ impl Blockable for InstanceBlock { type Form = InstanceBlockForm; async fn block(pool: &mut DbPool<'_>, instance_block_form: &Self::Form) -> Result { let conn = &mut get_conn(pool).await?; - insert_into(instance_block) + insert_into(instance_block::table) .values(instance_block_form) - .on_conflict((person_id, instance_id)) + .on_conflict((instance_block::person_id, instance_block::instance_id)) .do_update() .set(instance_block_form) .get_result::(conn) @@ -46,7 +64,7 @@ impl Blockable for InstanceBlock { instance_block_form: &Self::Form, ) -> Result { let conn = &mut get_conn(pool).await?; - diesel::delete(instance_block.find(( + diesel::delete(instance_block::table.find(( instance_block_form.person_id, instance_block_form.instance_id, ))) diff --git a/crates/db_schema/src/impls/person_block.rs b/crates/db_schema/src/impls/person_block.rs index 0dbf003d8..cfd41f6d6 100644 --- a/crates/db_schema/src/impls/person_block.rs +++ b/crates/db_schema/src/impls/person_block.rs @@ -1,7 +1,10 @@ use crate::{ newtypes::PersonId, - schema::person_block::dsl::{person_block, person_id, target_id}, - source::person_block::{PersonBlock, PersonBlockForm}, + schema::{person, person_block}, + source::{ + person::Person, + person_block::{PersonBlock, PersonBlockForm}, + }, traits::Blockable, utils::{get_conn, DbPool}, }; @@ -9,6 +12,8 @@ use diesel::{ dsl::{exists, insert_into}, result::Error, select, + ExpressionMethods, + JoinOnDsl, QueryDsl, }; use diesel_async::RunQueryDsl; @@ -20,8 +25,30 @@ impl PersonBlock { for_recipient_id: PersonId, ) -> Result { let conn = &mut get_conn(pool).await?; - select(exists(person_block.find((for_person_id, for_recipient_id)))) - .get_result(conn) + select(exists( + person_block::table.find((for_person_id, for_recipient_id)), + )) + .get_result(conn) + .await + } + + pub async fn for_person( + pool: &mut DbPool<'_>, + person_id: PersonId, + ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; + let target_person_alias = diesel::alias!(person as person1); + + person_block::table + .inner_join(person::table.on(person_block::person_id.eq(person::id))) + .inner_join( + target_person_alias.on(person_block::target_id.eq(target_person_alias.field(person::id))), + ) + .select(target_person_alias.fields(person::all_columns)) + .filter(person_block::person_id.eq(person_id)) + .filter(target_person_alias.field(person::deleted).eq(false)) + .order_by(person_block::published) + .load::(conn) .await } } @@ -34,9 +61,9 @@ impl Blockable for PersonBlock { person_block_form: &PersonBlockForm, ) -> Result { let conn = &mut get_conn(pool).await?; - insert_into(person_block) + insert_into(person_block::table) .values(person_block_form) - .on_conflict((person_id, target_id)) + .on_conflict((person_block::person_id, person_block::target_id)) .do_update() .set(person_block_form) .get_result::(conn) @@ -44,8 +71,10 @@ impl Blockable for PersonBlock { } async fn unblock(pool: &mut DbPool<'_>, person_block_form: &Self::Form) -> Result { let conn = &mut get_conn(pool).await?; - diesel::delete(person_block.find((person_block_form.person_id, person_block_form.target_id))) - .execute(conn) - .await + diesel::delete( + person_block::table.find((person_block_form.person_id, person_block_form.target_id)), + ) + .execute(conn) + .await } } diff --git a/crates/db_views_actor/src/community_block_view.rs b/crates/db_views_actor/src/community_block_view.rs deleted file mode 100644 index c7d3d1836..000000000 --- a/crates/db_views_actor/src/community_block_view.rs +++ /dev/null @@ -1,24 +0,0 @@ -use crate::structs::CommunityBlockView; -use diesel::{result::Error, ExpressionMethods, QueryDsl}; -use diesel_async::RunQueryDsl; -use lemmy_db_schema::{ - newtypes::PersonId, - schema::{community, community_block, person}, - utils::{get_conn, DbPool}, -}; - -impl CommunityBlockView { - pub async fn for_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - community_block::table - .inner_join(person::table) - .inner_join(community::table) - .select((person::all_columns, community::all_columns)) - .filter(community_block::person_id.eq(person_id)) - .filter(community::deleted.eq(false)) - .filter(community::removed.eq(false)) - .order_by(community_block::published) - .load::(conn) - .await - } -} diff --git a/crates/db_views_actor/src/instance_block_view.rs b/crates/db_views_actor/src/instance_block_view.rs deleted file mode 100644 index 05820862a..000000000 --- a/crates/db_views_actor/src/instance_block_view.rs +++ /dev/null @@ -1,27 +0,0 @@ -use crate::structs::InstanceBlockView; -use diesel::{result::Error, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl}; -use diesel_async::RunQueryDsl; -use lemmy_db_schema::{ - newtypes::PersonId, - schema::{instance, instance_block, person, site}, - utils::{get_conn, DbPool}, -}; - -impl InstanceBlockView { - pub async fn for_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - instance_block::table - .inner_join(person::table) - .inner_join(instance::table) - .left_join(site::table.on(site::instance_id.eq(instance::id))) - .select(( - person::all_columns, - instance::all_columns, - site::all_columns.nullable(), - )) - .filter(instance_block::person_id.eq(person_id)) - .order_by(instance_block::published) - .load::(conn) - .await - } -} diff --git a/crates/db_views_actor/src/lib.rs b/crates/db_views_actor/src/lib.rs index e9f8e4189..2ec9652e3 100644 --- a/crates/db_views_actor/src/lib.rs +++ b/crates/db_views_actor/src/lib.rs @@ -1,8 +1,6 @@ #[cfg(feature = "full")] pub mod comment_reply_view; #[cfg(feature = "full")] -pub mod community_block_view; -#[cfg(feature = "full")] pub mod community_follower_view; #[cfg(feature = "full")] pub mod community_moderator_view; @@ -11,10 +9,6 @@ pub mod community_person_ban_view; #[cfg(feature = "full")] pub mod community_view; #[cfg(feature = "full")] -pub mod instance_block_view; -#[cfg(feature = "full")] -pub mod person_block_view; -#[cfg(feature = "full")] pub mod person_mention_view; #[cfg(feature = "full")] pub mod person_view; diff --git a/crates/db_views_actor/src/person_block_view.rs b/crates/db_views_actor/src/person_block_view.rs deleted file mode 100644 index 5f028acd8..000000000 --- a/crates/db_views_actor/src/person_block_view.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::structs::PersonBlockView; -use diesel::{result::Error, ExpressionMethods, JoinOnDsl, QueryDsl}; -use diesel_async::RunQueryDsl; -use lemmy_db_schema::{ - newtypes::PersonId, - schema::{person, person_block}, - utils::{get_conn, DbPool}, -}; - -impl PersonBlockView { - pub async fn for_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let target_person_alias = diesel::alias!(person as person1); - - person_block::table - .inner_join(person::table.on(person_block::person_id.eq(person::id))) - .inner_join( - target_person_alias.on(person_block::target_id.eq(target_person_alias.field(person::id))), - ) - .select(( - person::all_columns, - target_person_alias.fields(person::all_columns), - )) - .filter(person_block::person_id.eq(person_id)) - .filter(target_person_alias.field(person::deleted).eq(false)) - .order_by(person_block::published) - .load::(conn) - .await - } -} diff --git a/crates/db_views_actor/src/structs.rs b/crates/db_views_actor/src/structs.rs index 2356d2be4..2992d575d 100644 --- a/crates/db_views_actor/src/structs.rs +++ b/crates/db_views_actor/src/structs.rs @@ -6,11 +6,9 @@ use lemmy_db_schema::{ comment::Comment, comment_reply::CommentReply, community::Community, - instance::Instance, person::Person, person_mention::PersonMention, post::Post, - site::Site, }, SubscribedType, }; @@ -19,28 +17,6 @@ use serde_with::skip_serializing_none; #[cfg(feature = "full")] use ts_rs::TS; -#[derive(Debug, Serialize, Deserialize, Clone)] -#[cfg_attr(feature = "full", derive(TS, Queryable))] -#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] -#[cfg_attr(feature = "full", ts(export))] -/// A community block. -pub struct CommunityBlockView { - pub person: Person, - pub community: Community, -} - -#[skip_serializing_none] -#[derive(Debug, Serialize, Deserialize, Clone)] -#[cfg_attr(feature = "full", derive(TS, Queryable))] -#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] -#[cfg_attr(feature = "full", ts(export))] -/// An instance block by a user. -pub struct InstanceBlockView { - pub person: Person, - pub instance: Instance, - pub site: Option, -} - #[derive(Debug, Serialize, Deserialize, Clone)] #[cfg_attr(feature = "full", derive(TS, Queryable))] #[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] @@ -83,16 +59,6 @@ pub struct CommunityView { pub banned_from_community: bool, } -#[derive(Debug, Serialize, Deserialize, Clone)] -#[cfg_attr(feature = "full", derive(TS, Queryable))] -#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] -#[cfg_attr(feature = "full", ts(export))] -/// A person block. -pub struct PersonBlockView { - pub person: Person, - pub target: Person, -} - #[skip_serializing_none] #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] #[cfg_attr(feature = "full", derive(TS, Queryable))]