]> Untitled Git - lemmy.git/blob - lemmy_db_queries/src/aggregates/comment_aggregates.rs
Merge pull request #1328 from LemmyNet/move_views_to_diesel
[lemmy.git] / lemmy_db_queries / src / aggregates / comment_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::comment_aggregates;
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "comment_aggregates"]
7 pub struct CommentAggregates {
8   pub id: i32,
9   pub comment_id: i32,
10   pub score: i64,
11   pub upvotes: i64,
12   pub downvotes: i64,
13   pub published: chrono::NaiveDateTime,
14 }
15
16 impl CommentAggregates {
17   pub fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
18     comment_aggregates::table
19       .filter(comment_aggregates::comment_id.eq(comment_id))
20       .first::<Self>(conn)
21   }
22 }
23
24 #[cfg(test)]
25 mod tests {
26   use crate::{
27     aggregates::comment_aggregates::CommentAggregates,
28     establish_unpooled_connection,
29     Crud,
30     Likeable,
31     ListingType,
32     SortType,
33   };
34   use lemmy_db_schema::source::{
35     comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
36     community::{Community, CommunityForm},
37     post::{Post, PostForm},
38     user::{UserForm, User_},
39   };
40
41   #[test]
42   fn test_crud() {
43     let conn = establish_unpooled_connection();
44
45     let new_user = UserForm {
46       name: "thommy_comment_agg".into(),
47       preferred_username: None,
48       password_encrypted: "nope".into(),
49       email: None,
50       matrix_user_id: None,
51       avatar: None,
52       banner: None,
53       admin: false,
54       banned: Some(false),
55       published: None,
56       updated: None,
57       show_nsfw: false,
58       theme: "browser".into(),
59       default_sort_type: SortType::Hot as i16,
60       default_listing_type: ListingType::Subscribed as i16,
61       lang: "browser".into(),
62       show_avatars: true,
63       send_notifications_to_email: false,
64       actor_id: None,
65       bio: None,
66       local: true,
67       private_key: None,
68       public_key: None,
69       last_refreshed_at: None,
70     };
71
72     let inserted_user = User_::create(&conn, &new_user).unwrap();
73
74     let another_user = UserForm {
75       name: "jerry_comment_agg".into(),
76       preferred_username: None,
77       password_encrypted: "nope".into(),
78       email: None,
79       matrix_user_id: None,
80       avatar: None,
81       banner: None,
82       admin: false,
83       banned: Some(false),
84       published: None,
85       updated: None,
86       show_nsfw: false,
87       theme: "browser".into(),
88       default_sort_type: SortType::Hot as i16,
89       default_listing_type: ListingType::Subscribed as i16,
90       lang: "browser".into(),
91       show_avatars: true,
92       send_notifications_to_email: false,
93       actor_id: None,
94       bio: None,
95       local: true,
96       private_key: None,
97       public_key: None,
98       last_refreshed_at: None,
99     };
100
101     let another_inserted_user = User_::create(&conn, &another_user).unwrap();
102
103     let new_community = CommunityForm {
104       name: "TIL_comment_agg".into(),
105       creator_id: inserted_user.id,
106       title: "nada".to_owned(),
107       description: None,
108       category_id: 1,
109       nsfw: false,
110       removed: None,
111       deleted: None,
112       updated: None,
113       actor_id: None,
114       local: true,
115       private_key: None,
116       public_key: None,
117       last_refreshed_at: None,
118       published: None,
119       icon: None,
120       banner: None,
121     };
122
123     let inserted_community = Community::create(&conn, &new_community).unwrap();
124
125     let new_post = PostForm {
126       name: "A test post".into(),
127       url: None,
128       body: None,
129       creator_id: inserted_user.id,
130       community_id: inserted_community.id,
131       removed: None,
132       deleted: None,
133       locked: None,
134       stickied: None,
135       nsfw: false,
136       updated: None,
137       embed_title: None,
138       embed_description: None,
139       embed_html: None,
140       thumbnail_url: None,
141       ap_id: None,
142       local: true,
143       published: None,
144     };
145
146     let inserted_post = Post::create(&conn, &new_post).unwrap();
147
148     let comment_form = CommentForm {
149       content: "A test comment".into(),
150       creator_id: inserted_user.id,
151       post_id: inserted_post.id,
152       removed: None,
153       deleted: None,
154       read: None,
155       parent_id: None,
156       published: None,
157       updated: None,
158       ap_id: None,
159       local: true,
160     };
161
162     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
163
164     let child_comment_form = CommentForm {
165       content: "A test comment".into(),
166       creator_id: inserted_user.id,
167       post_id: inserted_post.id,
168       removed: None,
169       deleted: None,
170       read: None,
171       parent_id: Some(inserted_comment.id),
172       published: None,
173       updated: None,
174       ap_id: None,
175       local: true,
176     };
177
178     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
179
180     let comment_like = CommentLikeForm {
181       comment_id: inserted_comment.id,
182       post_id: inserted_post.id,
183       user_id: inserted_user.id,
184       score: 1,
185     };
186
187     CommentLike::like(&conn, &comment_like).unwrap();
188
189     let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
190
191     assert_eq!(1, comment_aggs_before_delete.score);
192     assert_eq!(1, comment_aggs_before_delete.upvotes);
193     assert_eq!(0, comment_aggs_before_delete.downvotes);
194
195     // Add a post dislike from the other user
196     let comment_dislike = CommentLikeForm {
197       comment_id: inserted_comment.id,
198       post_id: inserted_post.id,
199       user_id: another_inserted_user.id,
200       score: -1,
201     };
202
203     CommentLike::like(&conn, &comment_dislike).unwrap();
204
205     let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
206
207     assert_eq!(0, comment_aggs_after_dislike.score);
208     assert_eq!(1, comment_aggs_after_dislike.upvotes);
209     assert_eq!(1, comment_aggs_after_dislike.downvotes);
210
211     // Remove the first comment like
212     CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
213     let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
214     assert_eq!(-1, after_like_remove.score);
215     assert_eq!(0, after_like_remove.upvotes);
216     assert_eq!(1, after_like_remove.downvotes);
217
218     // Remove the parent post
219     Post::delete(&conn, inserted_post.id).unwrap();
220
221     // Should be none found, since the post was deleted
222     let after_delete = CommentAggregates::read(&conn, inserted_comment.id);
223     assert!(after_delete.is_err());
224
225     // This should delete all the associated rows, and fire triggers
226     User_::delete(&conn, another_inserted_user.id).unwrap();
227     let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
228     assert_eq!(1, user_num_deleted);
229   }
230 }