1 use diesel::{dsl::*, result::Error, *};
2 use lemmy_db_queries::{
3 aggregates::person_aggregates::PersonAggregates,
11 use lemmy_db_schema::{
12 schema::{person, person_aggregates},
13 source::person::{Person, PersonSafe},
18 #[derive(Debug, Serialize, Clone)]
19 pub struct PersonViewSafe {
20 pub person: PersonSafe,
21 pub counts: PersonAggregates,
24 type PersonViewSafeTuple = (PersonSafe, PersonAggregates);
27 pub fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
28 let (person, counts) = person::table
30 .inner_join(person_aggregates::table)
31 .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
32 .first::<PersonViewSafeTuple>(conn)?;
33 Ok(Self { person, counts })
36 pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
37 let admins = person::table
38 .inner_join(person_aggregates::table)
39 .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
40 .filter(person::admin.eq(true))
41 .order_by(person::published)
42 .load::<PersonViewSafeTuple>(conn)?;
44 Ok(Self::from_tuple_to_vec(admins))
47 pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
48 let banned = person::table
49 .inner_join(person_aggregates::table)
50 .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
51 .filter(person::banned.eq(true))
52 .load::<PersonViewSafeTuple>(conn)?;
54 Ok(Self::from_tuple_to_vec(banned))
58 pub struct PersonQueryBuilder<'a> {
59 conn: &'a PgConnection,
60 sort: Option<SortType>,
61 search_term: Option<String>,
66 impl<'a> PersonQueryBuilder<'a> {
67 pub fn create(conn: &'a PgConnection) -> Self {
77 pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
78 self.sort = sort.get_optional();
82 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
83 self.search_term = search_term.get_optional();
87 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
88 self.page = page.get_optional();
92 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
93 self.limit = limit.get_optional();
97 pub fn list(self) -> Result<Vec<PersonViewSafe>, Error> {
98 let mut query = person::table
99 .inner_join(person_aggregates::table)
100 .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
103 if let Some(search_term) = self.search_term {
104 query = query.filter(person::name.ilike(fuzzy_search(&search_term)));
107 query = match self.sort.unwrap_or(SortType::Hot) {
108 SortType::Hot => query
109 .order_by(person_aggregates::comment_score.desc())
110 .then_order_by(person::published.desc()),
111 SortType::Active => query
112 .order_by(person_aggregates::comment_score.desc())
113 .then_order_by(person::published.desc()),
114 SortType::New | SortType::MostComments | SortType::NewComments => {
115 query.order_by(person::published.desc())
117 SortType::TopAll => query.order_by(person_aggregates::comment_score.desc()),
118 SortType::TopYear => query
119 .filter(person::published.gt(now - 1.years()))
120 .order_by(person_aggregates::comment_score.desc()),
121 SortType::TopMonth => query
122 .filter(person::published.gt(now - 1.months()))
123 .order_by(person_aggregates::comment_score.desc()),
124 SortType::TopWeek => query
125 .filter(person::published.gt(now - 1.weeks()))
126 .order_by(person_aggregates::comment_score.desc()),
127 SortType::TopDay => query
128 .filter(person::published.gt(now - 1.days()))
129 .order_by(person_aggregates::comment_score.desc()),
132 let (limit, offset) = limit_and_offset(self.page, self.limit);
133 query = query.limit(limit).offset(offset);
135 let res = query.load::<PersonViewSafeTuple>(self.conn)?;
137 Ok(PersonViewSafe::from_tuple_to_vec(res))
141 impl ViewToVec for PersonViewSafe {
142 type DbTuple = PersonViewSafeTuple;
143 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
147 person: a.0.to_owned(),
148 counts: a.1.to_owned(),
150 .collect::<Vec<Self>>()