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,
27 banned_from_community -> Bool,
28 creator_name -> Varchar,
29 creator_avatar -> Nullable<Text>,
34 user_id -> Nullable<Int4>,
35 my_vote -> Nullable<Int4>,
36 saved -> Nullable<Bool>,
38 recipient_actor_id -> Text,
39 recipient_local -> Bool,
44 user_mention_fast_view (id) {
46 user_mention_id -> Int4,
48 creator_actor_id -> Text,
49 creator_local -> Bool,
52 parent_id -> Nullable<Int4>,
56 published -> Timestamp,
57 updated -> Nullable<Timestamp>,
60 community_actor_id -> Text,
61 community_local -> Bool,
62 community_name -> Varchar,
64 banned_from_community -> Bool,
65 creator_name -> Varchar,
66 creator_avatar -> Nullable<Text>,
71 user_id -> Nullable<Int4>,
72 my_vote -> Nullable<Int4>,
73 saved -> Nullable<Bool>,
75 recipient_actor_id -> Text,
76 recipient_local -> Bool,
81 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
83 #[table_name = "user_mention_fast_view"]
84 pub struct UserMentionView {
86 pub user_mention_id: i32,
88 pub creator_actor_id: String,
89 pub creator_local: bool,
91 pub post_name: String,
92 pub parent_id: Option<i32>,
96 pub published: chrono::NaiveDateTime,
97 pub updated: Option<chrono::NaiveDateTime>,
99 pub community_id: i32,
100 pub community_actor_id: String,
101 pub community_local: bool,
102 pub community_name: String,
104 pub banned_from_community: bool,
105 pub creator_name: String,
106 pub creator_avatar: Option<String>,
111 pub user_id: Option<i32>,
112 pub my_vote: Option<i32>,
113 pub saved: Option<bool>,
114 pub recipient_id: i32,
115 pub recipient_actor_id: String,
116 pub recipient_local: bool,
119 pub struct UserMentionQueryBuilder<'a> {
120 conn: &'a PgConnection,
121 query: super::user_mention_view::user_mention_fast_view::BoxedQuery<'a, Pg>,
129 impl<'a> UserMentionQueryBuilder<'a> {
130 pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
131 use super::user_mention_view::user_mention_fast_view::dsl::*;
133 let query = user_mention_fast_view.into_boxed();
135 UserMentionQueryBuilder {
139 sort: &SortType::New,
146 pub fn sort(mut self, sort: &'a SortType) -> Self {
151 pub fn unread_only(mut self, unread_only: bool) -> Self {
152 self.unread_only = unread_only;
156 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
157 self.page = page.get_optional();
161 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
162 self.limit = limit.get_optional();
166 pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
167 use super::user_mention_view::user_mention_fast_view::dsl::*;
169 let mut query = self.query;
171 if self.unread_only {
172 query = query.filter(read.eq(false));
176 .filter(user_id.eq(self.for_user_id))
177 .filter(recipient_id.eq(self.for_user_id));
179 query = match self.sort {
180 SortType::Hot => query
181 .order_by(hot_rank.desc())
182 .then_order_by(published.desc()),
183 SortType::New => query.order_by(published.desc()),
184 SortType::TopAll => query.order_by(score.desc()),
185 SortType::TopYear => query
186 .filter(published.gt(now - 1.years()))
187 .order_by(score.desc()),
188 SortType::TopMonth => query
189 .filter(published.gt(now - 1.months()))
190 .order_by(score.desc()),
191 SortType::TopWeek => query
192 .filter(published.gt(now - 1.weeks()))
193 .order_by(score.desc()),
194 SortType::TopDay => query
195 .filter(published.gt(now - 1.days()))
196 .order_by(score.desc()),
197 // _ => query.order_by(published.desc()),
200 let (limit, offset) = limit_and_offset(self.page, self.limit);
204 .load::<UserMentionView>(self.conn)
208 impl UserMentionView {
211 from_user_mention_id: i32,
212 from_recipient_id: i32,
213 ) -> Result<Self, Error> {
214 use super::user_mention_view::user_mention_fast_view::dsl::*;
216 user_mention_fast_view
217 .filter(user_mention_id.eq(from_user_mention_id))
218 .filter(user_id.eq(from_recipient_id))