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 query = query.filter(name.ilike(fuzzy_search(&search_term)));
186 // The view lets you pass a null user_id, if you're not logged in
190 .order_by(hot_rank.desc())
191 .then_order_by(number_of_subscribers.desc())
192 .filter(user_id.is_null())
194 SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()),
195 SortType::TopAll => match self.from_user_id {
196 Some(from_user_id) => {
198 .filter(user_id.eq(from_user_id))
199 .order_by((subscribed.asc(), number_of_subscribers.desc()))
203 .order_by(number_of_subscribers.desc())
204 .filter(user_id.is_null())
211 query = query.filter(nsfw.eq(false));
214 let (limit, offset) = limit_and_offset(self.page, self.limit);
218 .filter(removed.eq(false))
219 .filter(deleted.eq(false))
220 .load::<CommunityView>(self.conn)
227 from_community_id: i32,
228 from_user_id: Option<i32>,
229 ) -> Result<Self, Error> {
230 use super::community_view::community_mview::dsl::*;
232 let mut query = community_mview.into_boxed();
234 query = query.filter(id.eq(from_community_id));
236 // The view lets you pass a null user_id, if you're not logged in
237 if let Some(from_user_id) = from_user_id {
238 query = query.filter(user_id.eq(from_user_id));
240 query = query.filter(user_id.is_null());
243 query.first::<Self>(conn)
248 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
250 #[table_name = "community_moderator_view"]
251 pub struct CommunityModeratorView {
253 pub community_id: i32,
255 pub published: chrono::NaiveDateTime,
256 pub user_name: String,
257 pub avatar: Option<String>,
258 pub community_name: String,
261 impl CommunityModeratorView {
262 pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
263 use super::community_view::community_moderator_view::dsl::*;
264 community_moderator_view
265 .filter(community_id.eq(from_community_id))
269 pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
270 use super::community_view::community_moderator_view::dsl::*;
271 community_moderator_view
272 .filter(user_id.eq(from_user_id))
278 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
280 #[table_name = "community_follower_view"]
281 pub struct CommunityFollowerView {
283 pub community_id: i32,
285 pub published: chrono::NaiveDateTime,
286 pub user_name: String,
287 pub avatar: Option<String>,
288 pub community_name: String,
291 impl CommunityFollowerView {
292 pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
293 use super::community_view::community_follower_view::dsl::*;
294 community_follower_view
295 .filter(community_id.eq(from_community_id))
299 pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
300 use super::community_view::community_follower_view::dsl::*;
301 community_follower_view
302 .filter(user_id.eq(from_user_id))
308 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
310 #[table_name = "community_user_ban_view"]
311 pub struct CommunityUserBanView {
313 pub community_id: i32,
315 pub published: chrono::NaiveDateTime,
316 pub user_name: String,
317 pub avatar: Option<String>,
318 pub community_name: String,
321 impl CommunityUserBanView {
322 pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> {
323 use super::community_view::community_user_ban_view::dsl::*;
324 community_user_ban_view
325 .filter(community_id.eq(from_community_id))
329 pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> {
330 use super::community_view::community_user_ban_view::dsl::*;
331 community_user_ban_view
332 .filter(user_id.eq(from_user_id))
339 from_community_id: i32,
340 ) -> Result<Self, Error> {
341 use super::community_view::community_user_ban_view::dsl::*;
342 community_user_ban_view
343 .filter(user_id.eq(from_user_id))
344 .filter(community_id.eq(from_community_id))