4 // The faked schema since diesel doesn't do views
10 parent_id -> Nullable<Int4>,
14 published -> Timestamp,
15 updated -> Nullable<Timestamp>,
18 community_name -> Varchar,
20 banned_from_community -> Bool,
21 creator_name -> Varchar,
22 creator_avatar -> Nullable<Text>,
27 user_id -> Nullable<Int4>,
28 my_vote -> Nullable<Int4>,
29 subscribed -> Nullable<Bool>,
30 saved -> Nullable<Bool>,
39 parent_id -> Nullable<Int4>,
43 published -> Timestamp,
44 updated -> Nullable<Timestamp>,
47 community_name -> Varchar,
49 banned_from_community -> Bool,
50 creator_name -> Varchar,
51 creator_avatar -> Nullable<Text>,
56 user_id -> Nullable<Int4>,
57 my_vote -> Nullable<Int4>,
58 subscribed -> Nullable<Bool>,
59 saved -> Nullable<Bool>,
64 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
66 #[table_name = "comment_view"]
67 pub struct CommentView {
71 pub parent_id: Option<i32>,
75 pub published: chrono::NaiveDateTime,
76 pub updated: Option<chrono::NaiveDateTime>,
78 pub community_id: i32,
79 pub community_name: String,
81 pub banned_from_community: bool,
82 pub creator_name: String,
83 pub creator_avatar: Option<String>,
88 pub user_id: Option<i32>,
89 pub my_vote: Option<i32>,
90 pub subscribed: Option<bool>,
91 pub saved: Option<bool>,
94 pub struct CommentQueryBuilder<'a> {
95 conn: &'a PgConnection,
96 query: super::comment_view::comment_mview::BoxedQuery<'a, Pg>,
97 listing_type: ListingType,
99 for_community_id: Option<i32>,
100 for_post_id: Option<i32>,
101 for_creator_id: Option<i32>,
102 search_term: Option<String>,
103 my_user_id: Option<i32>,
109 impl<'a> CommentQueryBuilder<'a> {
110 pub fn create(conn: &'a PgConnection) -> Self {
111 use super::comment_view::comment_mview::dsl::*;
113 let query = comment_mview.into_boxed();
115 CommentQueryBuilder {
118 listing_type: ListingType::All,
119 sort: &SortType::New,
120 for_community_id: None,
122 for_creator_id: None,
131 pub fn listing_type(mut self, listing_type: ListingType) -> Self {
132 self.listing_type = listing_type;
136 pub fn sort(mut self, sort: &'a SortType) -> Self {
141 pub fn for_post_id<T: MaybeOptional<i32>>(mut self, for_post_id: T) -> Self {
142 self.for_post_id = for_post_id.get_optional();
146 pub fn for_creator_id<T: MaybeOptional<i32>>(mut self, for_creator_id: T) -> Self {
147 self.for_creator_id = for_creator_id.get_optional();
151 pub fn for_community_id<T: MaybeOptional<i32>>(mut self, for_community_id: T) -> Self {
152 self.for_community_id = for_community_id.get_optional();
156 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
157 self.search_term = search_term.get_optional();
161 pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
162 self.my_user_id = my_user_id.get_optional();
166 pub fn saved_only(mut self, saved_only: bool) -> Self {
167 self.saved_only = saved_only;
171 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
172 self.page = page.get_optional();
176 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
177 self.limit = limit.get_optional();
181 pub fn list(self) -> Result<Vec<CommentView>, Error> {
182 use super::comment_view::comment_mview::dsl::*;
184 let mut query = self.query;
186 // The view lets you pass a null user_id, if you're not logged in
187 if let Some(my_user_id) = self.my_user_id {
188 query = query.filter(user_id.eq(my_user_id));
190 query = query.filter(user_id.is_null());
193 if let Some(for_creator_id) = self.for_creator_id {
194 query = query.filter(creator_id.eq(for_creator_id));
197 if let Some(for_community_id) = self.for_community_id {
198 query = query.filter(community_id.eq(for_community_id));
201 if let Some(for_post_id) = self.for_post_id {
202 query = query.filter(post_id.eq(for_post_id));
205 if let Some(search_term) = self.search_term {
206 query = query.filter(content.ilike(fuzzy_search(&search_term)));
209 if let ListingType::Subscribed = self.listing_type {
210 query = query.filter(subscribed.eq(true));
214 query = query.filter(saved.eq(true));
217 query = match self.sort {
218 SortType::Hot => query
219 .order_by(hot_rank.desc())
220 .then_order_by(published.desc()),
221 SortType::New => query.order_by(published.desc()),
222 SortType::TopAll => query.order_by(score.desc()),
223 SortType::TopYear => query
224 .filter(published.gt(now - 1.years()))
225 .order_by(score.desc()),
226 SortType::TopMonth => query
227 .filter(published.gt(now - 1.months()))
228 .order_by(score.desc()),
229 SortType::TopWeek => query
230 .filter(published.gt(now - 1.weeks()))
231 .order_by(score.desc()),
232 SortType::TopDay => query
233 .filter(published.gt(now - 1.days()))
234 .order_by(score.desc()),
235 // _ => query.order_by(published.desc()),
238 let (limit, offset) = limit_and_offset(self.page, self.limit);
240 // Note: deleted and removed comments are done on the front side
244 .load::<CommentView>(self.conn)
251 from_comment_id: i32,
252 my_user_id: Option<i32>,
253 ) -> Result<Self, Error> {
254 use super::comment_view::comment_mview::dsl::*;
255 let mut query = comment_mview.into_boxed();
257 // The view lets you pass a null user_id, if you're not logged in
258 if let Some(my_user_id) = my_user_id {
259 query = query.filter(user_id.eq(my_user_id));
261 query = query.filter(user_id.is_null());
265 .filter(id.eq(from_comment_id))
266 .order_by(published.desc());
268 query.first::<Self>(conn)
272 // The faked schema since diesel doesn't do views
278 parent_id -> Nullable<Int4>,
282 published -> Timestamp,
283 updated -> Nullable<Timestamp>,
285 community_id -> Int4,
286 community_name -> Varchar,
288 banned_from_community -> Bool,
289 creator_name -> Varchar,
290 creator_avatar -> Nullable<Text>,
295 user_id -> Nullable<Int4>,
296 my_vote -> Nullable<Int4>,
297 subscribed -> Nullable<Bool>,
298 saved -> Nullable<Bool>,
299 recipient_id -> Int4,
304 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
306 #[table_name = "reply_view"]
307 pub struct ReplyView {
311 pub parent_id: Option<i32>,
315 pub published: chrono::NaiveDateTime,
316 pub updated: Option<chrono::NaiveDateTime>,
318 pub community_id: i32,
319 pub community_name: String,
321 pub banned_from_community: bool,
322 pub creator_name: String,
323 pub creator_avatar: Option<String>,
328 pub user_id: Option<i32>,
329 pub my_vote: Option<i32>,
330 pub subscribed: Option<bool>,
331 pub saved: Option<bool>,
332 pub recipient_id: i32,
335 pub struct ReplyQueryBuilder<'a> {
336 conn: &'a PgConnection,
337 query: super::comment_view::reply_view::BoxedQuery<'a, Pg>,
345 impl<'a> ReplyQueryBuilder<'a> {
346 pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
347 use super::comment_view::reply_view::dsl::*;
349 let query = reply_view.into_boxed();
355 sort: &SortType::New,
362 pub fn sort(mut self, sort: &'a SortType) -> Self {
367 pub fn unread_only(mut self, unread_only: bool) -> Self {
368 self.unread_only = unread_only;
372 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
373 self.page = page.get_optional();
377 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
378 self.limit = limit.get_optional();
382 pub fn list(self) -> Result<Vec<ReplyView>, Error> {
383 use super::comment_view::reply_view::dsl::*;
385 let mut query = self.query;
388 .filter(user_id.eq(self.for_user_id))
389 .filter(recipient_id.eq(self.for_user_id))
390 .filter(deleted.eq(false))
391 .filter(removed.eq(false));
393 if self.unread_only {
394 query = query.filter(read.eq(false));
397 query = match self.sort {
398 // SortType::Hot => query.order_by(hot_rank.desc()),
399 SortType::New => query.order_by(published.desc()),
400 SortType::TopAll => query.order_by(score.desc()),
401 SortType::TopYear => query
402 .filter(published.gt(now - 1.years()))
403 .order_by(score.desc()),
404 SortType::TopMonth => query
405 .filter(published.gt(now - 1.months()))
406 .order_by(score.desc()),
407 SortType::TopWeek => query
408 .filter(published.gt(now - 1.weeks()))
409 .order_by(score.desc()),
410 SortType::TopDay => query
411 .filter(published.gt(now - 1.days()))
412 .order_by(score.desc()),
413 _ => query.order_by(published.desc()),
416 let (limit, offset) = limit_and_offset(self.page, self.limit);
420 .load::<ReplyView>(self.conn)
426 use super::super::comment::*;
427 use super::super::community::*;
428 use super::super::post::*;
429 use super::super::user::*;
433 let conn = establish_unpooled_connection();
435 let new_user = UserForm {
436 name: "timmy".into(),
437 fedi_name: "rrf".into(),
438 preferred_username: None,
439 password_encrypted: "nope".into(),
441 matrix_user_id: None,
447 theme: "darkly".into(),
448 default_sort_type: SortType::Hot as i16,
449 default_listing_type: ListingType::Subscribed as i16,
450 lang: "browser".into(),
452 send_notifications_to_email: false,
455 let inserted_user = User_::create(&conn, &new_user).unwrap();
457 let new_community = CommunityForm {
458 name: "test community 5".to_string(),
459 title: "nada".to_owned(),
462 creator_id: inserted_user.id,
469 let inserted_community = Community::create(&conn, &new_community).unwrap();
471 let new_post = PostForm {
472 name: "A test post 2".into(),
473 creator_id: inserted_user.id,
476 community_id: inserted_community.id,
484 embed_description: None,
489 let inserted_post = Post::create(&conn, &new_post).unwrap();
491 let comment_form = CommentForm {
492 content: "A test comment 32".into(),
493 creator_id: inserted_user.id,
494 post_id: inserted_post.id,
502 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
504 let comment_like_form = CommentLikeForm {
505 comment_id: inserted_comment.id,
506 post_id: inserted_post.id,
507 user_id: inserted_user.id,
511 let _inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
513 let expected_comment_view_no_user = CommentView {
514 id: inserted_comment.id,
515 content: "A test comment 32".into(),
516 creator_id: inserted_user.id,
517 post_id: inserted_post.id,
518 community_id: inserted_community.id,
519 community_name: inserted_community.name.to_owned(),
525 banned_from_community: false,
526 published: inserted_comment.published,
528 creator_name: inserted_user.name.to_owned(),
529 creator_avatar: None,
540 let expected_comment_view_with_user = CommentView {
541 id: inserted_comment.id,
542 content: "A test comment 32".into(),
543 creator_id: inserted_user.id,
544 post_id: inserted_post.id,
545 community_id: inserted_community.id,
546 community_name: inserted_community.name.to_owned(),
552 banned_from_community: false,
553 published: inserted_comment.published,
555 creator_name: inserted_user.name.to_owned(),
556 creator_avatar: None,
561 user_id: Some(inserted_user.id),
567 let mut read_comment_views_no_user = CommentQueryBuilder::create(&conn)
568 .for_post_id(inserted_post.id)
571 read_comment_views_no_user[0].hot_rank = 0;
573 let mut read_comment_views_with_user = CommentQueryBuilder::create(&conn)
574 .for_post_id(inserted_post.id)
575 .my_user_id(inserted_user.id)
578 read_comment_views_with_user[0].hot_rank = 0;
580 let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap();
581 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
582 Post::delete(&conn, inserted_post.id).unwrap();
583 Community::delete(&conn, inserted_community.id).unwrap();
584 User_::delete(&conn, inserted_user.id).unwrap();
586 assert_eq!(expected_comment_view_no_user, read_comment_views_no_user[0]);
588 expected_comment_view_with_user,
589 read_comment_views_with_user[0]
591 assert_eq!(1, num_deleted);
592 assert_eq!(1, like_removed);