mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-16 09:24:00 +00:00
f4565d0603
* One pass at materialized views, only about 30% faster, not good. * Before merging master to test out bans. * DB Rework working, still need more testing. * Fixing accidental addadmin bug from asonix async merge. * Fixing the comment delete trigger * Some more DB additions. - Adding a hot_rank desc, published desc index to post_aggregates_fast. - Removed WITH CTE queries in favor of direct selects (since CTEs cant use indexes) * Removing some unecessary indexes. * Some more DB optimizings - Changing the fast_id pkeys to just ids on the fast tables. - Removing the private_message_fast, since the view contains no aggregates. - Comment and post voting now no longer pull from the views, they update the counts directly. * Adding community_agg_view and post_agg_views Credit: eiknat. * Adding user and comment_view migrations. (comment_view still broken) * Adding more views. Credit Eiknat.
167 lines
4.4 KiB
Rust
167 lines
4.4 KiB
Rust
use super::user_view::user_fast::BoxedQuery;
|
|
use crate::db::{fuzzy_search, limit_and_offset, MaybeOptional, SortType};
|
|
use diesel::{dsl::*, pg::Pg, result::Error, *};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
table! {
|
|
user_view (id) {
|
|
id -> Int4,
|
|
actor_id -> Text,
|
|
name -> Varchar,
|
|
avatar -> Nullable<Text>,
|
|
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,
|
|
}
|
|
}
|
|
|
|
table! {
|
|
user_fast (id) {
|
|
id -> Int4,
|
|
actor_id -> Text,
|
|
name -> Varchar,
|
|
avatar -> Nullable<Text>,
|
|
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,
|
|
}
|
|
}
|
|
|
|
#[derive(
|
|
Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
|
|
)]
|
|
#[table_name = "user_fast"]
|
|
pub struct UserView {
|
|
pub id: i32,
|
|
pub actor_id: String,
|
|
pub name: String,
|
|
pub avatar: Option<String>,
|
|
pub email: Option<String>,
|
|
pub matrix_user_id: Option<String>,
|
|
pub bio: Option<String>,
|
|
pub local: bool,
|
|
pub admin: bool,
|
|
pub banned: bool,
|
|
pub show_avatars: bool,
|
|
pub send_notifications_to_email: bool,
|
|
pub published: chrono::NaiveDateTime,
|
|
pub number_of_posts: i64,
|
|
pub post_score: i64,
|
|
pub number_of_comments: i64,
|
|
pub comment_score: i64,
|
|
}
|
|
|
|
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 {
|
|
use super::user_view::user_fast::dsl::*;
|
|
|
|
let query = user_fast.into_boxed();
|
|
|
|
UserQueryBuilder {
|
|
conn,
|
|
query,
|
|
sort: &SortType::Hot,
|
|
page: None,
|
|
limit: None,
|
|
}
|
|
}
|
|
|
|
pub fn sort(mut self, sort: &'a SortType) -> Self {
|
|
self.sort = sort;
|
|
self
|
|
}
|
|
|
|
pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
|
|
use super::user_view::user_fast::dsl::*;
|
|
if let Some(search_term) = search_term.get_optional() {
|
|
self.query = self.query.filter(name.ilike(fuzzy_search(&search_term)));
|
|
}
|
|
self
|
|
}
|
|
|
|
pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
|
|
self.page = page.get_optional();
|
|
self
|
|
}
|
|
|
|
pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
|
|
self.limit = limit.get_optional();
|
|
self
|
|
}
|
|
|
|
pub fn list(self) -> Result<Vec<UserView>, Error> {
|
|
use super::user_view::user_fast::dsl::*;
|
|
|
|
let mut query = self.query;
|
|
|
|
query = match self.sort {
|
|
SortType::Hot => query
|
|
.order_by(comment_score.desc())
|
|
.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()),
|
|
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()),
|
|
};
|
|
|
|
let (limit, offset) = limit_and_offset(self.page, self.limit);
|
|
query = query.limit(limit).offset(offset);
|
|
|
|
query.load::<UserView>(self.conn)
|
|
}
|
|
}
|
|
|
|
impl UserView {
|
|
pub fn read(conn: &PgConnection, from_user_id: i32) -> Result<Self, Error> {
|
|
use super::user_view::user_fast::dsl::*;
|
|
user_fast.find(from_user_id).first::<Self>(conn)
|
|
}
|
|
|
|
pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
|
use super::user_view::user_fast::dsl::*;
|
|
user_fast.filter(admin.eq(true)).load::<Self>(conn)
|
|
}
|
|
|
|
pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
|
use super::user_view::user_fast::dsl::*;
|
|
user_fast.filter(banned.eq(true)).load::<Self>(conn)
|
|
}
|
|
}
|