3 // The faked schema since diesel doesn't do views
9 parent_id -> Nullable<Int4>,
13 published -> Timestamp,
14 updated -> Nullable<Timestamp>,
18 banned_from_community -> Bool,
19 creator_name -> Varchar,
23 user_id -> Nullable<Int4>,
24 my_vote -> Nullable<Int4>,
25 saved -> Nullable<Bool>,
29 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
30 #[table_name="comment_view"]
31 pub struct CommentView {
35 pub parent_id: Option<i32>,
39 pub published: chrono::NaiveDateTime,
40 pub updated: Option<chrono::NaiveDateTime>,
42 pub community_id: i32,
44 pub banned_from_community: bool,
45 pub creator_name: String,
49 pub user_id: Option<i32>,
50 pub my_vote: Option<i32>,
51 pub saved: Option<bool>,
56 pub fn list(conn: &PgConnection,
58 for_post_id: Option<i32>,
59 for_creator_id: Option<i32>,
60 search_term: Option<String>,
61 my_user_id: Option<i32>,
65 ) -> Result<Vec<Self>, Error> {
66 use super::comment_view::comment_view::dsl::*;
68 let (limit, offset) = limit_and_offset(page, limit);
70 // TODO no limits here?
71 let mut query = comment_view.into_boxed();
73 // The view lets you pass a null user_id, if you're not logged in
74 if let Some(my_user_id) = my_user_id {
75 query = query.filter(user_id.eq(my_user_id));
77 query = query.filter(user_id.is_null());
80 if let Some(for_creator_id) = for_creator_id {
81 query = query.filter(creator_id.eq(for_creator_id));
84 if let Some(for_post_id) = for_post_id {
85 query = query.filter(post_id.eq(for_post_id));
88 if let Some(search_term) = search_term {
89 query = query.filter(content.ilike(fuzzy_search(&search_term)));
93 query = query.filter(saved.eq(true));
97 // SortType::Hot => query.order_by(hot_rank.desc()),
98 SortType::New => query.order_by(published.desc()),
99 SortType::TopAll => query.order_by(score.desc()),
100 SortType::TopYear => query
101 .filter(published.gt(now - 1.years()))
102 .order_by(score.desc()),
103 SortType::TopMonth => query
104 .filter(published.gt(now - 1.months()))
105 .order_by(score.desc()),
106 SortType::TopWeek => query
107 .filter(published.gt(now - 1.weeks()))
108 .order_by(score.desc()),
109 SortType::TopDay => query
110 .filter(published.gt(now - 1.days()))
111 .order_by(score.desc()),
112 _ => query.order_by(published.desc())
115 // Note: deleted and removed comments are done on the front side
122 pub fn read(conn: &PgConnection, from_comment_id: i32, my_user_id: Option<i32>) -> Result<Self, Error> {
123 use super::comment_view::comment_view::dsl::*;
125 let mut query = comment_view.into_boxed();
127 // The view lets you pass a null user_id, if you're not logged in
128 if let Some(my_user_id) = my_user_id {
129 query = query.filter(user_id.eq(my_user_id));
131 query = query.filter(user_id.is_null());
134 query = query.filter(id.eq(from_comment_id)).order_by(published.desc());
136 query.first::<Self>(conn)
142 // The faked schema since diesel doesn't do views
148 parent_id -> Nullable<Int4>,
152 published -> Timestamp,
153 updated -> Nullable<Timestamp>,
155 community_id -> Int4,
157 banned_from_community -> Bool,
158 creator_name -> Varchar,
162 user_id -> Nullable<Int4>,
163 my_vote -> Nullable<Int4>,
164 saved -> Nullable<Bool>,
165 recipient_id -> Int4,
169 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
170 #[table_name="reply_view"]
171 pub struct ReplyView {
175 pub parent_id: Option<i32>,
179 pub published: chrono::NaiveDateTime,
180 pub updated: Option<chrono::NaiveDateTime>,
182 pub community_id: i32,
184 pub banned_from_community: bool,
185 pub creator_name: String,
189 pub user_id: Option<i32>,
190 pub my_vote: Option<i32>,
191 pub saved: Option<bool>,
192 pub recipient_id: i32,
197 pub fn get_replies(conn: &PgConnection,
203 ) -> Result<Vec<Self>, Error> {
204 use super::comment_view::reply_view::dsl::*;
206 let (limit, offset) = limit_and_offset(page, limit);
208 let mut query = reply_view.into_boxed();
211 .filter(user_id.eq(for_user_id))
212 .filter(recipient_id.eq(for_user_id));
215 query = query.filter(read.eq(false));
219 // SortType::Hot => query.order_by(hot_rank.desc()),
220 SortType::New => query.order_by(published.desc()),
221 SortType::TopAll => query.order_by(score.desc()),
222 SortType::TopYear => query
223 .filter(published.gt(now - 1.years()))
224 .order_by(score.desc()),
225 SortType::TopMonth => query
226 .filter(published.gt(now - 1.months()))
227 .order_by(score.desc()),
228 SortType::TopWeek => query
229 .filter(published.gt(now - 1.weeks()))
230 .order_by(score.desc()),
231 SortType::TopDay => query
232 .filter(published.gt(now - 1.days()))
233 .order_by(score.desc()),
234 _ => query.order_by(published.desc())
248 use super::super::post::*;
249 use super::super::community::*;
250 use super::super::user::*;
251 use super::super::comment::*;
254 let conn = establish_connection();
256 let new_user = UserForm {
257 name: "timmy".into(),
258 fedi_name: "rrf".into(),
259 preferred_username: None,
260 password_encrypted: "nope".into(),
267 let inserted_user = User_::create(&conn, &new_user).unwrap();
269 let new_community = CommunityForm {
270 name: "test community 5".to_string(),
271 title: "nada".to_owned(),
274 creator_id: inserted_user.id,
280 let inserted_community = Community::create(&conn, &new_community).unwrap();
282 let new_post = PostForm {
283 name: "A test post 2".into(),
284 creator_id: inserted_user.id,
287 community_id: inserted_community.id,
294 let inserted_post = Post::create(&conn, &new_post).unwrap();
296 let comment_form = CommentForm {
297 content: "A test comment 32".into(),
298 creator_id: inserted_user.id,
299 post_id: inserted_post.id,
307 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
309 let comment_like_form = CommentLikeForm {
310 comment_id: inserted_comment.id,
311 post_id: inserted_post.id,
312 user_id: inserted_user.id,
316 let _inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
318 let expected_comment_view_no_user = CommentView {
319 id: inserted_comment.id,
320 content: "A test comment 32".into(),
321 creator_id: inserted_user.id,
322 post_id: inserted_post.id,
323 community_id: inserted_community.id,
329 banned_from_community: false,
330 published: inserted_comment.published,
332 creator_name: inserted_user.name.to_owned(),
341 let expected_comment_view_with_user = CommentView {
342 id: inserted_comment.id,
343 content: "A test comment 32".into(),
344 creator_id: inserted_user.id,
345 post_id: inserted_post.id,
346 community_id: inserted_community.id,
352 banned_from_community: false,
353 published: inserted_comment.published,
355 creator_name: inserted_user.name.to_owned(),
359 user_id: Some(inserted_user.id),
364 let read_comment_views_no_user = CommentView::list(
367 Some(inserted_post.id),
374 let read_comment_views_with_user = CommentView::list(
377 Some(inserted_post.id),
380 Some(inserted_user.id),
384 let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap();
385 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
386 Post::delete(&conn, inserted_post.id).unwrap();
387 Community::delete(&conn, inserted_community.id).unwrap();
388 User_::delete(&conn, inserted_user.id).unwrap();
390 assert_eq!(expected_comment_view_no_user, read_comment_views_no_user[0]);
391 assert_eq!(expected_comment_view_with_user, read_comment_views_with_user[0]);
392 assert_eq!(1, num_deleted);
393 assert_eq!(1, like_removed);