Simplify queries function

pull/3663/head
dull b 2023-07-19 06:02:51 +00:00
parent 2aec4d68a8
commit d3d309cab3
1 changed files with 11 additions and 22 deletions

View File

@ -11,7 +11,7 @@ use diesel::{
QueryDsl, QueryDsl,
}; };
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use futures::future::BoxFuture; use futures::future::{BoxFuture, FutureExt};
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::CommentAggregates, aggregates::structs::CommentAggregates,
newtypes::{CommentReportId, CommunityId, PersonId}, newtypes::{CommentReportId, CommunityId, PersonId},
@ -36,7 +36,7 @@ use lemmy_db_schema::{
traits::JoinView, traits::JoinView,
utils::{get_conn, limit_and_offset, DbConn, DbPool}, 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); 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 read = move |mut conn: DbConn<'a>, report_id: CommentReportId, my_person_id: PersonId| {
let fut = async move { async move {
let res = full_query( let res = full_query(
comment_report::table.find(report_id).into_boxed(), comment_report::table.find(report_id).into_boxed(),
my_person_id, my_person_id,
@ -111,17 +111,12 @@ fn queries<'a>() -> (
) )
.first::<<CommentReportView as JoinView>::JoinTuple>(&mut conn) .first::<<CommentReportView as JoinView>::JoinTuple>(&mut conn)
.await?; .await?;
Ok::<<CommentReportView as JoinView>::JoinTuple, Error>(res) Ok::<_, Error>(res)
}; }
let b: Pin< .boxed()
Box<
dyn Future<Output = Result<<CommentReportView as JoinView>::JoinTuple, Error>> + Send + '_,
>,
> = Box::pin(fut);
b
}; };
let list = move |mut conn: DbConn<'a>, options: CommentReportQuery, my_person: &'a Person| { 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); let mut query = full_query(comment_report::table.into_boxed(), my_person.id, false);
if let Some(community_id) = options.community_id { 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)?; let (limit, offset) = limit_and_offset(options.page, options.limit)?;
query = query query = query
.order_by(comment_report::published.desc()) .order_by(comment_report::published.desc())
.limit(limit) .limit(limit)
.offset(offset); .offset(offset);
@ -156,16 +152,9 @@ fn queries<'a>() -> (
.load::<<CommentReportView as JoinView>::JoinTuple>(&mut conn) .load::<<CommentReportView as JoinView>::JoinTuple>(&mut conn)
.await? .await?
}; };
Ok::<Vec<<CommentReportView as JoinView>::JoinTuple>, Error>(res) Ok::<_, Error>(res)
}; }
let b: Pin< .boxed()
Box<
dyn Future<Output = Result<Vec<<CommentReportView as JoinView>::JoinTuple>, Error>>
+ Send
+ '_,
>,
> = Box::pin(fut);
b
}; };
(read, list) (read, list)
} }