4 // The faked schema since diesel doesn't do views
6 user_mention_view (id) {
8 user_mention_id -> Int4,
10 creator_actor_id -> Text,
11 creator_local -> Bool,
13 parent_id -> Nullable<Int4>,
17 published -> Timestamp,
18 updated -> Nullable<Timestamp>,
21 community_actor_id -> Text,
22 community_local -> Bool,
23 community_name -> Varchar,
25 banned_from_community -> Bool,
26 creator_name -> Varchar,
27 creator_avatar -> Nullable<Text>,
32 user_id -> Nullable<Int4>,
33 my_vote -> Nullable<Int4>,
34 saved -> Nullable<Bool>,
36 recipient_actor_id -> Text,
37 recipient_local -> Bool,
42 user_mention_mview (id) {
44 user_mention_id -> Int4,
46 creator_actor_id -> Text,
47 creator_local -> Bool,
49 parent_id -> Nullable<Int4>,
53 published -> Timestamp,
54 updated -> Nullable<Timestamp>,
57 community_actor_id -> Text,
58 community_local -> Bool,
59 community_name -> Varchar,
61 banned_from_community -> Bool,
62 creator_name -> Varchar,
63 creator_avatar -> Nullable<Text>,
68 user_id -> Nullable<Int4>,
69 my_vote -> Nullable<Int4>,
70 saved -> Nullable<Bool>,
72 recipient_actor_id -> Text,
73 recipient_local -> Bool,
78 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
80 #[table_name = "user_mention_view"]
81 pub struct UserMentionView {
83 pub user_mention_id: i32,
85 pub creator_actor_id: String,
86 pub creator_local: bool,
88 pub parent_id: Option<i32>,
92 pub published: chrono::NaiveDateTime,
93 pub updated: Option<chrono::NaiveDateTime>,
95 pub community_id: i32,
96 pub community_actor_id: String,
97 pub community_local: bool,
98 pub community_name: String,
100 pub banned_from_community: bool,
101 pub creator_name: String,
102 pub creator_avatar: Option<String>,
107 pub user_id: Option<i32>,
108 pub my_vote: Option<i32>,
109 pub saved: Option<bool>,
110 pub recipient_id: i32,
111 pub recipient_actor_id: String,
112 pub recipient_local: bool,
115 pub struct UserMentionQueryBuilder<'a> {
116 conn: &'a PgConnection,
117 query: super::user_mention_view::user_mention_mview::BoxedQuery<'a, Pg>,
125 impl<'a> UserMentionQueryBuilder<'a> {
126 pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
127 use super::user_mention_view::user_mention_mview::dsl::*;
129 let query = user_mention_mview.into_boxed();
131 UserMentionQueryBuilder {
135 sort: &SortType::New,
142 pub fn sort(mut self, sort: &'a SortType) -> Self {
147 pub fn unread_only(mut self, unread_only: bool) -> Self {
148 self.unread_only = unread_only;
152 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
153 self.page = page.get_optional();
157 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
158 self.limit = limit.get_optional();
162 pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
163 use super::user_mention_view::user_mention_mview::dsl::*;
165 let mut query = self.query;
167 if self.unread_only {
168 query = query.filter(read.eq(false));
172 .filter(user_id.eq(self.for_user_id))
173 .filter(recipient_id.eq(self.for_user_id));
175 query = match self.sort {
176 SortType::Hot => query
177 .order_by(hot_rank.desc())
178 .then_order_by(published.desc()),
179 SortType::New => query.order_by(published.desc()),
180 SortType::TopAll => query.order_by(score.desc()),
181 SortType::TopYear => query
182 .filter(published.gt(now - 1.years()))
183 .order_by(score.desc()),
184 SortType::TopMonth => query
185 .filter(published.gt(now - 1.months()))
186 .order_by(score.desc()),
187 SortType::TopWeek => query
188 .filter(published.gt(now - 1.weeks()))
189 .order_by(score.desc()),
190 SortType::TopDay => query
191 .filter(published.gt(now - 1.days()))
192 .order_by(score.desc()),
193 // _ => query.order_by(published.desc()),
196 let (limit, offset) = limit_and_offset(self.page, self.limit);
200 .load::<UserMentionView>(self.conn)
204 impl UserMentionView {
207 from_user_mention_id: i32,
208 from_recipient_id: i32,
209 ) -> Result<Self, Error> {
210 use super::user_mention_view::user_mention_view::dsl::*;
213 .filter(user_mention_id.eq(from_user_mention_id))
214 .filter(user_id.eq(from_recipient_id))