use lemmy_db_schema::{
aggregates::structs::PersonAggregates,
newtypes::PersonId,
+ schema,
schema::{person, person_aggregates},
source::person::Person,
traits::JoinView,
type PersonViewTuple = (Person, PersonAggregates);
impl PersonView {
- pub async fn read(pool: &DbPool, person_id: PersonId) -> Result<Self, Error> {
+ pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
let res = person::table
.find(person_id)
Ok(Self::from_tuple(res))
}
- pub async fn admins(pool: &DbPool) -> Result<Vec<Self>, Error> {
+ pub async fn is_admin(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<bool, Error> {
+ use schema::person::dsl::{admin, id, person};
+ let conn = &mut get_conn(pool).await?;
+ let is_admin = person
+ .filter(id.eq(person_id))
+ .select(admin)
+ .first::<bool>(conn)
+ .await?;
+ Ok(is_admin)
+ }
+ pub async fn admins(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?;
let admins = person::table
.inner_join(person_aggregates::table)
Ok(admins.into_iter().map(Self::from_tuple).collect())
}
- pub async fn banned(pool: &DbPool) -> Result<Vec<Self>, Error> {
+ pub async fn banned(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?;
let banned = person::table
.inner_join(person_aggregates::table)
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
-pub struct PersonQuery<'a> {
+pub struct PersonQuery<'a, 'b: 'a> {
#[builder(!default)]
- pool: &'a DbPool,
+ pool: &'a mut DbPool<'b>,
sort: Option<SortType>,
search_term: Option<String>,
page: Option<i64>,
limit: Option<i64>,
}
-impl<'a> PersonQuery<'a> {
+impl<'a, 'b: 'a> PersonQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<PersonView>, Error> {
let conn = &mut get_conn(self.pool).await?;
let mut query = person::table
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)?;