Try more stuff

This commit is contained in:
dull b 2023-07-19 01:41:16 +00:00
parent a1fa3ee284
commit 47000ff9a8

View file

@ -3,11 +3,14 @@ use diesel::{
dsl, dsl,
dsl::now, dsl::now,
helper_types::AliasedFields, helper_types::AliasedFields,
query_builder::{AsQuery, Query}, pg::Pg,
query_builder::{AsQuery, Query, QueryFragment, QueryId, SelectQuery},
query_dsl::methods, query_dsl::methods,
result::Error, result::Error,
sql_types, sql_types,
sql_types::Nullable,
BoolExpressionMethods, BoolExpressionMethods,
BoxableExpression,
Expression, Expression,
ExpressionMethods, ExpressionMethods,
JoinOnDsl, JoinOnDsl,
@ -16,7 +19,7 @@ use diesel::{
QueryDsl, QueryDsl,
Table, Table,
}; };
use diesel_async::RunQueryDsl; use diesel_async::{methods::LoadQuery, RunQueryDsl};
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::CommentAggregates, aggregates::structs::CommentAggregates,
newtypes::{CommentReportId, CommunityId, PersonId}, newtypes::{CommentReportId, CommunityId, PersonId},
@ -39,46 +42,58 @@ use lemmy_db_schema::{
post::Post, post::Post,
}, },
traits::JoinView, 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); diesel::alias!(person as person_alias_1: PersonAlias1, person as person_alias_2:PersonAlias2);
type Ac<T> = <T as Table>::AllColumns; type Selection = (
<comment_report::table as Table>::AllColumns,
<comment::table as Table>::AllColumns,
<post::table as Table>::AllColumns,
<community::table as Table>::AllColumns,
<person::table as Table>::AllColumns,
AliasedFields<PersonAlias1, <person::table as Table>::AllColumns>,
<comment_aggregates::table as Table>::AllColumns,
dsl::Nullable<<community_person_ban::table as Table>::AllColumns>,
dsl::Nullable<comment_like::score>,
dsl::Nullable<AliasedFields<PersonAlias2, <person::table as Table>::AllColumns>>,
);
fn full_query<'query, Conn, Q, C, R>( /*trait BoxedFilter<T>:methods::FilterDsl<T,Output=Self> {}
query: comment_report::table,
impl<T:methods::FilterDsl
trait BoxedJoin: 'static + methods::LimitDsl<Output = Self> + LoadQuery + Send + AsQuery<SqlType = <Selection as Expression>::SqlType>+FilterDsl<dsl::Eq<post::community_id,CommunityId>,Output=Self> where <Self as AsQuery>::Query: QueryFragment<Pg>+QueryId+Send+'static {}
impl<T: 'static + methods::LimitDsl<Output = T> + LoadQuery + Send + AsQuery<SqlType = <Selection as Expression>::SqlType>> BoxedJoin for T where <T as AsQuery>::Query: QueryFragment<Pg>+QueryId+Send+'static {}*/
fn full_query<'a>(
mut query: comment_report::BoxedQuery<'a, Pg>,
//report_id: Option<CommentReportId>,
my_person_id: PersonId, my_person_id: PersonId,
community_person_ban_condition: C, include_expired: bool,
) -> dsl::Select< ) -> impl 'a + SelectQuery<SqlType = <Selection as Expression>::SqlType>
impl Query + Table, /*comment_report::BoxedQuery<
( '_,
Ac<comment_report::table>, Pg,
Ac<comment::table>, <Selection as Expression>::SqlType,
Ac<post::table>, /*(
Ac<community::table>, comment_report::SqlType,
Ac<person::table>, comment::SqlType,
AliasedFields<person::table, Ac<person::table>>, post::SqlType,
Ac<comment_aggregates::table>, community::SqlType,
dsl::NullableSelect<Ac<community_person_ban::table>>, person::SqlType,
dsl::NullableSelect<comment_like::score>, AliasedFields<PersonAlias1, <person::table as Table>::AllColumns>,
dsl::NullableSelect<AliasedFields<person::table, Ac<person::table>>>, comment_aggregates::SqlType,
), Nullable<community_person_ban::SqlType>,
> Nullable<<comment_like::score as Expression>::SqlType>,
where Nullable<AliasedFields<PersonAlias2, <person::table as Table>::AllColumns>>,
/*Q: AsQuery, )*/
<Q as AsQuery>::Query: Table, /*< >*/ {
SqlType = <comment_report::table as AsQuery>::SqlType, //if let Some(report_id) = report_id {query = query.find(report_id);}
AllColumns = <comment_report::table as Table>::AllColumns, query
>*/*/ .inner_join(comment::table.on(comment_report::comment_id.eq(comment::id)))
C: Expression<SqlType = sql_types::Nullable<sql_types::Bool>> + Clone,
R: methods::LimitDsl,
dsl::Limit<R>: methods::LoadQuery<'query, Conn, <CommentReportView as JoinView>::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(post::table.on(comment::post_id.eq(post::id)))
.inner_join(community::table.on(post::community_id.eq(community::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::table.on(comment_report::creator_id.eq(person::id)))
@ -91,7 +106,13 @@ where
community::id community::id
.eq(community_person_ban::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_condition), .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( .left_join(
@ -102,9 +123,10 @@ where
), ),
) )
.left_join( .left_join(
person_alias_2.on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())), person_alias_2
.on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
) )
.select(( .select::<Selection>((
comment_report::all_columns, comment_report::all_columns,
comment::all_columns, comment::all_columns,
post::all_columns, post::all_columns,
@ -116,6 +138,7 @@ where
comment_like::score.nullable(), comment_like::score.nullable(),
person_alias_2.fields(person::all_columns).nullable(), person_alias_2.fields(person::all_columns).nullable(),
)) ))
//.into_boxed()
} }
impl CommentReportView { impl CommentReportView {
@ -129,7 +152,11 @@ impl CommentReportView {
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
let res = full_query(comment_report::table.find(report_id), my_person_id, true) let res = full_query(
comment_report::table.find(report_id).into_boxed(),
my_person_id,
true,
)
.first::<<CommentReportView as JoinView>::JoinTuple>(conn) .first::<<CommentReportView as JoinView>::JoinTuple>(conn)
.await?; .await?;
@ -195,14 +222,7 @@ impl CommentReportQuery {
) -> Result<Vec<CommentReportView>, Error> { ) -> Result<Vec<CommentReportView>, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
let mut query = full_query( let mut query = full_query(comment_report::table.into_boxed(), my_person.id, false);
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 { if let Some(community_id) = self.community_id {
query = query.filter(post::community_id.eq(community_id)); query = query.filter(post::community_id.eq(community_id));