4 // The faked schema since diesel doesn't do views
6 user_mention_view (id) {
8 user_mention_id -> Int4,
11 parent_id -> Nullable<Int4>,
15 published -> Timestamp,
16 updated -> Nullable<Timestamp>,
19 community_name -> Varchar,
21 banned_from_community -> Bool,
22 creator_name -> Varchar,
23 creator_avatar -> Nullable<Text>,
28 user_id -> Nullable<Int4>,
29 my_vote -> Nullable<Int4>,
30 saved -> Nullable<Bool>,
36 user_mention_mview (id) {
38 user_mention_id -> Int4,
41 parent_id -> Nullable<Int4>,
45 published -> Timestamp,
46 updated -> Nullable<Timestamp>,
49 community_name -> Varchar,
51 banned_from_community -> Bool,
52 creator_name -> Varchar,
53 creator_avatar -> Nullable<Text>,
58 user_id -> Nullable<Int4>,
59 my_vote -> Nullable<Int4>,
60 saved -> Nullable<Bool>,
66 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
68 #[table_name = "user_mention_view"]
69 pub struct UserMentionView {
71 pub user_mention_id: i32,
74 pub parent_id: Option<i32>,
78 pub published: chrono::NaiveDateTime,
79 pub updated: Option<chrono::NaiveDateTime>,
81 pub community_id: i32,
82 pub community_name: String,
84 pub banned_from_community: bool,
85 pub creator_name: String,
86 pub creator_avatar: Option<String>,
91 pub user_id: Option<i32>,
92 pub my_vote: Option<i32>,
93 pub saved: Option<bool>,
94 pub recipient_id: i32,
97 pub struct UserMentionQueryBuilder<'a> {
98 conn: &'a PgConnection,
99 query: super::user_mention_view::user_mention_mview::BoxedQuery<'a, Pg>,
107 impl<'a> UserMentionQueryBuilder<'a> {
108 pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
109 use super::user_mention_view::user_mention_mview::dsl::*;
111 let query = user_mention_mview.into_boxed();
113 UserMentionQueryBuilder {
117 sort: &SortType::New,
124 pub fn sort(mut self, sort: &'a SortType) -> Self {
129 pub fn unread_only(mut self, unread_only: bool) -> Self {
130 self.unread_only = unread_only;
134 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
135 self.page = page.get_optional();
139 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
140 self.limit = limit.get_optional();
144 pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
145 use super::user_mention_view::user_mention_mview::dsl::*;
147 let mut query = self.query;
149 if self.unread_only {
150 query = query.filter(read.eq(false));
154 .filter(user_id.eq(self.for_user_id))
155 .filter(recipient_id.eq(self.for_user_id));
157 query = match self.sort {
158 SortType::Hot => query
159 .order_by(hot_rank.desc())
160 .then_order_by(published.desc()),
161 SortType::New => query.order_by(published.desc()),
162 SortType::TopAll => query.order_by(score.desc()),
163 SortType::TopYear => query
164 .filter(published.gt(now - 1.years()))
165 .order_by(score.desc()),
166 SortType::TopMonth => query
167 .filter(published.gt(now - 1.months()))
168 .order_by(score.desc()),
169 SortType::TopWeek => query
170 .filter(published.gt(now - 1.weeks()))
171 .order_by(score.desc()),
172 SortType::TopDay => query
173 .filter(published.gt(now - 1.days()))
174 .order_by(score.desc()),
175 // _ => query.order_by(published.desc()),
178 let (limit, offset) = limit_and_offset(self.page, self.limit);
182 .load::<UserMentionView>(self.conn)
186 impl UserMentionView {
189 from_user_mention_id: i32,
190 from_recipient_id: i32,
191 ) -> Result<Self, Error> {
192 use super::user_mention_view::user_mention_view::dsl::*;
195 .filter(user_mention_id.eq(from_user_mention_id))
196 .filter(user_id.eq(from_recipient_id))