1 use crate::{limit_and_offset, MaybeOptional, SortType};
2 use diesel::{dsl::*, pg::Pg, result::Error, *};
3 use serde::{Deserialize, Serialize};
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,
87 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
89 #[table_name = "user_mention_fast_view"]
90 pub struct UserMentionView {
92 pub user_mention_id: i32,
94 pub creator_actor_id: String,
95 pub creator_local: bool,
97 pub post_name: String,
98 pub parent_id: Option<i32>,
102 pub published: chrono::NaiveDateTime,
103 pub updated: Option<chrono::NaiveDateTime>,
105 pub community_id: i32,
106 pub community_actor_id: String,
107 pub community_local: bool,
108 pub community_name: String,
109 pub community_icon: Option<String>,
111 pub banned_from_community: bool,
112 pub creator_name: String,
113 pub creator_preferred_username: Option<String>,
114 pub creator_avatar: Option<String>,
119 pub hot_rank_active: i32,
120 pub user_id: Option<i32>,
121 pub my_vote: Option<i32>,
122 pub saved: Option<bool>,
123 pub recipient_id: i32,
124 pub recipient_actor_id: String,
125 pub recipient_local: bool,
128 pub struct UserMentionQueryBuilder<'a> {
129 conn: &'a PgConnection,
130 query: super::user_mention_view::user_mention_fast_view::BoxedQuery<'a, Pg>,
138 impl<'a> UserMentionQueryBuilder<'a> {
139 pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
140 use super::user_mention_view::user_mention_fast_view::dsl::*;
142 let query = user_mention_fast_view.into_boxed();
144 UserMentionQueryBuilder {
148 sort: &SortType::New,
155 pub fn sort(mut self, sort: &'a SortType) -> Self {
160 pub fn unread_only(mut self, unread_only: bool) -> Self {
161 self.unread_only = unread_only;
165 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
166 self.page = page.get_optional();
170 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
171 self.limit = limit.get_optional();
175 pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
176 use super::user_mention_view::user_mention_fast_view::dsl::*;
178 let mut query = self.query;
180 if self.unread_only {
181 query = query.filter(read.eq(false));
185 .filter(user_id.eq(self.for_user_id))
186 .filter(recipient_id.eq(self.for_user_id));
188 query = match self.sort {
189 SortType::Hot => query
190 .order_by(hot_rank.desc())
191 .then_order_by(published.desc()),
192 SortType::Active => query
193 .order_by(hot_rank_active.desc())
194 .then_order_by(published.desc()),
195 SortType::New => query.order_by(published.desc()),
196 SortType::TopAll => query.order_by(score.desc()),
197 SortType::TopYear => query
198 .filter(published.gt(now - 1.years()))
199 .order_by(score.desc()),
200 SortType::TopMonth => query
201 .filter(published.gt(now - 1.months()))
202 .order_by(score.desc()),
203 SortType::TopWeek => query
204 .filter(published.gt(now - 1.weeks()))
205 .order_by(score.desc()),
206 SortType::TopDay => query
207 .filter(published.gt(now - 1.days()))
208 .order_by(score.desc()),
209 // _ => query.order_by(published.desc()),
212 let (limit, offset) = limit_and_offset(self.page, self.limit);
216 .load::<UserMentionView>(self.conn)
220 impl UserMentionView {
223 from_user_mention_id: i32,
224 from_recipient_id: i32,
225 ) -> Result<Self, Error> {
226 use super::user_mention_view::user_mention_fast_view::dsl::*;
228 user_mention_fast_view
229 .filter(user_mention_id.eq(from_user_mention_id))
230 .filter(user_id.eq(from_recipient_id))