From 3da5f83a8b4928368bf58e0061091c270259a226 Mon Sep 17 00:00:00 2001 From: dull b Date: Mon, 17 Jul 2023 19:25:37 +0000 Subject: [PATCH 01/28] Try stuff --- crates/db_views/src/comment_report_view.rs | 182 +++++++++++---------- 1 file changed, 93 insertions(+), 89 deletions(-) diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index a09971dbe0..095743e974 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -1,12 +1,20 @@ use crate::structs::CommentReportView; use diesel::{ + dsl, dsl::now, + helper_types::AliasedFields, + query_builder::{AsQuery, Query}, + query_dsl::methods, result::Error, + sql_types, BoolExpressionMethods, + Expression, ExpressionMethods, JoinOnDsl, + JoinTo, NullableExpressionMethods, QueryDsl, + Table, }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ @@ -34,6 +42,82 @@ use lemmy_db_schema::{ utils::{get_conn, limit_and_offset, DbPool}, }; +diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); + +type Ac = ::AllColumns; + +fn full_query<'query, Conn, Q, C, R>( + query: comment_report::table, + my_person_id: PersonId, + community_person_ban_condition: C, +) -> dsl::Select< + impl Query + Table, + ( + Ac, + Ac, + Ac, + Ac, + Ac, + AliasedFields>, + Ac, + dsl::NullableSelect>, + dsl::NullableSelect, + dsl::NullableSelect>>, + ), +> +where + /*Q: AsQuery, + ::Query: Table, /*< + SqlType = ::SqlType, + AllColumns = ::AllColumns, + >*/*/ + C: Expression> + Clone, + R: methods::LimitDsl, + dsl::Limit: methods::LoadQuery<'query, Conn, ::JoinTuple>, +{ + QueryDsl::inner_join( + query, + comment::table.on(comment_report::comment_id.eq(comment::id)), + ) + .inner_join(post::table.on(comment::post_id.eq(post::id))) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) + .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join( + comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), + ) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)) + .and(community_person_ban_condition), + ), + ) + .left_join( + comment_like::table.on( + comment::id + .eq(comment_like::comment_id) + .and(comment_like::person_id.eq(my_person_id)), + ), + ) + .left_join( + person_alias_2.on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + ) + .select(( + comment_report::all_columns, + comment::all_columns, + post::all_columns, + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), + comment_aggregates::all_columns, + community_person_ban::all_columns.nullable(), + comment_like::score.nullable(), + person_alias_2.fields(person::all_columns).nullable(), + )) +} + impl CommentReportView { /// returns the CommentReportView for the provided report_id /// @@ -45,48 +129,7 @@ impl CommentReportView { ) -> Result { let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - - let res = comment_report::table - .find(report_id) - .inner_join(comment::table) - .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) - .inner_join( - comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), - ) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)), - ), - ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(my_person_id)), - ), - ) - .left_join( - person_alias_2 - .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - comment_report::all_columns, - comment::all_columns, - post::all_columns, - community::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - comment_like::score.nullable(), - person_alias_2.fields(person::all_columns).nullable(), - )) + let res = full_query(comment_report::table.find(report_id), my_person_id, true) .first::<::JoinTuple>(conn) .await?; @@ -152,53 +195,14 @@ impl CommentReportQuery { ) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - - let mut query = comment_report::table - .inner_join(comment::table) - .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) - .inner_join( - comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), - ) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)) - .and( - community_person_ban::expires - .is_null() - .or(community_person_ban::expires.gt(now)), - ), - ), - ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(my_person.id)), - ), - ) - .left_join( - person_alias_2 - .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - comment_report::all_columns, - comment::all_columns, - post::all_columns, - community::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - comment_like::score.nullable(), - person_alias_2.fields(person::all_columns).nullable(), - )) - .into_boxed(); + let mut query = full_query( + comment_report::table, + my_person.id, + community_person_ban::expires + .is_null() + .or(community_person_ban::expires.gt(now)), + ) + .into_boxed(); if let Some(community_id) = self.community_id { query = query.filter(post::community_id.eq(community_id)); From 178bd43cac8c7674d30d2c285ed47ca0493ad659 Mon Sep 17 00:00:00 2001 From: dull b Date: Mon, 17 Jul 2023 19:25:42 +0000 Subject: [PATCH 02/28] Revert "Try stuff" This reverts commit 3da5f83a8b4928368bf58e0061091c270259a226. --- crates/db_views/src/comment_report_view.rs | 182 ++++++++++----------- 1 file changed, 89 insertions(+), 93 deletions(-) diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index 095743e974..a09971dbe0 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -1,20 +1,12 @@ use crate::structs::CommentReportView; use diesel::{ - dsl, dsl::now, - helper_types::AliasedFields, - query_builder::{AsQuery, Query}, - query_dsl::methods, result::Error, - sql_types, BoolExpressionMethods, - Expression, ExpressionMethods, JoinOnDsl, - JoinTo, NullableExpressionMethods, QueryDsl, - Table, }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ @@ -42,82 +34,6 @@ use lemmy_db_schema::{ utils::{get_conn, limit_and_offset, DbPool}, }; -diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); - -type Ac = ::AllColumns; - -fn full_query<'query, Conn, Q, C, R>( - query: comment_report::table, - my_person_id: PersonId, - community_person_ban_condition: C, -) -> dsl::Select< - impl Query + Table, - ( - Ac, - Ac, - Ac, - Ac, - Ac, - AliasedFields>, - Ac, - dsl::NullableSelect>, - dsl::NullableSelect, - dsl::NullableSelect>>, - ), -> -where - /*Q: AsQuery, - ::Query: Table, /*< - SqlType = ::SqlType, - AllColumns = ::AllColumns, - >*/*/ - C: Expression> + Clone, - R: methods::LimitDsl, - dsl::Limit: methods::LoadQuery<'query, Conn, ::JoinTuple>, -{ - QueryDsl::inner_join( - query, - comment::table.on(comment_report::comment_id.eq(comment::id)), - ) - .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) - .inner_join( - comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), - ) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)) - .and(community_person_ban_condition), - ), - ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(my_person_id)), - ), - ) - .left_join( - person_alias_2.on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - comment_report::all_columns, - comment::all_columns, - post::all_columns, - community::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - comment_like::score.nullable(), - person_alias_2.fields(person::all_columns).nullable(), - )) -} - impl CommentReportView { /// returns the CommentReportView for the provided report_id /// @@ -129,7 +45,48 @@ impl CommentReportView { ) -> Result { let conn = &mut get_conn(pool).await?; - let res = full_query(comment_report::table.find(report_id), my_person_id, true) + let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); + + let res = comment_report::table + .find(report_id) + .inner_join(comment::table) + .inner_join(post::table.on(comment::post_id.eq(post::id))) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) + .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join( + comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), + ) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)), + ), + ) + .left_join( + comment_like::table.on( + comment::id + .eq(comment_like::comment_id) + .and(comment_like::person_id.eq(my_person_id)), + ), + ) + .left_join( + person_alias_2 + .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + ) + .select(( + comment_report::all_columns, + comment::all_columns, + post::all_columns, + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), + comment_aggregates::all_columns, + community_person_ban::all_columns.nullable(), + comment_like::score.nullable(), + person_alias_2.fields(person::all_columns).nullable(), + )) .first::<::JoinTuple>(conn) .await?; @@ -195,14 +152,53 @@ impl CommentReportQuery { ) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let mut query = full_query( - comment_report::table, - my_person.id, - community_person_ban::expires - .is_null() - .or(community_person_ban::expires.gt(now)), - ) - .into_boxed(); + let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); + + let mut query = comment_report::table + .inner_join(comment::table) + .inner_join(post::table.on(comment::post_id.eq(post::id))) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) + .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join( + comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), + ) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)) + .and( + community_person_ban::expires + .is_null() + .or(community_person_ban::expires.gt(now)), + ), + ), + ) + .left_join( + comment_like::table.on( + comment::id + .eq(comment_like::comment_id) + .and(comment_like::person_id.eq(my_person.id)), + ), + ) + .left_join( + person_alias_2 + .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + ) + .select(( + comment_report::all_columns, + comment::all_columns, + post::all_columns, + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), + comment_aggregates::all_columns, + community_person_ban::all_columns.nullable(), + comment_like::score.nullable(), + person_alias_2.fields(person::all_columns).nullable(), + )) + .into_boxed(); if let Some(community_id) = self.community_id { query = query.filter(post::community_id.eq(community_id)); From b9f9a2316e7cd37082319608c606a1c7db057206 Mon Sep 17 00:00:00 2001 From: dull b Date: Mon, 17 Jul 2023 19:30:33 +0000 Subject: [PATCH 03/28] Revert "Revert "Try stuff"" This reverts commit 178bd43cac8c7674d30d2c285ed47ca0493ad659. --- crates/db_views/src/comment_report_view.rs | 182 +++++++++++---------- 1 file changed, 93 insertions(+), 89 deletions(-) diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index a09971dbe0..095743e974 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -1,12 +1,20 @@ use crate::structs::CommentReportView; use diesel::{ + dsl, dsl::now, + helper_types::AliasedFields, + query_builder::{AsQuery, Query}, + query_dsl::methods, result::Error, + sql_types, BoolExpressionMethods, + Expression, ExpressionMethods, JoinOnDsl, + JoinTo, NullableExpressionMethods, QueryDsl, + Table, }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ @@ -34,6 +42,82 @@ use lemmy_db_schema::{ utils::{get_conn, limit_and_offset, DbPool}, }; +diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); + +type Ac = ::AllColumns; + +fn full_query<'query, Conn, Q, C, R>( + query: comment_report::table, + my_person_id: PersonId, + community_person_ban_condition: C, +) -> dsl::Select< + impl Query + Table, + ( + Ac, + Ac, + Ac, + Ac, + Ac, + AliasedFields>, + Ac, + dsl::NullableSelect>, + dsl::NullableSelect, + dsl::NullableSelect>>, + ), +> +where + /*Q: AsQuery, + ::Query: Table, /*< + SqlType = ::SqlType, + AllColumns = ::AllColumns, + >*/*/ + C: Expression> + Clone, + R: methods::LimitDsl, + dsl::Limit: methods::LoadQuery<'query, Conn, ::JoinTuple>, +{ + QueryDsl::inner_join( + query, + comment::table.on(comment_report::comment_id.eq(comment::id)), + ) + .inner_join(post::table.on(comment::post_id.eq(post::id))) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) + .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join( + comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), + ) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)) + .and(community_person_ban_condition), + ), + ) + .left_join( + comment_like::table.on( + comment::id + .eq(comment_like::comment_id) + .and(comment_like::person_id.eq(my_person_id)), + ), + ) + .left_join( + person_alias_2.on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + ) + .select(( + comment_report::all_columns, + comment::all_columns, + post::all_columns, + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), + comment_aggregates::all_columns, + community_person_ban::all_columns.nullable(), + comment_like::score.nullable(), + person_alias_2.fields(person::all_columns).nullable(), + )) +} + impl CommentReportView { /// returns the CommentReportView for the provided report_id /// @@ -45,48 +129,7 @@ impl CommentReportView { ) -> Result { let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - - let res = comment_report::table - .find(report_id) - .inner_join(comment::table) - .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) - .inner_join( - comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), - ) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)), - ), - ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(my_person_id)), - ), - ) - .left_join( - person_alias_2 - .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - comment_report::all_columns, - comment::all_columns, - post::all_columns, - community::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - comment_like::score.nullable(), - person_alias_2.fields(person::all_columns).nullable(), - )) + let res = full_query(comment_report::table.find(report_id), my_person_id, true) .first::<::JoinTuple>(conn) .await?; @@ -152,53 +195,14 @@ impl CommentReportQuery { ) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - - let mut query = comment_report::table - .inner_join(comment::table) - .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) - .inner_join( - comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), - ) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)) - .and( - community_person_ban::expires - .is_null() - .or(community_person_ban::expires.gt(now)), - ), - ), - ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(my_person.id)), - ), - ) - .left_join( - person_alias_2 - .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - comment_report::all_columns, - comment::all_columns, - post::all_columns, - community::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - comment_like::score.nullable(), - person_alias_2.fields(person::all_columns).nullable(), - )) - .into_boxed(); + let mut query = full_query( + comment_report::table, + my_person.id, + community_person_ban::expires + .is_null() + .or(community_person_ban::expires.gt(now)), + ) + .into_boxed(); if let Some(community_id) = self.community_id { query = query.filter(post::community_id.eq(community_id)); From ccd498dd7228050ae05d2022e9106034fd4132f8 Mon Sep 17 00:00:00 2001 From: dull b Date: Mon, 17 Jul 2023 19:44:00 +0000 Subject: [PATCH 04/28] Revert "Revert "Revert "Try stuff""" This reverts commit b9f9a2316e7cd37082319608c606a1c7db057206. --- crates/db_views/src/comment_report_view.rs | 182 ++++++++++----------- 1 file changed, 89 insertions(+), 93 deletions(-) diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index 095743e974..a09971dbe0 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -1,20 +1,12 @@ use crate::structs::CommentReportView; use diesel::{ - dsl, dsl::now, - helper_types::AliasedFields, - query_builder::{AsQuery, Query}, - query_dsl::methods, result::Error, - sql_types, BoolExpressionMethods, - Expression, ExpressionMethods, JoinOnDsl, - JoinTo, NullableExpressionMethods, QueryDsl, - Table, }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ @@ -42,82 +34,6 @@ use lemmy_db_schema::{ utils::{get_conn, limit_and_offset, DbPool}, }; -diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); - -type Ac = ::AllColumns; - -fn full_query<'query, Conn, Q, C, R>( - query: comment_report::table, - my_person_id: PersonId, - community_person_ban_condition: C, -) -> dsl::Select< - impl Query + Table, - ( - Ac, - Ac, - Ac, - Ac, - Ac, - AliasedFields>, - Ac, - dsl::NullableSelect>, - dsl::NullableSelect, - dsl::NullableSelect>>, - ), -> -where - /*Q: AsQuery, - ::Query: Table, /*< - SqlType = ::SqlType, - AllColumns = ::AllColumns, - >*/*/ - C: Expression> + Clone, - R: methods::LimitDsl, - dsl::Limit: methods::LoadQuery<'query, Conn, ::JoinTuple>, -{ - QueryDsl::inner_join( - query, - comment::table.on(comment_report::comment_id.eq(comment::id)), - ) - .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) - .inner_join( - comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), - ) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)) - .and(community_person_ban_condition), - ), - ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(my_person_id)), - ), - ) - .left_join( - person_alias_2.on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - comment_report::all_columns, - comment::all_columns, - post::all_columns, - community::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - comment_like::score.nullable(), - person_alias_2.fields(person::all_columns).nullable(), - )) -} - impl CommentReportView { /// returns the CommentReportView for the provided report_id /// @@ -129,7 +45,48 @@ impl CommentReportView { ) -> Result { let conn = &mut get_conn(pool).await?; - let res = full_query(comment_report::table.find(report_id), my_person_id, true) + let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); + + let res = comment_report::table + .find(report_id) + .inner_join(comment::table) + .inner_join(post::table.on(comment::post_id.eq(post::id))) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) + .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join( + comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), + ) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)), + ), + ) + .left_join( + comment_like::table.on( + comment::id + .eq(comment_like::comment_id) + .and(comment_like::person_id.eq(my_person_id)), + ), + ) + .left_join( + person_alias_2 + .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + ) + .select(( + comment_report::all_columns, + comment::all_columns, + post::all_columns, + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), + comment_aggregates::all_columns, + community_person_ban::all_columns.nullable(), + comment_like::score.nullable(), + person_alias_2.fields(person::all_columns).nullable(), + )) .first::<::JoinTuple>(conn) .await?; @@ -195,14 +152,53 @@ impl CommentReportQuery { ) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let mut query = full_query( - comment_report::table, - my_person.id, - community_person_ban::expires - .is_null() - .or(community_person_ban::expires.gt(now)), - ) - .into_boxed(); + let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); + + let mut query = comment_report::table + .inner_join(comment::table) + .inner_join(post::table.on(comment::post_id.eq(post::id))) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) + .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join( + comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), + ) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)) + .and( + community_person_ban::expires + .is_null() + .or(community_person_ban::expires.gt(now)), + ), + ), + ) + .left_join( + comment_like::table.on( + comment::id + .eq(comment_like::comment_id) + .and(comment_like::person_id.eq(my_person.id)), + ), + ) + .left_join( + person_alias_2 + .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + ) + .select(( + comment_report::all_columns, + comment::all_columns, + post::all_columns, + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), + comment_aggregates::all_columns, + community_person_ban::all_columns.nullable(), + comment_like::score.nullable(), + person_alias_2.fields(person::all_columns).nullable(), + )) + .into_boxed(); if let Some(community_id) = self.community_id { query = query.filter(post::community_id.eq(community_id)); From a1fa3ee284c9936f0e212daaaad187bb5869d1c4 Mon Sep 17 00:00:00 2001 From: dull b Date: Tue, 18 Jul 2023 02:51:40 +0000 Subject: [PATCH 05/28] Revert "Revert "Revert "Revert "Try stuff"""" This reverts commit ccd498dd7228050ae05d2022e9106034fd4132f8. --- crates/db_views/src/comment_report_view.rs | 182 +++++++++++---------- 1 file changed, 93 insertions(+), 89 deletions(-) diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index a09971dbe0..095743e974 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -1,12 +1,20 @@ use crate::structs::CommentReportView; use diesel::{ + dsl, dsl::now, + helper_types::AliasedFields, + query_builder::{AsQuery, Query}, + query_dsl::methods, result::Error, + sql_types, BoolExpressionMethods, + Expression, ExpressionMethods, JoinOnDsl, + JoinTo, NullableExpressionMethods, QueryDsl, + Table, }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ @@ -34,6 +42,82 @@ use lemmy_db_schema::{ utils::{get_conn, limit_and_offset, DbPool}, }; +diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); + +type Ac = ::AllColumns; + +fn full_query<'query, Conn, Q, C, R>( + query: comment_report::table, + my_person_id: PersonId, + community_person_ban_condition: C, +) -> dsl::Select< + impl Query + Table, + ( + Ac, + Ac, + Ac, + Ac, + Ac, + AliasedFields>, + Ac, + dsl::NullableSelect>, + dsl::NullableSelect, + dsl::NullableSelect>>, + ), +> +where + /*Q: AsQuery, + ::Query: Table, /*< + SqlType = ::SqlType, + AllColumns = ::AllColumns, + >*/*/ + C: Expression> + Clone, + R: methods::LimitDsl, + dsl::Limit: methods::LoadQuery<'query, Conn, ::JoinTuple>, +{ + QueryDsl::inner_join( + query, + comment::table.on(comment_report::comment_id.eq(comment::id)), + ) + .inner_join(post::table.on(comment::post_id.eq(post::id))) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) + .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join( + comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), + ) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)) + .and(community_person_ban_condition), + ), + ) + .left_join( + comment_like::table.on( + comment::id + .eq(comment_like::comment_id) + .and(comment_like::person_id.eq(my_person_id)), + ), + ) + .left_join( + person_alias_2.on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + ) + .select(( + comment_report::all_columns, + comment::all_columns, + post::all_columns, + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), + comment_aggregates::all_columns, + community_person_ban::all_columns.nullable(), + comment_like::score.nullable(), + person_alias_2.fields(person::all_columns).nullable(), + )) +} + impl CommentReportView { /// returns the CommentReportView for the provided report_id /// @@ -45,48 +129,7 @@ impl CommentReportView { ) -> Result { let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - - let res = comment_report::table - .find(report_id) - .inner_join(comment::table) - .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) - .inner_join( - comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), - ) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)), - ), - ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(my_person_id)), - ), - ) - .left_join( - person_alias_2 - .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - comment_report::all_columns, - comment::all_columns, - post::all_columns, - community::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - comment_like::score.nullable(), - person_alias_2.fields(person::all_columns).nullable(), - )) + let res = full_query(comment_report::table.find(report_id), my_person_id, true) .first::<::JoinTuple>(conn) .await?; @@ -152,53 +195,14 @@ impl CommentReportQuery { ) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - - let mut query = comment_report::table - .inner_join(comment::table) - .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) - .inner_join( - comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), - ) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)) - .and( - community_person_ban::expires - .is_null() - .or(community_person_ban::expires.gt(now)), - ), - ), - ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(my_person.id)), - ), - ) - .left_join( - person_alias_2 - .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - comment_report::all_columns, - comment::all_columns, - post::all_columns, - community::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - comment_like::score.nullable(), - person_alias_2.fields(person::all_columns).nullable(), - )) - .into_boxed(); + let mut query = full_query( + comment_report::table, + my_person.id, + community_person_ban::expires + .is_null() + .or(community_person_ban::expires.gt(now)), + ) + .into_boxed(); if let Some(community_id) = self.community_id { query = query.filter(post::community_id.eq(community_id)); From 47000ff9a8f0d9f8780cf5d5eeec4c5a313fe40a Mon Sep 17 00:00:00 2001 From: dull b Date: Wed, 19 Jul 2023 01:41:16 +0000 Subject: [PATCH 06/28] Try more stuff --- crates/db_views/src/comment_report_view.rs | 188 ++++++++++++--------- 1 file changed, 104 insertions(+), 84 deletions(-) diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index 095743e974..b8a4999d1d 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -3,11 +3,14 @@ use diesel::{ dsl, dsl::now, helper_types::AliasedFields, - query_builder::{AsQuery, Query}, + pg::Pg, + query_builder::{AsQuery, Query, QueryFragment, QueryId, SelectQuery}, query_dsl::methods, result::Error, sql_types, + sql_types::Nullable, BoolExpressionMethods, + BoxableExpression, Expression, ExpressionMethods, JoinOnDsl, @@ -16,7 +19,7 @@ use diesel::{ QueryDsl, Table, }; -use diesel_async::RunQueryDsl; +use diesel_async::{methods::LoadQuery, RunQueryDsl}; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, newtypes::{CommentReportId, CommunityId, PersonId}, @@ -39,83 +42,103 @@ use lemmy_db_schema::{ post::Post, }, traits::JoinView, - utils::{get_conn, limit_and_offset, DbPool}, + utils::{get_conn, limit_and_offset, DbConn, DbPool}, }; diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); -type Ac = ::AllColumns; +type Selection = ( + ::AllColumns, + ::AllColumns, + ::AllColumns, + ::AllColumns, + ::AllColumns, + AliasedFields::AllColumns>, + ::AllColumns, + dsl::Nullable<::AllColumns>, + dsl::Nullable, + dsl::Nullable::AllColumns>>, +); -fn full_query<'query, Conn, Q, C, R>( - query: comment_report::table, +/*trait BoxedFilter:methods::FilterDsl {} + +impl + LoadQuery + Send + AsQuery::SqlType>+FilterDsl,Output=Self> where ::Query: QueryFragment+QueryId+Send+'static {} + +impl + LoadQuery + Send + AsQuery::SqlType>> BoxedJoin for T where ::Query: QueryFragment+QueryId+Send+'static {}*/ + +fn full_query<'a>( + mut query: comment_report::BoxedQuery<'a, Pg>, + //report_id: Option, my_person_id: PersonId, - community_person_ban_condition: C, -) -> dsl::Select< - impl Query + Table, - ( - Ac, - Ac, - Ac, - Ac, - Ac, - AliasedFields>, - Ac, - dsl::NullableSelect>, - dsl::NullableSelect, - dsl::NullableSelect>>, - ), -> -where - /*Q: AsQuery, - ::Query: Table, /*< - SqlType = ::SqlType, - AllColumns = ::AllColumns, - >*/*/ - C: Expression> + Clone, - R: methods::LimitDsl, - dsl::Limit: methods::LoadQuery<'query, Conn, ::JoinTuple>, -{ - QueryDsl::inner_join( - query, - comment::table.on(comment_report::comment_id.eq(comment::id)), - ) - .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) - .inner_join( - comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), - ) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)) - .and(community_person_ban_condition), - ), - ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(my_person_id)), - ), - ) - .left_join( - person_alias_2.on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - comment_report::all_columns, - comment::all_columns, - post::all_columns, - community::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - comment_like::score.nullable(), - person_alias_2.fields(person::all_columns).nullable(), - )) + include_expired: bool, +) -> impl 'a + SelectQuery::SqlType> +/*comment_report::BoxedQuery< + '_, + Pg, + ::SqlType, + /*( + comment_report::SqlType, + comment::SqlType, + post::SqlType, + community::SqlType, + person::SqlType, + AliasedFields::AllColumns>, + comment_aggregates::SqlType, + Nullable, + Nullable<::SqlType>, + Nullable::AllColumns>>, + )*/ +>*/ { + //if let Some(report_id) = report_id {query = query.find(report_id);} + query + .inner_join(comment::table.on(comment_report::comment_id.eq(comment::id))) + .inner_join(post::table.on(comment::post_id.eq(post::id))) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) + .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join( + comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), + ) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)) + .and( + community_person_ban::expires + .is_null() + .or(community_person_ban::expires.gt(now)) + // TODO: avoid evaluation of this condition if include_expired is true + .or(!include_expired), + ), + ), + ) + .left_join( + comment_like::table.on( + comment::id + .eq(comment_like::comment_id) + .and(comment_like::person_id.eq(my_person_id)), + ), + ) + .left_join( + person_alias_2 + .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + ) + .select::(( + comment_report::all_columns, + comment::all_columns, + post::all_columns, + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), + comment_aggregates::all_columns, + community_person_ban::all_columns.nullable(), + comment_like::score.nullable(), + person_alias_2.fields(person::all_columns).nullable(), + )) + //.into_boxed() } impl CommentReportView { @@ -129,9 +152,13 @@ impl CommentReportView { ) -> Result { let conn = &mut get_conn(pool).await?; - let res = full_query(comment_report::table.find(report_id), my_person_id, true) - .first::<::JoinTuple>(conn) - .await?; + let res = full_query( + comment_report::table.find(report_id).into_boxed(), + my_person_id, + true, + ) + .first::<::JoinTuple>(conn) + .await?; Ok(Self::from_tuple(res)) } @@ -195,14 +222,7 @@ impl CommentReportQuery { ) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let mut query = full_query( - comment_report::table, - my_person.id, - community_person_ban::expires - .is_null() - .or(community_person_ban::expires.gt(now)), - ) - .into_boxed(); + let mut query = full_query(comment_report::table.into_boxed(), my_person.id, false); if let Some(community_id) = self.community_id { query = query.filter(post::community_id.eq(community_id)); From 2aec4d68a832f862263245e3fe7e221aaef49332 Mon Sep 17 00:00:00 2001 From: dull b Date: Wed, 19 Jul 2023 05:54:30 +0000 Subject: [PATCH 07/28] Add queries function --- Cargo.lock | 1 + crates/db_views/Cargo.toml | 1 + crates/db_views/src/comment_report_view.rs | 280 ++++++++++----------- 3 files changed, 137 insertions(+), 145 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 407cb9aae2..19c79c9d2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2762,6 +2762,7 @@ dependencies = [ "diesel", "diesel-async", "diesel_ltree", + "futures", "lemmy_db_schema", "serde", "serde_with", diff --git a/crates/db_views/Cargo.toml b/crates/db_views/Cargo.toml index 54a3aab6c4..21c68069c3 100644 --- a/crates/db_views/Cargo.toml +++ b/crates/db_views/Cargo.toml @@ -30,6 +30,7 @@ serde = { workspace = true } serde_with = { workspace = true } tracing = { workspace = true, optional = true } ts-rs = { workspace = true, optional = true } +futures = { workspace = true } [dev-dependencies] serial_test = { workspace = true } diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index b8a4999d1d..759f1be1c3 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -1,25 +1,17 @@ use crate::structs::CommentReportView; use diesel::{ - dsl, dsl::now, - helper_types::AliasedFields, pg::Pg, - query_builder::{AsQuery, Query, QueryFragment, QueryId, SelectQuery}, - query_dsl::methods, result::Error, sql_types, - sql_types::Nullable, BoolExpressionMethods, - BoxableExpression, - Expression, ExpressionMethods, JoinOnDsl, - JoinTo, NullableExpressionMethods, QueryDsl, - Table, }; -use diesel_async::{methods::LoadQuery, RunQueryDsl}; +use diesel_async::RunQueryDsl; +use futures::future::BoxFuture; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, newtypes::{CommentReportId, CommunityId, PersonId}, @@ -44,101 +36,138 @@ use lemmy_db_schema::{ traits::JoinView, utils::{get_conn, limit_and_offset, DbConn, DbPool}, }; +use std::{future::Future, pin::Pin}; diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); -type Selection = ( - ::AllColumns, - ::AllColumns, - ::AllColumns, - ::AllColumns, - ::AllColumns, - AliasedFields::AllColumns>, - ::AllColumns, - dsl::Nullable<::AllColumns>, - dsl::Nullable, - dsl::Nullable::AllColumns>>, -); - -/*trait BoxedFilter:methods::FilterDsl {} - -impl + LoadQuery + Send + AsQuery::SqlType>+FilterDsl,Output=Self> where ::Query: QueryFragment+QueryId+Send+'static {} - -impl + LoadQuery + Send + AsQuery::SqlType>> BoxedJoin for T where ::Query: QueryFragment+QueryId+Send+'static {}*/ - -fn full_query<'a>( - mut query: comment_report::BoxedQuery<'a, Pg>, - //report_id: Option, - my_person_id: PersonId, - include_expired: bool, -) -> impl 'a + SelectQuery::SqlType> -/*comment_report::BoxedQuery< - '_, - Pg, - ::SqlType, - /*( - comment_report::SqlType, - comment::SqlType, - post::SqlType, - community::SqlType, - person::SqlType, - AliasedFields::AllColumns>, - comment_aggregates::SqlType, - Nullable, - Nullable<::SqlType>, - Nullable::AllColumns>>, - )*/ ->*/ { - //if let Some(report_id) = report_id {query = query.find(report_id);} - query - .inner_join(comment::table.on(comment_report::comment_id.eq(comment::id))) - .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) - .inner_join( - comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), - ) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)) - .and( - community_person_ban::expires - .is_null() - .or(community_person_ban::expires.gt(now)) - // TODO: avoid evaluation of this condition if include_expired is true - .or(!include_expired), - ), - ), - ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(my_person_id)), - ), - ) - .left_join( - person_alias_2 - .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select::(( - comment_report::all_columns, - comment::all_columns, - post::all_columns, - community::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - comment_like::score.nullable(), - person_alias_2.fields(person::all_columns).nullable(), - )) - //.into_boxed() +fn queries<'a>() -> ( + impl Fn( + DbConn<'a>, + CommentReportId, + PersonId, + ) -> BoxFuture<'a, Result<::JoinTuple, Error>>, + impl Fn( + DbConn<'a>, + CommentReportQuery, + &'a Person, + ) -> BoxFuture<'a, Result::JoinTuple>, Error>>, +) { + let full_query = move |query: comment_report::BoxedQuery<'static, Pg>, + my_person_id: PersonId, + include_expired: bool| { + query + .inner_join(comment::table.on(comment_report::comment_id.eq(comment::id))) + .inner_join(post::table.on(comment::post_id.eq(post::id))) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) + .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join( + comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), + ) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)) + .and( + community_person_ban::expires + .is_null() + .or(community_person_ban::expires.gt(now)) + // TODO: avoid evaluation of expiration condition if include_expired is true + .or::<_, sql_types::Nullable>(include_expired), + ), + ), + ) + .left_join( + comment_like::table.on( + comment::id + .eq(comment_like::comment_id) + .and(comment_like::person_id.eq(my_person_id)), + ), + ) + .left_join( + person_alias_2 + .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + ) + .select(( + comment_report::all_columns, + comment::all_columns, + post::all_columns, + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), + comment_aggregates::all_columns, + community_person_ban::all_columns.nullable(), + comment_like::score.nullable(), + person_alias_2.fields(person::all_columns).nullable(), + )) + }; + let read = move |mut conn: DbConn<'a>, report_id: CommentReportId, my_person_id: PersonId| { + let fut = async move { + let res = full_query( + comment_report::table.find(report_id).into_boxed(), + my_person_id, + true, + ) + .first::<::JoinTuple>(&mut conn) + .await?; + Ok::<::JoinTuple, Error>(res) + }; + let b: Pin< + Box< + dyn Future::JoinTuple, Error>> + Send + '_, + >, + > = Box::pin(fut); + b + }; + let list = move |mut conn: DbConn<'a>, options: CommentReportQuery, my_person: &'a Person| { + let fut = async move { + let mut query = full_query(comment_report::table.into_boxed(), my_person.id, false); + + if let Some(community_id) = options.community_id { + query = query.filter(post::community_id.eq(community_id)); + } + + if options.unresolved_only.unwrap_or(false) { + query = query.filter(comment_report::resolved.eq(false)); + } + + let (limit, offset) = limit_and_offset(options.page, options.limit)?; + + query = query + .order_by(comment_report::published.desc()) + .limit(limit) + .offset(offset); + + // If its not an admin, get only the ones you mod + let res = if !my_person.admin { + query + .inner_join( + community_moderator::table.on( + community_moderator::community_id + .eq(post::community_id) + .and(community_moderator::person_id.eq(my_person.id)), + ), + ) + .load::<::JoinTuple>(&mut conn) + .await? + } else { + query + .load::<::JoinTuple>(&mut conn) + .await? + }; + Ok::::JoinTuple>, Error>(res) + }; + let b: Pin< + Box< + dyn Future::JoinTuple>, Error>> + + Send + + '_, + >, + > = Box::pin(fut); + b + }; + (read, list) } impl CommentReportView { @@ -150,15 +179,9 @@ impl CommentReportView { report_id: CommentReportId, my_person_id: PersonId, ) -> Result { - let conn = &mut get_conn(pool).await?; + let conn = get_conn(pool).await?; - let res = full_query( - comment_report::table.find(report_id).into_boxed(), - my_person_id, - true, - ) - .first::<::JoinTuple>(conn) - .await?; + let res = (queries().0)(conn, report_id, my_person_id).await?; Ok(Self::from_tuple(res)) } @@ -220,42 +243,9 @@ impl CommentReportQuery { pool: &mut DbPool<'_>, my_person: &Person, ) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - - let mut query = full_query(comment_report::table.into_boxed(), my_person.id, false); - - if let Some(community_id) = self.community_id { - query = query.filter(post::community_id.eq(community_id)); - } - - if self.unresolved_only.unwrap_or(false) { - query = query.filter(comment_report::resolved.eq(false)); - } + let conn = get_conn(pool).await?; - let (limit, offset) = limit_and_offset(self.page, self.limit)?; - - query = query - .order_by(comment_report::published.desc()) - .limit(limit) - .offset(offset); - - // If its not an admin, get only the ones you mod - let res = if !my_person.admin { - query - .inner_join( - community_moderator::table.on( - community_moderator::community_id - .eq(post::community_id) - .and(community_moderator::person_id.eq(my_person.id)), - ), - ) - .load::<::JoinTuple>(conn) - .await? - } else { - query - .load::<::JoinTuple>(conn) - .await? - }; + let res = (queries().1)(conn, self, my_person).await?; Ok(res.into_iter().map(CommentReportView::from_tuple).collect()) } From d3d309cab36822265727b006de5b8c3e5a5b7c86 Mon Sep 17 00:00:00 2001 From: dull b Date: Wed, 19 Jul 2023 06:02:51 +0000 Subject: [PATCH 08/28] Simplify queries function --- crates/db_views/src/comment_report_view.rs | 33 ++++++++-------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index 759f1be1c3..30e4f517ab 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -11,7 +11,7 @@ use diesel::{ QueryDsl, }; use diesel_async::RunQueryDsl; -use futures::future::BoxFuture; +use futures::future::{BoxFuture, FutureExt}; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, newtypes::{CommentReportId, CommunityId, PersonId}, @@ -36,7 +36,7 @@ use lemmy_db_schema::{ traits::JoinView, utils::{get_conn, limit_and_offset, DbConn, DbPool}, }; -use std::{future::Future, pin::Pin}; + diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); @@ -103,7 +103,7 @@ fn queries<'a>() -> ( )) }; let read = move |mut conn: DbConn<'a>, report_id: CommentReportId, my_person_id: PersonId| { - let fut = async move { + async move { let res = full_query( comment_report::table.find(report_id).into_boxed(), my_person_id, @@ -111,17 +111,12 @@ fn queries<'a>() -> ( ) .first::<::JoinTuple>(&mut conn) .await?; - Ok::<::JoinTuple, Error>(res) - }; - let b: Pin< - Box< - dyn Future::JoinTuple, Error>> + Send + '_, - >, - > = Box::pin(fut); - b + Ok::<_, Error>(res) + } + .boxed() }; let list = move |mut conn: DbConn<'a>, options: CommentReportQuery, my_person: &'a Person| { - let fut = async move { + async move { let mut query = full_query(comment_report::table.into_boxed(), my_person.id, false); if let Some(community_id) = options.community_id { @@ -135,6 +130,7 @@ fn queries<'a>() -> ( let (limit, offset) = limit_and_offset(options.page, options.limit)?; query = query + .order_by(comment_report::published.desc()) .limit(limit) .offset(offset); @@ -156,16 +152,9 @@ fn queries<'a>() -> ( .load::<::JoinTuple>(&mut conn) .await? }; - Ok::::JoinTuple>, Error>(res) - }; - let b: Pin< - Box< - dyn Future::JoinTuple>, Error>> - + Send - + '_, - >, - > = Box::pin(fut); - b + Ok::<_, Error>(res) + } + .boxed() }; (read, list) } From 69afed05c1807c3fef8d5b5872546fa22e60b4d0 Mon Sep 17 00:00:00 2001 From: dull b Date: Wed, 19 Jul 2023 18:11:21 +0000 Subject: [PATCH 09/28] Move aliases to db_schema --- crates/db_schema/src/aliases.rs | 5 ++ crates/db_schema/src/lib.rs | 2 + crates/db_views/src/comment_report_view.rs | 15 ++--- crates/db_views/src/post_report_view.rs | 21 +++---- .../src/private_message_report_view.rs | 56 +++++++++---------- crates/db_views/src/private_message_view.rs | 11 ++-- .../src/registration_application_view.rs | 25 ++++----- .../db_views_actor/src/comment_reply_view.rs | 12 ++-- .../db_views_actor/src/person_block_view.rs | 3 +- .../db_views_actor/src/person_mention_view.rs | 12 ++-- .../src/mod_add_community_view.rs | 10 ++-- crates/db_views_moderator/src/mod_add_view.rs | 11 ++-- .../src/mod_ban_from_community_view.rs | 8 +-- crates/db_views_moderator/src/mod_ban_view.rs | 11 ++-- .../src/mod_feature_post_view.rs | 7 ++- .../src/mod_lock_post_view.rs | 6 +- .../src/mod_remove_comment_view.rs | 8 +-- .../src/mod_remove_post_view.rs | 6 +- .../src/mod_transfer_community_view.rs | 10 ++-- .../down.sql | 2 +- .../up.sql | 4 +- .../down.sql | 6 +- .../up.sql | 6 +- .../down.sql | 6 +- .../2021-03-31-105915_add_bot_account/up.sql | 6 +- .../down.sql | 6 +- .../up.sql | 6 +- .../down.sql | 8 +-- .../up.sql | 8 +-- .../down.sql | 6 +- .../up.sql | 6 +- .../down.sql | 4 +- .../up.sql | 2 +- 33 files changed, 161 insertions(+), 154 deletions(-) create mode 100644 crates/db_schema/src/aliases.rs diff --git a/crates/db_schema/src/aliases.rs b/crates/db_schema/src/aliases.rs new file mode 100644 index 0000000000..0f251a154b --- /dev/null +++ b/crates/db_schema/src/aliases.rs @@ -0,0 +1,5 @@ +use crate::schema::person; + +diesel::alias! { + const PERSON_1 = person as person_1: Person1, person as person_2: Person2 +}; diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index acb069ca7d..badb083d8b 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -28,6 +28,8 @@ pub mod newtypes; #[rustfmt::skip] #[allow(clippy::wildcard_imports)] pub mod schema; +#[cfg(feature = "full")] +pub mod aliases; pub mod source; #[cfg(feature = "full")] pub mod traits; diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index 30e4f517ab..67ada36e10 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -14,6 +14,7 @@ use diesel_async::RunQueryDsl; use futures::future::{BoxFuture, FutureExt}; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, + aliases, newtypes::{CommentReportId, CommunityId, PersonId}, schema::{ comment, @@ -37,9 +38,6 @@ use lemmy_db_schema::{ utils::{get_conn, limit_and_offset, DbConn, DbPool}, }; - -diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); - fn queries<'a>() -> ( impl Fn( DbConn<'a>, @@ -60,7 +58,7 @@ fn queries<'a>() -> ( .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join(aliases::person_1.on(comment::creator_id.eq(aliases::person_1.field(person::id)))) .inner_join( comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), ) @@ -86,8 +84,8 @@ fn queries<'a>() -> ( ), ) .left_join( - person_alias_2 - .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + aliases::person_2 + .on(comment_report::resolver_id.eq(aliases::person_2.field(person::id).nullable())), ) .select(( comment_report::all_columns, @@ -95,11 +93,11 @@ fn queries<'a>() -> ( post::all_columns, community::all_columns, person::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), comment_like::score.nullable(), - person_alias_2.fields(person::all_columns).nullable(), + aliases::person_2.fields(person::all_columns).nullable(), )) }; let read = move |mut conn: DbConn<'a>, report_id: CommentReportId, my_person_id: PersonId| { @@ -130,7 +128,6 @@ fn queries<'a>() -> ( let (limit, offset) = limit_and_offset(options.page, options.limit)?; query = query - .order_by(comment_report::published.desc()) .limit(limit) .offset(offset); diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index 50b35b1c25..5ded63fcfc 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -10,6 +10,7 @@ use diesel::{ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ aggregates::structs::PostAggregates, + aliases, newtypes::{CommunityId, PersonId, PostReportId}, schema::{ community, @@ -53,7 +54,6 @@ impl PostReportView { my_person_id: PersonId, ) -> Result { let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let ( post_report, @@ -70,7 +70,7 @@ impl PostReportView { .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(person::table.on(post_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join(aliases::person_1.on(post::creator_id.eq(aliases::person_1.field(person::id)))) .left_join( community_person_ban::table.on( post::community_id @@ -87,18 +87,19 @@ impl PostReportView { ) .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id))) .left_join( - person_alias_2.on(post_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + aliases::person_2 + .on(post_report::resolver_id.eq(aliases::person_2.field(person::id).nullable())), ) .select(( post_report::all_columns, post::all_columns, community::all_columns, person::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), community_person_ban::all_columns.nullable(), post_like::score.nullable(), post_aggregates::all_columns, - person_alias_2.fields(person::all_columns.nullable()), + aliases::person_2.fields(person::all_columns.nullable()), )) .first::(conn) .await?; @@ -173,13 +174,12 @@ impl PostReportQuery { my_person: &Person, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let mut query = post_report::table .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(person::table.on(post_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join(aliases::person_1.on(post::creator_id.eq(aliases::person_1.field(person::id)))) .left_join( community_person_ban::table.on( post::community_id @@ -196,18 +196,19 @@ impl PostReportQuery { ) .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id))) .left_join( - person_alias_2.on(post_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + aliases::person_2 + .on(post_report::resolver_id.eq(aliases::person_2.field(person::id).nullable())), ) .select(( post_report::all_columns, post::all_columns, community::all_columns, person::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), community_person_ban::all_columns.nullable(), post_like::score.nullable(), post_aggregates::all_columns, - person_alias_2.fields(person::all_columns.nullable()), + aliases::person_2.fields(person::all_columns.nullable()), )) .into_boxed(); diff --git a/crates/db_views/src/private_message_report_view.rs b/crates/db_views/src/private_message_report_view.rs index 7ceca271ab..70ed568e53 100644 --- a/crates/db_views/src/private_message_report_view.rs +++ b/crates/db_views/src/private_message_report_view.rs @@ -2,6 +2,7 @@ use crate::structs::PrivateMessageReportView; use diesel::{result::Error, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::PrivateMessageReportId, schema::{person, private_message, private_message_report}, source::{ @@ -30,7 +31,6 @@ impl PrivateMessageReportView { report_id: PrivateMessageReportId, ) -> Result { let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let (private_message_report, private_message, private_message_creator, creator, resolver) = private_message_report::table @@ -38,20 +38,18 @@ impl PrivateMessageReportView { .inner_join(private_message::table) .inner_join(person::table.on(private_message::creator_id.eq(person::id))) .inner_join( - person_alias_1 - .on(private_message_report::creator_id.eq(person_alias_1.field(person::id))), - ) - .left_join( - person_alias_2.on( - private_message_report::resolver_id.eq(person_alias_2.field(person::id).nullable()), - ), + aliases::person_1 + .on(private_message_report::creator_id.eq(aliases::person_1.field(person::id))), ) + .left_join(aliases::person_2.on( + private_message_report::resolver_id.eq(aliases::person_2.field(person::id).nullable()), + )) .select(( private_message_report::all_columns, private_message::all_columns, person::all_columns, - person_alias_1.fields(person::all_columns), - person_alias_2.fields(person::all_columns).nullable(), + aliases::person_1.fields(person::all_columns), + aliases::person_2.fields(person::all_columns).nullable(), )) .first::(conn) .await?; @@ -90,26 +88,26 @@ pub struct PrivateMessageReportQuery { impl PrivateMessageReportQuery { pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - let mut query = private_message_report::table - .inner_join(private_message::table) - .inner_join(person::table.on(private_message::creator_id.eq(person::id))) - .inner_join( - person_alias_1.on(private_message_report::creator_id.eq(person_alias_1.field(person::id))), - ) - .left_join( - person_alias_2 - .on(private_message_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - private_message_report::all_columns, - private_message::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - person_alias_2.fields(person::all_columns).nullable(), - )) - .into_boxed(); + let mut query = + private_message_report::table + .inner_join(private_message::table) + .inner_join(person::table.on(private_message::creator_id.eq(person::id))) + .inner_join( + aliases::person_1 + .on(private_message_report::creator_id.eq(aliases::person_1.field(person::id))), + ) + .left_join(aliases::person_2.on( + private_message_report::resolver_id.eq(aliases::person_2.field(person::id).nullable()), + )) + .select(( + private_message_report::all_columns, + private_message::all_columns, + person::all_columns, + aliases::person_1.fields(person::all_columns), + aliases::person_2.fields(person::all_columns).nullable(), + )) + .into_boxed(); if self.unresolved_only.unwrap_or(false) { query = query.filter(private_message_report::resolved.eq(false)); diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index 863db8125e..ee742a949f 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -10,6 +10,7 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::{PersonId, PrivateMessageId}, schema::{person, private_message}, source::{person::Person, private_message::PrivateMessage}, @@ -26,19 +27,18 @@ impl PrivateMessageView { private_message_id: PrivateMessageId, ) -> Result { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); let (private_message, creator, recipient) = private_message::table .find(private_message_id) .inner_join(person::table.on(private_message::creator_id.eq(person::id))) .inner_join( - person_alias_1.on(private_message::recipient_id.eq(person_alias_1.field(person::id))), + aliases::person_1.on(private_message::recipient_id.eq(aliases::person_1.field(person::id))), ) .order_by(private_message::published.desc()) .select(( private_message::all_columns, person::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), )) .first::(conn) .await?; @@ -81,17 +81,16 @@ impl PrivateMessageQuery { recipient_id: PersonId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); let mut query = private_message::table .inner_join(person::table.on(private_message::creator_id.eq(person::id))) .inner_join( - person_alias_1.on(private_message::recipient_id.eq(person_alias_1.field(person::id))), + aliases::person_1.on(private_message::recipient_id.eq(aliases::person_1.field(person::id))), ) .select(( private_message::all_columns, person::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), )) .into_boxed(); diff --git a/crates/db_views/src/registration_application_view.rs b/crates/db_views/src/registration_application_view.rs index 106e41e433..02793266f1 100644 --- a/crates/db_views/src/registration_application_view.rs +++ b/crates/db_views/src/registration_application_view.rs @@ -9,6 +9,7 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, schema::{local_user, person, registration_application}, source::{ local_user::LocalUser, @@ -28,7 +29,6 @@ impl RegistrationApplicationView { registration_application_id: i32, ) -> Result { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); let (registration_application, creator_local_user, creator, admin) = registration_application::table @@ -37,16 +37,15 @@ impl RegistrationApplicationView { local_user::table.on(registration_application::local_user_id.eq(local_user::id)), ) .inner_join(person::table.on(local_user::person_id.eq(person::id))) - .left_join( - person_alias_1 - .on(registration_application::admin_id.eq(person_alias_1.field(person::id).nullable())), - ) + .left_join(aliases::person_1.on( + registration_application::admin_id.eq(aliases::person_1.field(person::id).nullable()), + )) .order_by(registration_application::published.desc()) .select(( registration_application::all_columns, local_user::all_columns, person::all_columns, - person_alias_1.fields(person::all_columns).nullable(), + aliases::person_1.fields(person::all_columns).nullable(), )) .first::(conn) .await?; @@ -65,14 +64,14 @@ impl RegistrationApplicationView { verified_email_only: bool, ) -> Result { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); let mut query = registration_application::table .inner_join(local_user::table.on(registration_application::local_user_id.eq(local_user::id))) .inner_join(person::table.on(local_user::person_id.eq(person::id))) .left_join( - person_alias_1 - .on(registration_application::admin_id.eq(person_alias_1.field(person::id).nullable())), + aliases::person_1.on( + registration_application::admin_id.eq(aliases::person_1.field(person::id).nullable()), + ), ) .filter(registration_application::admin_id.is_null()) .into_boxed(); @@ -102,21 +101,21 @@ impl RegistrationApplicationQuery { pool: &mut DbPool<'_>, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); let mut query = registration_application::table .inner_join(local_user::table.on(registration_application::local_user_id.eq(local_user::id))) .inner_join(person::table.on(local_user::person_id.eq(person::id))) .left_join( - person_alias_1 - .on(registration_application::admin_id.eq(person_alias_1.field(person::id).nullable())), + aliases::person_1.on( + registration_application::admin_id.eq(aliases::person_1.field(person::id).nullable()), + ), ) .order_by(registration_application::published.desc()) .select(( registration_application::all_columns, local_user::all_columns, person::all_columns, - person_alias_1.fields(person::all_columns).nullable(), + aliases::person_1.fields(person::all_columns).nullable(), )) .into_boxed(); diff --git a/crates/db_views_actor/src/comment_reply_view.rs b/crates/db_views_actor/src/comment_reply_view.rs index 4d7a8eac40..76bbbda4b0 100644 --- a/crates/db_views_actor/src/comment_reply_view.rs +++ b/crates/db_views_actor/src/comment_reply_view.rs @@ -10,6 +10,7 @@ use diesel::{ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, + aliases, newtypes::{CommentReplyId, PersonId}, schema::{ comment, @@ -59,7 +60,6 @@ impl CommentReplyView { my_person_id: Option, ) -> Result { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); @@ -83,7 +83,7 @@ impl CommentReplyView { .inner_join(person::table.on(comment::creator_id.eq(person::id))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person_alias_1) + .inner_join(aliases::person_1) .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) .left_join( community_person_ban::table.on( @@ -126,7 +126,7 @@ impl CommentReplyView { person::all_columns, post::all_columns, community::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), @@ -189,8 +189,6 @@ impl CommentReplyQuery { pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); - // The left join below will return None in this case let person_id_join = self.my_person_id.unwrap_or(PersonId(-1)); @@ -199,7 +197,7 @@ impl CommentReplyQuery { .inner_join(person::table.on(comment::creator_id.eq(person::id))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person_alias_1) + .inner_join(aliases::person_1) .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) .left_join( community_person_ban::table.on( @@ -242,7 +240,7 @@ impl CommentReplyQuery { person::all_columns, post::all_columns, community::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), diff --git a/crates/db_views_actor/src/person_block_view.rs b/crates/db_views_actor/src/person_block_view.rs index b317740dac..1e17d4a338 100644 --- a/crates/db_views_actor/src/person_block_view.rs +++ b/crates/db_views_actor/src/person_block_view.rs @@ -2,6 +2,7 @@ use crate::structs::PersonBlockView; use diesel::{result::Error, ExpressionMethods, JoinOnDsl, QueryDsl}; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::PersonId, schema::{person, person_block}, source::person::Person, @@ -14,7 +15,7 @@ type PersonBlockViewTuple = (Person, Person); 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); + let target_person_alias = aliases::person_1; let res = person_block::table .inner_join(person::table.on(person_block::person_id.eq(person::id))) diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index 3e142254ab..0b1c5f5e5f 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -11,6 +11,7 @@ use diesel::{ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, + aliases, newtypes::{PersonId, PersonMentionId}, schema::{ comment, @@ -60,7 +61,6 @@ impl PersonMentionView { my_person_id: Option, ) -> Result { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); @@ -84,7 +84,7 @@ impl PersonMentionView { .inner_join(person::table.on(comment::creator_id.eq(person::id))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person_alias_1) + .inner_join(aliases::person_1) .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) .left_join( community_person_ban::table.on( @@ -127,7 +127,7 @@ impl PersonMentionView { person::all_columns, post::all_columns, community::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), @@ -189,8 +189,6 @@ impl PersonMentionQuery { pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); - // The left join below will return None in this case let person_id_join = self.my_person_id.unwrap_or(PersonId(-1)); @@ -199,7 +197,7 @@ impl PersonMentionQuery { .inner_join(person::table.on(comment::creator_id.eq(person::id))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person_alias_1) + .inner_join(aliases::person_1) .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) .left_join( community_person_ban::table.on( @@ -247,7 +245,7 @@ impl PersonMentionQuery { person::all_columns, post::all_columns, community::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), diff --git a/crates/db_views_moderator/src/mod_add_community_view.rs b/crates/db_views_moderator/src/mod_add_community_view.rs index c1166b6dd3..1de610c33b 100644 --- a/crates/db_views_moderator/src/mod_add_community_view.rs +++ b/crates/db_views_moderator/src/mod_add_community_view.rs @@ -10,6 +10,7 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::PersonId, schema::{community, mod_add_community, person}, source::{community::Community, moderator::ModAddCommunity, person::Person}, @@ -22,7 +23,7 @@ type ModAddCommunityViewTuple = (ModAddCommunity, Option, Community, Per impl ModAddCommunityView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -34,13 +35,14 @@ impl ModAddCommunityView { .left_join(person::table.on(admin_names_join)) .inner_join(community::table) .inner_join( - person_alias_1.on(mod_add_community::other_person_id.eq(person_alias_1.field(person::id))), + aliases::person_1 + .on(mod_add_community::other_person_id.eq(aliases::person_1.field(person::id))), ) .select(( mod_add_community::all_columns, person::all_columns.nullable(), community::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), )) .into_boxed(); @@ -53,7 +55,7 @@ impl ModAddCommunityView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); + query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_add_view.rs b/crates/db_views_moderator/src/mod_add_view.rs index 7094db1238..91b81c0aa6 100644 --- a/crates/db_views_moderator/src/mod_add_view.rs +++ b/crates/db_views_moderator/src/mod_add_view.rs @@ -10,6 +10,7 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::PersonId, schema::{mod_add, person}, source::{moderator::ModAdd, person::Person}, @@ -22,7 +23,7 @@ type ModAddViewTuple = (ModAdd, Option, Person); impl ModAddView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -32,11 +33,13 @@ impl ModAddView { .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_add::table .left_join(person::table.on(admin_names_join)) - .inner_join(person_alias_1.on(mod_add::other_person_id.eq(person_alias_1.field(person::id)))) + .inner_join( + aliases::person_1.on(mod_add::other_person_id.eq(aliases::person_1.field(person::id))), + ) .select(( mod_add::all_columns, person::all_columns.nullable(), - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), )) .into_boxed(); @@ -45,7 +48,7 @@ impl ModAddView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); + query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_ban_from_community_view.rs b/crates/db_views_moderator/src/mod_ban_from_community_view.rs index ceed974bd1..1b20b78b75 100644 --- a/crates/db_views_moderator/src/mod_ban_from_community_view.rs +++ b/crates/db_views_moderator/src/mod_ban_from_community_view.rs @@ -10,6 +10,7 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::PersonId, schema::{community, mod_ban_from_community, person}, source::{community::Community, moderator::ModBanFromCommunity, person::Person}, @@ -23,7 +24,6 @@ impl ModBanFromCommunityView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -35,14 +35,14 @@ impl ModBanFromCommunityView { .left_join(person::table.on(admin_names_join)) .inner_join(community::table) .inner_join( - person_alias_1 - .on(mod_ban_from_community::other_person_id.eq(person_alias_1.field(person::id))), + aliases::person_1 + .on(mod_ban_from_community::other_person_id.eq(aliases::person_1.field(person::id))), ) .select(( mod_ban_from_community::all_columns, person::all_columns.nullable(), community::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), )) .into_boxed(); diff --git a/crates/db_views_moderator/src/mod_ban_view.rs b/crates/db_views_moderator/src/mod_ban_view.rs index 835804e673..52377b7013 100644 --- a/crates/db_views_moderator/src/mod_ban_view.rs +++ b/crates/db_views_moderator/src/mod_ban_view.rs @@ -10,6 +10,7 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::PersonId, schema::{mod_ban, person}, source::{moderator::ModBan, person::Person}, @@ -22,7 +23,7 @@ type ModBanViewTuple = (ModBan, Option, Person); impl ModBanView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -32,11 +33,13 @@ impl ModBanView { .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_ban::table .left_join(person::table.on(admin_names_join)) - .inner_join(person_alias_1.on(mod_ban::other_person_id.eq(person_alias_1.field(person::id)))) + .inner_join( + aliases::person_1.on(mod_ban::other_person_id.eq(aliases::person_1.field(person::id))), + ) .select(( mod_ban::all_columns, person::all_columns.nullable(), - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), )) .into_boxed(); @@ -45,7 +48,7 @@ impl ModBanView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); + query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_feature_post_view.rs b/crates/db_views_moderator/src/mod_feature_post_view.rs index 16a645f2c6..258e9f8ae2 100644 --- a/crates/db_views_moderator/src/mod_feature_post_view.rs +++ b/crates/db_views_moderator/src/mod_feature_post_view.rs @@ -10,6 +10,7 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::PersonId, schema::{community, mod_feature_post, person, post}, source::{community::Community, moderator::ModFeaturePost, person::Person, post::Post}, @@ -22,7 +23,7 @@ type ModFeaturePostViewTuple = (ModFeaturePost, Option, Post, Community) impl ModFeaturePostView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); + let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -33,7 +34,7 @@ impl ModFeaturePostView { let mut query = mod_feature_post::table .left_join(person::table.on(admin_names_join)) .inner_join(post::table) - .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join(aliases::person_1.on(post::creator_id.eq(aliases::person_1.field(person::id)))) .inner_join(community::table.on(post::community_id.eq(community::id))) .select(( mod_feature_post::all_columns, @@ -52,7 +53,7 @@ impl ModFeaturePostView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); + query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_lock_post_view.rs b/crates/db_views_moderator/src/mod_lock_post_view.rs index b970a3f6c0..35ca0e30e7 100644 --- a/crates/db_views_moderator/src/mod_lock_post_view.rs +++ b/crates/db_views_moderator/src/mod_lock_post_view.rs @@ -10,6 +10,7 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::PersonId, schema::{community, mod_lock_post, person, post}, source::{community::Community, moderator::ModLockPost, person::Person, post::Post}, @@ -23,7 +24,6 @@ impl ModLockPostView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -35,7 +35,7 @@ impl ModLockPostView { .left_join(person::table.on(admin_names_join)) .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join(aliases::person_1.on(post::creator_id.eq(aliases::person_1.field(person::id)))) .select(( mod_lock_post::all_columns, person::all_columns.nullable(), @@ -53,7 +53,7 @@ impl ModLockPostView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); + query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_remove_comment_view.rs b/crates/db_views_moderator/src/mod_remove_comment_view.rs index 946e0eb37c..f1e3146d5a 100644 --- a/crates/db_views_moderator/src/mod_remove_comment_view.rs +++ b/crates/db_views_moderator/src/mod_remove_comment_view.rs @@ -10,6 +10,7 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::PersonId, schema::{comment, community, mod_remove_comment, person, post}, source::{ @@ -35,7 +36,6 @@ type ModRemoveCommentViewTuple = ( impl ModRemoveCommentView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(lemmy_db_schema::schema::person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -46,14 +46,14 @@ impl ModRemoveCommentView { let mut query = mod_remove_comment::table .left_join(person::table.on(admin_names_join)) .inner_join(comment::table) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join(aliases::person_1.on(comment::creator_id.eq(aliases::person_1.field(person::id)))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) .select(( mod_remove_comment::all_columns, person::all_columns.nullable(), comment::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), post::all_columns, community::all_columns, )) @@ -68,7 +68,7 @@ impl ModRemoveCommentView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); + query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_remove_post_view.rs b/crates/db_views_moderator/src/mod_remove_post_view.rs index 74cd3c4899..cfcecfbd34 100644 --- a/crates/db_views_moderator/src/mod_remove_post_view.rs +++ b/crates/db_views_moderator/src/mod_remove_post_view.rs @@ -10,6 +10,7 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::PersonId, schema::{community, mod_remove_post, person, post}, source::{community::Community, moderator::ModRemovePost, person::Person, post::Post}, @@ -23,7 +24,6 @@ impl ModRemovePostView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -35,7 +35,7 @@ impl ModRemovePostView { .left_join(person::table.on(admin_names_join)) .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join(aliases::person_1.on(post::creator_id.eq(aliases::person_1.field(person::id)))) .select(( mod_remove_post::all_columns, person::all_columns.nullable(), @@ -53,7 +53,7 @@ impl ModRemovePostView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); + query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_transfer_community_view.rs b/crates/db_views_moderator/src/mod_transfer_community_view.rs index d84ed87da8..aefb58d712 100644 --- a/crates/db_views_moderator/src/mod_transfer_community_view.rs +++ b/crates/db_views_moderator/src/mod_transfer_community_view.rs @@ -10,6 +10,7 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::PersonId, schema::{community, mod_transfer_community, person}, source::{community::Community, moderator::ModTransferCommunity, person::Person}, @@ -23,7 +24,6 @@ impl ModTransferCommunityView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -35,14 +35,14 @@ impl ModTransferCommunityView { .left_join(person::table.on(admin_names_join)) .inner_join(community::table) .inner_join( - person_alias_1 - .on(mod_transfer_community::other_person_id.eq(person_alias_1.field(person::id))), + aliases::person_1 + .on(mod_transfer_community::other_person_id.eq(aliases::person_1.field(person::id))), ) .select(( mod_transfer_community::all_columns, person::all_columns.nullable(), community::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person_1.fields(person::all_columns), )) .into_boxed(); @@ -55,7 +55,7 @@ impl ModTransferCommunityView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); + query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/migrations/2021-03-09-171136_split_user_table_2/down.sql b/migrations/2021-03-09-171136_split_user_table_2/down.sql index e1d55a205b..af5f46d215 100644 --- a/migrations/2021-03-09-171136_split_user_table_2/down.sql +++ b/migrations/2021-03-09-171136_split_user_table_2/down.sql @@ -135,7 +135,7 @@ drop function -- user_ table -- Drop views -drop view person_alias_1, person_alias_2; +drop view aliases::person_1, aliases::person_2; -- Rename indexes alter index person__pkey rename to user__pkey; diff --git a/migrations/2021-03-09-171136_split_user_table_2/up.sql b/migrations/2021-03-09-171136_split_user_table_2/up.sql index db3547e5fb..a75a50385b 100644 --- a/migrations/2021-03-09-171136_split_user_table_2/up.sql +++ b/migrations/2021-03-09-171136_split_user_table_2/up.sql @@ -86,8 +86,8 @@ alter function site_aggregates_user_delete() rename to site_aggregates_person_de alter function site_aggregates_user_insert() rename to site_aggregates_person_insert; -- Create views -create view person_alias_1 as select * from person; -create view person_alias_2 as select * from person; +create view aliases::person_1 as select * from person; +create view aliases::person_2 as select * from person; -- Redo user aggregates into person_aggregates alter table user_aggregates rename to person_aggregates; diff --git a/migrations/2021-03-20-185321_move_matrix_id_to_person/down.sql b/migrations/2021-03-20-185321_move_matrix_id_to_person/down.sql index 990d8fe977..3925ba4b24 100644 --- a/migrations/2021-03-20-185321_move_matrix_id_to_person/down.sql +++ b/migrations/2021-03-20-185321_move_matrix_id_to_person/down.sql @@ -8,10 +8,10 @@ set from person p where p.id = lu.person_id; -drop view person_alias_1, person_alias_2; +drop view aliases::person_1, aliases::person_2; alter table person drop column matrix_user_id; alter table person drop column admin; -- Regenerate the person_alias views -create view person_alias_1 as select * from person; -create view person_alias_2 as select * from person; +create view aliases::person_1 as select * from person; +create view aliases::person_2 as select * from person; diff --git a/migrations/2021-03-20-185321_move_matrix_id_to_person/up.sql b/migrations/2021-03-20-185321_move_matrix_id_to_person/up.sql index d9ba3dd9c2..9c7bff371e 100644 --- a/migrations/2021-03-20-185321_move_matrix_id_to_person/up.sql +++ b/migrations/2021-03-20-185321_move_matrix_id_to_person/up.sql @@ -12,6 +12,6 @@ alter table local_user drop column matrix_user_id; alter table local_user drop column admin; -- Regenerate the person_alias views -drop view person_alias_1, person_alias_2; -create view person_alias_1 as select * from person; -create view person_alias_2 as select * from person; +drop view aliases::person_1, aliases::person_2; +create view aliases::person_1 as select * from person; +create view aliases::person_2 as select * from person; diff --git a/migrations/2021-03-31-105915_add_bot_account/down.sql b/migrations/2021-03-31-105915_add_bot_account/down.sql index 4f407ce862..ecd6c4fb83 100644 --- a/migrations/2021-03-31-105915_add_bot_account/down.sql +++ b/migrations/2021-03-31-105915_add_bot_account/down.sql @@ -1,6 +1,6 @@ -drop view person_alias_1, person_alias_2; +drop view aliases::person_1, aliases::person_2; alter table person drop column bot_account; -create view person_alias_1 as select * from person; -create view person_alias_2 as select * from person; +create view aliases::person_1 as select * from person; +create view aliases::person_2 as select * from person; alter table local_user drop column show_bot_accounts; diff --git a/migrations/2021-03-31-105915_add_bot_account/up.sql b/migrations/2021-03-31-105915_add_bot_account/up.sql index a1e207a1d6..7458194703 100644 --- a/migrations/2021-03-31-105915_add_bot_account/up.sql +++ b/migrations/2021-03-31-105915_add_bot_account/up.sql @@ -1,8 +1,8 @@ -- Add the bot_account column to the person table -drop view person_alias_1, person_alias_2; +drop view aliases::person_1, aliases::person_2; alter table person add column bot_account boolean not null default false; -create view person_alias_1 as select * from person; -create view person_alias_2 as select * from person; +create view aliases::person_1 as select * from person; +create view aliases::person_2 as select * from person; -- Add the show_bot_accounts to the local user table as a setting alter table local_user add column show_bot_accounts boolean not null default true; diff --git a/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/down.sql b/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/down.sql index 844c02d38b..4156f9e054 100644 --- a/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/down.sql +++ b/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/down.sql @@ -1,6 +1,6 @@ alter table person rename display_name to preferred_username; -- Regenerate the person_alias views -drop view person_alias_1, person_alias_2; -create view person_alias_1 as select * from person; -create view person_alias_2 as select * from person; +drop view aliases::person_1, aliases::person_2; +create view aliases::person_1 as select * from person; +create view aliases::person_2 as select * from person; diff --git a/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/up.sql b/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/up.sql index f4b9729c11..336096181b 100644 --- a/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/up.sql +++ b/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/up.sql @@ -1,6 +1,6 @@ alter table person rename preferred_username to display_name; -- Regenerate the person_alias views -drop view person_alias_1, person_alias_2; -create view person_alias_1 as select * from person; -create view person_alias_2 as select * from person; +drop view aliases::person_1, aliases::person_2; +create view aliases::person_1 as select * from person; +create view aliases::person_2 as select * from person; diff --git a/migrations/2021-07-20-102033_actor_name_length/down.sql b/migrations/2021-07-20-102033_actor_name_length/down.sql index 76cec4c91b..25890b3a19 100644 --- a/migrations/2021-07-20-102033_actor_name_length/down.sql +++ b/migrations/2021-07-20-102033_actor_name_length/down.sql @@ -1,10 +1,10 @@ -DROP VIEW person_alias_1; -DROP VIEW person_alias_2; +DROP VIEW aliases::person_1; +DROP VIEW aliases::person_2; ALTER TABLE community ALTER COLUMN name TYPE varchar(20); ALTER TABLE community ALTER COLUMN title TYPE varchar(100); ALTER TABLE person ALTER COLUMN name TYPE varchar(20); ALTER TABLE person ALTER COLUMN display_name TYPE varchar(20); -create view person_alias_1 as select * from person; -create view person_alias_2 as select * from person; +create view aliases::person_1 as select * from person; +create view aliases::person_2 as select * from person; diff --git a/migrations/2021-07-20-102033_actor_name_length/up.sql b/migrations/2021-07-20-102033_actor_name_length/up.sql index 2e7bc9dfb2..1f73e59460 100644 --- a/migrations/2021-07-20-102033_actor_name_length/up.sql +++ b/migrations/2021-07-20-102033_actor_name_length/up.sql @@ -1,10 +1,10 @@ -DROP VIEW person_alias_1; -DROP VIEW person_alias_2; +DROP VIEW aliases::person_1; +DROP VIEW aliases::person_2; ALTER TABLE community ALTER COLUMN name TYPE varchar(255); ALTER TABLE community ALTER COLUMN title TYPE varchar(255); ALTER TABLE person ALTER COLUMN name TYPE varchar(255); ALTER TABLE person ALTER COLUMN display_name TYPE varchar(255); -create view person_alias_1 as select * from person; -create view person_alias_2 as select * from person; +create view aliases::person_1 as select * from person; +create view aliases::person_2 as select * from person; diff --git a/migrations/2021-12-14-181537_add_temporary_bans/down.sql b/migrations/2021-12-14-181537_add_temporary_bans/down.sql index 83a3715ec4..efaf4f4818 100644 --- a/migrations/2021-12-14-181537_add_temporary_bans/down.sql +++ b/migrations/2021-12-14-181537_add_temporary_bans/down.sql @@ -1,7 +1,7 @@ -drop view person_alias_1, person_alias_2; +drop view aliases::person_1, aliases::person_2; alter table person drop column ban_expires; alter table community_person_ban drop column expires; -create view person_alias_1 as select * from person; -create view person_alias_2 as select * from person; +create view aliases::person_1 as select * from person; +create view aliases::person_2 as select * from person; diff --git a/migrations/2021-12-14-181537_add_temporary_bans/up.sql b/migrations/2021-12-14-181537_add_temporary_bans/up.sql index 7e6338361d..a86d736fd6 100644 --- a/migrations/2021-12-14-181537_add_temporary_bans/up.sql +++ b/migrations/2021-12-14-181537_add_temporary_bans/up.sql @@ -2,7 +2,7 @@ alter table person add column ban_expires timestamp; alter table community_person_ban add column expires timestamp; -drop view person_alias_1, person_alias_2; -create view person_alias_1 as select * from person; -create view person_alias_2 as select * from person; +drop view aliases::person_1, aliases::person_2; +create view aliases::person_1 as select * from person; +create view aliases::person_2 as select * from person; diff --git a/migrations/2022-09-24-161829_remove_table_aliases/down.sql b/migrations/2022-09-24-161829_remove_table_aliases/down.sql index 39b439a0aa..393acca30b 100644 --- a/migrations/2022-09-24-161829_remove_table_aliases/down.sql +++ b/migrations/2022-09-24-161829_remove_table_aliases/down.sql @@ -1,2 +1,2 @@ -create view person_alias_1 as select * from person; -create view person_alias_2 as select * from person; +create view aliases::person_1 as select * from person; +create view aliases::person_2 as select * from person; diff --git a/migrations/2022-09-24-161829_remove_table_aliases/up.sql b/migrations/2022-09-24-161829_remove_table_aliases/up.sql index 36eabecab7..d47e592611 100644 --- a/migrations/2022-09-24-161829_remove_table_aliases/up.sql +++ b/migrations/2022-09-24-161829_remove_table_aliases/up.sql @@ -1,2 +1,2 @@ -- Drop the alias views -drop view person_alias_1, person_alias_2; +drop view aliases::person_1, aliases::person_2; From b43e8301b2ddc75dfb0be6b2bd04e721111cbfd9 Mon Sep 17 00:00:00 2001 From: dull b Date: Wed, 19 Jul 2023 18:11:32 +0000 Subject: [PATCH 10/28] Revert "Move aliases to db_schema" This reverts commit 69afed05c1807c3fef8d5b5872546fa22e60b4d0. --- crates/db_schema/src/aliases.rs | 5 -- crates/db_schema/src/lib.rs | 2 - crates/db_views/src/comment_report_view.rs | 15 +++-- crates/db_views/src/post_report_view.rs | 21 ++++--- .../src/private_message_report_view.rs | 56 ++++++++++--------- crates/db_views/src/private_message_view.rs | 11 ++-- .../src/registration_application_view.rs | 25 +++++---- .../db_views_actor/src/comment_reply_view.rs | 12 ++-- .../db_views_actor/src/person_block_view.rs | 3 +- .../db_views_actor/src/person_mention_view.rs | 12 ++-- .../src/mod_add_community_view.rs | 10 ++-- crates/db_views_moderator/src/mod_add_view.rs | 11 ++-- .../src/mod_ban_from_community_view.rs | 8 +-- crates/db_views_moderator/src/mod_ban_view.rs | 11 ++-- .../src/mod_feature_post_view.rs | 7 +-- .../src/mod_lock_post_view.rs | 6 +- .../src/mod_remove_comment_view.rs | 8 +-- .../src/mod_remove_post_view.rs | 6 +- .../src/mod_transfer_community_view.rs | 10 ++-- .../down.sql | 2 +- .../up.sql | 4 +- .../down.sql | 6 +- .../up.sql | 6 +- .../down.sql | 6 +- .../2021-03-31-105915_add_bot_account/up.sql | 6 +- .../down.sql | 6 +- .../up.sql | 6 +- .../down.sql | 8 +-- .../up.sql | 8 +-- .../down.sql | 6 +- .../up.sql | 6 +- .../down.sql | 4 +- .../up.sql | 2 +- 33 files changed, 154 insertions(+), 161 deletions(-) delete mode 100644 crates/db_schema/src/aliases.rs diff --git a/crates/db_schema/src/aliases.rs b/crates/db_schema/src/aliases.rs deleted file mode 100644 index 0f251a154b..0000000000 --- a/crates/db_schema/src/aliases.rs +++ /dev/null @@ -1,5 +0,0 @@ -use crate::schema::person; - -diesel::alias! { - const PERSON_1 = person as person_1: Person1, person as person_2: Person2 -}; diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index badb083d8b..acb069ca7d 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -28,8 +28,6 @@ pub mod newtypes; #[rustfmt::skip] #[allow(clippy::wildcard_imports)] pub mod schema; -#[cfg(feature = "full")] -pub mod aliases; pub mod source; #[cfg(feature = "full")] pub mod traits; diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index 67ada36e10..30e4f517ab 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -14,7 +14,6 @@ use diesel_async::RunQueryDsl; use futures::future::{BoxFuture, FutureExt}; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, - aliases, newtypes::{CommentReportId, CommunityId, PersonId}, schema::{ comment, @@ -38,6 +37,9 @@ use lemmy_db_schema::{ utils::{get_conn, limit_and_offset, DbConn, DbPool}, }; + +diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); + fn queries<'a>() -> ( impl Fn( DbConn<'a>, @@ -58,7 +60,7 @@ fn queries<'a>() -> ( .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(aliases::person_1.on(comment::creator_id.eq(aliases::person_1.field(person::id)))) + .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) .inner_join( comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), ) @@ -84,8 +86,8 @@ fn queries<'a>() -> ( ), ) .left_join( - aliases::person_2 - .on(comment_report::resolver_id.eq(aliases::person_2.field(person::id).nullable())), + person_alias_2 + .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), ) .select(( comment_report::all_columns, @@ -93,11 +95,11 @@ fn queries<'a>() -> ( post::all_columns, community::all_columns, person::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), comment_like::score.nullable(), - aliases::person_2.fields(person::all_columns).nullable(), + person_alias_2.fields(person::all_columns).nullable(), )) }; let read = move |mut conn: DbConn<'a>, report_id: CommentReportId, my_person_id: PersonId| { @@ -128,6 +130,7 @@ fn queries<'a>() -> ( let (limit, offset) = limit_and_offset(options.page, options.limit)?; query = query + .order_by(comment_report::published.desc()) .limit(limit) .offset(offset); diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index 5ded63fcfc..50b35b1c25 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -10,7 +10,6 @@ use diesel::{ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ aggregates::structs::PostAggregates, - aliases, newtypes::{CommunityId, PersonId, PostReportId}, schema::{ community, @@ -54,6 +53,7 @@ impl PostReportView { my_person_id: PersonId, ) -> Result { let conn = &mut get_conn(pool).await?; + let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let ( post_report, @@ -70,7 +70,7 @@ impl PostReportView { .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(person::table.on(post_report::creator_id.eq(person::id))) - .inner_join(aliases::person_1.on(post::creator_id.eq(aliases::person_1.field(person::id)))) + .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) .left_join( community_person_ban::table.on( post::community_id @@ -87,19 +87,18 @@ impl PostReportView { ) .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id))) .left_join( - aliases::person_2 - .on(post_report::resolver_id.eq(aliases::person_2.field(person::id).nullable())), + person_alias_2.on(post_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), ) .select(( post_report::all_columns, post::all_columns, community::all_columns, person::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), community_person_ban::all_columns.nullable(), post_like::score.nullable(), post_aggregates::all_columns, - aliases::person_2.fields(person::all_columns.nullable()), + person_alias_2.fields(person::all_columns.nullable()), )) .first::(conn) .await?; @@ -174,12 +173,13 @@ impl PostReportQuery { my_person: &Person, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; + let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let mut query = post_report::table .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(person::table.on(post_report::creator_id.eq(person::id))) - .inner_join(aliases::person_1.on(post::creator_id.eq(aliases::person_1.field(person::id)))) + .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) .left_join( community_person_ban::table.on( post::community_id @@ -196,19 +196,18 @@ impl PostReportQuery { ) .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id))) .left_join( - aliases::person_2 - .on(post_report::resolver_id.eq(aliases::person_2.field(person::id).nullable())), + person_alias_2.on(post_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), ) .select(( post_report::all_columns, post::all_columns, community::all_columns, person::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), community_person_ban::all_columns.nullable(), post_like::score.nullable(), post_aggregates::all_columns, - aliases::person_2.fields(person::all_columns.nullable()), + person_alias_2.fields(person::all_columns.nullable()), )) .into_boxed(); diff --git a/crates/db_views/src/private_message_report_view.rs b/crates/db_views/src/private_message_report_view.rs index 70ed568e53..7ceca271ab 100644 --- a/crates/db_views/src/private_message_report_view.rs +++ b/crates/db_views/src/private_message_report_view.rs @@ -2,7 +2,6 @@ use crate::structs::PrivateMessageReportView; use diesel::{result::Error, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, newtypes::PrivateMessageReportId, schema::{person, private_message, private_message_report}, source::{ @@ -31,6 +30,7 @@ impl PrivateMessageReportView { report_id: PrivateMessageReportId, ) -> Result { let conn = &mut get_conn(pool).await?; + let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let (private_message_report, private_message, private_message_creator, creator, resolver) = private_message_report::table @@ -38,18 +38,20 @@ impl PrivateMessageReportView { .inner_join(private_message::table) .inner_join(person::table.on(private_message::creator_id.eq(person::id))) .inner_join( - aliases::person_1 - .on(private_message_report::creator_id.eq(aliases::person_1.field(person::id))), + person_alias_1 + .on(private_message_report::creator_id.eq(person_alias_1.field(person::id))), + ) + .left_join( + person_alias_2.on( + private_message_report::resolver_id.eq(person_alias_2.field(person::id).nullable()), + ), ) - .left_join(aliases::person_2.on( - private_message_report::resolver_id.eq(aliases::person_2.field(person::id).nullable()), - )) .select(( private_message_report::all_columns, private_message::all_columns, person::all_columns, - aliases::person_1.fields(person::all_columns), - aliases::person_2.fields(person::all_columns).nullable(), + person_alias_1.fields(person::all_columns), + person_alias_2.fields(person::all_columns).nullable(), )) .first::(conn) .await?; @@ -88,26 +90,26 @@ pub struct PrivateMessageReportQuery { impl PrivateMessageReportQuery { pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; + let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - let mut query = - private_message_report::table - .inner_join(private_message::table) - .inner_join(person::table.on(private_message::creator_id.eq(person::id))) - .inner_join( - aliases::person_1 - .on(private_message_report::creator_id.eq(aliases::person_1.field(person::id))), - ) - .left_join(aliases::person_2.on( - private_message_report::resolver_id.eq(aliases::person_2.field(person::id).nullable()), - )) - .select(( - private_message_report::all_columns, - private_message::all_columns, - person::all_columns, - aliases::person_1.fields(person::all_columns), - aliases::person_2.fields(person::all_columns).nullable(), - )) - .into_boxed(); + let mut query = private_message_report::table + .inner_join(private_message::table) + .inner_join(person::table.on(private_message::creator_id.eq(person::id))) + .inner_join( + person_alias_1.on(private_message_report::creator_id.eq(person_alias_1.field(person::id))), + ) + .left_join( + person_alias_2 + .on(private_message_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + ) + .select(( + private_message_report::all_columns, + private_message::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), + person_alias_2.fields(person::all_columns).nullable(), + )) + .into_boxed(); if self.unresolved_only.unwrap_or(false) { query = query.filter(private_message_report::resolved.eq(false)); diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index ee742a949f..863db8125e 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -10,7 +10,6 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, newtypes::{PersonId, PrivateMessageId}, schema::{person, private_message}, source::{person::Person, private_message::PrivateMessage}, @@ -27,18 +26,19 @@ impl PrivateMessageView { private_message_id: PrivateMessageId, ) -> Result { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); let (private_message, creator, recipient) = private_message::table .find(private_message_id) .inner_join(person::table.on(private_message::creator_id.eq(person::id))) .inner_join( - aliases::person_1.on(private_message::recipient_id.eq(aliases::person_1.field(person::id))), + person_alias_1.on(private_message::recipient_id.eq(person_alias_1.field(person::id))), ) .order_by(private_message::published.desc()) .select(( private_message::all_columns, person::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), )) .first::(conn) .await?; @@ -81,16 +81,17 @@ impl PrivateMessageQuery { recipient_id: PersonId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); let mut query = private_message::table .inner_join(person::table.on(private_message::creator_id.eq(person::id))) .inner_join( - aliases::person_1.on(private_message::recipient_id.eq(aliases::person_1.field(person::id))), + person_alias_1.on(private_message::recipient_id.eq(person_alias_1.field(person::id))), ) .select(( private_message::all_columns, person::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), )) .into_boxed(); diff --git a/crates/db_views/src/registration_application_view.rs b/crates/db_views/src/registration_application_view.rs index 02793266f1..106e41e433 100644 --- a/crates/db_views/src/registration_application_view.rs +++ b/crates/db_views/src/registration_application_view.rs @@ -9,7 +9,6 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, schema::{local_user, person, registration_application}, source::{ local_user::LocalUser, @@ -29,6 +28,7 @@ impl RegistrationApplicationView { registration_application_id: i32, ) -> Result { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); let (registration_application, creator_local_user, creator, admin) = registration_application::table @@ -37,15 +37,16 @@ impl RegistrationApplicationView { local_user::table.on(registration_application::local_user_id.eq(local_user::id)), ) .inner_join(person::table.on(local_user::person_id.eq(person::id))) - .left_join(aliases::person_1.on( - registration_application::admin_id.eq(aliases::person_1.field(person::id).nullable()), - )) + .left_join( + person_alias_1 + .on(registration_application::admin_id.eq(person_alias_1.field(person::id).nullable())), + ) .order_by(registration_application::published.desc()) .select(( registration_application::all_columns, local_user::all_columns, person::all_columns, - aliases::person_1.fields(person::all_columns).nullable(), + person_alias_1.fields(person::all_columns).nullable(), )) .first::(conn) .await?; @@ -64,14 +65,14 @@ impl RegistrationApplicationView { verified_email_only: bool, ) -> Result { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); let mut query = registration_application::table .inner_join(local_user::table.on(registration_application::local_user_id.eq(local_user::id))) .inner_join(person::table.on(local_user::person_id.eq(person::id))) .left_join( - aliases::person_1.on( - registration_application::admin_id.eq(aliases::person_1.field(person::id).nullable()), - ), + person_alias_1 + .on(registration_application::admin_id.eq(person_alias_1.field(person::id).nullable())), ) .filter(registration_application::admin_id.is_null()) .into_boxed(); @@ -101,21 +102,21 @@ impl RegistrationApplicationQuery { pool: &mut DbPool<'_>, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); let mut query = registration_application::table .inner_join(local_user::table.on(registration_application::local_user_id.eq(local_user::id))) .inner_join(person::table.on(local_user::person_id.eq(person::id))) .left_join( - aliases::person_1.on( - registration_application::admin_id.eq(aliases::person_1.field(person::id).nullable()), - ), + person_alias_1 + .on(registration_application::admin_id.eq(person_alias_1.field(person::id).nullable())), ) .order_by(registration_application::published.desc()) .select(( registration_application::all_columns, local_user::all_columns, person::all_columns, - aliases::person_1.fields(person::all_columns).nullable(), + person_alias_1.fields(person::all_columns).nullable(), )) .into_boxed(); diff --git a/crates/db_views_actor/src/comment_reply_view.rs b/crates/db_views_actor/src/comment_reply_view.rs index 76bbbda4b0..4d7a8eac40 100644 --- a/crates/db_views_actor/src/comment_reply_view.rs +++ b/crates/db_views_actor/src/comment_reply_view.rs @@ -10,7 +10,6 @@ use diesel::{ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, - aliases, newtypes::{CommentReplyId, PersonId}, schema::{ comment, @@ -60,6 +59,7 @@ impl CommentReplyView { my_person_id: Option, ) -> Result { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); @@ -83,7 +83,7 @@ impl CommentReplyView { .inner_join(person::table.on(comment::creator_id.eq(person::id))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(aliases::person_1) + .inner_join(person_alias_1) .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) .left_join( community_person_ban::table.on( @@ -126,7 +126,7 @@ impl CommentReplyView { person::all_columns, post::all_columns, community::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), @@ -189,6 +189,8 @@ impl CommentReplyQuery { pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); + // The left join below will return None in this case let person_id_join = self.my_person_id.unwrap_or(PersonId(-1)); @@ -197,7 +199,7 @@ impl CommentReplyQuery { .inner_join(person::table.on(comment::creator_id.eq(person::id))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(aliases::person_1) + .inner_join(person_alias_1) .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) .left_join( community_person_ban::table.on( @@ -240,7 +242,7 @@ impl CommentReplyQuery { person::all_columns, post::all_columns, community::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), diff --git a/crates/db_views_actor/src/person_block_view.rs b/crates/db_views_actor/src/person_block_view.rs index 1e17d4a338..b317740dac 100644 --- a/crates/db_views_actor/src/person_block_view.rs +++ b/crates/db_views_actor/src/person_block_view.rs @@ -2,7 +2,6 @@ use crate::structs::PersonBlockView; use diesel::{result::Error, ExpressionMethods, JoinOnDsl, QueryDsl}; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, newtypes::PersonId, schema::{person, person_block}, source::person::Person, @@ -15,7 +14,7 @@ type PersonBlockViewTuple = (Person, Person); 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 = aliases::person_1; + let target_person_alias = diesel::alias!(person as person1); let res = person_block::table .inner_join(person::table.on(person_block::person_id.eq(person::id))) diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index 0b1c5f5e5f..3e142254ab 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -11,7 +11,6 @@ use diesel::{ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, - aliases, newtypes::{PersonId, PersonMentionId}, schema::{ comment, @@ -61,6 +60,7 @@ impl PersonMentionView { my_person_id: Option, ) -> Result { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); @@ -84,7 +84,7 @@ impl PersonMentionView { .inner_join(person::table.on(comment::creator_id.eq(person::id))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(aliases::person_1) + .inner_join(person_alias_1) .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) .left_join( community_person_ban::table.on( @@ -127,7 +127,7 @@ impl PersonMentionView { person::all_columns, post::all_columns, community::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), @@ -189,6 +189,8 @@ impl PersonMentionQuery { pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); + // The left join below will return None in this case let person_id_join = self.my_person_id.unwrap_or(PersonId(-1)); @@ -197,7 +199,7 @@ impl PersonMentionQuery { .inner_join(person::table.on(comment::creator_id.eq(person::id))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(aliases::person_1) + .inner_join(person_alias_1) .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) .left_join( community_person_ban::table.on( @@ -245,7 +247,7 @@ impl PersonMentionQuery { person::all_columns, post::all_columns, community::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), diff --git a/crates/db_views_moderator/src/mod_add_community_view.rs b/crates/db_views_moderator/src/mod_add_community_view.rs index 1de610c33b..c1166b6dd3 100644 --- a/crates/db_views_moderator/src/mod_add_community_view.rs +++ b/crates/db_views_moderator/src/mod_add_community_view.rs @@ -10,7 +10,6 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, newtypes::PersonId, schema::{community, mod_add_community, person}, source::{community::Community, moderator::ModAddCommunity, person::Person}, @@ -23,7 +22,7 @@ type ModAddCommunityViewTuple = (ModAddCommunity, Option, Community, Per impl ModAddCommunityView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - + let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -35,14 +34,13 @@ impl ModAddCommunityView { .left_join(person::table.on(admin_names_join)) .inner_join(community::table) .inner_join( - aliases::person_1 - .on(mod_add_community::other_person_id.eq(aliases::person_1.field(person::id))), + person_alias_1.on(mod_add_community::other_person_id.eq(person_alias_1.field(person::id))), ) .select(( mod_add_community::all_columns, person::all_columns.nullable(), community::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), )) .into_boxed(); @@ -55,7 +53,7 @@ impl ModAddCommunityView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); + query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_add_view.rs b/crates/db_views_moderator/src/mod_add_view.rs index 91b81c0aa6..7094db1238 100644 --- a/crates/db_views_moderator/src/mod_add_view.rs +++ b/crates/db_views_moderator/src/mod_add_view.rs @@ -10,7 +10,6 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, newtypes::PersonId, schema::{mod_add, person}, source::{moderator::ModAdd, person::Person}, @@ -23,7 +22,7 @@ type ModAddViewTuple = (ModAdd, Option, Person); impl ModAddView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - + let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -33,13 +32,11 @@ impl ModAddView { .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_add::table .left_join(person::table.on(admin_names_join)) - .inner_join( - aliases::person_1.on(mod_add::other_person_id.eq(aliases::person_1.field(person::id))), - ) + .inner_join(person_alias_1.on(mod_add::other_person_id.eq(person_alias_1.field(person::id)))) .select(( mod_add::all_columns, person::all_columns.nullable(), - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), )) .into_boxed(); @@ -48,7 +45,7 @@ impl ModAddView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); + query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_ban_from_community_view.rs b/crates/db_views_moderator/src/mod_ban_from_community_view.rs index 1b20b78b75..ceed974bd1 100644 --- a/crates/db_views_moderator/src/mod_ban_from_community_view.rs +++ b/crates/db_views_moderator/src/mod_ban_from_community_view.rs @@ -10,7 +10,6 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, newtypes::PersonId, schema::{community, mod_ban_from_community, person}, source::{community::Community, moderator::ModBanFromCommunity, person::Person}, @@ -24,6 +23,7 @@ impl ModBanFromCommunityView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -35,14 +35,14 @@ impl ModBanFromCommunityView { .left_join(person::table.on(admin_names_join)) .inner_join(community::table) .inner_join( - aliases::person_1 - .on(mod_ban_from_community::other_person_id.eq(aliases::person_1.field(person::id))), + person_alias_1 + .on(mod_ban_from_community::other_person_id.eq(person_alias_1.field(person::id))), ) .select(( mod_ban_from_community::all_columns, person::all_columns.nullable(), community::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), )) .into_boxed(); diff --git a/crates/db_views_moderator/src/mod_ban_view.rs b/crates/db_views_moderator/src/mod_ban_view.rs index 52377b7013..835804e673 100644 --- a/crates/db_views_moderator/src/mod_ban_view.rs +++ b/crates/db_views_moderator/src/mod_ban_view.rs @@ -10,7 +10,6 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, newtypes::PersonId, schema::{mod_ban, person}, source::{moderator::ModBan, person::Person}, @@ -23,7 +22,7 @@ type ModBanViewTuple = (ModBan, Option, Person); impl ModBanView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - + let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -33,13 +32,11 @@ impl ModBanView { .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); let mut query = mod_ban::table .left_join(person::table.on(admin_names_join)) - .inner_join( - aliases::person_1.on(mod_ban::other_person_id.eq(aliases::person_1.field(person::id))), - ) + .inner_join(person_alias_1.on(mod_ban::other_person_id.eq(person_alias_1.field(person::id)))) .select(( mod_ban::all_columns, person::all_columns.nullable(), - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), )) .into_boxed(); @@ -48,7 +45,7 @@ impl ModBanView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); + query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_feature_post_view.rs b/crates/db_views_moderator/src/mod_feature_post_view.rs index 258e9f8ae2..16a645f2c6 100644 --- a/crates/db_views_moderator/src/mod_feature_post_view.rs +++ b/crates/db_views_moderator/src/mod_feature_post_view.rs @@ -10,7 +10,6 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, newtypes::PersonId, schema::{community, mod_feature_post, person, post}, source::{community::Community, moderator::ModFeaturePost, person::Person, post::Post}, @@ -23,7 +22,7 @@ type ModFeaturePostViewTuple = (ModFeaturePost, Option, Post, Community) impl ModFeaturePostView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; - + let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -34,7 +33,7 @@ impl ModFeaturePostView { let mut query = mod_feature_post::table .left_join(person::table.on(admin_names_join)) .inner_join(post::table) - .inner_join(aliases::person_1.on(post::creator_id.eq(aliases::person_1.field(person::id)))) + .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) .inner_join(community::table.on(post::community_id.eq(community::id))) .select(( mod_feature_post::all_columns, @@ -53,7 +52,7 @@ impl ModFeaturePostView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); + query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_lock_post_view.rs b/crates/db_views_moderator/src/mod_lock_post_view.rs index 35ca0e30e7..b970a3f6c0 100644 --- a/crates/db_views_moderator/src/mod_lock_post_view.rs +++ b/crates/db_views_moderator/src/mod_lock_post_view.rs @@ -10,7 +10,6 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, newtypes::PersonId, schema::{community, mod_lock_post, person, post}, source::{community::Community, moderator::ModLockPost, person::Person, post::Post}, @@ -24,6 +23,7 @@ impl ModLockPostView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -35,7 +35,7 @@ impl ModLockPostView { .left_join(person::table.on(admin_names_join)) .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(aliases::person_1.on(post::creator_id.eq(aliases::person_1.field(person::id)))) + .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) .select(( mod_lock_post::all_columns, person::all_columns.nullable(), @@ -53,7 +53,7 @@ impl ModLockPostView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); + query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_remove_comment_view.rs b/crates/db_views_moderator/src/mod_remove_comment_view.rs index f1e3146d5a..946e0eb37c 100644 --- a/crates/db_views_moderator/src/mod_remove_comment_view.rs +++ b/crates/db_views_moderator/src/mod_remove_comment_view.rs @@ -10,7 +10,6 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, newtypes::PersonId, schema::{comment, community, mod_remove_comment, person, post}, source::{ @@ -36,6 +35,7 @@ type ModRemoveCommentViewTuple = ( impl ModRemoveCommentView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(lemmy_db_schema::schema::person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -46,14 +46,14 @@ impl ModRemoveCommentView { let mut query = mod_remove_comment::table .left_join(person::table.on(admin_names_join)) .inner_join(comment::table) - .inner_join(aliases::person_1.on(comment::creator_id.eq(aliases::person_1.field(person::id)))) + .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) .select(( mod_remove_comment::all_columns, person::all_columns.nullable(), comment::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), post::all_columns, community::all_columns, )) @@ -68,7 +68,7 @@ impl ModRemoveCommentView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); + query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_remove_post_view.rs b/crates/db_views_moderator/src/mod_remove_post_view.rs index cfcecfbd34..74cd3c4899 100644 --- a/crates/db_views_moderator/src/mod_remove_post_view.rs +++ b/crates/db_views_moderator/src/mod_remove_post_view.rs @@ -10,7 +10,6 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, newtypes::PersonId, schema::{community, mod_remove_post, person, post}, source::{community::Community, moderator::ModRemovePost, person::Person, post::Post}, @@ -24,6 +23,7 @@ impl ModRemovePostView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -35,7 +35,7 @@ impl ModRemovePostView { .left_join(person::table.on(admin_names_join)) .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(aliases::person_1.on(post::creator_id.eq(aliases::person_1.field(person::id)))) + .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) .select(( mod_remove_post::all_columns, person::all_columns.nullable(), @@ -53,7 +53,7 @@ impl ModRemovePostView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); + query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/crates/db_views_moderator/src/mod_transfer_community_view.rs b/crates/db_views_moderator/src/mod_transfer_community_view.rs index aefb58d712..d84ed87da8 100644 --- a/crates/db_views_moderator/src/mod_transfer_community_view.rs +++ b/crates/db_views_moderator/src/mod_transfer_community_view.rs @@ -10,7 +10,6 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aliases, newtypes::PersonId, schema::{community, mod_transfer_community, person}, source::{community::Community, moderator::ModTransferCommunity, person::Person}, @@ -24,6 +23,7 @@ impl ModTransferCommunityView { pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; + let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -35,14 +35,14 @@ impl ModTransferCommunityView { .left_join(person::table.on(admin_names_join)) .inner_join(community::table) .inner_join( - aliases::person_1 - .on(mod_transfer_community::other_person_id.eq(aliases::person_1.field(person::id))), + person_alias_1 + .on(mod_transfer_community::other_person_id.eq(person_alias_1.field(person::id))), ) .select(( mod_transfer_community::all_columns, person::all_columns.nullable(), community::all_columns, - aliases::person_1.fields(person::all_columns), + person_alias_1.fields(person::all_columns), )) .into_boxed(); @@ -55,7 +55,7 @@ impl ModTransferCommunityView { }; if let Some(other_person_id) = params.other_person_id { - query = query.filter(aliases::person_1.field(person::id).eq(other_person_id)); + query = query.filter(person_alias_1.field(person::id).eq(other_person_id)); }; let (limit, offset) = limit_and_offset(params.page, params.limit)?; diff --git a/migrations/2021-03-09-171136_split_user_table_2/down.sql b/migrations/2021-03-09-171136_split_user_table_2/down.sql index af5f46d215..e1d55a205b 100644 --- a/migrations/2021-03-09-171136_split_user_table_2/down.sql +++ b/migrations/2021-03-09-171136_split_user_table_2/down.sql @@ -135,7 +135,7 @@ drop function -- user_ table -- Drop views -drop view aliases::person_1, aliases::person_2; +drop view person_alias_1, person_alias_2; -- Rename indexes alter index person__pkey rename to user__pkey; diff --git a/migrations/2021-03-09-171136_split_user_table_2/up.sql b/migrations/2021-03-09-171136_split_user_table_2/up.sql index a75a50385b..db3547e5fb 100644 --- a/migrations/2021-03-09-171136_split_user_table_2/up.sql +++ b/migrations/2021-03-09-171136_split_user_table_2/up.sql @@ -86,8 +86,8 @@ alter function site_aggregates_user_delete() rename to site_aggregates_person_de alter function site_aggregates_user_insert() rename to site_aggregates_person_insert; -- Create views -create view aliases::person_1 as select * from person; -create view aliases::person_2 as select * from person; +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; -- Redo user aggregates into person_aggregates alter table user_aggregates rename to person_aggregates; diff --git a/migrations/2021-03-20-185321_move_matrix_id_to_person/down.sql b/migrations/2021-03-20-185321_move_matrix_id_to_person/down.sql index 3925ba4b24..990d8fe977 100644 --- a/migrations/2021-03-20-185321_move_matrix_id_to_person/down.sql +++ b/migrations/2021-03-20-185321_move_matrix_id_to_person/down.sql @@ -8,10 +8,10 @@ set from person p where p.id = lu.person_id; -drop view aliases::person_1, aliases::person_2; +drop view person_alias_1, person_alias_2; alter table person drop column matrix_user_id; alter table person drop column admin; -- Regenerate the person_alias views -create view aliases::person_1 as select * from person; -create view aliases::person_2 as select * from person; +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; diff --git a/migrations/2021-03-20-185321_move_matrix_id_to_person/up.sql b/migrations/2021-03-20-185321_move_matrix_id_to_person/up.sql index 9c7bff371e..d9ba3dd9c2 100644 --- a/migrations/2021-03-20-185321_move_matrix_id_to_person/up.sql +++ b/migrations/2021-03-20-185321_move_matrix_id_to_person/up.sql @@ -12,6 +12,6 @@ alter table local_user drop column matrix_user_id; alter table local_user drop column admin; -- Regenerate the person_alias views -drop view aliases::person_1, aliases::person_2; -create view aliases::person_1 as select * from person; -create view aliases::person_2 as select * from person; +drop view person_alias_1, person_alias_2; +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; diff --git a/migrations/2021-03-31-105915_add_bot_account/down.sql b/migrations/2021-03-31-105915_add_bot_account/down.sql index ecd6c4fb83..4f407ce862 100644 --- a/migrations/2021-03-31-105915_add_bot_account/down.sql +++ b/migrations/2021-03-31-105915_add_bot_account/down.sql @@ -1,6 +1,6 @@ -drop view aliases::person_1, aliases::person_2; +drop view person_alias_1, person_alias_2; alter table person drop column bot_account; -create view aliases::person_1 as select * from person; -create view aliases::person_2 as select * from person; +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; alter table local_user drop column show_bot_accounts; diff --git a/migrations/2021-03-31-105915_add_bot_account/up.sql b/migrations/2021-03-31-105915_add_bot_account/up.sql index 7458194703..a1e207a1d6 100644 --- a/migrations/2021-03-31-105915_add_bot_account/up.sql +++ b/migrations/2021-03-31-105915_add_bot_account/up.sql @@ -1,8 +1,8 @@ -- Add the bot_account column to the person table -drop view aliases::person_1, aliases::person_2; +drop view person_alias_1, person_alias_2; alter table person add column bot_account boolean not null default false; -create view aliases::person_1 as select * from person; -create view aliases::person_2 as select * from person; +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; -- Add the show_bot_accounts to the local user table as a setting alter table local_user add column show_bot_accounts boolean not null default true; diff --git a/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/down.sql b/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/down.sql index 4156f9e054..844c02d38b 100644 --- a/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/down.sql +++ b/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/down.sql @@ -1,6 +1,6 @@ alter table person rename display_name to preferred_username; -- Regenerate the person_alias views -drop view aliases::person_1, aliases::person_2; -create view aliases::person_1 as select * from person; -create view aliases::person_2 as select * from person; +drop view person_alias_1, person_alias_2; +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; diff --git a/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/up.sql b/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/up.sql index 336096181b..f4b9729c11 100644 --- a/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/up.sql +++ b/migrations/2021-04-01-173552_rename_preferred_username_to_display_name/up.sql @@ -1,6 +1,6 @@ alter table person rename preferred_username to display_name; -- Regenerate the person_alias views -drop view aliases::person_1, aliases::person_2; -create view aliases::person_1 as select * from person; -create view aliases::person_2 as select * from person; +drop view person_alias_1, person_alias_2; +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; diff --git a/migrations/2021-07-20-102033_actor_name_length/down.sql b/migrations/2021-07-20-102033_actor_name_length/down.sql index 25890b3a19..76cec4c91b 100644 --- a/migrations/2021-07-20-102033_actor_name_length/down.sql +++ b/migrations/2021-07-20-102033_actor_name_length/down.sql @@ -1,10 +1,10 @@ -DROP VIEW aliases::person_1; -DROP VIEW aliases::person_2; +DROP VIEW person_alias_1; +DROP VIEW person_alias_2; ALTER TABLE community ALTER COLUMN name TYPE varchar(20); ALTER TABLE community ALTER COLUMN title TYPE varchar(100); ALTER TABLE person ALTER COLUMN name TYPE varchar(20); ALTER TABLE person ALTER COLUMN display_name TYPE varchar(20); -create view aliases::person_1 as select * from person; -create view aliases::person_2 as select * from person; +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; diff --git a/migrations/2021-07-20-102033_actor_name_length/up.sql b/migrations/2021-07-20-102033_actor_name_length/up.sql index 1f73e59460..2e7bc9dfb2 100644 --- a/migrations/2021-07-20-102033_actor_name_length/up.sql +++ b/migrations/2021-07-20-102033_actor_name_length/up.sql @@ -1,10 +1,10 @@ -DROP VIEW aliases::person_1; -DROP VIEW aliases::person_2; +DROP VIEW person_alias_1; +DROP VIEW person_alias_2; ALTER TABLE community ALTER COLUMN name TYPE varchar(255); ALTER TABLE community ALTER COLUMN title TYPE varchar(255); ALTER TABLE person ALTER COLUMN name TYPE varchar(255); ALTER TABLE person ALTER COLUMN display_name TYPE varchar(255); -create view aliases::person_1 as select * from person; -create view aliases::person_2 as select * from person; +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; diff --git a/migrations/2021-12-14-181537_add_temporary_bans/down.sql b/migrations/2021-12-14-181537_add_temporary_bans/down.sql index efaf4f4818..83a3715ec4 100644 --- a/migrations/2021-12-14-181537_add_temporary_bans/down.sql +++ b/migrations/2021-12-14-181537_add_temporary_bans/down.sql @@ -1,7 +1,7 @@ -drop view aliases::person_1, aliases::person_2; +drop view person_alias_1, person_alias_2; alter table person drop column ban_expires; alter table community_person_ban drop column expires; -create view aliases::person_1 as select * from person; -create view aliases::person_2 as select * from person; +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; diff --git a/migrations/2021-12-14-181537_add_temporary_bans/up.sql b/migrations/2021-12-14-181537_add_temporary_bans/up.sql index a86d736fd6..7e6338361d 100644 --- a/migrations/2021-12-14-181537_add_temporary_bans/up.sql +++ b/migrations/2021-12-14-181537_add_temporary_bans/up.sql @@ -2,7 +2,7 @@ alter table person add column ban_expires timestamp; alter table community_person_ban add column expires timestamp; -drop view aliases::person_1, aliases::person_2; -create view aliases::person_1 as select * from person; -create view aliases::person_2 as select * from person; +drop view person_alias_1, person_alias_2; +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; diff --git a/migrations/2022-09-24-161829_remove_table_aliases/down.sql b/migrations/2022-09-24-161829_remove_table_aliases/down.sql index 393acca30b..39b439a0aa 100644 --- a/migrations/2022-09-24-161829_remove_table_aliases/down.sql +++ b/migrations/2022-09-24-161829_remove_table_aliases/down.sql @@ -1,2 +1,2 @@ -create view aliases::person_1 as select * from person; -create view aliases::person_2 as select * from person; +create view person_alias_1 as select * from person; +create view person_alias_2 as select * from person; diff --git a/migrations/2022-09-24-161829_remove_table_aliases/up.sql b/migrations/2022-09-24-161829_remove_table_aliases/up.sql index d47e592611..36eabecab7 100644 --- a/migrations/2022-09-24-161829_remove_table_aliases/up.sql +++ b/migrations/2022-09-24-161829_remove_table_aliases/up.sql @@ -1,2 +1,2 @@ -- Drop the alias views -drop view aliases::person_1, aliases::person_2; +drop view person_alias_1, person_alias_2; From 60dddd7ff9de26c65de3227f3f5da8aa90b22649 Mon Sep 17 00:00:00 2001 From: dull b Date: Wed, 19 Jul 2023 18:25:22 +0000 Subject: [PATCH 11/28] Add ReadFuture and ListFuture --- crates/db_schema/src/utils.rs | 5 +++++ crates/db_views/src/comment_report_view.rs | 18 ++++-------------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/crates/db_schema/src/utils.rs b/crates/db_schema/src/utils.rs index 94c867d6b8..d1fea023a0 100644 --- a/crates/db_schema/src/utils.rs +++ b/crates/db_schema/src/utils.rs @@ -2,6 +2,7 @@ use crate::{ diesel::Connection, diesel_migrations::MigrationHarness, newtypes::DbUrl, + traits::JoinView, CommentSortType, SortType, }; @@ -404,6 +405,10 @@ where } } +pub type ResultFuture<'a, T> = BoxFuture<'a, Result>; +pub type ReadFuture<'a, T> = ResultFuture<'a, ::JoinTuple>; +pub type ListFuture<'a, T> = ResultFuture<'a, Vec<::JoinTuple>>; + #[cfg(test)] mod tests { #![allow(clippy::unwrap_used)] diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index 30e4f517ab..da21aef193 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -11,7 +11,7 @@ use diesel::{ QueryDsl, }; use diesel_async::RunQueryDsl; -use futures::future::{BoxFuture, FutureExt}; +use futures::future::{FutureExt}; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, newtypes::{CommentReportId, CommunityId, PersonId}, @@ -34,23 +34,14 @@ use lemmy_db_schema::{ post::Post, }, traits::JoinView, - utils::{get_conn, limit_and_offset, DbConn, DbPool}, + utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFuture, ReadFuture}, }; - diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); fn queries<'a>() -> ( - impl Fn( - DbConn<'a>, - CommentReportId, - PersonId, - ) -> BoxFuture<'a, Result<::JoinTuple, Error>>, - impl Fn( - DbConn<'a>, - CommentReportQuery, - &'a Person, - ) -> BoxFuture<'a, Result::JoinTuple>, Error>>, + impl Fn(DbConn<'a>, CommentReportId, PersonId) -> ReadFuture<'a, CommentReportView>, + impl Fn(DbConn<'a>, CommentReportQuery, &'a Person) -> ListFuture<'a, CommentReportView>, ) { let full_query = move |query: comment_report::BoxedQuery<'static, Pg>, my_person_id: PersonId, @@ -130,7 +121,6 @@ fn queries<'a>() -> ( let (limit, offset) = limit_and_offset(options.page, options.limit)?; query = query - .order_by(comment_report::published.desc()) .limit(limit) .offset(offset); From 078385d07f0e0c86e898465d4d29c4f1ba31f4b5 Mon Sep 17 00:00:00 2001 From: dull b Date: Wed, 19 Jul 2023 19:20:25 +0000 Subject: [PATCH 12/28] Refactor queries function and add Queries struct --- crates/db_schema/src/utils.rs | 70 +++++++++++++++++++++- crates/db_views/src/comment_report_view.rs | 66 +++++++++----------- 2 files changed, 96 insertions(+), 40 deletions(-) diff --git a/crates/db_schema/src/utils.rs b/crates/db_schema/src/utils.rs index d1fea023a0..33237e4844 100644 --- a/crates/db_schema/src/utils.rs +++ b/crates/db_schema/src/utils.rs @@ -406,8 +406,74 @@ where } pub type ResultFuture<'a, T> = BoxFuture<'a, Result>; -pub type ReadFuture<'a, T> = ResultFuture<'a, ::JoinTuple>; -pub type ListFuture<'a, T> = ResultFuture<'a, Vec<::JoinTuple>>; + +pub trait ReadFn<'a, T: JoinView, Args>: + Fn(DbConn<'a>, Args) -> ResultFuture<'a, ::JoinTuple> +{ +} + +impl< + 'a, + T: JoinView, + Args, + F: Fn(DbConn<'a>, Args) -> ResultFuture<'a, ::JoinTuple>, + > ReadFn<'a, T, Args> for F +{ +} + +pub trait ListFn<'a, T: JoinView, Args>: + Fn(DbConn<'a>, Args) -> ResultFuture<'a, Vec<::JoinTuple>> +{ +} + +impl< + 'a, + T: JoinView, + Args, + F: Fn(DbConn<'a>, Args) -> ResultFuture<'a, Vec<::JoinTuple>>, + > ListFn<'a, T, Args> for F +{ +} + +/// Allows read and list functions to capture a shared closure that has an inferred return type, which is useful for join logic +pub struct Queries { + pub read_fn: RF, + pub list_fn: LF, +} + +impl Queries { + pub fn new(read_fn: RF, list_fn: LF) -> Self { + Queries { read_fn, list_fn } + } + + pub async fn read<'a, T, Args>( + self, + pool: &'a mut DbPool<'_>, + args: Args, + ) -> Result + where + T: JoinView, + RF: ReadFn<'a, T, Args>, + { + let conn = get_conn(pool).await?; + let res = (self.read_fn)(conn, args).await?; + Ok(T::from_tuple(res)) + } + + pub async fn list<'a, T, Args>( + self, + pool: &'a mut DbPool<'_>, + args: Args, + ) -> Result, DieselError> + where + T: JoinView, + LF: ListFn<'a, T, Args>, + { + let conn = get_conn(pool).await?; + let res = (self.list_fn)(conn, args).await?; + Ok(res.into_iter().map(T::from_tuple).collect()) + } +} #[cfg(test)] mod tests { diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index da21aef193..fd30b63b64 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -11,7 +11,7 @@ use diesel::{ QueryDsl, }; use diesel_async::RunQueryDsl; -use futures::future::{FutureExt}; +use futures::future::FutureExt; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, newtypes::{CommentReportId, CommunityId, PersonId}, @@ -34,18 +34,18 @@ use lemmy_db_schema::{ post::Post, }, traits::JoinView, - utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFuture, ReadFuture}, + utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, }; diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); -fn queries<'a>() -> ( - impl Fn(DbConn<'a>, CommentReportId, PersonId) -> ReadFuture<'a, CommentReportView>, - impl Fn(DbConn<'a>, CommentReportQuery, &'a Person) -> ListFuture<'a, CommentReportView>, -) { - let full_query = move |query: comment_report::BoxedQuery<'static, Pg>, - my_person_id: PersonId, - include_expired: bool| { +fn queries<'a>() -> Queries< + impl ReadFn<'a, CommentReportView, (CommentReportId, PersonId)>, + impl ListFn<'a, CommentReportView, (CommentReportQuery, &'a Person)>, +> { + let all_joins = move |query: comment_report::BoxedQuery<'static, Pg>, + my_person_id: PersonId, + include_expired: bool| { query .inner_join(comment::table.on(comment_report::comment_id.eq(comment::id))) .inner_join(post::table.on(comment::post_id.eq(post::id))) @@ -93,22 +93,20 @@ fn queries<'a>() -> ( person_alias_2.fields(person::all_columns).nullable(), )) }; - let read = move |mut conn: DbConn<'a>, report_id: CommentReportId, my_person_id: PersonId| { - async move { - let res = full_query( - comment_report::table.find(report_id).into_boxed(), - my_person_id, - true, - ) - .first::<::JoinTuple>(&mut conn) - .await?; - Ok::<_, Error>(res) - } + + let read = move |mut conn: DbConn<'a>, (report_id, my_person_id): (CommentReportId, PersonId)| { + all_joins( + comment_report::table.find(report_id).into_boxed(), + my_person_id, + true, + ) + .first::<::JoinTuple>(&mut conn) .boxed() }; - let list = move |mut conn: DbConn<'a>, options: CommentReportQuery, my_person: &'a Person| { + + let list = move |mut conn: DbConn<'a>, (options, my_person): (CommentReportQuery, &'a Person)| { async move { - let mut query = full_query(comment_report::table.into_boxed(), my_person.id, false); + let mut query = all_joins(comment_report::table.into_boxed(), my_person.id, false); if let Some(community_id) = options.community_id { query = query.filter(post::community_id.eq(community_id)); @@ -126,7 +124,7 @@ fn queries<'a>() -> ( .offset(offset); // If its not an admin, get only the ones you mod - let res = if !my_person.admin { + if !my_person.admin { query .inner_join( community_moderator::table.on( @@ -136,17 +134,17 @@ fn queries<'a>() -> ( ), ) .load::<::JoinTuple>(&mut conn) - .await? + .await } else { query .load::<::JoinTuple>(&mut conn) - .await? - }; - Ok::<_, Error>(res) + .await + } } .boxed() }; - (read, list) + + Queries::new(read, list) } impl CommentReportView { @@ -158,11 +156,7 @@ impl CommentReportView { report_id: CommentReportId, my_person_id: PersonId, ) -> Result { - let conn = get_conn(pool).await?; - - let res = (queries().0)(conn, report_id, my_person_id).await?; - - Ok(Self::from_tuple(res)) + queries().read(pool, (report_id, my_person_id)).await } /// Returns the current unresolved post report count for the communities you mod @@ -222,11 +216,7 @@ impl CommentReportQuery { pool: &mut DbPool<'_>, my_person: &Person, ) -> Result, Error> { - let conn = get_conn(pool).await?; - - let res = (queries().1)(conn, self, my_person).await?; - - Ok(res.into_iter().map(CommentReportView::from_tuple).collect()) + queries().list(pool, (self, my_person)).await } } From 4e006521859d0ea9b9a3f26fc8342e5ac1db42d7 Mon Sep 17 00:00:00 2001 From: dull b Date: Wed, 19 Jul 2023 20:08:17 +0000 Subject: [PATCH 13/28] Box futures in Queries::new --- Cargo.lock | 1 - crates/db_schema/src/utils.rs | 26 ++++++-- crates/db_views/Cargo.toml | 1 - crates/db_views/src/comment_report_view.rs | 76 ++++++++++------------ 4 files changed, 58 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 19c79c9d2c..407cb9aae2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2762,7 +2762,6 @@ dependencies = [ "diesel", "diesel-async", "diesel_ltree", - "futures", "lemmy_db_schema", "serde", "serde_with", diff --git a/crates/db_schema/src/utils.rs b/crates/db_schema/src/utils.rs index 33237e4844..d8f52984ad 100644 --- a/crates/db_schema/src/utils.rs +++ b/crates/db_schema/src/utils.rs @@ -26,7 +26,7 @@ use diesel_async::{ }, }; use diesel_migrations::EmbeddedMigrations; -use futures_util::{future::BoxFuture, FutureExt}; +use futures_util::{future::BoxFuture, Future, FutureExt}; use lemmy_utils::{ error::{LemmyError, LemmyErrorExt, LemmyErrorType}, settings::structs::Settings, @@ -441,11 +441,29 @@ pub struct Queries { pub list_fn: LF, } -impl Queries { - pub fn new(read_fn: RF, list_fn: LF) -> Self { - Queries { read_fn, list_fn } +// `()` is used to prevent type inference error +impl Queries<(), ()> { + pub fn new<'a, RFut, LFut, RT, LT, RA, LA, RF2, LF2>( + read_fn: RF2, + list_fn: LF2, + ) -> Queries, impl ListFn<'a, LT, LA>> + where + RFut: Future::JoinTuple, DieselError>> + Sized + Send + 'a, + LFut: + Future::JoinTuple>, DieselError>> + Sized + Send + 'a, + RT: JoinView, + LT: JoinView, + RF2: Fn(DbConn<'a>, RA) -> RFut, + LF2: Fn(DbConn<'a>, LA) -> LFut, + { + Queries { + read_fn: move |conn, args| read_fn(conn, args).boxed(), + list_fn: move |conn, args| list_fn(conn, args).boxed(), + } } +} +impl Queries { pub async fn read<'a, T, Args>( self, pool: &'a mut DbPool<'_>, diff --git a/crates/db_views/Cargo.toml b/crates/db_views/Cargo.toml index 21c68069c3..54a3aab6c4 100644 --- a/crates/db_views/Cargo.toml +++ b/crates/db_views/Cargo.toml @@ -30,7 +30,6 @@ serde = { workspace = true } serde_with = { workspace = true } tracing = { workspace = true, optional = true } ts-rs = { workspace = true, optional = true } -futures = { workspace = true } [dev-dependencies] serial_test = { workspace = true } diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index fd30b63b64..b7d9aba712 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -11,7 +11,6 @@ use diesel::{ QueryDsl, }; use diesel_async::RunQueryDsl; -use futures::future::FutureExt; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, newtypes::{CommentReportId, CommunityId, PersonId}, @@ -94,54 +93,51 @@ fn queries<'a>() -> Queries< )) }; - let read = move |mut conn: DbConn<'a>, (report_id, my_person_id): (CommentReportId, PersonId)| { + let read = move |mut conn: DbConn<'a>, (report_id, my_person_id): (CommentReportId, PersonId)| async move { all_joins( comment_report::table.find(report_id).into_boxed(), my_person_id, true, ) .first::<::JoinTuple>(&mut conn) - .boxed() + .await }; - let list = move |mut conn: DbConn<'a>, (options, my_person): (CommentReportQuery, &'a Person)| { - async move { - let mut query = all_joins(comment_report::table.into_boxed(), my_person.id, false); - - if let Some(community_id) = options.community_id { - query = query.filter(post::community_id.eq(community_id)); - } - - if options.unresolved_only.unwrap_or(false) { - query = query.filter(comment_report::resolved.eq(false)); - } - - let (limit, offset) = limit_and_offset(options.page, options.limit)?; - - query = query - .order_by(comment_report::published.desc()) - .limit(limit) - .offset(offset); - - // If its not an admin, get only the ones you mod - if !my_person.admin { - query - .inner_join( - community_moderator::table.on( - community_moderator::community_id - .eq(post::community_id) - .and(community_moderator::person_id.eq(my_person.id)), - ), - ) - .load::<::JoinTuple>(&mut conn) - .await - } else { - query - .load::<::JoinTuple>(&mut conn) - .await - } + let list = move |mut conn: DbConn<'a>, (options, my_person): (CommentReportQuery, &'a Person)| async move { + let mut query = all_joins(comment_report::table.into_boxed(), my_person.id, false); + + if let Some(community_id) = options.community_id { + query = query.filter(post::community_id.eq(community_id)); + } + + if options.unresolved_only.unwrap_or(false) { + query = query.filter(comment_report::resolved.eq(false)); + } + + let (limit, offset) = limit_and_offset(options.page, options.limit)?; + + query = query + .order_by(comment_report::published.desc()) + .limit(limit) + .offset(offset); + + // If its not an admin, get only the ones you mod + if !my_person.admin { + query + .inner_join( + community_moderator::table.on( + community_moderator::community_id + .eq(post::community_id) + .and(community_moderator::person_id.eq(my_person.id)), + ), + ) + .load::<::JoinTuple>(&mut conn) + .await + } else { + query + .load::<::JoinTuple>(&mut conn) + .await } - .boxed() }; Queries::new(read, list) From d472aeee25c98fdd018b43d5ff487afb052bb942 Mon Sep 17 00:00:00 2001 From: dull b Date: Wed, 19 Jul 2023 20:44:42 +0000 Subject: [PATCH 14/28] Use from_tuple --- crates/db_views/src/comment_view.rs | 36 +++---------- crates/db_views/src/local_user_view.rs | 40 ++++---------- crates/db_views/src/post_report_view.rs | 26 +-------- crates/db_views/src/post_view.rs | 38 +++---------- .../src/private_message_report_view.rs | 53 ++++++++----------- crates/db_views/src/private_message_view.rs | 8 +-- .../src/registration_application_view.rs | 46 +++++++--------- .../db_views_actor/src/comment_reply_view.rs | 30 +---------- crates/db_views_actor/src/community_view.rs | 11 ++-- .../db_views_actor/src/person_mention_view.rs | 30 +---------- 10 files changed, 74 insertions(+), 244 deletions(-) diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 9a9685c79e..faeff7617c 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -65,18 +65,7 @@ impl CommentView { // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); - let ( - comment, - creator, - post, - community, - counts, - creator_banned_from_community, - follower, - saved, - creator_blocked, - comment_like, - ) = comment::table + let mut res = comment::table .find(comment_id) .inner_join(person::table) .inner_join(post::table) @@ -132,26 +121,13 @@ impl CommentView { .first::(conn) .await?; - // If a person is given, then my_vote, if None, should be 0, not null + // If a person is given, then my_vote (res.9), if None, should be 0, not null // Necessary to differentiate between other person's votes - let my_vote = if my_person_id.is_some() && comment_like.is_none() { - Some(0) - } else { - comment_like - }; + if my_person_id.is_some() && res.9.is_none() { + res.9 = Some(0); + } - Ok(CommentView { - comment, - post, - creator, - community, - counts, - creator_banned_from_community: creator_banned_from_community.is_some(), - subscribed: CommunityFollower::to_subscribed_type(&follower), - saved: saved.is_some(), - creator_blocked: creator_blocked.is_some(), - my_vote, - }) + Ok(Self::from_tuple(res)) } } diff --git a/crates/db_views/src/local_user_view.rs b/crates/db_views/src/local_user_view.rs index 567ca3feb3..b8b7f5d7c8 100644 --- a/crates/db_views/src/local_user_view.rs +++ b/crates/db_views/src/local_user_view.rs @@ -16,7 +16,7 @@ impl LocalUserView { pub async fn read(pool: &mut DbPool<'_>, local_user_id: LocalUserId) -> Result { let conn = &mut get_conn(pool).await?; - let (local_user, person, counts) = local_user::table + let res = local_user::table .find(local_user_id) .inner_join(person::table) .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) @@ -27,16 +27,12 @@ impl LocalUserView { )) .first::(conn) .await?; - Ok(Self { - local_user, - person, - counts, - }) + Ok(Self::from_tuple(res)) } pub async fn read_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { let conn = &mut get_conn(pool).await?; - let (local_user, person, counts) = local_user::table + let res = local_user::table .filter(person::id.eq(person_id)) .inner_join(person::table) .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) @@ -47,16 +43,12 @@ impl LocalUserView { )) .first::(conn) .await?; - Ok(Self { - local_user, - person, - counts, - }) + Ok(Self::from_tuple(res)) } pub async fn read_from_name(pool: &mut DbPool<'_>, name: &str) -> Result { let conn = &mut get_conn(pool).await?; - let (local_user, person, counts) = local_user::table + let res = local_user::table .filter(lower(person::name).eq(name.to_lowercase())) .inner_join(person::table) .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) @@ -67,11 +59,7 @@ impl LocalUserView { )) .first::(conn) .await?; - Ok(Self { - local_user, - person, - counts, - }) + Ok(Self::from_tuple(res)) } pub async fn find_by_email_or_name( @@ -79,7 +67,7 @@ impl LocalUserView { name_or_email: &str, ) -> Result { let conn = &mut get_conn(pool).await?; - let (local_user, person, counts) = local_user::table + let res = local_user::table .inner_join(person::table) .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) .filter( @@ -94,16 +82,12 @@ impl LocalUserView { )) .first::(conn) .await?; - Ok(Self { - local_user, - person, - counts, - }) + Ok(Self::from_tuple(res)) } pub async fn find_by_email(pool: &mut DbPool<'_>, from_email: &str) -> Result { let conn = &mut get_conn(pool).await?; - let (local_user, person, counts) = local_user::table + let res = local_user::table .inner_join(person::table) .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) .filter(local_user::email.eq(from_email)) @@ -114,11 +98,7 @@ impl LocalUserView { )) .first::(conn) .await?; - Ok(Self { - local_user, - person, - counts, - }) + Ok(Self::from_tuple(res)) } pub async fn list_admins_with_emails(pool: &mut DbPool<'_>) -> Result, Error> { diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index 50b35b1c25..d5b544480a 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -55,17 +55,7 @@ impl PostReportView { let conn = &mut get_conn(pool).await?; let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - let ( - post_report, - post, - community, - creator, - post_creator, - creator_banned_from_community, - post_like, - counts, - resolver, - ) = post_report::table + let res = post_report::table .find(report_id) .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) @@ -103,19 +93,7 @@ impl PostReportView { .first::(conn) .await?; - let my_vote = post_like; - - Ok(Self { - post_report, - post, - community, - creator, - post_creator, - creator_banned_from_community: creator_banned_from_community.is_some(), - my_vote, - counts, - resolver, - }) + Ok(Self::from_tuple(res)) } /// returns the current unresolved post report count for the communities you mod diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 48faeca285..a75203d8cb 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -155,41 +155,15 @@ impl PostView { .filter(post::deleted.eq(false)); } - let ( - post, - creator, - community, - creator_banned_from_community, - counts, - follower, - saved, - read, - creator_blocked, - post_like, - unread_comments, - ) = query.first::(conn).await?; - - // If a person is given, then my_vote, if None, should be 0, not null + let mut res = query.first::(conn).await?; + + // If a person is given, then my_vote (res.9), if None, should be 0, not null // Necessary to differentiate between other person's votes - let my_vote = if my_person_id.is_some() && post_like.is_none() { - Some(0) - } else { - post_like + if my_person_id.is_some() && res.9.is_none() { + res.9 = Some(0) }; - Ok(PostView { - post, - creator, - community, - creator_banned_from_community: creator_banned_from_community.is_some(), - counts, - subscribed: CommunityFollower::to_subscribed_type(&follower), - saved: saved.is_some(), - read: read.is_some(), - creator_blocked: creator_blocked.is_some(), - my_vote, - unread_comments, - }) + Ok(Self::from_tuple(res)) } } diff --git a/crates/db_views/src/private_message_report_view.rs b/crates/db_views/src/private_message_report_view.rs index 7ceca271ab..dd9adb42be 100644 --- a/crates/db_views/src/private_message_report_view.rs +++ b/crates/db_views/src/private_message_report_view.rs @@ -32,37 +32,28 @@ impl PrivateMessageReportView { let conn = &mut get_conn(pool).await?; let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - let (private_message_report, private_message, private_message_creator, creator, resolver) = - private_message_report::table - .find(report_id) - .inner_join(private_message::table) - .inner_join(person::table.on(private_message::creator_id.eq(person::id))) - .inner_join( - person_alias_1 - .on(private_message_report::creator_id.eq(person_alias_1.field(person::id))), - ) - .left_join( - person_alias_2.on( - private_message_report::resolver_id.eq(person_alias_2.field(person::id).nullable()), - ), - ) - .select(( - private_message_report::all_columns, - private_message::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - person_alias_2.fields(person::all_columns).nullable(), - )) - .first::(conn) - .await?; - - Ok(Self { - private_message_report, - private_message, - private_message_creator, - creator, - resolver, - }) + let res = private_message_report::table + .find(report_id) + .inner_join(private_message::table) + .inner_join(person::table.on(private_message::creator_id.eq(person::id))) + .inner_join( + person_alias_1.on(private_message_report::creator_id.eq(person_alias_1.field(person::id))), + ) + .left_join( + person_alias_2 + .on(private_message_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + ) + .select(( + private_message_report::all_columns, + private_message::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), + person_alias_2.fields(person::all_columns).nullable(), + )) + .first::(conn) + .await?; + + Ok(Self::from_tuple(res)) } /// Returns the current unresolved post report count for the communities you mod diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index 863db8125e..fd9bc739b3 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -28,7 +28,7 @@ impl PrivateMessageView { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); - let (private_message, creator, recipient) = private_message::table + let res = private_message::table .find(private_message_id) .inner_join(person::table.on(private_message::creator_id.eq(person::id))) .inner_join( @@ -43,11 +43,7 @@ impl PrivateMessageView { .first::(conn) .await?; - Ok(PrivateMessageView { - private_message, - creator, - recipient, - }) + Ok(Self::from_tuple(res)) } /// Gets the number of unread messages diff --git a/crates/db_views/src/registration_application_view.rs b/crates/db_views/src/registration_application_view.rs index 106e41e433..f4b6eba593 100644 --- a/crates/db_views/src/registration_application_view.rs +++ b/crates/db_views/src/registration_application_view.rs @@ -30,33 +30,25 @@ impl RegistrationApplicationView { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); - let (registration_application, creator_local_user, creator, admin) = - registration_application::table - .find(registration_application_id) - .inner_join( - local_user::table.on(registration_application::local_user_id.eq(local_user::id)), - ) - .inner_join(person::table.on(local_user::person_id.eq(person::id))) - .left_join( - person_alias_1 - .on(registration_application::admin_id.eq(person_alias_1.field(person::id).nullable())), - ) - .order_by(registration_application::published.desc()) - .select(( - registration_application::all_columns, - local_user::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns).nullable(), - )) - .first::(conn) - .await?; - - Ok(RegistrationApplicationView { - registration_application, - creator_local_user, - creator, - admin, - }) + let res = registration_application::table + .find(registration_application_id) + .inner_join(local_user::table.on(registration_application::local_user_id.eq(local_user::id))) + .inner_join(person::table.on(local_user::person_id.eq(person::id))) + .left_join( + person_alias_1 + .on(registration_application::admin_id.eq(person_alias_1.field(person::id).nullable())), + ) + .order_by(registration_application::published.desc()) + .select(( + registration_application::all_columns, + local_user::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns).nullable(), + )) + .first::(conn) + .await?; + + Ok(Self::from_tuple(res)) } /// Returns the current unread registration_application count diff --git a/crates/db_views_actor/src/comment_reply_view.rs b/crates/db_views_actor/src/comment_reply_view.rs index 4d7a8eac40..3608ab25ee 100644 --- a/crates/db_views_actor/src/comment_reply_view.rs +++ b/crates/db_views_actor/src/comment_reply_view.rs @@ -64,20 +64,7 @@ impl CommentReplyView { // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); - let ( - comment_reply, - comment, - creator, - post, - community, - recipient, - counts, - creator_banned_from_community, - follower, - saved, - creator_blocked, - my_vote, - ) = comment_reply::table + let res = comment_reply::table .find(comment_reply_id) .inner_join(comment::table) .inner_join(person::table.on(comment::creator_id.eq(person::id))) @@ -137,20 +124,7 @@ impl CommentReplyView { .first::(conn) .await?; - Ok(CommentReplyView { - comment_reply, - comment, - creator, - post, - community, - recipient, - counts, - creator_banned_from_community: creator_banned_from_community.is_some(), - subscribed: CommunityFollower::to_subscribed_type(&follower), - saved: saved.is_some(), - creator_blocked: creator_blocked.is_some(), - my_vote, - }) + Ok(Self::from_tuple(res)) } /// Gets the number of unread replies diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index 64dc090903..1a4afe7f60 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -74,14 +74,9 @@ impl CommunityView { .filter(community::deleted.eq(false)); } - let (community, counts, follower, blocked) = query.first::(conn).await?; - - Ok(CommunityView { - community, - subscribed: CommunityFollower::to_subscribed_type(&follower), - blocked: blocked.is_some(), - counts, - }) + let res = query.first::(conn).await?; + + Ok(Self::from_tuple(res)) } pub async fn is_mod_or_admin( diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index 3e142254ab..a69bb227e6 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -65,20 +65,7 @@ impl PersonMentionView { // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); - let ( - person_mention, - comment, - creator, - post, - community, - recipient, - counts, - creator_banned_from_community, - follower, - saved, - creator_blocked, - my_vote, - ) = person_mention::table + let res = person_mention::table .find(person_mention_id) .inner_join(comment::table) .inner_join(person::table.on(comment::creator_id.eq(person::id))) @@ -138,20 +125,7 @@ impl PersonMentionView { .first::(conn) .await?; - Ok(PersonMentionView { - person_mention, - comment, - creator, - post, - community, - recipient, - counts, - creator_banned_from_community: creator_banned_from_community.is_some(), - subscribed: CommunityFollower::to_subscribed_type(&follower), - saved: saved.is_some(), - creator_blocked: creator_blocked.is_some(), - my_vote, - }) + Ok(Self::from_tuple(res)) } /// Gets the number of unread mentions From d2d08ddb3bf6b1a8897f3afe49a55662b3927b45 Mon Sep 17 00:00:00 2001 From: dull b Date: Thu, 20 Jul 2023 02:52:52 +0000 Subject: [PATCH 15/28] Add comment_view::queries and improve comment_report_view::queries --- crates/db_schema/src/aliases.rs | 3 + crates/db_schema/src/lib.rs | 2 + crates/db_views/src/comment_report_view.rs | 21 +- crates/db_views/src/comment_view.rs | 232 +++++++++------------ 4 files changed, 115 insertions(+), 143 deletions(-) create mode 100644 crates/db_schema/src/aliases.rs diff --git a/crates/db_schema/src/aliases.rs b/crates/db_schema/src/aliases.rs new file mode 100644 index 0000000000..3b97521328 --- /dev/null +++ b/crates/db_schema/src/aliases.rs @@ -0,0 +1,3 @@ +use crate::schema::person; + +diesel::alias!(person as person1: Person1, person as person2: Person2); diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index acb069ca7d..badb083d8b 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -28,6 +28,8 @@ pub mod newtypes; #[rustfmt::skip] #[allow(clippy::wildcard_imports)] pub mod schema; +#[cfg(feature = "full")] +pub mod aliases; pub mod source; #[cfg(feature = "full")] pub mod traits; diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index b7d9aba712..f0f7f2762f 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -13,6 +13,7 @@ use diesel::{ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, + aliases, newtypes::{CommentReportId, CommunityId, PersonId}, schema::{ comment, @@ -36,21 +37,19 @@ use lemmy_db_schema::{ utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, }; -diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2); - fn queries<'a>() -> Queries< impl ReadFn<'a, CommentReportView, (CommentReportId, PersonId)>, impl ListFn<'a, CommentReportView, (CommentReportQuery, &'a Person)>, > { - let all_joins = move |query: comment_report::BoxedQuery<'static, Pg>, - my_person_id: PersonId, - include_expired: bool| { + let all_joins = |query: comment_report::BoxedQuery<'static, Pg>, + my_person_id: PersonId, + include_expired: bool| { query - .inner_join(comment::table.on(comment_report::comment_id.eq(comment::id))) + .inner_join(comment::table) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(person::table.on(comment_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join(aliases::person1.on(comment::creator_id.eq(aliases::person1.field(person::id)))) .inner_join( comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), ) @@ -76,8 +75,8 @@ fn queries<'a>() -> Queries< ), ) .left_join( - person_alias_2 - .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + aliases::person2 + .on(comment_report::resolver_id.eq(aliases::person2.field(person::id).nullable())), ) .select(( comment_report::all_columns, @@ -85,11 +84,11 @@ fn queries<'a>() -> Queries< post::all_columns, community::all_columns, person::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), comment_like::score.nullable(), - person_alias_2.fields(person::all_columns).nullable(), + aliases::person2.fields(person::all_columns).nullable(), )) }; diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index faeff7617c..83e02cc730 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -1,5 +1,6 @@ use crate::structs::CommentView; use diesel::{ + pg::Pg, result::Error, BoolExpressionMethods, ExpressionMethods, @@ -36,7 +37,7 @@ use lemmy_db_schema::{ post::Post, }, traits::JoinView, - utils::{fuzzy_search, get_conn, limit_and_offset, DbPool}, + utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, CommentSortType, ListingType, }; @@ -54,19 +55,14 @@ type CommentViewTuple = ( Option, ); -impl CommentView { - pub async fn read( - pool: &mut DbPool<'_>, - comment_id: CommentId, - my_person_id: Option, - ) -> Result { - let conn = &mut get_conn(pool).await?; - +fn queries<'a>() -> Queries< + impl ReadFn<'a, CommentView, (CommentId, Option)>, + impl ListFn<'a, CommentView, CommentQuery<'a>>, +> { + let all_joins = |query: comment::BoxedQuery<'static, Pg>, my_person_id: Option| { // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); - - let mut res = comment::table - .find(comment_id) + query .inner_join(person::table) .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) @@ -106,89 +102,37 @@ impl CommentView { .and(comment_like::person_id.eq(person_id_join)), ), ) - .select(( - comment::all_columns, - person::all_columns, - post::all_columns, - community::all_columns, - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - community_follower::all_columns.nullable(), - comment_saved::all_columns.nullable(), - person_block::all_columns.nullable(), - comment_like::score.nullable(), - )) - .first::(conn) - .await?; - - // If a person is given, then my_vote (res.9), if None, should be 0, not null - // Necessary to differentiate between other person's votes - if my_person_id.is_some() && res.9.is_none() { - res.9 = Some(0); - } - - Ok(Self::from_tuple(res)) - } -} - -#[derive(Default)] -pub struct CommentQuery<'a> { - pub listing_type: Option, - pub sort: Option, - pub community_id: Option, - pub post_id: Option, - pub parent_path: Option, - pub creator_id: Option, - pub local_user: Option<&'a LocalUser>, - pub search_term: Option, - pub saved_only: Option, - pub show_deleted_and_removed: Option, - pub page: Option, - pub limit: Option, - pub max_depth: Option, -} + }; + let selection = ( + comment::all_columns, + person::all_columns, + post::all_columns, + community::all_columns, + comment_aggregates::all_columns, + community_person_ban::all_columns.nullable(), + community_follower::all_columns.nullable(), + comment_saved::all_columns.nullable(), + person_block::all_columns.nullable(), + comment_like::score.nullable(), + ); + + let read = move |mut conn: DbConn<'a>, + (comment_id, my_person_id): (CommentId, Option)| async move { + all_joins(comment::table.find(comment_id).into_boxed(), my_person_id) + .select(selection) + .first::(&mut conn) + .await + }; -impl<'a> CommentQuery<'a> { - pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { - let conn = &mut get_conn(pool).await?; + let list = move |mut conn: DbConn<'a>, options: CommentQuery<'a>| async move { + let person_id = options.local_user.map(|l| l.person_id); + let local_user_id = options.local_user.map(|l| l.id); // The left join below will return None in this case - let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); - let local_user_id_join = self.local_user.map(|l| l.id).unwrap_or(LocalUserId(-1)); + let person_id_join = person_id.unwrap_or(PersonId(-1)); + let local_user_id_join = local_user_id.unwrap_or(LocalUserId(-1)); - let mut query = comment::table - .inner_join(person::table) - .inner_join(post::table) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(comment_aggregates::table) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)), - ), - ) - .left_join( - community_follower::table.on( - post::community_id - .eq(community_follower::community_id) - .and(community_follower::person_id.eq(person_id_join)), - ), - ) - .left_join( - comment_saved::table.on( - comment::id - .eq(comment_saved::comment_id) - .and(comment_saved::person_id.eq(person_id_join)), - ), - ) - .left_join( - person_block::table.on( - comment::creator_id - .eq(person_block::target_id) - .and(person_block::person_id.eq(person_id_join)), - ), - ) + let mut query = all_joins(comment::table.into_boxed(), person_id) .left_join( community_block::table.on( community::id @@ -196,13 +140,6 @@ impl<'a> CommentQuery<'a> { .and(community_block::person_id.eq(person_id_join)), ), ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(person_id_join)), - ), - ) .left_join( local_user_language::table.on( comment::language_id @@ -210,41 +147,29 @@ impl<'a> CommentQuery<'a> { .and(local_user_language::local_user_id.eq(local_user_id_join)), ), ) - .select(( - comment::all_columns, - person::all_columns, - post::all_columns, - community::all_columns, - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - community_follower::all_columns.nullable(), - comment_saved::all_columns.nullable(), - person_block::all_columns.nullable(), - comment_like::score.nullable(), - )) - .into_boxed(); - - if let Some(creator_id) = self.creator_id { + .select(selection); + + if let Some(creator_id) = options.creator_id { query = query.filter(comment::creator_id.eq(creator_id)); }; - if let Some(post_id) = self.post_id { + if let Some(post_id) = options.post_id { query = query.filter(comment::post_id.eq(post_id)); }; - if let Some(parent_path) = self.parent_path.as_ref() { + if let Some(parent_path) = options.parent_path.as_ref() { query = query.filter(comment::path.contained_by(parent_path)); }; - if let Some(search_term) = self.search_term { + if let Some(search_term) = options.search_term { query = query.filter(comment::content.ilike(fuzzy_search(&search_term))); }; - if let Some(community_id) = self.community_id { + if let Some(community_id) = options.community_id { query = query.filter(post::community_id.eq(community_id)); } - if let Some(listing_type) = self.listing_type { + if let Some(listing_type) = options.listing_type { match listing_type { ListingType::Subscribed => { query = query.filter(community_follower::person_id.is_not_null()) @@ -266,33 +191,37 @@ impl<'a> CommentQuery<'a> { } } - if self.saved_only.unwrap_or(false) { + if options.saved_only.unwrap_or(false) { query = query.filter(comment_saved::comment_id.is_not_null()); } - if !self.show_deleted_and_removed.unwrap_or(true) { + if !options.show_deleted_and_removed.unwrap_or(true) { query = query.filter(comment::deleted.eq(false)); query = query.filter(comment::removed.eq(false)); } - if !self.local_user.map(|l| l.show_bot_accounts).unwrap_or(true) { + if !options + .local_user + .map(|l| l.show_bot_accounts) + .unwrap_or(true) + { query = query.filter(person::bot_account.eq(false)); }; - if self.local_user.is_some() { + if options.local_user.is_some() { // Filter out the rows with missing languages query = query.filter(local_user_language::language_id.is_not_null()); // Don't show blocked communities or persons - if self.post_id.is_none() { + if options.post_id.is_none() { query = query.filter(community_block::person_id.is_null()); } query = query.filter(person_block::person_id.is_null()); } // A Max depth given means its a tree fetch - let (limit, offset) = if let Some(max_depth) = self.max_depth { - let depth_limit = if let Some(parent_path) = self.parent_path.as_ref() { + let (limit, offset) = if let Some(max_depth) = options.max_depth { + let depth_limit = if let Some(parent_path) = options.parent_path.as_ref() { parent_path.0.split('.').count() as i32 + max_depth // Add one because of root "0" } else { @@ -316,11 +245,11 @@ impl<'a> CommentQuery<'a> { // (i64::MAX, 0) (300, 0) } else { - // limit_and_offset_unlimited(self.page, self.limit) - limit_and_offset(self.page, self.limit)? + // limit_and_offset_unlimited(options.page, options.limit) + limit_and_offset(options.page, options.limit)? }; - query = match self.sort.unwrap_or(CommentSortType::Hot) { + query = match options.sort.unwrap_or(CommentSortType::Hot) { CommentSortType::Hot => query.then_order_by(comment_aggregates::hot_rank.desc()), CommentSortType::New => query.then_order_by(comment::published.desc()), CommentSortType::Old => query.then_order_by(comment::published.asc()), @@ -328,13 +257,52 @@ impl<'a> CommentQuery<'a> { }; // Note: deleted and removed comments are done on the front side - let res = query + query .limit(limit) .offset(offset) - .load::(conn) - .await?; + .load::(&mut conn) + .await + }; + + Queries::new(read, list) +} + +impl CommentView { + pub async fn read( + pool: &mut DbPool<'_>, + comment_id: CommentId, + my_person_id: Option, + ) -> Result { + // If a person is given, then my_vote (res.9), if None, should be 0, not null + // Necessary to differentiate between other person's votes + let mut res = queries().read(pool, (comment_id, my_person_id)).await?; + if my_person_id.is_some() && res.my_vote.is_none() { + res.my_vote = Some(0); + } + Ok(res) + } +} + +#[derive(Default)] +pub struct CommentQuery<'a> { + pub listing_type: Option, + pub sort: Option, + pub community_id: Option, + pub post_id: Option, + pub parent_path: Option, + pub creator_id: Option, + pub local_user: Option<&'a LocalUser>, + pub search_term: Option, + pub saved_only: Option, + pub show_deleted_and_removed: Option, + pub page: Option, + pub limit: Option, + pub max_depth: Option, +} - Ok(res.into_iter().map(CommentView::from_tuple).collect()) +impl<'a> CommentQuery<'a> { + pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { + queries().list(pool, self).await } } From 5874aec90519f0b97252c8a0fd4a09a6f1a11284 Mon Sep 17 00:00:00 2001 From: dull b Date: Thu, 20 Jul 2023 20:30:52 +0000 Subject: [PATCH 16/28] Add local_user_view::queries --- crates/db_views/src/comment_view.rs | 1 + crates/db_views/src/local_user_view.rs | 158 +++++++++++-------------- 2 files changed, 73 insertions(+), 86 deletions(-) diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 83e02cc730..1da4b87dc9 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -103,6 +103,7 @@ fn queries<'a>() -> Queries< ), ) }; + let selection = ( comment::all_columns, person::all_columns, diff --git a/crates/db_views/src/local_user_view.rs b/crates/db_views/src/local_user_view.rs index b8b7f5d7c8..23a8b8f051 100644 --- a/crates/db_views/src/local_user_view.rs +++ b/crates/db_views/src/local_user_view.rs @@ -7,116 +7,102 @@ use lemmy_db_schema::{ schema::{local_user, person, person_aggregates}, source::{local_user::LocalUser, person::Person}, traits::JoinView, - utils::{functions::lower, get_conn, DbPool}, + utils::{functions::lower, DbConn, DbPool, ListFn, Queries, ReadFn}, }; type LocalUserViewTuple = (LocalUser, Person, PersonAggregates); -impl LocalUserView { - pub async fn read(pool: &mut DbPool<'_>, local_user_id: LocalUserId) -> Result { - let conn = &mut get_conn(pool).await?; +enum ReadBy<'a> { + Id(LocalUserId), + Person(PersonId), + Name(&'a str), + NameOrEmail(&'a str), + Email(&'a str), +} - let res = local_user::table - .find(local_user_id) - .inner_join(person::table) +enum ListMode { + AdminsWithEmails, +} + +fn queries<'a>( +) -> Queries>, impl ListFn<'a, LocalUserView, ListMode>> { + let selection = ( + local_user::all_columns, + person::all_columns, + person_aggregates::all_columns, + ); + + let read = move |mut conn: DbConn<'a>, search: ReadBy<'a>| async move { + let mut query = local_user::table.into_boxed(); + query = match search { + ReadBy::Id(local_user_id) => query.filter(local_user::id.eq(local_user_id)), + ReadBy::Email(from_email) => query.filter(local_user::email.eq(from_email)), + _ => query, + }; + let mut query = query.inner_join(person::table); + query = match search { + ReadBy::Person(person_id) => query.filter(person::id.eq(person_id)), + ReadBy::Name(name) => query.filter(lower(person::name).eq(name.to_lowercase())), + ReadBy::NameOrEmail(name_or_email) => query.filter( + lower(person::name) + .eq(lower(name_or_email)) + .or(local_user::email.eq(name_or_email)), + ), + _ => query, + }; + query .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) - .select(( - local_user::all_columns, - person::all_columns, - person_aggregates::all_columns, - )) - .first::(conn) - .await?; - Ok(Self::from_tuple(res)) + .select(selection) + .first::(&mut conn) + .await + }; + + let list = move |mut conn: DbConn<'a>, mode: ListMode| async move { + match mode { + ListMode::AdminsWithEmails => { + local_user::table + .filter(local_user::email.is_not_null()) + .filter(person::admin.eq(true)) + .inner_join(person::table) + .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) + .select(selection) + .load::(&mut conn) + .await + } + } + }; + + Queries::new(read, list) +} + +impl LocalUserView { + pub async fn read(pool: &mut DbPool<'_>, local_user_id: LocalUserId) -> Result { + queries().read(pool, ReadBy::Id(local_user_id)).await } pub async fn read_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { - let conn = &mut get_conn(pool).await?; - let res = local_user::table - .filter(person::id.eq(person_id)) - .inner_join(person::table) - .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) - .select(( - local_user::all_columns, - person::all_columns, - person_aggregates::all_columns, - )) - .first::(conn) - .await?; - Ok(Self::from_tuple(res)) + queries().read(pool, ReadBy::Person(person_id)).await } pub async fn read_from_name(pool: &mut DbPool<'_>, name: &str) -> Result { - let conn = &mut get_conn(pool).await?; - let res = local_user::table - .filter(lower(person::name).eq(name.to_lowercase())) - .inner_join(person::table) - .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) - .select(( - local_user::all_columns, - person::all_columns, - person_aggregates::all_columns, - )) - .first::(conn) - .await?; - Ok(Self::from_tuple(res)) + queries().read(pool, ReadBy::Name(name)).await } pub async fn find_by_email_or_name( pool: &mut DbPool<'_>, name_or_email: &str, ) -> Result { - let conn = &mut get_conn(pool).await?; - let res = local_user::table - .inner_join(person::table) - .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) - .filter( - lower(person::name) - .eq(lower(name_or_email)) - .or(local_user::email.eq(name_or_email)), - ) - .select(( - local_user::all_columns, - person::all_columns, - person_aggregates::all_columns, - )) - .first::(conn) - .await?; - Ok(Self::from_tuple(res)) + queries() + .read(pool, ReadBy::NameOrEmail(name_or_email)) + .await } pub async fn find_by_email(pool: &mut DbPool<'_>, from_email: &str) -> Result { - let conn = &mut get_conn(pool).await?; - let res = local_user::table - .inner_join(person::table) - .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) - .filter(local_user::email.eq(from_email)) - .select(( - local_user::all_columns, - person::all_columns, - person_aggregates::all_columns, - )) - .first::(conn) - .await?; - Ok(Self::from_tuple(res)) + queries().read(pool, ReadBy::Email(from_email)).await } pub async fn list_admins_with_emails(pool: &mut DbPool<'_>) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let res = local_user::table - .filter(person::admin.eq(true)) - .filter(local_user::email.is_not_null()) - .inner_join(person::table) - .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) - .select(( - local_user::all_columns, - person::all_columns, - person_aggregates::all_columns, - )) - .load::(conn) - .await?; - - Ok(res.into_iter().map(LocalUserView::from_tuple).collect()) + queries().list(pool, ListMode::AdminsWithEmails).await } } From 56fd2d7236231f824aa7194358a1ea346fc39453 Mon Sep 17 00:00:00 2001 From: dull b Date: Thu, 20 Jul 2023 20:51:37 +0000 Subject: [PATCH 17/28] Add post_report_view::queries --- crates/db_views/src/post_report_view.rs | 166 +++++++++++------------- 1 file changed, 73 insertions(+), 93 deletions(-) diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index d5b544480a..14dbb9df44 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -1,5 +1,6 @@ use crate::structs::PostReportView; use diesel::{ + pg::Pg, result::Error, BoolExpressionMethods, ExpressionMethods, @@ -10,6 +11,7 @@ use diesel::{ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ aggregates::structs::PostAggregates, + aliases, newtypes::{CommunityId, PersonId, PostReportId}, schema::{ community, @@ -28,7 +30,7 @@ use lemmy_db_schema::{ post_report::PostReport, }, traits::JoinView, - utils::{get_conn, limit_and_offset, DbPool}, + utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, }; type PostReportViewTuple = ( @@ -43,24 +45,16 @@ type PostReportViewTuple = ( Option, ); -impl PostReportView { - /// returns the PostReportView for the provided report_id - /// - /// * `report_id` - the report id to obtain - pub async fn read( - pool: &mut DbPool<'_>, - report_id: PostReportId, - my_person_id: PersonId, - ) -> Result { - let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - - let res = post_report::table - .find(report_id) +fn queries<'a>() -> Queries< + impl ReadFn<'a, PostReportView, (PostReportId, PersonId)>, + impl ListFn<'a, PostReportView, (PostReportQuery, &'a Person)>, +> { + let all_joins = |query: post_report::BoxedQuery<'static, Pg>, my_person_id: PersonId| { + query .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(person::table.on(post_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) + .inner_join(aliases::person1.on(post::creator_id.eq(aliases::person1.field(person::id)))) .left_join( community_person_ban::table.on( post::community_id @@ -77,23 +71,79 @@ impl PostReportView { ) .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id))) .left_join( - person_alias_2.on(post_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), + aliases::person2 + .on(post_report::resolver_id.eq(aliases::person2.field(person::id).nullable())), ) .select(( post_report::all_columns, post::all_columns, community::all_columns, person::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person1.fields(person::all_columns), community_person_ban::all_columns.nullable(), post_like::score.nullable(), post_aggregates::all_columns, - person_alias_2.fields(person::all_columns.nullable()), + aliases::person2.fields(person::all_columns.nullable()), )) - .first::(conn) - .await?; + }; + + let read = move |mut conn: DbConn<'a>, (report_id, my_person_id): (PostReportId, PersonId)| async move { + all_joins( + post_report::table.find(report_id).into_boxed(), + my_person_id, + ) + .first::(&mut conn) + .await + }; + + let list = move |mut conn: DbConn<'a>, (options, my_person): (PostReportQuery, &'a Person)| async move { + let mut query = all_joins(post_report::table.into_boxed(), my_person.id); + + if let Some(community_id) = options.community_id { + query = query.filter(post::community_id.eq(community_id)); + } + + if options.unresolved_only.unwrap_or(false) { + query = query.filter(post_report::resolved.eq(false)); + } + + let (limit, offset) = limit_and_offset(options.page, options.limit)?; + + query = query + .order_by(post_report::published.desc()) + .limit(limit) + .offset(offset); - Ok(Self::from_tuple(res)) + // If its not an admin, get only the ones you mod + if !my_person.admin { + query + .inner_join( + community_moderator::table.on( + community_moderator::community_id + .eq(post::community_id) + .and(community_moderator::person_id.eq(my_person.id)), + ), + ) + .load::(&mut conn) + .await + } else { + query.load::(&mut conn).await + } + }; + + Queries::new(read, list) +} + +impl PostReportView { + /// returns the PostReportView for the provided report_id + /// + /// * `report_id` - the report id to obtain + pub async fn read( + pool: &mut DbPool<'_>, + report_id: PostReportId, + my_person_id: PersonId, + ) -> Result { + queries().read(pool, (report_id, my_person_id)).await } /// returns the current unresolved post report count for the communities you mod @@ -150,77 +200,7 @@ impl PostReportQuery { pool: &mut DbPool<'_>, my_person: &Person, ) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - - let mut query = post_report::table - .inner_join(post::table) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person::table.on(post_report::creator_id.eq(person::id))) - .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) - .left_join( - community_person_ban::table.on( - post::community_id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(post::creator_id)), - ), - ) - .left_join( - post_like::table.on( - post::id - .eq(post_like::post_id) - .and(post_like::person_id.eq(my_person.id)), - ), - ) - .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id))) - .left_join( - person_alias_2.on(post_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - post_report::all_columns, - post::all_columns, - community::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - community_person_ban::all_columns.nullable(), - post_like::score.nullable(), - post_aggregates::all_columns, - person_alias_2.fields(person::all_columns.nullable()), - )) - .into_boxed(); - - if let Some(community_id) = self.community_id { - query = query.filter(post::community_id.eq(community_id)); - } - - if self.unresolved_only.unwrap_or(false) { - query = query.filter(post_report::resolved.eq(false)); - } - - let (limit, offset) = limit_and_offset(self.page, self.limit)?; - - query = query - .order_by(post_report::published.desc()) - .limit(limit) - .offset(offset); - - // If its not an admin, get only the ones you mod - let res = if !my_person.admin { - query - .inner_join( - community_moderator::table.on( - community_moderator::community_id - .eq(post::community_id) - .and(community_moderator::person_id.eq(my_person.id)), - ), - ) - .load::(conn) - .await? - } else { - query.load::(conn).await? - }; - - Ok(res.into_iter().map(PostReportView::from_tuple).collect()) + queries().list(pool, (self, my_person)).await } } From 80bbd11e6d1e6bace78c5f2741639def4fc3f979 Mon Sep 17 00:00:00 2001 From: dull b Date: Thu, 20 Jul 2023 21:23:31 +0000 Subject: [PATCH 18/28] Ad post_view::queries --- crates/db_views/src/post_view.rs | 258 +++++++++++++------------------ 1 file changed, 111 insertions(+), 147 deletions(-) diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index a75203d8cb..5a7d96edb3 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -40,7 +40,7 @@ use lemmy_db_schema::{ post::{Post, PostRead, PostSaved}, }, traits::JoinView, - utils::{fuzzy_search, get_conn, limit_and_offset, DbPool}, + utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, ListingType, SortType, }; @@ -62,19 +62,15 @@ type PostViewTuple = ( sql_function!(fn coalesce(x: sql_types::Nullable, y: sql_types::BigInt) -> sql_types::BigInt); -impl PostView { - pub async fn read( - pool: &mut DbPool<'_>, - post_id: PostId, - my_person_id: Option, - is_mod_or_admin: Option, - ) -> Result { - let conn = &mut get_conn(pool).await?; - +fn queries<'a>() -> Queries< + impl ReadFn<'a, PostView, (PostId, Option, Option)>, + impl ListFn<'a, PostView, PostQuery<'a>>, +> { + let all_joins = |query: post::BoxedQuery<'a, Pg>, my_person_id: Option| { // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); - let mut query = post::table - .find(post_id) + + query .inner_join(person::table) .inner_join(community::table) .left_join( @@ -127,23 +123,33 @@ impl PostView { .and(person_post_aggregates::person_id.eq(person_id_join)), ), ) - .select(( - post::all_columns, - person::all_columns, - community::all_columns, - community_person_ban::all_columns.nullable(), - post_aggregates::all_columns, - community_follower::all_columns.nullable(), - post_saved::all_columns.nullable(), - post_read::all_columns.nullable(), - person_block::all_columns.nullable(), - post_like::score.nullable(), - coalesce( - post_aggregates::comments.nullable() - person_post_aggregates::read_comments.nullable(), - post_aggregates::comments, - ), - )) - .into_boxed(); + }; + + let selection = ( + post::all_columns, + person::all_columns, + community::all_columns, + community_person_ban::all_columns.nullable(), + post_aggregates::all_columns, + community_follower::all_columns.nullable(), + post_saved::all_columns.nullable(), + post_read::all_columns.nullable(), + person_block::all_columns.nullable(), + post_like::score.nullable(), + coalesce( + post_aggregates::comments.nullable() - person_post_aggregates::read_comments.nullable(), + post_aggregates::comments, + ), + ); + + let read = move |mut conn: DbConn<'a>, + (post_id, my_person_id, is_mod_or_admin): ( + PostId, + Option, + Option, + )| async move { + let mut query = + all_joins(post::table.find(post_id).into_boxed(), my_person_id).select(selection); // Hide deleted and removed for non-admins or mods // Note: one special use case for this flag variable is when end-user-delete post or mod-removed post. @@ -155,81 +161,18 @@ impl PostView { .filter(post::deleted.eq(false)); } - let mut res = query.first::(conn).await?; - - // If a person is given, then my_vote (res.9), if None, should be 0, not null - // Necessary to differentiate between other person's votes - if my_person_id.is_some() && res.9.is_none() { - res.9 = Some(0) - }; - - Ok(Self::from_tuple(res)) - } -} - -#[derive(Default)] -pub struct PostQuery<'a> { - pub listing_type: Option, - pub sort: Option, - pub creator_id: Option, - pub community_id: Option, - pub local_user: Option<&'a LocalUser>, - pub search_term: Option, - pub url_search: Option, - pub saved_only: Option, - /// Used to show deleted or removed posts for admins - pub is_mod_or_admin: Option, - pub page: Option, - pub limit: Option, -} + query.first::(&mut conn).await + }; -impl<'a> PostQuery<'a> { - pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { - let conn = &mut get_conn(pool).await?; + let list = move |mut conn: DbConn<'a>, options: PostQuery<'a>| async move { + let person_id = options.local_user.map(|l| l.person_id); + let local_user_id = options.local_user.map(|l| l.id); // The left join below will return None in this case - let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); - let local_user_id_join = self.local_user.map(|l| l.id).unwrap_or(LocalUserId(-1)); + let person_id_join = person_id.unwrap_or(PersonId(-1)); + let local_user_id_join = local_user_id.unwrap_or(LocalUserId(-1)); - let mut query = post::table - .inner_join(person::table) - .inner_join(community::table) - .left_join( - community_person_ban::table.on( - post::community_id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(post::creator_id)), - ), - ) - .inner_join(post_aggregates::table) - .left_join( - community_follower::table.on( - post::community_id - .eq(community_follower::community_id) - .and(community_follower::person_id.eq(person_id_join)), - ), - ) - .left_join( - post_saved::table.on( - post::id - .eq(post_saved::post_id) - .and(post_saved::person_id.eq(person_id_join)), - ), - ) - .left_join( - post_read::table.on( - post::id - .eq(post_read::post_id) - .and(post_read::person_id.eq(person_id_join)), - ), - ) - .left_join( - person_block::table.on( - post::creator_id - .eq(person_block::target_id) - .and(person_block::person_id.eq(person_id_join)), - ), - ) + let mut query = all_joins(post::table.into_boxed(), person_id) .left_join( community_block::table.on( post::community_id @@ -237,20 +180,6 @@ impl<'a> PostQuery<'a> { .and(community_block::person_id.eq(person_id_join)), ), ) - .left_join( - post_like::table.on( - post::id - .eq(post_like::post_id) - .and(post_like::person_id.eq(person_id_join)), - ), - ) - .left_join( - person_post_aggregates::table.on( - post::id - .eq(person_post_aggregates::post_id) - .and(person_post_aggregates::person_id.eq(person_id_join)), - ), - ) .left_join( local_user_language::table.on( post::language_id @@ -258,27 +187,11 @@ impl<'a> PostQuery<'a> { .and(local_user_language::local_user_id.eq(local_user_id_join)), ), ) - .select(( - post::all_columns, - person::all_columns, - community::all_columns, - community_person_ban::all_columns.nullable(), - post_aggregates::all_columns, - community_follower::all_columns.nullable(), - post_saved::all_columns.nullable(), - post_read::all_columns.nullable(), - person_block::all_columns.nullable(), - post_like::score.nullable(), - coalesce( - post_aggregates::comments.nullable() - person_post_aggregates::read_comments.nullable(), - post_aggregates::comments, - ), - )) - .into_boxed(); + .select(selection); // Hide deleted and removed for non-admins or mods // TODO This eventually needs to show posts where you are the creator - if !self.is_mod_or_admin.unwrap_or(false) { + if !options.is_mod_or_admin.unwrap_or(false) { query = query .filter(community::removed.eq(false)) .filter(community::deleted.eq(false)) @@ -286,19 +199,19 @@ impl<'a> PostQuery<'a> { .filter(post::deleted.eq(false)); } - if self.community_id.is_none() { + if options.community_id.is_none() { query = query.then_order_by(post_aggregates::featured_local.desc()); - } else if let Some(community_id) = self.community_id { + } else if let Some(community_id) = options.community_id { query = query .filter(post::community_id.eq(community_id)) .then_order_by(post_aggregates::featured_community.desc()); } - if let Some(creator_id) = self.creator_id { + if let Some(creator_id) = options.creator_id { query = query.filter(post::creator_id.eq(creator_id)); } - if let Some(listing_type) = self.listing_type { + if let Some(listing_type) = options.listing_type { match listing_type { ListingType::Subscribed => { query = query.filter(community_follower::person_id.is_not_null()) @@ -320,11 +233,11 @@ impl<'a> PostQuery<'a> { } } - if let Some(url_search) = self.url_search { + if let Some(url_search) = options.url_search { query = query.filter(post::url.eq(url_search)); } - if let Some(search_term) = self.search_term { + if let Some(search_term) = options.search_term { let searcher = fuzzy_search(&search_term); query = query.filter( post::name @@ -333,26 +246,34 @@ impl<'a> PostQuery<'a> { ); } - if !self.local_user.map(|l| l.show_nsfw).unwrap_or(false) { + if !options.local_user.map(|l| l.show_nsfw).unwrap_or(false) { query = query .filter(post::nsfw.eq(false)) .filter(community::nsfw.eq(false)); }; - if !self.local_user.map(|l| l.show_bot_accounts).unwrap_or(true) { + if !options + .local_user + .map(|l| l.show_bot_accounts) + .unwrap_or(true) + { query = query.filter(person::bot_account.eq(false)); }; - if self.saved_only.unwrap_or(false) { + if options.saved_only.unwrap_or(false) { query = query.filter(post_saved::post_id.is_not_null()); } // Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read // setting wont be able to see saved posts. - else if !self.local_user.map(|l| l.show_read_posts).unwrap_or(true) { + else if !options + .local_user + .map(|l| l.show_read_posts) + .unwrap_or(true) + { query = query.filter(post_read::post_id.is_null()); } - if self.local_user.is_some() { + if options.local_user.is_some() { // Filter out the rows with missing languages query = query.filter(local_user_language::language_id.is_not_null()); @@ -361,7 +282,7 @@ impl<'a> PostQuery<'a> { query = query.filter(person_block::person_id.is_null()); } - query = match self.sort.unwrap_or(SortType::Hot) { + query = match options.sort.unwrap_or(SortType::Hot) { SortType::Active => query.then_order_by(post_aggregates::hot_rank_active.desc()), SortType::Hot => query.then_order_by(post_aggregates::hot_rank.desc()), SortType::New => query.then_order_by(post_aggregates::published.desc()), @@ -415,15 +336,58 @@ impl<'a> PostQuery<'a> { .then_order_by(post_aggregates::published.desc()), }; - let (limit, offset) = limit_and_offset(self.page, self.limit)?; + let (limit, offset) = limit_and_offset(options.page, options.limit)?; query = query.limit(limit).offset(offset); debug!("Post View Query: {:?}", debug_query::(&query)); - let res = query.load::(conn).await?; + query.load::(&mut conn).await + }; + + Queries::new(read, list) +} + +impl PostView { + pub async fn read( + pool: &mut DbPool<'_>, + post_id: PostId, + my_person_id: Option, + is_mod_or_admin: Option, + ) -> Result { + let mut res = queries() + .read(pool, (post_id, my_person_id, is_mod_or_admin)) + .await?; + + // If a person is given, then my_vote, if None, should be 0, not null + // Necessary to differentiate between other person's votes + if my_person_id.is_some() && res.my_vote.is_none() { + res.my_vote = Some(0) + }; + + Ok(res) + } +} - Ok(res.into_iter().map(PostView::from_tuple).collect()) +#[derive(Default)] +pub struct PostQuery<'a> { + pub listing_type: Option, + pub sort: Option, + pub creator_id: Option, + pub community_id: Option, + pub local_user: Option<&'a LocalUser>, + pub search_term: Option, + pub url_search: Option, + pub saved_only: Option, + /// Used to show deleted or removed posts for admins + pub is_mod_or_admin: Option, + pub page: Option, + pub limit: Option, +} + +impl<'a> PostQuery<'a> { + pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { + queries().list(pool, self).await } } From e6dca527f01a2acbe6ac0214450c26fd68318731 Mon Sep 17 00:00:00 2001 From: dull b Date: Thu, 20 Jul 2023 21:37:07 +0000 Subject: [PATCH 19/28] Add private_message_report_view::queries --- crates/db_views/src/post_view.rs | 2 +- .../src/private_message_report_view.rs | 131 +++++++++--------- 2 files changed, 64 insertions(+), 69 deletions(-) diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 5a7d96edb3..ed75de31ba 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -66,7 +66,7 @@ fn queries<'a>() -> Queries< impl ReadFn<'a, PostView, (PostId, Option, Option)>, impl ListFn<'a, PostView, PostQuery<'a>>, > { - let all_joins = |query: post::BoxedQuery<'a, Pg>, my_person_id: Option| { + let all_joins = |query: post::BoxedQuery<'static, Pg>, my_person_id: Option| { // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); diff --git a/crates/db_views/src/private_message_report_view.rs b/crates/db_views/src/private_message_report_view.rs index dd9adb42be..141ada4660 100644 --- a/crates/db_views/src/private_message_report_view.rs +++ b/crates/db_views/src/private_message_report_view.rs @@ -1,7 +1,15 @@ use crate::structs::PrivateMessageReportView; -use diesel::{result::Error, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl}; +use diesel::{ + pg::Pg, + result::Error, + ExpressionMethods, + JoinOnDsl, + NullableExpressionMethods, + QueryDsl, +}; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::PrivateMessageReportId, schema::{person, private_message, private_message_report}, source::{ @@ -10,7 +18,7 @@ use lemmy_db_schema::{ private_message_report::PrivateMessageReport, }, traits::JoinView, - utils::{get_conn, limit_and_offset, DbPool}, + utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, }; type PrivateMessageReportViewTuple = ( @@ -21,6 +29,57 @@ type PrivateMessageReportViewTuple = ( Option, ); +fn queries<'a>() -> Queries< + impl ReadFn<'a, PrivateMessageReportView, PrivateMessageReportId>, + impl ListFn<'a, PrivateMessageReportView, PrivateMessageReportQuery>, +> { + let all_joins = + |query: private_message_report::BoxedQuery<'static, Pg>| { + query + .inner_join(private_message::table) + .inner_join(person::table.on(private_message::creator_id.eq(person::id))) + .inner_join( + aliases::person1 + .on(private_message_report::creator_id.eq(aliases::person1.field(person::id))), + ) + .left_join(aliases::person2.on( + private_message_report::resolver_id.eq(aliases::person2.field(person::id).nullable()), + )) + .select(( + private_message_report::all_columns, + private_message::all_columns, + person::all_columns, + aliases::person1.fields(person::all_columns), + aliases::person2.fields(person::all_columns).nullable(), + )) + }; + + let read = move |mut conn: DbConn<'a>, report_id: PrivateMessageReportId| async move { + all_joins(private_message_report::table.find(report_id).into_boxed()) + .first::(&mut conn) + .await + }; + + let list = move |mut conn: DbConn<'a>, options: PrivateMessageReportQuery| async move { + let mut query = all_joins(private_message_report::table.into_boxed()); + + if options.unresolved_only.unwrap_or(false) { + query = query.filter(private_message_report::resolved.eq(false)); + } + + let (limit, offset) = limit_and_offset(options.page, options.limit)?; + + query + .order_by(private_message::published.desc()) + .limit(limit) + .offset(offset) + .load::(&mut conn) + .await + }; + + Queries::new(read, list) +} + impl PrivateMessageReportView { /// returns the PrivateMessageReportView for the provided report_id /// @@ -29,31 +88,7 @@ impl PrivateMessageReportView { pool: &mut DbPool<'_>, report_id: PrivateMessageReportId, ) -> Result { - let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - - let res = private_message_report::table - .find(report_id) - .inner_join(private_message::table) - .inner_join(person::table.on(private_message::creator_id.eq(person::id))) - .inner_join( - person_alias_1.on(private_message_report::creator_id.eq(person_alias_1.field(person::id))), - ) - .left_join( - person_alias_2 - .on(private_message_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - private_message_report::all_columns, - private_message::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - person_alias_2.fields(person::all_columns).nullable(), - )) - .first::(conn) - .await?; - - Ok(Self::from_tuple(res)) + queries().read(pool, report_id).await } /// Returns the current unresolved post report count for the communities you mod @@ -80,47 +115,7 @@ pub struct PrivateMessageReportQuery { impl PrivateMessageReportQuery { pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - - let mut query = private_message_report::table - .inner_join(private_message::table) - .inner_join(person::table.on(private_message::creator_id.eq(person::id))) - .inner_join( - person_alias_1.on(private_message_report::creator_id.eq(person_alias_1.field(person::id))), - ) - .left_join( - person_alias_2 - .on(private_message_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), - ) - .select(( - private_message_report::all_columns, - private_message::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - person_alias_2.fields(person::all_columns).nullable(), - )) - .into_boxed(); - - if self.unresolved_only.unwrap_or(false) { - query = query.filter(private_message_report::resolved.eq(false)); - } - - let (limit, offset) = limit_and_offset(self.page, self.limit)?; - - query = query - .order_by(private_message::published.desc()) - .limit(limit) - .offset(offset); - - let res = query.load::(conn).await?; - - Ok( - res - .into_iter() - .map(PrivateMessageReportView::from_tuple) - .collect(), - ) + queries().list(pool, self).await } } From 02ca3351bba2eec875c78dccbdd3590d3a5c04d5 Mon Sep 17 00:00:00 2001 From: dull b Date: Thu, 20 Jul 2023 22:03:53 +0000 Subject: [PATCH 20/28] private_message_view, registration_application_view --- crates/db_views/src/private_message_view.rs | 139 +++++++++--------- .../src/registration_application_view.rs | 115 +++++++-------- 2 files changed, 124 insertions(+), 130 deletions(-) diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index fd9bc739b3..ddf61ba67f 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -10,40 +10,87 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, newtypes::{PersonId, PrivateMessageId}, schema::{person, private_message}, source::{person::Person, private_message::PrivateMessage}, traits::JoinView, - utils::{get_conn, limit_and_offset, DbPool}, + utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, }; use tracing::debug; type PrivateMessageViewTuple = (PrivateMessage, Person, Person); +fn queries<'a>() -> Queries< + impl ReadFn<'a, PrivateMessageView, PrivateMessageId>, + impl ListFn<'a, PrivateMessageView, (PrivateMessageQuery, PersonId)>, +> { + let all_joins = |query: private_message::BoxedQuery<'static, Pg>| { + query + .inner_join(person::table.on(private_message::creator_id.eq(person::id))) + .inner_join( + aliases::person1.on(private_message::recipient_id.eq(aliases::person1.field(person::id))), + ) + }; + + let selection = ( + private_message::all_columns, + person::all_columns, + aliases::person1.fields(person::all_columns), + ); + + let read = move |mut conn: DbConn<'a>, private_message_id: PrivateMessageId| async move { + all_joins(private_message::table.find(private_message_id).into_boxed()) + .order_by(private_message::published.desc()) + .select(selection) + .first::(&mut conn) + .await + }; + + let list = move |mut conn: DbConn<'a>, + (options, recipient_id): (PrivateMessageQuery, PersonId)| async move { + let mut query = all_joins(private_message::table.into_boxed()).select(selection); + + // If its unread, I only want the ones to me + if options.unread_only.unwrap_or(false) { + query = query + .filter(private_message::read.eq(false)) + .filter(private_message::recipient_id.eq(recipient_id)); + } + // Otherwise, I want the ALL view to show both sent and received + else { + query = query.filter( + private_message::recipient_id + .eq(recipient_id) + .or(private_message::creator_id.eq(recipient_id)), + ) + } + + let (limit, offset) = limit_and_offset(options.page, options.limit)?; + + query = query + .filter(private_message::deleted.eq(false)) + .limit(limit) + .offset(offset) + .order_by(private_message::published.desc()); + + debug!( + "Private Message View Query: {:?}", + debug_query::(&query) + ); + + query.load::(&mut conn).await + }; + + Queries::new(read, list) +} + impl PrivateMessageView { pub async fn read( pool: &mut DbPool<'_>, private_message_id: PrivateMessageId, ) -> Result { - let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); - - let res = private_message::table - .find(private_message_id) - .inner_join(person::table.on(private_message::creator_id.eq(person::id))) - .inner_join( - person_alias_1.on(private_message::recipient_id.eq(person_alias_1.field(person::id))), - ) - .order_by(private_message::published.desc()) - .select(( - private_message::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - )) - .first::(conn) - .await?; - - Ok(Self::from_tuple(res)) + queries().read(pool, private_message_id).await } /// Gets the number of unread messages @@ -76,57 +123,7 @@ impl PrivateMessageQuery { pool: &mut DbPool<'_>, recipient_id: PersonId, ) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); - - let mut query = private_message::table - .inner_join(person::table.on(private_message::creator_id.eq(person::id))) - .inner_join( - person_alias_1.on(private_message::recipient_id.eq(person_alias_1.field(person::id))), - ) - .select(( - private_message::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns), - )) - .into_boxed(); - - // If its unread, I only want the ones to me - if self.unread_only.unwrap_or(false) { - query = query - .filter(private_message::read.eq(false)) - .filter(private_message::recipient_id.eq(recipient_id)); - } - // Otherwise, I want the ALL view to show both sent and received - else { - query = query.filter( - private_message::recipient_id - .eq(recipient_id) - .or(private_message::creator_id.eq(recipient_id)), - ) - } - - let (limit, offset) = limit_and_offset(self.page, self.limit)?; - - query = query - .filter(private_message::deleted.eq(false)) - .limit(limit) - .offset(offset) - .order_by(private_message::published.desc()); - - debug!( - "Private Message View Query: {:?}", - debug_query::(&query) - ); - - let res = query.load::(conn).await?; - - Ok( - res - .into_iter() - .map(PrivateMessageView::from_tuple) - .collect(), - ) + queries().list(pool, (self, recipient_id)).await } } diff --git a/crates/db_views/src/registration_application_view.rs b/crates/db_views/src/registration_application_view.rs index f4b6eba593..5005bcff17 100644 --- a/crates/db_views/src/registration_application_view.rs +++ b/crates/db_views/src/registration_application_view.rs @@ -1,6 +1,7 @@ use crate::structs::RegistrationApplicationView; use diesel::{ dsl::count, + pg::Pg, result::Error, ExpressionMethods, JoinOnDsl, @@ -9,6 +10,7 @@ use diesel::{ }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ + aliases, schema::{local_user, person, registration_application}, source::{ local_user::LocalUser, @@ -16,39 +18,75 @@ use lemmy_db_schema::{ registration_application::RegistrationApplication, }, traits::JoinView, - utils::{get_conn, limit_and_offset, DbPool}, + utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, }; type RegistrationApplicationViewTuple = (RegistrationApplication, LocalUser, Person, Option); -impl RegistrationApplicationView { - pub async fn read( - pool: &mut DbPool<'_>, - registration_application_id: i32, - ) -> Result { - let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); - - let res = registration_application::table - .find(registration_application_id) +fn queries<'a>() -> Queries< + impl ReadFn<'a, RegistrationApplicationView, i32>, + impl ListFn<'a, RegistrationApplicationView, RegistrationApplicationQuery>, +> { + let all_joins = |query: registration_application::BoxedQuery<'static, Pg>| { + query .inner_join(local_user::table.on(registration_application::local_user_id.eq(local_user::id))) .inner_join(person::table.on(local_user::person_id.eq(person::id))) .left_join( - person_alias_1 - .on(registration_application::admin_id.eq(person_alias_1.field(person::id).nullable())), + aliases::person1 + .on(registration_application::admin_id.eq(aliases::person1.field(person::id).nullable())), ) .order_by(registration_application::published.desc()) .select(( registration_application::all_columns, local_user::all_columns, person::all_columns, - person_alias_1.fields(person::all_columns).nullable(), + aliases::person1.fields(person::all_columns).nullable(), )) - .first::(conn) - .await?; + }; + + let read = move |mut conn: DbConn<'a>, registration_application_id: i32| async move { + all_joins( + registration_application::table + .find(registration_application_id) + .into_boxed(), + ) + .first::(&mut conn) + .await + }; + + let list = move |mut conn: DbConn<'a>, options: RegistrationApplicationQuery| async move { + let mut query = all_joins(registration_application::table.into_boxed()); + + if options.unread_only.unwrap_or(false) { + query = query.filter(registration_application::admin_id.is_null()) + } + + if options.verified_email_only.unwrap_or(false) { + query = query.filter(local_user::email_verified.eq(true)) + } + + let (limit, offset) = limit_and_offset(options.page, options.limit)?; + + query = query + .limit(limit) + .offset(offset) + .order_by(registration_application::published.desc()); - Ok(Self::from_tuple(res)) + query + .load::(&mut conn) + .await + }; + + Queries::new(read, list) +} + +impl RegistrationApplicationView { + pub async fn read( + pool: &mut DbPool<'_>, + registration_application_id: i32, + ) -> Result { + queries().read(pool, registration_application_id).await } /// Returns the current unread registration_application count @@ -93,48 +131,7 @@ impl RegistrationApplicationQuery { self, pool: &mut DbPool<'_>, ) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); - - let mut query = registration_application::table - .inner_join(local_user::table.on(registration_application::local_user_id.eq(local_user::id))) - .inner_join(person::table.on(local_user::person_id.eq(person::id))) - .left_join( - person_alias_1 - .on(registration_application::admin_id.eq(person_alias_1.field(person::id).nullable())), - ) - .order_by(registration_application::published.desc()) - .select(( - registration_application::all_columns, - local_user::all_columns, - person::all_columns, - person_alias_1.fields(person::all_columns).nullable(), - )) - .into_boxed(); - - if self.unread_only.unwrap_or(false) { - query = query.filter(registration_application::admin_id.is_null()) - } - - if self.verified_email_only.unwrap_or(false) { - query = query.filter(local_user::email_verified.eq(true)) - } - - let (limit, offset) = limit_and_offset(self.page, self.limit)?; - - query = query - .limit(limit) - .offset(offset) - .order_by(registration_application::published.desc()); - - let res = query.load::(conn).await?; - - Ok( - res - .into_iter() - .map(RegistrationApplicationView::from_tuple) - .collect(), - ) + queries().list(pool, self).await } } From f6f392c602c0bed6ebb79d89f389d6ae1151de4d Mon Sep 17 00:00:00 2001 From: dull b Date: Thu, 20 Jul 2023 23:54:48 +0000 Subject: [PATCH 21/28] Use 'a in BoxedQuery --- crates/db_views/src/comment_report_view.rs | 2 +- crates/db_views/src/comment_view.rs | 2 +- crates/db_views/src/post_report_view.rs | 2 +- crates/db_views/src/post_view.rs | 2 +- crates/db_views/src/private_message_report_view.rs | 2 +- crates/db_views/src/private_message_view.rs | 2 +- crates/db_views/src/registration_application_view.rs | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index f0f7f2762f..8a1ae5c992 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -41,7 +41,7 @@ fn queries<'a>() -> Queries< impl ReadFn<'a, CommentReportView, (CommentReportId, PersonId)>, impl ListFn<'a, CommentReportView, (CommentReportQuery, &'a Person)>, > { - let all_joins = |query: comment_report::BoxedQuery<'static, Pg>, + let all_joins = |query: comment_report::BoxedQuery<'a, Pg>, my_person_id: PersonId, include_expired: bool| { query diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 1da4b87dc9..c65b0b601d 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -59,7 +59,7 @@ fn queries<'a>() -> Queries< impl ReadFn<'a, CommentView, (CommentId, Option)>, impl ListFn<'a, CommentView, CommentQuery<'a>>, > { - let all_joins = |query: comment::BoxedQuery<'static, Pg>, my_person_id: Option| { + let all_joins = |query: comment::BoxedQuery<'a, Pg>, my_person_id: Option| { // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); query diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index 14dbb9df44..02768b81c6 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -49,7 +49,7 @@ fn queries<'a>() -> Queries< impl ReadFn<'a, PostReportView, (PostReportId, PersonId)>, impl ListFn<'a, PostReportView, (PostReportQuery, &'a Person)>, > { - let all_joins = |query: post_report::BoxedQuery<'static, Pg>, my_person_id: PersonId| { + let all_joins = |query: post_report::BoxedQuery<'a, Pg>, my_person_id: PersonId| { query .inner_join(post::table) .inner_join(community::table.on(post::community_id.eq(community::id))) diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index ed75de31ba..5a7d96edb3 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -66,7 +66,7 @@ fn queries<'a>() -> Queries< impl ReadFn<'a, PostView, (PostId, Option, Option)>, impl ListFn<'a, PostView, PostQuery<'a>>, > { - let all_joins = |query: post::BoxedQuery<'static, Pg>, my_person_id: Option| { + let all_joins = |query: post::BoxedQuery<'a, Pg>, my_person_id: Option| { // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); diff --git a/crates/db_views/src/private_message_report_view.rs b/crates/db_views/src/private_message_report_view.rs index 141ada4660..ce87abaec4 100644 --- a/crates/db_views/src/private_message_report_view.rs +++ b/crates/db_views/src/private_message_report_view.rs @@ -34,7 +34,7 @@ fn queries<'a>() -> Queries< impl ListFn<'a, PrivateMessageReportView, PrivateMessageReportQuery>, > { let all_joins = - |query: private_message_report::BoxedQuery<'static, Pg>| { + |query: private_message_report::BoxedQuery<'a, Pg>| { query .inner_join(private_message::table) .inner_join(person::table.on(private_message::creator_id.eq(person::id))) diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index ddf61ba67f..55d6583bae 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -25,7 +25,7 @@ fn queries<'a>() -> Queries< impl ReadFn<'a, PrivateMessageView, PrivateMessageId>, impl ListFn<'a, PrivateMessageView, (PrivateMessageQuery, PersonId)>, > { - let all_joins = |query: private_message::BoxedQuery<'static, Pg>| { + let all_joins = |query: private_message::BoxedQuery<'a, Pg>| { query .inner_join(person::table.on(private_message::creator_id.eq(person::id))) .inner_join( diff --git a/crates/db_views/src/registration_application_view.rs b/crates/db_views/src/registration_application_view.rs index 5005bcff17..50ca651368 100644 --- a/crates/db_views/src/registration_application_view.rs +++ b/crates/db_views/src/registration_application_view.rs @@ -28,7 +28,7 @@ fn queries<'a>() -> Queries< impl ReadFn<'a, RegistrationApplicationView, i32>, impl ListFn<'a, RegistrationApplicationView, RegistrationApplicationQuery>, > { - let all_joins = |query: registration_application::BoxedQuery<'static, Pg>| { + let all_joins = |query: registration_application::BoxedQuery<'a, Pg>| { query .inner_join(local_user::table.on(registration_application::local_user_id.eq(local_user::id))) .inner_join(person::table.on(local_user::person_id.eq(person::id))) From c1fec40a892dd44a8945cd25f7394097e9580edc Mon Sep 17 00:00:00 2001 From: dull b Date: Fri, 21 Jul 2023 18:54:36 +0000 Subject: [PATCH 22/28] comment_reply_view, community_view --- .../db_views_actor/src/comment_reply_view.rs | 175 ++++++--------- crates/db_views_actor/src/community_view.rs | 206 +++++++++--------- 2 files changed, 170 insertions(+), 211 deletions(-) diff --git a/crates/db_views_actor/src/comment_reply_view.rs b/crates/db_views_actor/src/comment_reply_view.rs index 3608ab25ee..11a69a51ba 100644 --- a/crates/db_views_actor/src/comment_reply_view.rs +++ b/crates/db_views_actor/src/comment_reply_view.rs @@ -1,5 +1,6 @@ use crate::structs::CommentReplyView; use diesel::{ + pg::Pg, result::Error, BoolExpressionMethods, ExpressionMethods, @@ -10,6 +11,7 @@ use diesel::{ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, + aliases, newtypes::{CommentReplyId, PersonId}, schema::{ comment, @@ -33,7 +35,7 @@ use lemmy_db_schema::{ post::Post, }, traits::JoinView, - utils::{get_conn, limit_and_offset, DbPool}, + utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, CommentSortType, }; @@ -52,25 +54,20 @@ type CommentReplyViewTuple = ( Option, ); -impl CommentReplyView { - pub async fn read( - pool: &mut DbPool<'_>, - comment_reply_id: CommentReplyId, - my_person_id: Option, - ) -> Result { - let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); - +fn queries<'a>() -> Queries< + impl ReadFn<'a, CommentReplyView, (CommentReplyId, Option)>, + impl ListFn<'a, CommentReplyView, CommentReplyQuery>, +> { + let all_joins = |query: comment_reply::BoxedQuery<'a, Pg>, my_person_id: Option| { // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); - let res = comment_reply::table - .find(comment_reply_id) + query .inner_join(comment::table) .inner_join(person::table.on(comment::creator_id.eq(person::id))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person_alias_1) + .inner_join(aliases::person1) .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) .left_join( community_person_ban::table.on( @@ -113,7 +110,7 @@ impl CommentReplyView { person::all_columns, post::all_columns, community::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), @@ -121,10 +118,60 @@ impl CommentReplyView { person_block::all_columns.nullable(), comment_like::score.nullable(), )) - .first::(conn) - .await?; + }; + + let read = + move |mut conn: DbConn<'a>, + (comment_reply_id, my_person_id): (CommentReplyId, Option)| async move { + all_joins( + comment_reply::table.find(comment_reply_id).into_boxed(), + my_person_id, + ) + .first::(&mut conn) + .await + }; + + let list = move |mut conn: DbConn<'a>, options: CommentReplyQuery| async move { + let mut query = all_joins(comment_reply::table.into_boxed(), options.my_person_id); + + if let Some(recipient_id) = options.recipient_id { + query = query.filter(comment_reply::recipient_id.eq(recipient_id)); + } - Ok(Self::from_tuple(res)) + if options.unread_only.unwrap_or(false) { + query = query.filter(comment_reply::read.eq(false)); + } + + if !options.show_bot_accounts.unwrap_or(true) { + query = query.filter(person::bot_account.eq(false)); + }; + + query = match options.sort.unwrap_or(CommentSortType::New) { + CommentSortType::Hot => query.then_order_by(comment_aggregates::hot_rank.desc()), + CommentSortType::New => query.then_order_by(comment_reply::published.desc()), + CommentSortType::Old => query.then_order_by(comment_reply::published.asc()), + CommentSortType::Top => query.order_by(comment_aggregates::score.desc()), + }; + + let (limit, offset) = limit_and_offset(options.page, options.limit)?; + + query + .limit(limit) + .offset(offset) + .load::(&mut conn) + .await + }; + + Queries::new(read, list) +} + +impl CommentReplyView { + pub async fn read( + pool: &mut DbPool<'_>, + comment_reply_id: CommentReplyId, + my_person_id: Option, + ) -> Result { + queries().read(pool, (comment_reply_id, my_person_id)).await } /// Gets the number of unread replies @@ -161,99 +208,7 @@ pub struct CommentReplyQuery { impl CommentReplyQuery { pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - - let person_alias_1 = diesel::alias!(person as person1); - - // The left join below will return None in this case - let person_id_join = self.my_person_id.unwrap_or(PersonId(-1)); - - let mut query = comment_reply::table - .inner_join(comment::table) - .inner_join(person::table.on(comment::creator_id.eq(person::id))) - .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person_alias_1) - .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)), - ), - ) - .left_join( - community_follower::table.on( - post::community_id - .eq(community_follower::community_id) - .and(community_follower::person_id.eq(person_id_join)), - ), - ) - .left_join( - comment_saved::table.on( - comment::id - .eq(comment_saved::comment_id) - .and(comment_saved::person_id.eq(person_id_join)), - ), - ) - .left_join( - person_block::table.on( - comment::creator_id - .eq(person_block::target_id) - .and(person_block::person_id.eq(person_id_join)), - ), - ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(person_id_join)), - ), - ) - .select(( - comment_reply::all_columns, - comment::all_columns, - person::all_columns, - post::all_columns, - community::all_columns, - person_alias_1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - community_follower::all_columns.nullable(), - comment_saved::all_columns.nullable(), - person_block::all_columns.nullable(), - comment_like::score.nullable(), - )) - .into_boxed(); - - if let Some(recipient_id) = self.recipient_id { - query = query.filter(comment_reply::recipient_id.eq(recipient_id)); - } - - if self.unread_only.unwrap_or(false) { - query = query.filter(comment_reply::read.eq(false)); - } - - if !self.show_bot_accounts.unwrap_or(true) { - query = query.filter(person::bot_account.eq(false)); - }; - - query = match self.sort.unwrap_or(CommentSortType::New) { - CommentSortType::Hot => query.then_order_by(comment_aggregates::hot_rank.desc()), - CommentSortType::New => query.then_order_by(comment_reply::published.desc()), - CommentSortType::Old => query.then_order_by(comment_reply::published.asc()), - CommentSortType::Top => query.order_by(comment_aggregates::score.desc()), - }; - - let (limit, offset) = limit_and_offset(self.page, self.limit)?; - - let res = query - .limit(limit) - .offset(offset) - .load::(conn) - .await?; - - Ok(res.into_iter().map(CommentReplyView::from_tuple).collect()) + queries().list(pool, self).await } } diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index 1a4afe7f60..14d69c4a33 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -1,5 +1,6 @@ use crate::structs::{CommunityModeratorView, CommunityView, PersonView}; use diesel::{ + pg::Pg, result::Error, BoolExpressionMethods, ExpressionMethods, @@ -19,7 +20,7 @@ use lemmy_db_schema::{ local_user::LocalUser, }, traits::JoinView, - utils::{fuzzy_search, get_conn, limit_and_offset, DbPool}, + utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, ListingType, SortType, }; @@ -31,19 +32,15 @@ type CommunityViewTuple = ( Option, ); -impl CommunityView { - pub async fn read( - pool: &mut DbPool<'_>, - community_id: CommunityId, - my_person_id: Option, - is_mod_or_admin: Option, - ) -> Result { - let conn = &mut get_conn(pool).await?; +fn queries<'a>() -> Queries< + impl ReadFn<'a, CommunityView, (CommunityId, Option, Option)>, + impl ListFn<'a, CommunityView, CommunityQuery<'a>>, +> { + let all_joins = |query: community::BoxedQuery<'a, Pg>, my_person_id: Option| { // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); - let mut query = community::table - .find(community_id) + query .inner_join(community_aggregates::table) .left_join( community_follower::table.on( @@ -59,106 +56,68 @@ impl CommunityView { .and(community_block::person_id.eq(person_id_join)), ), ) - .select(( - community::all_columns, - community_aggregates::all_columns, - community_follower::all_columns.nullable(), - community_block::all_columns.nullable(), - )) - .into_boxed(); + }; + + let selection = ( + community::all_columns, + community_aggregates::all_columns, + community_follower::all_columns.nullable(), + community_block::all_columns.nullable(), + ); + + let not_removed_or_deleted = community::removed + .eq(false) + .and(community::deleted.eq(false)); + + let read = move |mut conn: DbConn<'a>, + (community_id, my_person_id, is_mod_or_admin): ( + CommunityId, + Option, + Option, + )| async move { + let mut query = all_joins( + community::table.find(community_id).into_boxed(), + my_person_id, + ) + .select(selection); // Hide deleted and removed for non-admins or mods if !is_mod_or_admin.unwrap_or(false) { - query = query - .filter(community::removed.eq(false)) - .filter(community::deleted.eq(false)); + query = query.filter(not_removed_or_deleted); } - let res = query.first::(conn).await?; - - Ok(Self::from_tuple(res)) - } - - pub async fn is_mod_or_admin( - pool: &mut DbPool<'_>, - person_id: PersonId, - community_id: CommunityId, - ) -> Result { - let is_mod = - CommunityModeratorView::is_community_moderator(pool, community_id, person_id).await?; - if is_mod { - return Ok(true); - } - - PersonView::is_admin(pool, person_id).await - } -} - -#[derive(Default)] -pub struct CommunityQuery<'a> { - pub listing_type: Option, - pub sort: Option, - pub local_user: Option<&'a LocalUser>, - pub search_term: Option, - pub is_mod_or_admin: Option, - pub show_nsfw: Option, - pub page: Option, - pub limit: Option, -} + query.first::(&mut conn).await + }; -impl<'a> CommunityQuery<'a> { - pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { + let list = move |mut conn: DbConn<'a>, options: CommunityQuery<'a>| async move { use SortType::*; - let conn = &mut get_conn(pool).await?; + let my_person_id = options.local_user.map(|l| l.person_id); // The left join below will return None in this case - let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); + let person_id_join = my_person_id.unwrap_or(PersonId(-1)); - let mut query = community::table - .inner_join(community_aggregates::table) + let mut query = all_joins(community::table.into_boxed(), my_person_id) .left_join(local_user::table.on(local_user::person_id.eq(person_id_join))) - .left_join( - community_follower::table.on( - community::id - .eq(community_follower::community_id) - .and(community_follower::person_id.eq(person_id_join)), - ), - ) - .left_join( - community_block::table.on( - community::id - .eq(community_block::community_id) - .and(community_block::person_id.eq(person_id_join)), - ), - ) - .select(( - community::all_columns, - community_aggregates::all_columns, - community_follower::all_columns.nullable(), - community_block::all_columns.nullable(), - )) - .into_boxed(); - - if let Some(search_term) = self.search_term { + .select(selection); + + if let Some(search_term) = options.search_term { let searcher = fuzzy_search(&search_term); query = query .filter(community::name.ilike(searcher.clone())) - .or_filter(community::title.ilike(searcher)); - }; + .or_filter(community::title.ilike(searcher)) + } // Hide deleted and removed for non-admins or mods - if !self.is_mod_or_admin.unwrap_or(false) { - query = query - .filter(community::removed.eq(false)) - .filter(community::deleted.eq(false)) - .filter( - community::hidden - .eq(false) - .or(community_follower::person_id.eq(person_id_join)), - ); + if !options.is_mod_or_admin.unwrap_or(false) { + query = query.filter(not_removed_or_deleted).filter( + community::hidden + .eq(false) + .or(community_follower::person_id.eq(person_id_join)), + ); } - match self.sort.unwrap_or(Hot) { + + match options.sort.unwrap_or(Hot) { Hot | Active => query = query.order_by(community_aggregates::hot_rank.desc()), NewComments | TopDay | TopTwelveHour | TopSixHour | TopHour => { query = query.order_by(community_aggregates::users_active_day.desc()) @@ -176,7 +135,7 @@ impl<'a> CommunityQuery<'a> { TopWeek => query = query.order_by(community_aggregates::users_active_week.desc()), }; - if let Some(listing_type) = self.listing_type { + if let Some(listing_type) = options.listing_type { query = match listing_type { ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)), ListingType::Local => query.filter(community::local.eq(true)), @@ -185,24 +144,69 @@ impl<'a> CommunityQuery<'a> { } // Don't show blocked communities or nsfw communities if not enabled in profile - if self.local_user.is_some() { + if options.local_user.is_some() { query = query.filter(community_block::person_id.is_null()); query = query.filter(community::nsfw.eq(false).or(local_user::show_nsfw.eq(true))); } else { // No person in request, only show nsfw communities if show_nsfw is passed into request - if !self.show_nsfw.unwrap_or(false) { + if !options.show_nsfw.unwrap_or(false) { query = query.filter(community::nsfw.eq(false)); } } - let (limit, offset) = limit_and_offset(self.page, self.limit)?; - let res = query + let (limit, offset) = limit_and_offset(options.page, options.limit)?; + query .limit(limit) .offset(offset) - .load::(conn) - .await?; + .load::(&mut conn) + .await + }; + + Queries::new(read, list) +} + +impl CommunityView { + pub async fn read( + pool: &mut DbPool<'_>, + community_id: CommunityId, + my_person_id: Option, + is_mod_or_admin: Option, + ) -> Result { + queries() + .read(pool, (community_id, my_person_id, is_mod_or_admin)) + .await + } + + pub async fn is_mod_or_admin( + pool: &mut DbPool<'_>, + person_id: PersonId, + community_id: CommunityId, + ) -> Result { + let is_mod = + CommunityModeratorView::is_community_moderator(pool, community_id, person_id).await?; + if is_mod { + return Ok(true); + } - Ok(res.into_iter().map(CommunityView::from_tuple).collect()) + PersonView::is_admin(pool, person_id).await + } +} + +#[derive(Default)] +pub struct CommunityQuery<'a> { + pub listing_type: Option, + pub sort: Option, + pub local_user: Option<&'a LocalUser>, + pub search_term: Option, + pub is_mod_or_admin: Option, + pub show_nsfw: Option, + pub page: Option, + pub limit: Option, +} + +impl<'a> CommunityQuery<'a> { + pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { + queries().list(pool, self).await } } From 460e532213494f42ae6d006aa591c72af94c8dae Mon Sep 17 00:00:00 2001 From: dull b Date: Fri, 21 Jul 2023 18:57:48 +0000 Subject: [PATCH 23/28] Change aliases to inline module --- crates/db_schema/src/aliases.rs | 3 --- crates/db_schema/src/lib.rs | 5 ++++- 2 files changed, 4 insertions(+), 4 deletions(-) delete mode 100644 crates/db_schema/src/aliases.rs diff --git a/crates/db_schema/src/aliases.rs b/crates/db_schema/src/aliases.rs deleted file mode 100644 index 3b97521328..0000000000 --- a/crates/db_schema/src/aliases.rs +++ /dev/null @@ -1,3 +0,0 @@ -use crate::schema::person; - -diesel::alias!(person as person1: Person1, person as person2: Person2); diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index badb083d8b..feb21b408f 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -29,7 +29,10 @@ pub mod newtypes; #[allow(clippy::wildcard_imports)] pub mod schema; #[cfg(feature = "full")] -pub mod aliases; +pub mod aliases { + use crate::schema::person; + diesel::alias!(person as person1: Person1, person as person2: Person2); +} pub mod source; #[cfg(feature = "full")] pub mod traits; From 4cd95af98fe2c9729783acfa0e27e927bb238068 Mon Sep 17 00:00:00 2001 From: dull b Date: Fri, 21 Jul 2023 19:25:04 +0000 Subject: [PATCH 24/28] person_mention_view --- .../db_views_actor/src/person_mention_view.rs | 199 ++++++++---------- 1 file changed, 83 insertions(+), 116 deletions(-) diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index a69bb227e6..574dc55356 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -1,7 +1,9 @@ use crate::structs::PersonMentionView; use diesel::{ dsl::now, + pg::Pg, result::Error, + sql_types, BoolExpressionMethods, ExpressionMethods, JoinOnDsl, @@ -11,6 +13,7 @@ use diesel::{ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, + aliases, newtypes::{PersonId, PersonMentionId}, schema::{ comment, @@ -34,7 +37,7 @@ use lemmy_db_schema::{ post::Post, }, traits::JoinView, - utils::{get_conn, limit_and_offset, DbPool}, + utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, CommentSortType, }; @@ -53,31 +56,35 @@ type PersonMentionViewTuple = ( Option, ); -impl PersonMentionView { - pub async fn read( - pool: &mut DbPool<'_>, - person_mention_id: PersonMentionId, - my_person_id: Option, - ) -> Result { - let conn = &mut get_conn(pool).await?; - let person_alias_1 = diesel::alias!(person as person1); - +fn queries<'a>() -> Queries< + impl ReadFn<'a, PersonMentionView, (PersonMentionId, Option)>, + impl ListFn<'a, PersonMentionView, PersonMentionQuery>, +> { + let all_joins = |query: person_mention::BoxedQuery<'a, Pg>, + include_expired_bans: bool, + my_person_id: Option| { // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); - let res = person_mention::table - .find(person_mention_id) + query .inner_join(comment::table) .inner_join(person::table.on(comment::creator_id.eq(person::id))) .inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person_alias_1) + .inner_join(aliases::person1) .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) .left_join( community_person_ban::table.on( community::id .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)), + .and(community_person_ban::person_id.eq(comment::creator_id)) + .and( + community_person_ban::expires + .is_null() + .or(community_person_ban::expires.gt(now)) + // TODO: avoid evaluation of expiration condition if include_expired_bans is true + .or::<_, sql_types::Nullable>(include_expired_bans), + ), ), ) .left_join( @@ -114,7 +121,7 @@ impl PersonMentionView { person::all_columns, post::all_columns, community::all_columns, - person_alias_1.fields(person::all_columns), + aliases::person1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), @@ -122,10 +129,67 @@ impl PersonMentionView { person_block::all_columns.nullable(), comment_like::score.nullable(), )) - .first::(conn) - .await?; + }; + + let read = + move |mut conn: DbConn<'a>, + (person_mention_id, my_person_id): (PersonMentionId, Option)| async move { + all_joins( + person_mention::table.find(person_mention_id).into_boxed(), + true, + my_person_id, + ) + .first::(&mut conn) + .await + }; + + let list = move |mut conn: DbConn<'a>, options: PersonMentionQuery| async move { + let mut query = all_joins( + person_mention::table.into_boxed(), + false, + options.my_person_id, + ); + + if let Some(recipient_id) = options.recipient_id { + query = query.filter(person_mention::recipient_id.eq(recipient_id)); + } + + if options.unread_only.unwrap_or(false) { + query = query.filter(person_mention::read.eq(false)); + } + + if !options.show_bot_accounts.unwrap_or(true) { + query = query.filter(person::bot_account.eq(false)); + }; + + query = match options.sort.unwrap_or(CommentSortType::Hot) { + CommentSortType::Hot => query.then_order_by(comment_aggregates::hot_rank.desc()), + CommentSortType::New => query.then_order_by(comment::published.desc()), + CommentSortType::Old => query.then_order_by(comment::published.asc()), + CommentSortType::Top => query.order_by(comment_aggregates::score.desc()), + }; - Ok(Self::from_tuple(res)) + let (limit, offset) = limit_and_offset(options.page, options.limit)?; + + query + .limit(limit) + .offset(offset) + .load::(&mut conn) + .await + }; + + Queries::new(read, list) +} + +impl PersonMentionView { + pub async fn read( + pool: &mut DbPool<'_>, + person_mention_id: PersonMentionId, + my_person_id: Option, + ) -> Result { + queries() + .read(pool, (person_mention_id, my_person_id)) + .await } /// Gets the number of unread mentions @@ -161,104 +225,7 @@ pub struct PersonMentionQuery { impl PersonMentionQuery { pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - - let person_alias_1 = diesel::alias!(person as person1); - - // The left join below will return None in this case - let person_id_join = self.my_person_id.unwrap_or(PersonId(-1)); - - let mut query = person_mention::table - .inner_join(comment::table) - .inner_join(person::table.on(comment::creator_id.eq(person::id))) - .inner_join(post::table.on(comment::post_id.eq(post::id))) - .inner_join(community::table.on(post::community_id.eq(community::id))) - .inner_join(person_alias_1) - .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)) - .and( - community_person_ban::expires - .is_null() - .or(community_person_ban::expires.gt(now)), - ), - ), - ) - .left_join( - community_follower::table.on( - post::community_id - .eq(community_follower::community_id) - .and(community_follower::person_id.eq(person_id_join)), - ), - ) - .left_join( - comment_saved::table.on( - comment::id - .eq(comment_saved::comment_id) - .and(comment_saved::person_id.eq(person_id_join)), - ), - ) - .left_join( - person_block::table.on( - comment::creator_id - .eq(person_block::target_id) - .and(person_block::person_id.eq(person_id_join)), - ), - ) - .left_join( - comment_like::table.on( - comment::id - .eq(comment_like::comment_id) - .and(comment_like::person_id.eq(person_id_join)), - ), - ) - .select(( - person_mention::all_columns, - comment::all_columns, - person::all_columns, - post::all_columns, - community::all_columns, - person_alias_1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - community_follower::all_columns.nullable(), - comment_saved::all_columns.nullable(), - person_block::all_columns.nullable(), - comment_like::score.nullable(), - )) - .into_boxed(); - - if let Some(recipient_id) = self.recipient_id { - query = query.filter(person_mention::recipient_id.eq(recipient_id)); - } - - if self.unread_only.unwrap_or(false) { - query = query.filter(person_mention::read.eq(false)); - } - - if !self.show_bot_accounts.unwrap_or(true) { - query = query.filter(person::bot_account.eq(false)); - }; - - query = match self.sort.unwrap_or(CommentSortType::Hot) { - CommentSortType::Hot => query.then_order_by(comment_aggregates::hot_rank.desc()), - CommentSortType::New => query.then_order_by(comment::published.desc()), - CommentSortType::Old => query.then_order_by(comment::published.asc()), - CommentSortType::Top => query.order_by(comment_aggregates::score.desc()), - }; - - let (limit, offset) = limit_and_offset(self.page, self.limit)?; - - let res = query - .limit(limit) - .offset(offset) - .load::(conn) - .await?; - - Ok(res.into_iter().map(PersonMentionView::from_tuple).collect()) + queries().list(pool, self).await } } From d98f54135f4f839c01833a4c93654777c5a90b6c Mon Sep 17 00:00:00 2001 From: dull b Date: Fri, 21 Jul 2023 19:42:10 +0000 Subject: [PATCH 25/28] person_view --- crates/db_views_actor/src/person_view.rs | 199 ++++++++++++----------- 1 file changed, 104 insertions(+), 95 deletions(-) diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index e6baa1fc6b..0f242c08aa 100644 --- a/crates/db_views_actor/src/person_view.rs +++ b/crates/db_views_actor/src/person_view.rs @@ -1,6 +1,7 @@ use crate::structs::PersonView; use diesel::{ dsl::{now, IntervalDsl}, + pg::Pg, result::Error, BoolExpressionMethods, ExpressionMethods, @@ -15,23 +16,112 @@ use lemmy_db_schema::{ schema::{person, person_aggregates}, source::person::Person, traits::JoinView, - utils::{fuzzy_search, get_conn, limit_and_offset, DbPool}, + utils::{fuzzy_search, get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, SortType, }; -use std::iter::Iterator; type PersonViewTuple = (Person, PersonAggregates); -impl PersonView { - pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { - let conn = &mut get_conn(pool).await?; - let res = person::table - .find(person_id) +enum ListMode { + Admins, + Banned, + Query(PersonQuery), +} + +fn queries<'a>( +) -> Queries, impl ListFn<'a, PersonView, ListMode>> { + let all_joins = |query: person::BoxedQuery<'a, Pg>| { + query .inner_join(person_aggregates::table) .select((person::all_columns, person_aggregates::all_columns)) - .first::(conn) - .await?; - Ok(Self::from_tuple(res)) + }; + + let read = move |mut conn: DbConn<'a>, person_id: PersonId| async move { + all_joins(person::table.find(person_id).into_boxed()) + .first::(&mut conn) + .await + }; + + let list = move |mut conn: DbConn<'a>, mode: ListMode| async move { + let mut query = all_joins(person::table.into_boxed()); + match mode { + ListMode::Admins => { + query = query + .filter(person::admin.eq(true)) + .filter(person::deleted.eq(false)) + .order_by(person::published); + } + ListMode::Banned => { + query = query + .filter( + person::banned.eq(true).and( + person::ban_expires + .is_null() + .or(person::ban_expires.gt(now)), + ), + ) + .filter(person::deleted.eq(false)); + } + ListMode::Query(options) => { + if let Some(search_term) = options.search_term { + let searcher = fuzzy_search(&search_term); + query = query + .filter(person::name.ilike(searcher.clone())) + .or_filter(person::display_name.ilike(searcher)); + } + + query = match options.sort.unwrap_or(SortType::Hot) { + SortType::New | SortType::NewComments => query.order_by(person::published.desc()), + SortType::Old => query.order_by(person::published.asc()), + SortType::Hot | SortType::Active | SortType::TopAll => { + query.order_by(person_aggregates::comment_score.desc()) + } + SortType::MostComments => query.order_by(person_aggregates::comment_count.desc()), + SortType::TopYear => query + .filter(person::published.gt(now - 1.years())) + .order_by(person_aggregates::comment_score.desc()), + SortType::TopMonth => query + .filter(person::published.gt(now - 1.months())) + .order_by(person_aggregates::comment_score.desc()), + SortType::TopWeek => query + .filter(person::published.gt(now - 1.weeks())) + .order_by(person_aggregates::comment_score.desc()), + SortType::TopDay => query + .filter(person::published.gt(now - 1.days())) + .order_by(person_aggregates::comment_score.desc()), + SortType::TopHour => query + .filter(person::published.gt(now - 1.hours())) + .order_by(person_aggregates::comment_score.desc()), + SortType::TopSixHour => query + .filter(person::published.gt(now - 6.hours())) + .order_by(person_aggregates::comment_score.desc()), + SortType::TopTwelveHour => query + .filter(person::published.gt(now - 12.hours())) + .order_by(person_aggregates::comment_score.desc()), + SortType::TopThreeMonths => query + .filter(person::published.gt(now - 3.months())) + .order_by(person_aggregates::comment_score.desc()), + SortType::TopSixMonths => query + .filter(person::published.gt(now - 6.months())) + .order_by(person_aggregates::comment_score.desc()), + SortType::TopNineMonths => query + .filter(person::published.gt(now - 9.months())) + .order_by(person_aggregates::comment_score.desc()), + }; + + let (limit, offset) = limit_and_offset(options.page, options.limit)?; + query = query.limit(limit).offset(offset); + } + } + query.load::(&mut conn).await + }; + + Queries::new(read, list) +} + +impl PersonView { + pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { + queries().read(pool, person_id).await } pub async fn is_admin(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { @@ -44,37 +134,13 @@ impl PersonView { .await?; Ok(is_admin) } - pub async fn admins(pool: &mut DbPool<'_>) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let admins = person::table - .inner_join(person_aggregates::table) - .select((person::all_columns, person_aggregates::all_columns)) - .filter(person::admin.eq(true)) - .filter(person::deleted.eq(false)) - .order_by(person::published) - .load::(conn) - .await?; - Ok(admins.into_iter().map(Self::from_tuple).collect()) + pub async fn admins(pool: &mut DbPool<'_>) -> Result, Error> { + queries().list(pool, ListMode::Admins).await } pub async fn banned(pool: &mut DbPool<'_>) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let banned = person::table - .inner_join(person_aggregates::table) - .select((person::all_columns, person_aggregates::all_columns)) - .filter( - person::banned.eq(true).and( - person::ban_expires - .is_null() - .or(person::ban_expires.gt(now)), - ), - ) - .filter(person::deleted.eq(false)) - .load::(conn) - .await?; - - Ok(banned.into_iter().map(Self::from_tuple).collect()) + queries().list(pool, ListMode::Banned).await } } @@ -88,64 +154,7 @@ pub struct PersonQuery { impl PersonQuery { pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let mut query = person::table - .inner_join(person_aggregates::table) - .select((person::all_columns, person_aggregates::all_columns)) - .into_boxed(); - - if let Some(search_term) = self.search_term { - let searcher = fuzzy_search(&search_term); - query = query - .filter(person::name.ilike(searcher.clone())) - .or_filter(person::display_name.ilike(searcher)); - } - - query = match self.sort.unwrap_or(SortType::Hot) { - SortType::New | SortType::NewComments => query.order_by(person::published.desc()), - SortType::Old => query.order_by(person::published.asc()), - SortType::Hot | SortType::Active | SortType::TopAll => { - query.order_by(person_aggregates::comment_score.desc()) - } - SortType::MostComments => query.order_by(person_aggregates::comment_count.desc()), - SortType::TopYear => query - .filter(person::published.gt(now - 1.years())) - .order_by(person_aggregates::comment_score.desc()), - SortType::TopMonth => query - .filter(person::published.gt(now - 1.months())) - .order_by(person_aggregates::comment_score.desc()), - SortType::TopWeek => query - .filter(person::published.gt(now - 1.weeks())) - .order_by(person_aggregates::comment_score.desc()), - SortType::TopDay => query - .filter(person::published.gt(now - 1.days())) - .order_by(person_aggregates::comment_score.desc()), - SortType::TopHour => query - .filter(person::published.gt(now - 1.hours())) - .order_by(person_aggregates::comment_score.desc()), - SortType::TopSixHour => query - .filter(person::published.gt(now - 6.hours())) - .order_by(person_aggregates::comment_score.desc()), - SortType::TopTwelveHour => query - .filter(person::published.gt(now - 12.hours())) - .order_by(person_aggregates::comment_score.desc()), - SortType::TopThreeMonths => query - .filter(person::published.gt(now - 3.months())) - .order_by(person_aggregates::comment_score.desc()), - SortType::TopSixMonths => query - .filter(person::published.gt(now - 6.months())) - .order_by(person_aggregates::comment_score.desc()), - SortType::TopNineMonths => query - .filter(person::published.gt(now - 9.months())) - .order_by(person_aggregates::comment_score.desc()), - }; - - let (limit, offset) = limit_and_offset(self.page, self.limit)?; - query = query.limit(limit).offset(offset); - - let res = query.load::(conn).await?; - - Ok(res.into_iter().map(PersonView::from_tuple).collect()) + queries().list(pool, ListMode::Query(self)).await } } From bcadb9cc946af3c9e27e3d6c1dbdb01de78e9153 Mon Sep 17 00:00:00 2001 From: dull b Date: Sat, 22 Jul 2023 03:32:25 +0000 Subject: [PATCH 26/28] Use separate community_person_ban joins instead of including boolean literal in join-on clause --- crates/db_views/src/comment_report_view.rs | 68 +++++++++-------- .../db_views_actor/src/person_mention_view.rs | 76 +++++++++---------- 2 files changed, 74 insertions(+), 70 deletions(-) diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index 8a1ae5c992..7f7b8e0ea4 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -3,7 +3,6 @@ use diesel::{ dsl::now, pg::Pg, result::Error, - sql_types, BoolExpressionMethods, ExpressionMethods, JoinOnDsl, @@ -41,9 +40,7 @@ fn queries<'a>() -> Queries< impl ReadFn<'a, CommentReportView, (CommentReportId, PersonId)>, impl ListFn<'a, CommentReportView, (CommentReportQuery, &'a Person)>, > { - let all_joins = |query: comment_report::BoxedQuery<'a, Pg>, - my_person_id: PersonId, - include_expired: bool| { + let all_joins = |query: comment_report::BoxedQuery<'a, Pg>, my_person_id: PersonId| { query .inner_join(comment::table) .inner_join(post::table.on(comment::post_id.eq(post::id))) @@ -53,20 +50,6 @@ fn queries<'a>() -> Queries< .inner_join( comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)), ) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)) - .and( - community_person_ban::expires - .is_null() - .or(community_person_ban::expires.gt(now)) - // TODO: avoid evaluation of expiration condition if include_expired is true - .or::<_, sql_types::Nullable>(include_expired), - ), - ), - ) .left_join( comment_like::table.on( comment::id @@ -78,32 +61,53 @@ fn queries<'a>() -> Queries< aliases::person2 .on(comment_report::resolver_id.eq(aliases::person2.field(person::id).nullable())), ) - .select(( - comment_report::all_columns, - comment::all_columns, - post::all_columns, - community::all_columns, - person::all_columns, - aliases::person1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - comment_like::score.nullable(), - aliases::person2.fields(person::all_columns).nullable(), - )) }; + let selection = ( + comment_report::all_columns, + comment::all_columns, + post::all_columns, + community::all_columns, + person::all_columns, + aliases::person1.fields(person::all_columns), + comment_aggregates::all_columns, + community_person_ban::all_columns.nullable(), + comment_like::score.nullable(), + aliases::person2.fields(person::all_columns).nullable(), + ); + let read = move |mut conn: DbConn<'a>, (report_id, my_person_id): (CommentReportId, PersonId)| async move { all_joins( comment_report::table.find(report_id).into_boxed(), my_person_id, - true, ) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)), + ), + ) + .select(selection) .first::<::JoinTuple>(&mut conn) .await }; let list = move |mut conn: DbConn<'a>, (options, my_person): (CommentReportQuery, &'a Person)| async move { - let mut query = all_joins(comment_report::table.into_boxed(), my_person.id, false); + let mut query = all_joins(comment_report::table.into_boxed(), my_person.id) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)) + .and( + community_person_ban::expires + .is_null() + .or(community_person_ban::expires.gt(now)), + ), + ), + ) + .select(selection); if let Some(community_id) = options.community_id { query = query.filter(post::community_id.eq(community_id)); diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index 574dc55356..66ba79d255 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -3,7 +3,6 @@ use diesel::{ dsl::now, pg::Pg, result::Error, - sql_types, BoolExpressionMethods, ExpressionMethods, JoinOnDsl, @@ -60,9 +59,7 @@ fn queries<'a>() -> Queries< impl ReadFn<'a, PersonMentionView, (PersonMentionId, Option)>, impl ListFn<'a, PersonMentionView, PersonMentionQuery>, > { - let all_joins = |query: person_mention::BoxedQuery<'a, Pg>, - include_expired_bans: bool, - my_person_id: Option| { + let all_joins = |query: person_mention::BoxedQuery<'a, Pg>, my_person_id: Option| { // The left join below will return None in this case let person_id_join = my_person_id.unwrap_or(PersonId(-1)); @@ -73,20 +70,6 @@ fn queries<'a>() -> Queries< .inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(aliases::person1) .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id))) - .left_join( - community_person_ban::table.on( - community::id - .eq(community_person_ban::community_id) - .and(community_person_ban::person_id.eq(comment::creator_id)) - .and( - community_person_ban::expires - .is_null() - .or(community_person_ban::expires.gt(now)) - // TODO: avoid evaluation of expiration condition if include_expired_bans is true - .or::<_, sql_types::Nullable>(include_expired_bans), - ), - ), - ) .left_join( community_follower::table.on( post::community_id @@ -115,40 +98,57 @@ fn queries<'a>() -> Queries< .and(comment_like::person_id.eq(person_id_join)), ), ) - .select(( - person_mention::all_columns, - comment::all_columns, - person::all_columns, - post::all_columns, - community::all_columns, - aliases::person1.fields(person::all_columns), - comment_aggregates::all_columns, - community_person_ban::all_columns.nullable(), - community_follower::all_columns.nullable(), - comment_saved::all_columns.nullable(), - person_block::all_columns.nullable(), - comment_like::score.nullable(), - )) }; + let selection = ( + person_mention::all_columns, + comment::all_columns, + person::all_columns, + post::all_columns, + community::all_columns, + aliases::person1.fields(person::all_columns), + comment_aggregates::all_columns, + community_person_ban::all_columns.nullable(), + community_follower::all_columns.nullable(), + comment_saved::all_columns.nullable(), + person_block::all_columns.nullable(), + comment_like::score.nullable(), + ); + let read = move |mut conn: DbConn<'a>, (person_mention_id, my_person_id): (PersonMentionId, Option)| async move { all_joins( person_mention::table.find(person_mention_id).into_boxed(), - true, my_person_id, ) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)), + ), + ) + .select(selection) .first::(&mut conn) .await }; let list = move |mut conn: DbConn<'a>, options: PersonMentionQuery| async move { - let mut query = all_joins( - person_mention::table.into_boxed(), - false, - options.my_person_id, - ); + let mut query = all_joins(person_mention::table.into_boxed(), options.my_person_id) + .left_join( + community_person_ban::table.on( + community::id + .eq(community_person_ban::community_id) + .and(community_person_ban::person_id.eq(comment::creator_id)) + .and( + community_person_ban::expires + .is_null() + .or(community_person_ban::expires.gt(now)), + ), + ), + ) + .select(selection); if let Some(recipient_id) = options.recipient_id { query = query.filter(person_mention::recipient_id.eq(recipient_id)); From cc29a429b2bfdc829544a33d1a2b5d442f9054da Mon Sep 17 00:00:00 2001 From: dull b Date: Wed, 26 Jul 2023 21:36:38 +0000 Subject: [PATCH 27/28] Fix comment_view --- crates/db_views/src/comment_view.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index a0cf0a6ac6..32f53ac2be 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -239,7 +239,7 @@ fn queries<'a>() -> Queries< query = query.filter(nlevel(comment::path).le(depth_limit)); // only order if filtering by a post id. DOS potential otherwise and max_depth + !post_id isn't used anyways (afaik) - if self.post_id.is_some() { + if options.post_id.is_some() { // Always order by the parent path first query = query.order_by(subpath(comment::path, 0, -1)); } From dc49a67da99109c492e0be3964a071cf20addf91 Mon Sep 17 00:00:00 2001 From: Dull Bananas Date: Thu, 27 Jul 2023 00:44:38 +0000 Subject: [PATCH 28/28] rerun ci