1 use crate::{limit_and_offset, MaybeOptional, SortType};
2 use diesel::{dsl::*, pg::Pg, result::Error, *};
5 // The faked schema since diesel doesn't do views
7 user_mention_view (id) {
9 user_mention_id -> Int4,
11 creator_actor_id -> Text,
12 creator_local -> Bool,
15 parent_id -> Nullable<Int4>,
19 published -> Timestamp,
20 updated -> Nullable<Timestamp>,
23 community_actor_id -> Text,
24 community_local -> Bool,
25 community_name -> Varchar,
26 community_icon -> Nullable<Text>,
28 banned_from_community -> Bool,
29 creator_name -> Varchar,
30 creator_preferred_username -> Nullable<Varchar>,
31 creator_avatar -> Nullable<Text>,
36 hot_rank_active -> Int4,
37 user_id -> Nullable<Int4>,
38 my_vote -> Nullable<Int4>,
39 saved -> Nullable<Bool>,
41 recipient_actor_id -> Text,
42 recipient_local -> Bool,
47 user_mention_fast_view (id) {
49 user_mention_id -> Int4,
51 creator_actor_id -> Text,
52 creator_local -> Bool,
55 parent_id -> Nullable<Int4>,
59 published -> Timestamp,
60 updated -> Nullable<Timestamp>,
63 community_actor_id -> Text,
64 community_local -> Bool,
65 community_name -> Varchar,
66 community_icon -> Nullable<Text>,
68 banned_from_community -> Bool,
69 creator_name -> Varchar,
70 creator_preferred_username -> Nullable<Varchar>,
71 creator_avatar -> Nullable<Text>,
76 hot_rank_active -> Int4,
77 user_id -> Nullable<Int4>,
78 my_vote -> Nullable<Int4>,
79 saved -> Nullable<Bool>,
81 recipient_actor_id -> Text,
82 recipient_local -> Bool,
86 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)]
87 #[table_name = "user_mention_fast_view"]
88 pub struct UserMentionView {
90 pub user_mention_id: i32,
92 pub creator_actor_id: String,
93 pub creator_local: bool,
95 pub post_name: String,
96 pub parent_id: Option<i32>,
100 pub published: chrono::NaiveDateTime,
101 pub updated: Option<chrono::NaiveDateTime>,
103 pub community_id: i32,
104 pub community_actor_id: String,
105 pub community_local: bool,
106 pub community_name: String,
107 pub community_icon: Option<String>,
109 pub banned_from_community: bool,
110 pub creator_name: String,
111 pub creator_preferred_username: Option<String>,
112 pub creator_avatar: Option<String>,
117 pub hot_rank_active: i32,
118 pub user_id: Option<i32>,
119 pub my_vote: Option<i32>,
120 pub saved: Option<bool>,
121 pub recipient_id: i32,
122 pub recipient_actor_id: String,
123 pub recipient_local: bool,
126 pub struct UserMentionQueryBuilder<'a> {
127 conn: &'a PgConnection,
128 query: super::user_mention_view::user_mention_fast_view::BoxedQuery<'a, Pg>,
136 impl<'a> UserMentionQueryBuilder<'a> {
137 pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
138 use super::user_mention_view::user_mention_fast_view::dsl::*;
140 let query = user_mention_fast_view.into_boxed();
142 UserMentionQueryBuilder {
146 sort: &SortType::New,
153 pub fn sort(mut self, sort: &'a SortType) -> Self {
158 pub fn unread_only(mut self, unread_only: bool) -> Self {
159 self.unread_only = unread_only;
163 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
164 self.page = page.get_optional();
168 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
169 self.limit = limit.get_optional();
173 pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
174 use super::user_mention_view::user_mention_fast_view::dsl::*;
176 let mut query = self.query;
178 if self.unread_only {
179 query = query.filter(read.eq(false));
183 .filter(user_id.eq(self.for_user_id))
184 .filter(recipient_id.eq(self.for_user_id));
186 query = match self.sort {
187 SortType::Hot => query
188 .order_by(hot_rank.desc())
189 .then_order_by(published.desc()),
190 SortType::Active => query
191 .order_by(hot_rank_active.desc())
192 .then_order_by(published.desc()),
193 SortType::New => query.order_by(published.desc()),
194 SortType::TopAll => query.order_by(score.desc()),
195 SortType::TopYear => query
196 .filter(published.gt(now - 1.years()))
197 .order_by(score.desc()),
198 SortType::TopMonth => query
199 .filter(published.gt(now - 1.months()))
200 .order_by(score.desc()),
201 SortType::TopWeek => query
202 .filter(published.gt(now - 1.weeks()))
203 .order_by(score.desc()),
204 SortType::TopDay => query
205 .filter(published.gt(now - 1.days()))
206 .order_by(score.desc()),
207 // _ => query.order_by(published.desc()),
210 let (limit, offset) = limit_and_offset(self.page, self.limit);
214 .load::<UserMentionView>(self.conn)
218 impl UserMentionView {
221 from_user_mention_id: i32,
222 from_recipient_id: i32,
223 ) -> Result<Self, Error> {
224 use super::user_mention_view::user_mention_fast_view::dsl::*;
226 user_mention_fast_view
227 .filter(user_mention_id.eq(from_user_mention_id))
228 .filter(user_id.eq(from_recipient_id))