2019-05-05 05:20:38 +00:00
|
|
|
use super::*;
|
2019-04-08 05:19:02 +00:00
|
|
|
|
|
|
|
table! {
|
|
|
|
user_view (id) {
|
|
|
|
id -> Int4,
|
|
|
|
name -> Varchar,
|
|
|
|
fedi_name -> Varchar,
|
2019-04-16 23:04:23 +00:00
|
|
|
admin -> Bool,
|
|
|
|
banned -> Bool,
|
2019-04-08 05:19:02 +00:00
|
|
|
published -> Timestamp,
|
|
|
|
number_of_posts -> BigInt,
|
|
|
|
post_score -> BigInt,
|
|
|
|
number_of_comments -> BigInt,
|
|
|
|
comment_score -> BigInt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
#[derive(
|
|
|
|
Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
|
|
|
|
)]
|
|
|
|
#[table_name = "user_view"]
|
2019-04-08 05:19:02 +00:00
|
|
|
pub struct UserView {
|
|
|
|
pub id: i32,
|
|
|
|
pub name: String,
|
|
|
|
pub fedi_name: String,
|
2019-04-16 23:04:23 +00:00
|
|
|
pub admin: bool,
|
|
|
|
pub banned: bool,
|
2019-04-08 05:19:02 +00:00
|
|
|
pub published: chrono::NaiveDateTime,
|
|
|
|
pub number_of_posts: i64,
|
|
|
|
pub post_score: i64,
|
|
|
|
pub number_of_comments: i64,
|
|
|
|
pub comment_score: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserView {
|
2019-09-07 15:35:05 +00:00
|
|
|
pub fn list(
|
|
|
|
conn: &PgConnection,
|
|
|
|
sort: &SortType,
|
|
|
|
search_term: Option<String>,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
2019-08-10 17:32:06 +00:00
|
|
|
use super::user_view::user_view::dsl::*;
|
|
|
|
|
|
|
|
let (limit, offset) = limit_and_offset(page, limit);
|
|
|
|
|
|
|
|
let mut query = user_view.into_boxed();
|
|
|
|
|
|
|
|
if let Some(search_term) = search_term {
|
|
|
|
query = query.filter(name.ilike(fuzzy_search(&search_term)));
|
|
|
|
};
|
|
|
|
|
|
|
|
query = match sort {
|
2019-09-07 15:35:05 +00:00
|
|
|
SortType::Hot => query
|
|
|
|
.order_by(comment_score.desc())
|
2019-08-10 17:32:06 +00:00
|
|
|
.then_order_by(published.desc()),
|
|
|
|
SortType::New => query.order_by(published.desc()),
|
|
|
|
SortType::TopAll => query.order_by(comment_score.desc()),
|
|
|
|
SortType::TopYear => query
|
|
|
|
.filter(published.gt(now - 1.years()))
|
|
|
|
.order_by(comment_score.desc()),
|
2019-09-07 15:35:05 +00:00
|
|
|
SortType::TopMonth => query
|
|
|
|
.filter(published.gt(now - 1.months()))
|
|
|
|
.order_by(comment_score.desc()),
|
|
|
|
SortType::TopWeek => query
|
|
|
|
.filter(published.gt(now - 1.weeks()))
|
|
|
|
.order_by(comment_score.desc()),
|
|
|
|
SortType::TopDay => query
|
|
|
|
.filter(published.gt(now - 1.days()))
|
|
|
|
.order_by(comment_score.desc()),
|
2019-08-10 17:32:06 +00:00
|
|
|
};
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
query = query.limit(limit).offset(offset);
|
2019-08-10 17:32:06 +00:00
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
query.load::<Self>(conn)
|
2019-08-10 17:32:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
pub fn read(conn: &PgConnection, from_user_id: i32) -> Result<Self, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::user_view::user_view::dsl::*;
|
2019-04-08 05:19:02 +00:00
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
user_view.find(from_user_id).first::<Self>(conn)
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::user_view::user_view::dsl::*;
|
2019-09-07 15:35:05 +00:00
|
|
|
user_view.filter(admin.eq(true)).load::<Self>(conn)
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::user_view::user_view::dsl::*;
|
2019-09-07 15:35:05 +00:00
|
|
|
user_view.filter(banned.eq(true)).load::<Self>(conn)
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|