1 use crate::structs::PersonViewSafe;
2 use diesel::{dsl::*, result::Error, *};
4 aggregates::structs::PersonAggregates,
6 schema::{person, person_aggregates},
7 source::person::{Person, PersonSafe},
8 traits::{MaybeOptional, ToSafe, ViewToVec},
9 utils::{fuzzy_search, limit_and_offset, SortType},
12 type PersonViewSafeTuple = (PersonSafe, PersonAggregates);
15 pub fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
16 let (person, counts) = person::table
18 .inner_join(person_aggregates::table)
19 .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
20 .first::<PersonViewSafeTuple>(conn)?;
21 Ok(Self { person, counts })
24 pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
25 let admins = person::table
26 .inner_join(person_aggregates::table)
27 .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
28 .filter(person::admin.eq(true))
29 .order_by(person::published)
30 .load::<PersonViewSafeTuple>(conn)?;
32 Ok(Self::from_tuple_to_vec(admins))
35 pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
36 let banned = person::table
37 .inner_join(person_aggregates::table)
38 .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
40 person::banned.eq(true).and(
43 .or(person::ban_expires.gt(now)),
46 .load::<PersonViewSafeTuple>(conn)?;
48 Ok(Self::from_tuple_to_vec(banned))
52 pub struct PersonQueryBuilder<'a> {
53 conn: &'a PgConnection,
54 sort: Option<SortType>,
55 search_term: Option<String>,
60 impl<'a> PersonQueryBuilder<'a> {
61 pub fn create(conn: &'a PgConnection) -> Self {
71 pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
72 self.sort = sort.get_optional();
76 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
77 self.search_term = search_term.get_optional();
81 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
82 self.page = page.get_optional();
86 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
87 self.limit = limit.get_optional();
91 pub fn list(self) -> Result<Vec<PersonViewSafe>, Error> {
92 let mut query = person::table
93 .inner_join(person_aggregates::table)
94 .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
97 if let Some(search_term) = self.search_term {
98 query = query.filter(person::name.ilike(fuzzy_search(&search_term)));
101 query = match self.sort.unwrap_or(SortType::Hot) {
102 SortType::Hot => query
103 .order_by(person_aggregates::comment_score.desc())
104 .then_order_by(person::published.desc()),
105 SortType::Active => query
106 .order_by(person_aggregates::comment_score.desc())
107 .then_order_by(person::published.desc()),
108 SortType::New | SortType::MostComments | SortType::NewComments => {
109 query.order_by(person::published.desc())
111 SortType::TopAll => query.order_by(person_aggregates::comment_score.desc()),
112 SortType::TopYear => query
113 .filter(person::published.gt(now - 1.years()))
114 .order_by(person_aggregates::comment_score.desc()),
115 SortType::TopMonth => query
116 .filter(person::published.gt(now - 1.months()))
117 .order_by(person_aggregates::comment_score.desc()),
118 SortType::TopWeek => query
119 .filter(person::published.gt(now - 1.weeks()))
120 .order_by(person_aggregates::comment_score.desc()),
121 SortType::TopDay => query
122 .filter(person::published.gt(now - 1.days()))
123 .order_by(person_aggregates::comment_score.desc()),
126 let (limit, offset) = limit_and_offset(self.page, self.limit);
127 query = query.limit(limit).offset(offset);
129 let res = query.load::<PersonViewSafeTuple>(self.conn)?;
131 Ok(PersonViewSafe::from_tuple_to_vec(res))
135 impl ViewToVec for PersonViewSafe {
136 type DbTuple = PersonViewSafeTuple;
137 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
141 person: a.0.to_owned(),
142 counts: a.1.to_owned(),
144 .collect::<Vec<Self>>()