2020-07-07 14:54:44 +00:00
|
|
|
use super::user_view::user_fast::BoxedQuery;
|
2020-07-10 18:15:41 +00:00
|
|
|
use crate::{fuzzy_search, limit_and_offset, MaybeOptional, SortType};
|
2020-05-16 14:04:08 +00:00
|
|
|
use diesel::{dsl::*, pg::Pg, result::Error, *};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2019-04-08 05:19:02 +00:00
|
|
|
|
|
|
|
table! {
|
|
|
|
user_view (id) {
|
|
|
|
id -> Int4,
|
2020-04-14 19:12:19 +00:00
|
|
|
actor_id -> Text,
|
2019-04-08 05:19:02 +00:00
|
|
|
name -> Varchar,
|
2020-08-05 16:03:46 +00:00
|
|
|
preferred_username -> Nullable<Varchar>,
|
2019-12-29 20:39:48 +00:00
|
|
|
avatar -> Nullable<Text>,
|
2020-08-05 16:03:46 +00:00
|
|
|
banner -> Nullable<Text>,
|
2020-01-01 20:46:14 +00:00
|
|
|
email -> Nullable<Text>,
|
2020-01-22 21:35:29 +00:00
|
|
|
matrix_user_id -> Nullable<Text>,
|
2020-04-14 19:12:19 +00:00
|
|
|
bio -> Nullable<Text>,
|
|
|
|
local -> Bool,
|
2019-04-16 23:04:23 +00:00
|
|
|
admin -> Bool,
|
|
|
|
banned -> Bool,
|
2020-01-02 21:55:54 +00:00
|
|
|
show_avatars -> Bool,
|
|
|
|
send_notifications_to_email -> 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:00:28 +00:00
|
|
|
table! {
|
2020-07-07 14:54:44 +00:00
|
|
|
user_fast (id) {
|
2020-06-30 01:00:28 +00:00
|
|
|
id -> Int4,
|
|
|
|
actor_id -> Text,
|
|
|
|
name -> Varchar,
|
2020-08-05 16:03:46 +00:00
|
|
|
preferred_username -> Nullable<Varchar>,
|
2020-06-30 01:00:28 +00:00
|
|
|
avatar -> Nullable<Text>,
|
2020-08-05 16:03:46 +00:00
|
|
|
banner -> Nullable<Text>,
|
2020-06-30 01:00:28 +00:00
|
|
|
email -> Nullable<Text>,
|
|
|
|
matrix_user_id -> Nullable<Text>,
|
|
|
|
bio -> Nullable<Text>,
|
|
|
|
local -> Bool,
|
|
|
|
admin -> Bool,
|
|
|
|
banned -> Bool,
|
|
|
|
show_avatars -> Bool,
|
|
|
|
send_notifications_to_email -> Bool,
|
|
|
|
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,
|
|
|
|
)]
|
2020-07-07 14:54:44 +00:00
|
|
|
#[table_name = "user_fast"]
|
2019-04-08 05:19:02 +00:00
|
|
|
pub struct UserView {
|
|
|
|
pub id: i32,
|
2020-04-14 19:12:19 +00:00
|
|
|
pub actor_id: String,
|
2019-04-08 05:19:02 +00:00
|
|
|
pub name: String,
|
2020-08-05 16:03:46 +00:00
|
|
|
pub preferred_username: Option<String>,
|
2019-12-29 20:39:48 +00:00
|
|
|
pub avatar: Option<String>,
|
2020-08-05 16:03:46 +00:00
|
|
|
pub banner: Option<String>,
|
2020-07-27 13:23:08 +00:00
|
|
|
pub email: Option<String>, // TODO this shouldn't be in this view
|
2020-01-22 21:35:29 +00:00
|
|
|
pub matrix_user_id: Option<String>,
|
2020-04-14 19:12:19 +00:00
|
|
|
pub bio: Option<String>,
|
|
|
|
pub local: bool,
|
2019-04-16 23:04:23 +00:00
|
|
|
pub admin: bool,
|
|
|
|
pub banned: bool,
|
2020-07-27 13:23:08 +00:00
|
|
|
pub show_avatars: bool, // TODO this is a setting, probably doesn't need to be here
|
|
|
|
pub send_notifications_to_email: bool, // TODO also never used
|
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,
|
|
|
|
}
|
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
pub struct UserQueryBuilder<'a> {
|
|
|
|
conn: &'a PgConnection,
|
|
|
|
query: BoxedQuery<'a, Pg>,
|
|
|
|
sort: &'a SortType,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> UserQueryBuilder<'a> {
|
|
|
|
pub fn create(conn: &'a PgConnection) -> Self {
|
2020-07-07 14:54:44 +00:00
|
|
|
use super::user_view::user_fast::dsl::*;
|
2019-08-10 17:32:06 +00:00
|
|
|
|
2020-07-07 14:54:44 +00:00
|
|
|
let query = user_fast.into_boxed();
|
2019-08-10 17:32:06 +00:00
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
UserQueryBuilder {
|
|
|
|
conn,
|
|
|
|
query,
|
|
|
|
sort: &SortType::Hot,
|
|
|
|
page: None,
|
|
|
|
limit: None,
|
|
|
|
}
|
|
|
|
}
|
2019-08-10 17:32:06 +00:00
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
pub fn sort(mut self, sort: &'a SortType) -> Self {
|
|
|
|
self.sort = sort;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-10 23:10:39 +00:00
|
|
|
pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
|
2020-07-07 14:54:44 +00:00
|
|
|
use super::user_view::user_fast::dsl::*;
|
2019-12-10 23:10:39 +00:00
|
|
|
if let Some(search_term) = search_term.get_optional() {
|
|
|
|
self.query = self.query.filter(name.ilike(fuzzy_search(&search_term)));
|
2019-12-08 20:39:54 +00:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-10 23:10:39 +00:00
|
|
|
pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
|
|
|
|
self.page = page.get_optional();
|
2019-12-08 20:39:54 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-10 23:10:39 +00:00
|
|
|
pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
|
|
|
|
self.limit = limit.get_optional();
|
2019-12-08 20:39:54 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list(self) -> Result<Vec<UserView>, Error> {
|
2020-07-07 14:54:44 +00:00
|
|
|
use super::user_view::user_fast::dsl::*;
|
2020-09-03 13:48:26 +00:00
|
|
|
use diesel::sql_types::{Nullable, Text};
|
2019-08-10 17:32:06 +00:00
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
let mut query = self.query;
|
|
|
|
|
|
|
|
query = match self.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()),
|
2020-08-05 16:03:46 +00:00
|
|
|
SortType::Active => query
|
|
|
|
.order_by(comment_score.desc())
|
|
|
|
.then_order_by(published.desc()),
|
2019-08-10 17:32:06 +00:00
|
|
|
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-12-08 20:39:54 +00:00
|
|
|
let (limit, offset) = limit_and_offset(self.page, self.limit);
|
2019-09-07 15:35:05 +00:00
|
|
|
query = query.limit(limit).offset(offset);
|
2019-08-10 17:32:06 +00:00
|
|
|
|
2020-09-03 13:48:26 +00:00
|
|
|
// The select is necessary here to not get back emails
|
|
|
|
query = query.select((
|
|
|
|
id,
|
|
|
|
actor_id,
|
|
|
|
name,
|
|
|
|
preferred_username,
|
|
|
|
avatar,
|
|
|
|
banner,
|
|
|
|
"".into_sql::<Nullable<Text>>(),
|
|
|
|
matrix_user_id,
|
|
|
|
bio,
|
|
|
|
local,
|
|
|
|
admin,
|
|
|
|
banned,
|
|
|
|
show_avatars,
|
|
|
|
send_notifications_to_email,
|
|
|
|
published,
|
|
|
|
number_of_posts,
|
|
|
|
post_score,
|
|
|
|
number_of_comments,
|
|
|
|
comment_score,
|
|
|
|
));
|
2019-12-08 20:39:54 +00:00
|
|
|
query.load::<UserView>(self.conn)
|
2019-08-10 17:32:06 +00:00
|
|
|
}
|
2019-12-08 20:39:54 +00:00
|
|
|
}
|
2019-08-10 17:32:06 +00:00
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
impl UserView {
|
2019-04-16 23:04:23 +00:00
|
|
|
pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
2020-07-07 14:54:44 +00:00
|
|
|
use super::user_view::user_fast::dsl::*;
|
2020-07-14 14:09:13 +00:00
|
|
|
use diesel::sql_types::{Nullable, Text};
|
2020-07-13 13:42:03 +00:00
|
|
|
user_fast
|
2020-07-15 14:00:55 +00:00
|
|
|
// The select is necessary here to not get back emails
|
2020-07-14 14:09:13 +00:00
|
|
|
.select((
|
|
|
|
id,
|
|
|
|
actor_id,
|
|
|
|
name,
|
2020-08-05 16:03:46 +00:00
|
|
|
preferred_username,
|
2020-07-14 14:09:13 +00:00
|
|
|
avatar,
|
2020-08-05 16:03:46 +00:00
|
|
|
banner,
|
2020-07-14 14:09:13 +00:00
|
|
|
"".into_sql::<Nullable<Text>>(),
|
|
|
|
matrix_user_id,
|
|
|
|
bio,
|
|
|
|
local,
|
|
|
|
admin,
|
|
|
|
banned,
|
|
|
|
show_avatars,
|
|
|
|
send_notifications_to_email,
|
|
|
|
published,
|
|
|
|
number_of_posts,
|
|
|
|
post_score,
|
|
|
|
number_of_comments,
|
|
|
|
comment_score,
|
|
|
|
))
|
2020-07-13 13:42:03 +00:00
|
|
|
.filter(admin.eq(true))
|
|
|
|
.order_by(published)
|
|
|
|
.load::<Self>(conn)
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
2020-07-07 14:54:44 +00:00
|
|
|
use super::user_view::user_fast::dsl::*;
|
2020-07-14 14:09:13 +00:00
|
|
|
use diesel::sql_types::{Nullable, Text};
|
2020-07-15 14:00:55 +00:00
|
|
|
user_fast
|
2020-07-14 14:09:13 +00:00
|
|
|
.select((
|
|
|
|
id,
|
|
|
|
actor_id,
|
|
|
|
name,
|
2020-08-05 16:03:46 +00:00
|
|
|
preferred_username,
|
2020-07-14 14:09:13 +00:00
|
|
|
avatar,
|
2020-08-05 16:03:46 +00:00
|
|
|
banner,
|
2020-07-14 14:09:13 +00:00
|
|
|
"".into_sql::<Nullable<Text>>(),
|
|
|
|
matrix_user_id,
|
|
|
|
bio,
|
|
|
|
local,
|
|
|
|
admin,
|
|
|
|
banned,
|
|
|
|
show_avatars,
|
|
|
|
send_notifications_to_email,
|
|
|
|
published,
|
|
|
|
number_of_posts,
|
|
|
|
post_score,
|
|
|
|
number_of_comments,
|
|
|
|
comment_score,
|
|
|
|
))
|
|
|
|
.filter(banned.eq(true))
|
|
|
|
.load::<Self>(conn)
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
2020-08-08 02:43:33 +00:00
|
|
|
|
|
|
|
pub fn get_user_secure(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
|
|
|
|
use super::user_view::user_fast::dsl::*;
|
|
|
|
use diesel::sql_types::{Nullable, Text};
|
|
|
|
user_fast
|
|
|
|
.select((
|
|
|
|
id,
|
|
|
|
actor_id,
|
|
|
|
name,
|
|
|
|
preferred_username,
|
|
|
|
avatar,
|
|
|
|
banner,
|
|
|
|
"".into_sql::<Nullable<Text>>(),
|
|
|
|
matrix_user_id,
|
|
|
|
bio,
|
|
|
|
local,
|
|
|
|
admin,
|
|
|
|
banned,
|
|
|
|
show_avatars,
|
|
|
|
send_notifications_to_email,
|
|
|
|
published,
|
|
|
|
number_of_posts,
|
|
|
|
post_score,
|
|
|
|
number_of_comments,
|
|
|
|
comment_score,
|
|
|
|
))
|
|
|
|
.find(user_id)
|
|
|
|
.first::<Self>(conn)
|
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|