2020-06-30 00:02:38 +00:00
|
|
|
use super::community_view::community_view::BoxedQuery;
|
2020-05-16 14:04:08 +00:00
|
|
|
use crate::db::{fuzzy_search, limit_and_offset, MaybeOptional, SortType};
|
|
|
|
use diesel::{pg::Pg, result::Error, *};
|
|
|
|
use serde::{Deserialize, Serialize};
|
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,
|
2020-04-14 19:12:19 +00:00
|
|
|
actor_id -> Text,
|
|
|
|
local -> Bool,
|
|
|
|
last_refreshed_at -> Timestamp,
|
|
|
|
creator_actor_id -> Text,
|
|
|
|
creator_local -> Bool,
|
2019-04-03 23:01:20 +00:00
|
|
|
creator_name -> Varchar,
|
2019-12-29 20:39:48 +00:00
|
|
|
creator_avatar -> Nullable<Text>,
|
2019-04-03 23:01:20 +00:00
|
|
|
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,
|
2020-04-14 19:12:19 +00:00
|
|
|
user_actor_id -> Text,
|
|
|
|
user_local -> Bool,
|
2019-04-04 20:53:32 +00:00
|
|
|
user_name -> Varchar,
|
2019-12-29 20:39:48 +00:00
|
|
|
avatar -> Nullable<Text>,
|
2020-04-14 19:12:19 +00:00
|
|
|
community_actor_id -> Text,
|
|
|
|
community_local -> Bool,
|
2019-04-04 20:53:32 +00:00
|
|
|
community_name -> Varchar,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
table! {
|
|
|
|
community_follower_view (id) {
|
|
|
|
id -> Int4,
|
|
|
|
community_id -> Int4,
|
|
|
|
user_id -> Int4,
|
|
|
|
published -> Timestamp,
|
2020-04-14 19:12:19 +00:00
|
|
|
user_actor_id -> Text,
|
|
|
|
user_local -> Bool,
|
2019-04-04 20:53:32 +00:00
|
|
|
user_name -> Varchar,
|
2019-12-29 20:39:48 +00:00
|
|
|
avatar -> Nullable<Text>,
|
2020-04-14 19:12:19 +00:00
|
|
|
community_actor_id -> Text,
|
|
|
|
community_local -> Bool,
|
2019-04-04 20:53:32 +00:00
|
|
|
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,
|
2020-04-14 19:12:19 +00:00
|
|
|
user_actor_id -> Text,
|
|
|
|
user_local -> Bool,
|
2019-04-15 23:12:06 +00:00
|
|
|
user_name -> Varchar,
|
2019-12-29 20:39:48 +00:00
|
|
|
avatar -> Nullable<Text>,
|
2020-04-14 19:12:19 +00:00
|
|
|
community_actor_id -> Text,
|
|
|
|
community_local -> Bool,
|
2019-04-15 23:12:06 +00:00
|
|
|
community_name -> Varchar,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
#[derive(
|
|
|
|
Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
|
|
|
|
)]
|
|
|
|
#[table_name = "community_view"]
|
2019-04-03 23:01:20 +00:00
|
|
|
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,
|
2020-04-14 19:12:19 +00:00
|
|
|
pub actor_id: String,
|
|
|
|
pub local: bool,
|
|
|
|
pub last_refreshed_at: chrono::NaiveDateTime,
|
|
|
|
pub creator_actor_id: String,
|
|
|
|
pub creator_local: bool,
|
2019-04-03 23:01:20 +00:00
|
|
|
pub creator_name: String,
|
2019-12-29 20:39:48 +00:00
|
|
|
pub creator_avatar: Option<String>,
|
2019-04-03 23:01:20 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
pub struct CommunityQueryBuilder<'a> {
|
|
|
|
conn: &'a PgConnection,
|
|
|
|
query: BoxedQuery<'a, Pg>,
|
|
|
|
sort: &'a SortType,
|
|
|
|
from_user_id: Option<i32>,
|
|
|
|
show_nsfw: bool,
|
|
|
|
search_term: Option<String>,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> CommunityQueryBuilder<'a> {
|
|
|
|
pub fn create(conn: &'a PgConnection) -> Self {
|
2020-06-30 00:02:38 +00:00
|
|
|
use super::community_view::community_view::dsl::*;
|
2019-04-05 06:26:38 +00:00
|
|
|
|
2020-06-30 00:02:38 +00:00
|
|
|
let query = community_view.into_boxed();
|
2019-04-05 06:26:38 +00:00
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
CommunityQueryBuilder {
|
|
|
|
conn,
|
|
|
|
query,
|
|
|
|
sort: &SortType::Hot,
|
|
|
|
from_user_id: None,
|
|
|
|
show_nsfw: true,
|
|
|
|
search_term: None,
|
|
|
|
page: None,
|
|
|
|
limit: None,
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 06:26:38 +00:00
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
pub fn sort(mut self, sort: &'a SortType) -> Self {
|
|
|
|
self.sort = sort;
|
|
|
|
self
|
|
|
|
}
|
2019-04-05 06:26:38 +00:00
|
|
|
|
2020-01-02 11:30:00 +00:00
|
|
|
pub fn for_user<T: MaybeOptional<i32>>(mut self, from_user_id: T) -> Self {
|
2019-12-10 23:10:39 +00:00
|
|
|
self.from_user_id = from_user_id.get_optional();
|
2019-12-08 20:39:54 +00:00
|
|
|
self
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
|
|
|
|
self.show_nsfw = show_nsfw;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-10 23:10:39 +00:00
|
|
|
pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
|
|
|
|
self.search_term = search_term.get_optional();
|
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<CommunityView>, Error> {
|
2020-06-30 00:02:38 +00:00
|
|
|
use super::community_view::community_view::dsl::*;
|
2019-04-05 06:26:38 +00:00
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
let mut query = self.query;
|
2019-04-10 06:19:12 +00:00
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
if let Some(search_term) = self.search_term {
|
2020-03-08 23:14:52 +00:00
|
|
|
let searcher = fuzzy_search(&search_term);
|
|
|
|
query = query
|
|
|
|
.filter(name.ilike(searcher.to_owned()))
|
|
|
|
.or_filter(title.ilike(searcher.to_owned()))
|
|
|
|
.or_filter(description.ilike(searcher));
|
2019-08-10 17:32:06 +00:00
|
|
|
};
|
|
|
|
|
2019-04-05 06:26:38 +00:00
|
|
|
// The view lets you pass a null user_id, if you're not logged in
|
2019-12-08 20:39:54 +00:00
|
|
|
match self.sort {
|
2019-09-07 15:35:05 +00:00
|
|
|
SortType::Hot => {
|
|
|
|
query = query
|
|
|
|
.order_by(hot_rank.desc())
|
|
|
|
.then_order_by(number_of_subscribers.desc())
|
|
|
|
.filter(user_id.is_null())
|
|
|
|
}
|
|
|
|
SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()),
|
2019-12-08 20:39:54 +00:00
|
|
|
SortType::TopAll => match self.from_user_id {
|
2019-09-07 15:35:05 +00:00
|
|
|
Some(from_user_id) => {
|
|
|
|
query = query
|
|
|
|
.filter(user_id.eq(from_user_id))
|
|
|
|
.order_by((subscribed.asc(), number_of_subscribers.desc()))
|
2019-04-10 06:19:12 +00:00
|
|
|
}
|
2019-09-07 15:35:05 +00:00
|
|
|
None => {
|
|
|
|
query = query
|
|
|
|
.order_by(number_of_subscribers.desc())
|
|
|
|
.filter(user_id.is_null())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
2019-04-10 06:19:12 +00:00
|
|
|
};
|
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
if !self.show_nsfw {
|
2019-08-14 02:52:43 +00:00
|
|
|
query = query.filter(nsfw.eq(false));
|
|
|
|
};
|
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
let (limit, offset) = limit_and_offset(self.page, self.limit);
|
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-12-08 20:39:54 +00:00
|
|
|
.load::<CommunityView>(self.conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommunityView {
|
|
|
|
pub fn read(
|
|
|
|
conn: &PgConnection,
|
|
|
|
from_community_id: i32,
|
|
|
|
from_user_id: Option<i32>,
|
|
|
|
) -> Result<Self, Error> {
|
2020-06-30 00:02:38 +00:00
|
|
|
use super::community_view::community_view::dsl::*;
|
2019-12-08 20:39:54 +00:00
|
|
|
|
2020-06-30 00:02:38 +00:00
|
|
|
let mut query = community_view.into_boxed();
|
2019-12-08 20:39:54 +00:00
|
|
|
|
|
|
|
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-09-07 15:35:05 +00:00
|
|
|
#[derive(
|
|
|
|
Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
|
|
|
|
)]
|
|
|
|
#[table_name = "community_moderator_view"]
|
2019-04-04 20:53:32 +00:00
|
|
|
pub struct CommunityModeratorView {
|
|
|
|
pub id: i32,
|
|
|
|
pub community_id: i32,
|
|
|
|
pub user_id: i32,
|
|
|
|
pub published: chrono::NaiveDateTime,
|
2020-04-14 19:12:19 +00:00
|
|
|
pub user_actor_id: String,
|
|
|
|
pub user_local: bool,
|
2019-09-07 15:35:05 +00:00
|
|
|
pub user_name: String,
|
2019-12-29 20:39:48 +00:00
|
|
|
pub avatar: Option<String>,
|
2020-04-14 19:12:19 +00:00
|
|
|
pub community_actor_id: String,
|
|
|
|
pub community_local: bool,
|
2019-04-04 20:53:32 +00:00
|
|
|
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-09-07 15:35:05 +00:00
|
|
|
community_moderator_view
|
|
|
|
.filter(community_id.eq(from_community_id))
|
|
|
|
.load::<Self>(conn)
|
2019-04-04 20:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-07 15:35:05 +00:00
|
|
|
community_moderator_view
|
|
|
|
.filter(user_id.eq(from_user_id))
|
|
|
|
.load::<Self>(conn)
|
2019-04-04 20:53:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
#[derive(
|
|
|
|
Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
|
|
|
|
)]
|
|
|
|
#[table_name = "community_follower_view"]
|
2019-04-05 19:14:54 +00:00
|
|
|
pub struct CommunityFollowerView {
|
|
|
|
pub id: i32,
|
|
|
|
pub community_id: i32,
|
|
|
|
pub user_id: i32,
|
|
|
|
pub published: chrono::NaiveDateTime,
|
2020-04-14 19:12:19 +00:00
|
|
|
pub user_actor_id: String,
|
|
|
|
pub user_local: bool,
|
2019-09-07 15:35:05 +00:00
|
|
|
pub user_name: String,
|
2019-12-29 20:39:48 +00:00
|
|
|
pub avatar: Option<String>,
|
2020-04-14 19:12:19 +00:00
|
|
|
pub community_actor_id: String,
|
|
|
|
pub community_local: bool,
|
2019-04-05 19:14:54 +00:00
|
|
|
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-09-07 15:35:05 +00:00
|
|
|
community_follower_view
|
|
|
|
.filter(community_id.eq(from_community_id))
|
|
|
|
.load::<Self>(conn)
|
2019-04-05 19:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-07 15:35:05 +00:00
|
|
|
community_follower_view
|
|
|
|
.filter(user_id.eq(from_user_id))
|
|
|
|
.load::<Self>(conn)
|
2019-04-05 19:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
#[derive(
|
|
|
|
Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
|
|
|
|
)]
|
|
|
|
#[table_name = "community_user_ban_view"]
|
2019-04-15 23:12:06 +00:00
|
|
|
pub struct CommunityUserBanView {
|
|
|
|
pub id: i32,
|
|
|
|
pub community_id: i32,
|
|
|
|
pub user_id: i32,
|
|
|
|
pub published: chrono::NaiveDateTime,
|
2020-04-14 19:12:19 +00:00
|
|
|
pub user_actor_id: String,
|
|
|
|
pub user_local: bool,
|
2019-09-07 15:35:05 +00:00
|
|
|
pub user_name: String,
|
2019-12-29 20:39:48 +00:00
|
|
|
pub avatar: Option<String>,
|
2020-04-14 19:12:19 +00:00
|
|
|
pub community_actor_id: String,
|
|
|
|
pub community_local: bool,
|
2019-04-15 23:12:06 +00:00
|
|
|
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-09-07 15:35:05 +00:00
|
|
|
community_user_ban_view
|
|
|
|
.filter(community_id.eq(from_community_id))
|
|
|
|
.load::<Self>(conn)
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-07 15:35:05 +00:00
|
|
|
community_user_ban_view
|
|
|
|
.filter(user_id.eq(from_user_id))
|
|
|
|
.load::<Self>(conn)
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|