2019-05-05 05:20:38 +00:00
|
|
|
use super::*;
|
2019-04-03 23:01:20 +00:00
|
|
|
|
|
|
|
table! {
|
|
|
|
community_view (id) {
|
|
|
|
id -> Int4,
|
|
|
|
name -> Varchar,
|
|
|
|
title -> Varchar,
|
|
|
|
description -> Nullable<Text>,
|
|
|
|
category_id -> Int4,
|
|
|
|
creator_id -> Int4,
|
2019-04-20 04:06:25 +00:00
|
|
|
removed -> Bool,
|
2019-04-03 23:01:20 +00:00
|
|
|
published -> Timestamp,
|
|
|
|
updated -> Nullable<Timestamp>,
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted -> Bool,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw -> Bool,
|
2019-04-03 23:01:20 +00:00
|
|
|
creator_name -> Varchar,
|
|
|
|
category_name -> Varchar,
|
|
|
|
number_of_subscribers -> BigInt,
|
|
|
|
number_of_posts -> BigInt,
|
2019-04-04 20:00:19 +00:00
|
|
|
number_of_comments -> BigInt,
|
2019-05-02 05:26:31 +00:00
|
|
|
hot_rank -> Int4,
|
2019-04-05 06:26:38 +00:00
|
|
|
user_id -> Nullable<Int4>,
|
|
|
|
subscribed -> Nullable<Bool>,
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 20:53:32 +00:00
|
|
|
table! {
|
|
|
|
community_moderator_view (id) {
|
|
|
|
id -> Int4,
|
|
|
|
community_id -> Int4,
|
|
|
|
user_id -> Int4,
|
|
|
|
published -> Timestamp,
|
|
|
|
user_name -> Varchar,
|
|
|
|
community_name -> Varchar,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
table! {
|
|
|
|
community_follower_view (id) {
|
|
|
|
id -> Int4,
|
|
|
|
community_id -> Int4,
|
|
|
|
user_id -> Int4,
|
|
|
|
published -> Timestamp,
|
|
|
|
user_name -> Varchar,
|
|
|
|
community_name -> Varchar,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
table! {
|
|
|
|
community_user_ban_view (id) {
|
|
|
|
id -> Int4,
|
|
|
|
community_id -> Int4,
|
|
|
|
user_id -> Int4,
|
|
|
|
published -> Timestamp,
|
|
|
|
user_name -> Varchar,
|
|
|
|
community_name -> Varchar,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
table! {
|
2019-07-13 21:14:59 +00:00
|
|
|
site_view (id) {
|
|
|
|
id -> Int4,
|
|
|
|
name -> Varchar,
|
|
|
|
description -> Nullable<Text>,
|
|
|
|
creator_id -> Int4,
|
|
|
|
published -> Timestamp,
|
|
|
|
updated -> Nullable<Timestamp>,
|
|
|
|
creator_name -> Varchar,
|
|
|
|
number_of_users -> BigInt,
|
|
|
|
number_of_posts -> BigInt,
|
|
|
|
number_of_comments -> BigInt,
|
|
|
|
}
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 23:01:20 +00:00
|
|
|
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
|
|
|
|
#[table_name="community_view"]
|
|
|
|
pub struct CommunityView {
|
|
|
|
pub id: i32,
|
|
|
|
pub name: String,
|
|
|
|
pub title: String,
|
|
|
|
pub description: Option<String>,
|
|
|
|
pub category_id: i32,
|
|
|
|
pub creator_id: i32,
|
2019-04-20 04:06:25 +00:00
|
|
|
pub removed: bool,
|
2019-04-03 23:01:20 +00:00
|
|
|
pub published: chrono::NaiveDateTime,
|
|
|
|
pub updated: Option<chrono::NaiveDateTime>,
|
2019-04-29 19:14:54 +00:00
|
|
|
pub deleted: bool,
|
2019-08-14 02:52:43 +00:00
|
|
|
pub nsfw: bool,
|
2019-04-03 23:01:20 +00:00
|
|
|
pub creator_name: String,
|
|
|
|
pub category_name: String,
|
|
|
|
pub number_of_subscribers: i64,
|
2019-04-04 20:00:19 +00:00
|
|
|
pub number_of_posts: i64,
|
2019-04-05 06:26:38 +00:00
|
|
|
pub number_of_comments: i64,
|
2019-05-02 05:26:31 +00:00
|
|
|
pub hot_rank: i32,
|
2019-04-05 06:26:38 +00:00
|
|
|
pub user_id: Option<i32>,
|
|
|
|
pub subscribed: Option<bool>,
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CommunityView {
|
2019-04-05 06:26:38 +00:00
|
|
|
pub fn read(conn: &PgConnection, from_community_id: i32, from_user_id: Option<i32>) -> Result<Self, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::community_view::community_view::dsl::*;
|
2019-04-05 06:26:38 +00:00
|
|
|
|
|
|
|
let mut query = community_view.into_boxed();
|
|
|
|
|
|
|
|
query = query.filter(id.eq(from_community_id));
|
|
|
|
|
|
|
|
// The view lets you pass a null user_id, if you're not logged in
|
|
|
|
if let Some(from_user_id) = from_user_id {
|
|
|
|
query = query.filter(user_id.eq(from_user_id));
|
|
|
|
} else {
|
|
|
|
query = query.filter(user_id.is_null());
|
|
|
|
};
|
|
|
|
|
|
|
|
query.first::<Self>(conn)
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 02:52:43 +00:00
|
|
|
pub fn list(
|
|
|
|
conn: &PgConnection,
|
|
|
|
sort: &SortType,
|
|
|
|
from_user_id: Option<i32>,
|
|
|
|
show_nsfw: bool,
|
|
|
|
search_term: Option<String>,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::community_view::community_view::dsl::*;
|
2019-04-05 06:26:38 +00:00
|
|
|
let mut query = community_view.into_boxed();
|
|
|
|
|
2019-04-17 18:30:13 +00:00
|
|
|
let (limit, offset) = limit_and_offset(page, limit);
|
2019-04-10 06:19:12 +00:00
|
|
|
|
2019-08-10 17:32:06 +00:00
|
|
|
if let Some(search_term) = search_term {
|
|
|
|
query = query.filter(name.ilike(fuzzy_search(&search_term)));
|
|
|
|
};
|
|
|
|
|
2019-04-05 06:26:38 +00:00
|
|
|
// The view lets you pass a null user_id, if you're not logged in
|
2019-04-10 06:19:12 +00:00
|
|
|
match sort {
|
2019-07-04 01:52:17 +00:00
|
|
|
SortType::Hot => query = query.order_by(hot_rank.desc())
|
2019-07-04 01:58:24 +00:00
|
|
|
.then_order_by(number_of_subscribers.desc())
|
2019-07-04 01:52:17 +00:00
|
|
|
.filter(user_id.is_null()),
|
2019-07-13 21:14:59 +00:00
|
|
|
SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()),
|
|
|
|
SortType::TopAll => {
|
|
|
|
match from_user_id {
|
|
|
|
Some(from_user_id) => query = query.filter(user_id.eq(from_user_id)).order_by((subscribed.asc(), number_of_subscribers.desc())),
|
|
|
|
None => query = query.order_by(number_of_subscribers.desc()).filter(user_id.is_null())
|
|
|
|
}
|
2019-04-10 06:19:12 +00:00
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
};
|
|
|
|
|
2019-08-14 02:52:43 +00:00
|
|
|
if !show_nsfw {
|
|
|
|
query = query.filter(nsfw.eq(false));
|
|
|
|
};
|
|
|
|
|
2019-04-17 18:30:13 +00:00
|
|
|
query
|
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
|
|
|
.filter(removed.eq(false))
|
2019-04-29 19:14:54 +00:00
|
|
|
.filter(deleted.eq(false))
|
2019-04-17 18:30:13 +00:00
|
|
|
.load::<Self>(conn)
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 20:53:32 +00:00
|
|
|
|
|
|
|
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
|
|
|
|
#[table_name="community_moderator_view"]
|
|
|
|
pub struct CommunityModeratorView {
|
|
|
|
pub id: i32,
|
|
|
|
pub community_id: i32,
|
|
|
|
pub user_id: i32,
|
|
|
|
pub published: chrono::NaiveDateTime,
|
|
|
|
pub user_name : String,
|
|
|
|
pub community_name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommunityModeratorView {
|
|
|
|
pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::community_view::community_moderator_view::dsl::*;
|
2019-04-04 20:53:32 +00:00
|
|
|
community_moderator_view.filter(community_id.eq(from_community_id)).load::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::community_view::community_moderator_view::dsl::*;
|
2019-04-04 20:53:32 +00:00
|
|
|
community_moderator_view.filter(user_id.eq(from_user_id)).load::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 19:14:54 +00:00
|
|
|
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
|
|
|
|
#[table_name="community_follower_view"]
|
|
|
|
pub struct CommunityFollowerView {
|
|
|
|
pub id: i32,
|
|
|
|
pub community_id: i32,
|
|
|
|
pub user_id: i32,
|
|
|
|
pub published: chrono::NaiveDateTime,
|
|
|
|
pub user_name : String,
|
|
|
|
pub community_name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommunityFollowerView {
|
|
|
|
pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::community_view::community_follower_view::dsl::*;
|
2019-04-05 19:14:54 +00:00
|
|
|
community_follower_view.filter(community_id.eq(from_community_id)).load::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::community_view::community_follower_view::dsl::*;
|
2019-04-05 19:14:54 +00:00
|
|
|
community_follower_view.filter(user_id.eq(from_user_id)).load::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
|
|
|
|
#[table_name="community_user_ban_view"]
|
|
|
|
pub struct CommunityUserBanView {
|
|
|
|
pub id: i32,
|
|
|
|
pub community_id: i32,
|
|
|
|
pub user_id: i32,
|
|
|
|
pub published: chrono::NaiveDateTime,
|
|
|
|
pub user_name : String,
|
|
|
|
pub community_name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommunityUserBanView {
|
|
|
|
pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::community_view::community_user_ban_view::dsl::*;
|
2019-04-15 23:12:06 +00:00
|
|
|
community_user_ban_view.filter(community_id.eq(from_community_id)).load::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::community_view::community_user_ban_view::dsl::*;
|
2019-04-15 23:12:06 +00:00
|
|
|
community_user_ban_view.filter(user_id.eq(from_user_id)).load::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(conn: &PgConnection, from_user_id: i32, from_community_id: i32) -> Result<Self, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::community_view::community_user_ban_view::dsl::*;
|
2019-04-15 23:12:06 +00:00
|
|
|
community_user_ban_view
|
|
|
|
.filter(user_id.eq(from_user_id))
|
|
|
|
.filter(community_id.eq(from_community_id))
|
|
|
|
.first::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
|
|
|
|
#[table_name="site_view"]
|
|
|
|
pub struct SiteView {
|
|
|
|
pub id: i32,
|
|
|
|
pub name: String,
|
|
|
|
pub description: Option<String>,
|
|
|
|
pub creator_id: i32,
|
|
|
|
pub published: chrono::NaiveDateTime,
|
|
|
|
pub updated: Option<chrono::NaiveDateTime>,
|
|
|
|
pub creator_name: String,
|
|
|
|
pub number_of_users: i64,
|
|
|
|
pub number_of_posts: i64,
|
|
|
|
pub number_of_comments: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SiteView {
|
|
|
|
pub fn read(conn: &PgConnection) -> Result<Self, Error> {
|
2019-05-03 01:34:21 +00:00
|
|
|
use super::community_view::site_view::dsl::*;
|
2019-04-16 23:04:23 +00:00
|
|
|
site_view.first::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|