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>,
20 banned_from_community -> Bool,
21 creator_name -> Varchar,
22 creator_avatar -> Nullable<Text>,
26 user_id -> Nullable<Int4>,
27 my_vote -> Nullable<Int4>,
28 saved -> Nullable<Bool>,
34 user_mention_mview (id) {
36 user_mention_id -> Int4,
39 parent_id -> Nullable<Int4>,
43 published -> Timestamp,
44 updated -> Nullable<Timestamp>,
48 banned_from_community -> Bool,
49 creator_name -> Varchar,
50 creator_avatar -> Nullable<Text>,
54 user_id -> Nullable<Int4>,
55 my_vote -> Nullable<Int4>,
56 saved -> Nullable<Bool>,
62 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
64 #[table_name = "user_mention_view"]
65 pub struct UserMentionView {
67 pub user_mention_id: i32,
70 pub parent_id: Option<i32>,
74 pub published: chrono::NaiveDateTime,
75 pub updated: Option<chrono::NaiveDateTime>,
77 pub community_id: i32,
79 pub banned_from_community: bool,
80 pub creator_name: String,
81 pub creator_avatar: Option<String>,
85 pub user_id: Option<i32>,
86 pub my_vote: Option<i32>,
87 pub saved: Option<bool>,
88 pub recipient_id: i32,
91 pub struct UserMentionQueryBuilder<'a> {
92 conn: &'a PgConnection,
93 query: super::user_mention_view::user_mention_mview::BoxedQuery<'a, Pg>,
101 impl<'a> UserMentionQueryBuilder<'a> {
102 pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
103 use super::user_mention_view::user_mention_mview::dsl::*;
105 let query = user_mention_mview.into_boxed();
107 UserMentionQueryBuilder {
111 sort: &SortType::New,
118 pub fn sort(mut self, sort: &'a SortType) -> Self {
123 pub fn unread_only(mut self, unread_only: bool) -> Self {
124 self.unread_only = unread_only;
128 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
129 self.page = page.get_optional();
133 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
134 self.limit = limit.get_optional();
138 pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
139 use super::user_mention_view::user_mention_mview::dsl::*;
141 let mut query = self.query;
143 if self.unread_only {
144 query = query.filter(read.eq(false));
148 .filter(user_id.eq(self.for_user_id))
149 .filter(recipient_id.eq(self.for_user_id));
151 query = match self.sort {
152 // SortType::Hot => query.order_by(hot_rank.desc()),
153 SortType::New => query.order_by(published.desc()),
154 SortType::TopAll => query.order_by(score.desc()),
155 SortType::TopYear => query
156 .filter(published.gt(now - 1.years()))
157 .order_by(score.desc()),
158 SortType::TopMonth => query
159 .filter(published.gt(now - 1.months()))
160 .order_by(score.desc()),
161 SortType::TopWeek => query
162 .filter(published.gt(now - 1.weeks()))
163 .order_by(score.desc()),
164 SortType::TopDay => query
165 .filter(published.gt(now - 1.days()))
166 .order_by(score.desc()),
167 _ => query.order_by(published.desc()),
170 let (limit, offset) = limit_and_offset(self.page, self.limit);
174 .load::<UserMentionView>(self.conn)
178 impl UserMentionView {
181 from_user_mention_id: i32,
182 from_recipient_id: i32,
183 ) -> Result<Self, Error> {
184 use super::user_mention_view::user_mention_view::dsl::*;
187 .filter(user_mention_id.eq(from_user_mention_id))
188 .filter(user_id.eq(from_recipient_id))