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(hot_rank.desc(), published.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(),
268 let inserted_user = User_::create(&conn, &new_user).unwrap();
270 let new_community = CommunityForm {
271 name: "test community 5".to_string(),
272 title: "nada".to_owned(),
275 creator_id: inserted_user.id,
282 let inserted_community = Community::create(&conn, &new_community).unwrap();
284 let new_post = PostForm {
285 name: "A test post 2".into(),
286 creator_id: inserted_user.id,
289 community_id: inserted_community.id,
297 let inserted_post = Post::create(&conn, &new_post).unwrap();
299 let comment_form = CommentForm {
300 content: "A test comment 32".into(),
301 creator_id: inserted_user.id,
302 post_id: inserted_post.id,
310 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
312 let comment_like_form = CommentLikeForm {
313 comment_id: inserted_comment.id,
314 post_id: inserted_post.id,
315 user_id: inserted_user.id,
319 let _inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
321 let expected_comment_view_no_user = CommentView {
322 id: inserted_comment.id,
323 content: "A test comment 32".into(),
324 creator_id: inserted_user.id,
325 post_id: inserted_post.id,
326 community_id: inserted_community.id,
332 banned_from_community: false,
333 published: inserted_comment.published,
335 creator_name: inserted_user.name.to_owned(),
344 let expected_comment_view_with_user = CommentView {
345 id: inserted_comment.id,
346 content: "A test comment 32".into(),
347 creator_id: inserted_user.id,
348 post_id: inserted_post.id,
349 community_id: inserted_community.id,
355 banned_from_community: false,
356 published: inserted_comment.published,
358 creator_name: inserted_user.name.to_owned(),
362 user_id: Some(inserted_user.id),
367 let read_comment_views_no_user = CommentView::list(
370 Some(inserted_post.id),
377 let read_comment_views_with_user = CommentView::list(
380 Some(inserted_post.id),
383 Some(inserted_user.id),
387 let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap();
388 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
389 Post::delete(&conn, inserted_post.id).unwrap();
390 Community::delete(&conn, inserted_community.id).unwrap();
391 User_::delete(&conn, inserted_user.id).unwrap();
393 assert_eq!(expected_comment_view_no_user, read_comment_views_no_user[0]);
394 assert_eq!(expected_comment_view_with_user, read_comment_views_with_user[0]);
395 assert_eq!(1, num_deleted);
396 assert_eq!(1, like_removed);