]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/user_aggregates.rs
fcda1d462cc76745baddb574203affa692136466
[lemmy.git] / crates / db_queries / src / aggregates / user_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::user_aggregates;
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "user_aggregates"]
7 pub struct UserAggregates {
8   pub id: i32,
9   pub user_id: i32,
10   pub post_count: i64,
11   pub post_score: i64,
12   pub comment_count: i64,
13   pub comment_score: i64,
14 }
15
16 impl UserAggregates {
17   pub fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
18     user_aggregates::table
19       .filter(user_aggregates::user_id.eq(user_id))
20       .first::<Self>(conn)
21   }
22 }
23
24 #[cfg(test)]
25 mod tests {
26   use crate::{
27     aggregates::user_aggregates::UserAggregates,
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, PostLike, PostLikeForm},
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_user_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       inbox_url: None,
71       shared_inbox_url: None,
72     };
73
74     let inserted_user = User_::create(&conn, &new_user).unwrap();
75
76     let another_user = UserForm {
77       name: "jerry_user_agg".into(),
78       preferred_username: None,
79       password_encrypted: "nope".into(),
80       email: None,
81       matrix_user_id: None,
82       avatar: None,
83       banner: None,
84       admin: false,
85       banned: Some(false),
86       published: None,
87       updated: None,
88       show_nsfw: false,
89       theme: "browser".into(),
90       default_sort_type: SortType::Hot as i16,
91       default_listing_type: ListingType::Subscribed as i16,
92       lang: "browser".into(),
93       show_avatars: true,
94       send_notifications_to_email: false,
95       actor_id: None,
96       bio: None,
97       local: true,
98       private_key: None,
99       public_key: None,
100       last_refreshed_at: None,
101       inbox_url: None,
102       shared_inbox_url: None,
103     };
104
105     let another_inserted_user = User_::create(&conn, &another_user).unwrap();
106
107     let new_community = CommunityForm {
108       name: "TIL_site_agg".into(),
109       creator_id: inserted_user.id,
110       title: "nada".to_owned(),
111       description: None,
112       nsfw: false,
113       removed: None,
114       deleted: None,
115       updated: None,
116       actor_id: None,
117       local: true,
118       private_key: None,
119       public_key: None,
120       last_refreshed_at: None,
121       published: None,
122       icon: None,
123       banner: None,
124       followers_url: None,
125       inbox_url: None,
126       shared_inbox_url: None,
127     };
128
129     let inserted_community = Community::create(&conn, &new_community).unwrap();
130
131     let new_post = PostForm {
132       name: "A test post".into(),
133       url: None,
134       body: None,
135       creator_id: inserted_user.id,
136       community_id: inserted_community.id,
137       removed: None,
138       deleted: None,
139       locked: None,
140       stickied: None,
141       nsfw: false,
142       updated: None,
143       embed_title: None,
144       embed_description: None,
145       embed_html: None,
146       thumbnail_url: None,
147       ap_id: None,
148       local: true,
149       published: None,
150     };
151
152     let inserted_post = Post::create(&conn, &new_post).unwrap();
153
154     let post_like = PostLikeForm {
155       post_id: inserted_post.id,
156       user_id: inserted_user.id,
157       score: 1,
158     };
159
160     let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
161
162     let comment_form = CommentForm {
163       content: "A test comment".into(),
164       creator_id: inserted_user.id,
165       post_id: inserted_post.id,
166       removed: None,
167       deleted: None,
168       read: None,
169       parent_id: None,
170       published: None,
171       updated: None,
172       ap_id: None,
173       local: true,
174     };
175
176     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
177
178     let mut comment_like = CommentLikeForm {
179       comment_id: inserted_comment.id,
180       user_id: inserted_user.id,
181       post_id: inserted_post.id,
182       score: 1,
183     };
184
185     let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
186
187     let mut child_comment_form = CommentForm {
188       content: "A test comment".into(),
189       creator_id: inserted_user.id,
190       post_id: inserted_post.id,
191       removed: None,
192       deleted: None,
193       read: None,
194       parent_id: Some(inserted_comment.id),
195       published: None,
196       updated: None,
197       ap_id: None,
198       local: true,
199     };
200
201     let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
202
203     let child_comment_like = CommentLikeForm {
204       comment_id: inserted_child_comment.id,
205       user_id: another_inserted_user.id,
206       post_id: inserted_post.id,
207       score: 1,
208     };
209
210     let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
211
212     let user_aggregates_before_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
213
214     assert_eq!(1, user_aggregates_before_delete.post_count);
215     assert_eq!(1, user_aggregates_before_delete.post_score);
216     assert_eq!(2, user_aggregates_before_delete.comment_count);
217     assert_eq!(2, user_aggregates_before_delete.comment_score);
218
219     // Remove a post like
220     PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
221     let after_post_like_remove = UserAggregates::read(&conn, inserted_user.id).unwrap();
222     assert_eq!(0, after_post_like_remove.post_score);
223
224     // Remove a parent comment (the scores should also be removed)
225     Comment::delete(&conn, inserted_comment.id).unwrap();
226     let after_parent_comment_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
227     assert_eq!(0, after_parent_comment_delete.comment_count);
228     assert_eq!(0, after_parent_comment_delete.comment_score);
229
230     // Add in the two comments again, then delete the post.
231     let new_parent_comment = Comment::create(&conn, &comment_form).unwrap();
232     child_comment_form.parent_id = Some(new_parent_comment.id);
233     Comment::create(&conn, &child_comment_form).unwrap();
234     comment_like.comment_id = new_parent_comment.id;
235     CommentLike::like(&conn, &comment_like).unwrap();
236     let after_comment_add = UserAggregates::read(&conn, inserted_user.id).unwrap();
237     assert_eq!(2, after_comment_add.comment_count);
238     assert_eq!(1, after_comment_add.comment_score);
239
240     Post::delete(&conn, inserted_post.id).unwrap();
241     let after_post_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
242     assert_eq!(0, after_post_delete.comment_score);
243     assert_eq!(0, after_post_delete.comment_count);
244     assert_eq!(0, after_post_delete.post_score);
245     assert_eq!(0, after_post_delete.post_count);
246
247     // This should delete all the associated rows, and fire triggers
248     let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
249     assert_eq!(1, user_num_deleted);
250     User_::delete(&conn, another_inserted_user.id).unwrap();
251
252     // Should be none found
253     let after_delete = UserAggregates::read(&conn, inserted_user.id);
254     assert!(after_delete.is_err());
255   }
256 }