1 use super::community_view::community_mview::BoxedQuery;
10 description -> Nullable<Text>,
14 published -> Timestamp,
15 updated -> Nullable<Timestamp>,
18 creator_name -> Varchar,
19 creator_avatar -> Nullable<Text>,
20 category_name -> Varchar,
21 number_of_subscribers -> BigInt,
22 number_of_posts -> BigInt,
23 number_of_comments -> BigInt,
25 user_id -> Nullable<Int4>,
26 subscribed -> Nullable<Bool>,
31 community_mview (id) {
35 description -> Nullable<Text>,
39 published -> Timestamp,
40 updated -> Nullable<Timestamp>,
43 creator_name -> Varchar,
44 creator_avatar -> Nullable<Text>,
45 category_name -> Varchar,
46 number_of_subscribers -> BigInt,
47 number_of_posts -> BigInt,
48 number_of_comments -> BigInt,
50 user_id -> Nullable<Int4>,
51 subscribed -> Nullable<Bool>,
56 community_moderator_view (id) {
60 published -> Timestamp,
62 avatar -> Nullable<Text>,
63 community_name -> Varchar,
68 community_follower_view (id) {
72 published -> Timestamp,
74 avatar -> Nullable<Text>,
75 community_name -> Varchar,
80 community_user_ban_view (id) {
84 published -> Timestamp,
86 avatar -> Nullable<Text>,
87 community_name -> Varchar,
92 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
94 #[table_name = "community_view"]
95 pub struct CommunityView {
99 pub description: Option<String>,
100 pub category_id: i32,
103 pub published: chrono::NaiveDateTime,
104 pub updated: Option<chrono::NaiveDateTime>,
107 pub creator_name: String,
108 pub creator_avatar: Option<String>,
109 pub category_name: String,
110 pub number_of_subscribers: i64,
111 pub number_of_posts: i64,
112 pub number_of_comments: i64,
114 pub user_id: Option<i32>,
115 pub subscribed: Option<bool>,
118 pub struct CommunityQueryBuilder<'a> {
119 conn: &'a PgConnection,
120 query: BoxedQuery<'a, Pg>,
122 from_user_id: Option<i32>,
124 search_term: Option<String>,
129 impl<'a> CommunityQueryBuilder<'a> {
130 pub fn create(conn: &'a PgConnection) -> Self {
131 use super::community_view::community_mview::dsl::*;
133 let query = community_mview.into_boxed();
135 CommunityQueryBuilder {
138 sort: &SortType::Hot,
147 pub fn sort(mut self, sort: &'a SortType) -> Self {
152 pub fn for_user<T: MaybeOptional<i32>>(mut self, from_user_id: T) -> Self {
153 self.from_user_id = from_user_id.get_optional();
157 pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
158 self.show_nsfw = show_nsfw;
162 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
163 self.search_term = search_term.get_optional();
167 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
168 self.page = page.get_optional();
172 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
173 self.limit = limit.get_optional();
177 pub fn list(self) -> Result<Vec<CommunityView>, Error> {
178 use super::community_view::community_mview::dsl::*;
180 let mut query = self.query;
182 if let Some(search_term) = self.search_term {
183 let searcher = fuzzy_search(&search_term);
185 .filter(name.ilike(searcher.to_owned()))
186 .or_filter(title.ilike(searcher.to_owned()))
187 .or_filter(description.ilike(searcher));
190 // The view lets you pass a null user_id, if you're not logged in
194 .order_by(hot_rank.desc())
195 .then_order_by(number_of_subscribers.desc())
196 .filter(user_id.is_null())
198 SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()),
199 SortType::TopAll => match self.from_user_id {
200 Some(from_user_id) => {
202 .filter(user_id.eq(from_user_id))
203 .order_by((subscribed.asc(), number_of_subscribers.desc()))
207 .order_by(number_of_subscribers.desc())
208 .filter(user_id.is_null())
215 query = query.filter(nsfw.eq(false));
218 let (limit, offset) = limit_and_offset(self.page, self.limit);
222 .filter(removed.eq(false))
223 .filter(deleted.eq(false))
224 .load::<CommunityView>(self.conn)
231 from_community_id: i32,
232 from_user_id: Option<i32>,
233 ) -> Result<Self, Error> {
234 use super::community_view::community_mview::dsl::*;
236 let mut query = community_mview.into_boxed();
238 query = query.filter(id.eq(from_community_id));
240 // The view lets you pass a null user_id, if you're not logged in
241 if let Some(from_user_id) = from_user_id {
242 query = query.filter(user_id.eq(from_user_id));
244 query = query.filter(user_id.is_null());
247 query.first::<Self>(conn)
252 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
254 #[table_name = "community_moderator_view"]
255 pub struct CommunityModeratorView {
257 pub community_id: i32,
259 pub published: chrono::NaiveDateTime,
260 pub user_name: String,
261 pub avatar: Option<String>,
262 pub community_name: String,
265 impl CommunityModeratorView {
266 pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
267 use super::community_view::community_moderator_view::dsl::*;
268 community_moderator_view
269 .filter(community_id.eq(from_community_id))
273 pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
274 use super::community_view::community_moderator_view::dsl::*;
275 community_moderator_view
276 .filter(user_id.eq(from_user_id))
282 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
284 #[table_name = "community_follower_view"]
285 pub struct CommunityFollowerView {
287 pub community_id: i32,
289 pub published: chrono::NaiveDateTime,
290 pub user_name: String,
291 pub avatar: Option<String>,
292 pub community_name: String,
295 impl CommunityFollowerView {
296 pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
297 use super::community_view::community_follower_view::dsl::*;
298 community_follower_view
299 .filter(community_id.eq(from_community_id))
303 pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
304 use super::community_view::community_follower_view::dsl::*;
305 community_follower_view
306 .filter(user_id.eq(from_user_id))
312 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
314 #[table_name = "community_user_ban_view"]
315 pub struct CommunityUserBanView {
317 pub community_id: i32,
319 pub published: chrono::NaiveDateTime,
320 pub user_name: String,
321 pub avatar: Option<String>,
322 pub community_name: String,
325 impl CommunityUserBanView {
326 pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
327 use super::community_view::community_user_ban_view::dsl::*;
328 community_user_ban_view
329 .filter(community_id.eq(from_community_id))
333 pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
334 use super::community_view::community_user_ban_view::dsl::*;
335 community_user_ban_view
336 .filter(user_id.eq(from_user_id))
343 from_community_id: i32,
344 ) -> Result<Self, Error> {
345 use super::community_view::community_user_ban_view::dsl::*;
346 community_user_ban_view
347 .filter(user_id.eq(from_user_id))
348 .filter(community_id.eq(from_community_id))