2020-07-10 18:15:41 +00:00
|
|
|
use crate::{limit_and_offset, MaybeOptional, SortType};
|
2020-05-16 14:04:08 +00:00
|
|
|
use diesel::{dsl::*, pg::Pg, result::Error, *};
|
2020-09-14 15:29:50 +00:00
|
|
|
use serde::Serialize;
|
2019-10-20 00:46:29 +00:00
|
|
|
|
|
|
|
// The faked schema since diesel doesn't do views
|
|
|
|
table! {
|
|
|
|
user_mention_view (id) {
|
|
|
|
id -> Int4,
|
|
|
|
user_mention_id -> Int4,
|
|
|
|
creator_id -> Int4,
|
2020-04-14 19:12:19 +00:00
|
|
|
creator_actor_id -> Text,
|
|
|
|
creator_local -> Bool,
|
2019-10-20 00:46:29 +00:00
|
|
|
post_id -> Int4,
|
2020-07-13 06:18:11 +00:00
|
|
|
post_name -> Varchar,
|
2019-10-20 00:46:29 +00:00
|
|
|
parent_id -> Nullable<Int4>,
|
|
|
|
content -> Text,
|
|
|
|
removed -> Bool,
|
|
|
|
read -> Bool,
|
|
|
|
published -> Timestamp,
|
|
|
|
updated -> Nullable<Timestamp>,
|
|
|
|
deleted -> Bool,
|
|
|
|
community_id -> Int4,
|
2020-04-14 19:12:19 +00:00
|
|
|
community_actor_id -> Text,
|
|
|
|
community_local -> Bool,
|
2020-02-08 04:05:15 +00:00
|
|
|
community_name -> Varchar,
|
2020-08-05 16:03:46 +00:00
|
|
|
community_icon -> Nullable<Text>,
|
2019-10-20 00:46:29 +00:00
|
|
|
banned -> Bool,
|
|
|
|
banned_from_community -> Bool,
|
|
|
|
creator_name -> Varchar,
|
2020-08-05 16:03:46 +00:00
|
|
|
creator_preferred_username -> Nullable<Varchar>,
|
2019-12-29 20:39:48 +00:00
|
|
|
creator_avatar -> Nullable<Text>,
|
2019-10-20 00:46:29 +00:00
|
|
|
score -> BigInt,
|
|
|
|
upvotes -> BigInt,
|
|
|
|
downvotes -> BigInt,
|
2020-02-08 04:05:15 +00:00
|
|
|
hot_rank -> Int4,
|
2020-08-05 16:03:46 +00:00
|
|
|
hot_rank_active -> Int4,
|
2019-10-20 00:46:29 +00:00
|
|
|
user_id -> Nullable<Int4>,
|
|
|
|
my_vote -> Nullable<Int4>,
|
|
|
|
saved -> Nullable<Bool>,
|
|
|
|
recipient_id -> Int4,
|
2020-04-14 19:12:19 +00:00
|
|
|
recipient_actor_id -> Text,
|
|
|
|
recipient_local -> Bool,
|
2019-10-20 00:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:00:28 +00:00
|
|
|
table! {
|
2020-07-07 14:54:44 +00:00
|
|
|
user_mention_fast_view (id) {
|
2020-06-30 01:00:28 +00:00
|
|
|
id -> Int4,
|
|
|
|
user_mention_id -> Int4,
|
|
|
|
creator_id -> Int4,
|
|
|
|
creator_actor_id -> Text,
|
|
|
|
creator_local -> Bool,
|
|
|
|
post_id -> Int4,
|
2020-07-13 06:18:11 +00:00
|
|
|
post_name -> Varchar,
|
2020-06-30 01:00:28 +00:00
|
|
|
parent_id -> Nullable<Int4>,
|
|
|
|
content -> Text,
|
|
|
|
removed -> Bool,
|
|
|
|
read -> Bool,
|
|
|
|
published -> Timestamp,
|
|
|
|
updated -> Nullable<Timestamp>,
|
|
|
|
deleted -> Bool,
|
|
|
|
community_id -> Int4,
|
|
|
|
community_actor_id -> Text,
|
|
|
|
community_local -> Bool,
|
|
|
|
community_name -> Varchar,
|
2020-08-05 16:03:46 +00:00
|
|
|
community_icon -> Nullable<Text>,
|
2020-06-30 01:00:28 +00:00
|
|
|
banned -> Bool,
|
|
|
|
banned_from_community -> Bool,
|
|
|
|
creator_name -> Varchar,
|
2020-08-05 16:03:46 +00:00
|
|
|
creator_preferred_username -> Nullable<Varchar>,
|
2020-06-30 01:00:28 +00:00
|
|
|
creator_avatar -> Nullable<Text>,
|
|
|
|
score -> BigInt,
|
|
|
|
upvotes -> BigInt,
|
|
|
|
downvotes -> BigInt,
|
|
|
|
hot_rank -> Int4,
|
2020-08-05 16:03:46 +00:00
|
|
|
hot_rank_active -> Int4,
|
2020-06-30 01:00:28 +00:00
|
|
|
user_id -> Nullable<Int4>,
|
|
|
|
my_vote -> Nullable<Int4>,
|
|
|
|
saved -> Nullable<Bool>,
|
|
|
|
recipient_id -> Int4,
|
|
|
|
recipient_actor_id -> Text,
|
|
|
|
recipient_local -> Bool,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 15:29:50 +00:00
|
|
|
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)]
|
2020-07-07 14:54:44 +00:00
|
|
|
#[table_name = "user_mention_fast_view"]
|
2019-10-20 00:46:29 +00:00
|
|
|
pub struct UserMentionView {
|
|
|
|
pub id: i32,
|
|
|
|
pub user_mention_id: i32,
|
|
|
|
pub creator_id: i32,
|
2020-04-14 19:12:19 +00:00
|
|
|
pub creator_actor_id: String,
|
|
|
|
pub creator_local: bool,
|
2019-10-20 00:46:29 +00:00
|
|
|
pub post_id: i32,
|
2020-07-13 06:18:11 +00:00
|
|
|
pub post_name: String,
|
2019-10-20 00:46:29 +00:00
|
|
|
pub parent_id: Option<i32>,
|
|
|
|
pub content: String,
|
|
|
|
pub removed: bool,
|
|
|
|
pub read: bool,
|
|
|
|
pub published: chrono::NaiveDateTime,
|
|
|
|
pub updated: Option<chrono::NaiveDateTime>,
|
|
|
|
pub deleted: bool,
|
|
|
|
pub community_id: i32,
|
2020-04-14 19:12:19 +00:00
|
|
|
pub community_actor_id: String,
|
|
|
|
pub community_local: bool,
|
2020-02-08 04:05:15 +00:00
|
|
|
pub community_name: String,
|
2020-08-05 16:03:46 +00:00
|
|
|
pub community_icon: Option<String>,
|
2019-10-20 00:46:29 +00:00
|
|
|
pub banned: bool,
|
|
|
|
pub banned_from_community: bool,
|
|
|
|
pub creator_name: String,
|
2020-08-05 16:03:46 +00:00
|
|
|
pub creator_preferred_username: Option<String>,
|
2019-12-29 20:39:48 +00:00
|
|
|
pub creator_avatar: Option<String>,
|
2019-10-20 00:46:29 +00:00
|
|
|
pub score: i64,
|
|
|
|
pub upvotes: i64,
|
|
|
|
pub downvotes: i64,
|
2020-02-08 04:05:15 +00:00
|
|
|
pub hot_rank: i32,
|
2020-08-05 16:03:46 +00:00
|
|
|
pub hot_rank_active: i32,
|
2019-10-20 00:46:29 +00:00
|
|
|
pub user_id: Option<i32>,
|
|
|
|
pub my_vote: Option<i32>,
|
|
|
|
pub saved: Option<bool>,
|
|
|
|
pub recipient_id: i32,
|
2020-04-14 19:12:19 +00:00
|
|
|
pub recipient_actor_id: String,
|
|
|
|
pub recipient_local: bool,
|
2019-10-20 00:46:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
pub struct UserMentionQueryBuilder<'a> {
|
|
|
|
conn: &'a PgConnection,
|
2020-07-07 14:54:44 +00:00
|
|
|
query: super::user_mention_view::user_mention_fast_view::BoxedQuery<'a, Pg>,
|
2019-12-08 20:39:54 +00:00
|
|
|
for_user_id: i32,
|
|
|
|
sort: &'a SortType,
|
|
|
|
unread_only: bool,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> UserMentionQueryBuilder<'a> {
|
|
|
|
pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
|
2020-07-07 14:54:44 +00:00
|
|
|
use super::user_mention_view::user_mention_fast_view::dsl::*;
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2020-07-07 14:54:44 +00:00
|
|
|
let query = user_mention_fast_view.into_boxed();
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
UserMentionQueryBuilder {
|
|
|
|
conn,
|
|
|
|
query,
|
2019-12-29 20:39:48 +00:00
|
|
|
for_user_id,
|
2019-12-08 20:39:54 +00:00
|
|
|
sort: &SortType::New,
|
|
|
|
unread_only: false,
|
|
|
|
page: None,
|
|
|
|
limit: None,
|
|
|
|
}
|
|
|
|
}
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
pub fn sort(mut self, sort: &'a SortType) -> Self {
|
|
|
|
self.sort = sort;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unread_only(mut self, unread_only: bool) -> Self {
|
|
|
|
self.unread_only = unread_only;
|
|
|
|
self
|
|
|
|
}
|
2019-10-20 00:46:29 +00:00
|
|
|
|
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<UserMentionView>, Error> {
|
2020-07-07 14:54:44 +00:00
|
|
|
use super::user_mention_view::user_mention_fast_view::dsl::*;
|
2019-12-08 20:39:54 +00:00
|
|
|
|
|
|
|
let mut query = self.query;
|
|
|
|
|
|
|
|
if self.unread_only {
|
2019-10-20 00:46:29 +00:00
|
|
|
query = query.filter(read.eq(false));
|
|
|
|
}
|
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
query = query
|
|
|
|
.filter(user_id.eq(self.for_user_id))
|
|
|
|
.filter(recipient_id.eq(self.for_user_id));
|
|
|
|
|
|
|
|
query = match self.sort {
|
2020-02-08 04:05:15 +00:00
|
|
|
SortType::Hot => query
|
|
|
|
.order_by(hot_rank.desc())
|
|
|
|
.then_order_by(published.desc()),
|
2020-08-05 16:03:46 +00:00
|
|
|
SortType::Active => query
|
|
|
|
.order_by(hot_rank_active.desc())
|
|
|
|
.then_order_by(published.desc()),
|
2019-10-20 00:46:29 +00:00
|
|
|
SortType::New => query.order_by(published.desc()),
|
|
|
|
SortType::TopAll => query.order_by(score.desc()),
|
|
|
|
SortType::TopYear => query
|
|
|
|
.filter(published.gt(now - 1.years()))
|
|
|
|
.order_by(score.desc()),
|
|
|
|
SortType::TopMonth => query
|
|
|
|
.filter(published.gt(now - 1.months()))
|
|
|
|
.order_by(score.desc()),
|
|
|
|
SortType::TopWeek => query
|
|
|
|
.filter(published.gt(now - 1.weeks()))
|
|
|
|
.order_by(score.desc()),
|
|
|
|
SortType::TopDay => query
|
|
|
|
.filter(published.gt(now - 1.days()))
|
|
|
|
.order_by(score.desc()),
|
2020-02-08 04:05:15 +00:00
|
|
|
// _ => query.order_by(published.desc()),
|
2019-10-20 00:46:29 +00:00
|
|
|
};
|
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
let (limit, offset) = limit_and_offset(self.page, self.limit);
|
|
|
|
query
|
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
|
|
|
.load::<UserMentionView>(self.conn)
|
2019-10-20 00:46:29 +00:00
|
|
|
}
|
2019-12-08 20:39:54 +00:00
|
|
|
}
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
impl UserMentionView {
|
2019-10-20 00:46:29 +00:00
|
|
|
pub fn read(
|
|
|
|
conn: &PgConnection,
|
|
|
|
from_user_mention_id: i32,
|
|
|
|
from_recipient_id: i32,
|
|
|
|
) -> Result<Self, Error> {
|
2020-07-07 14:54:44 +00:00
|
|
|
use super::user_mention_view::user_mention_fast_view::dsl::*;
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2020-07-07 14:54:44 +00:00
|
|
|
user_mention_fast_view
|
2019-10-20 00:46:29 +00:00
|
|
|
.filter(user_mention_id.eq(from_user_mention_id))
|
|
|
|
.filter(user_id.eq(from_recipient_id))
|
|
|
|
.first::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|