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