1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::user_aggregates;
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "user_aggregates"]
7 pub struct UserAggregates {
12 pub comment_count: i64,
13 pub comment_score: i64,
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))
27 aggregates::user_aggregates::UserAggregates,
28 establish_unpooled_connection,
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_},
40 use serial_test::serial;
45 let conn = establish_unpooled_connection();
47 let new_user = UserForm {
48 name: "thommy_user_agg".into(),
49 preferred_username: None,
50 password_encrypted: "nope".into(),
60 theme: "browser".into(),
61 default_sort_type: SortType::Hot as i16,
62 default_listing_type: ListingType::Subscribed as i16,
63 lang: "browser".into(),
65 send_notifications_to_email: false,
71 last_refreshed_at: None,
73 shared_inbox_url: None,
76 let inserted_user = User_::create(&conn, &new_user).unwrap();
78 let another_user = UserForm {
79 name: "jerry_user_agg".into(),
80 preferred_username: None,
81 password_encrypted: "nope".into(),
91 theme: "browser".into(),
92 default_sort_type: SortType::Hot as i16,
93 default_listing_type: ListingType::Subscribed as i16,
94 lang: "browser".into(),
96 send_notifications_to_email: false,
102 last_refreshed_at: None,
104 shared_inbox_url: None,
107 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
109 let new_community = CommunityForm {
110 name: "TIL_site_agg".into(),
111 creator_id: inserted_user.id,
112 title: "nada".to_owned(),
122 last_refreshed_at: None,
128 shared_inbox_url: None,
131 let inserted_community = Community::create(&conn, &new_community).unwrap();
133 let new_post = PostForm {
134 name: "A test post".into(),
137 creator_id: inserted_user.id,
138 community_id: inserted_community.id,
146 embed_description: None,
154 let inserted_post = Post::create(&conn, &new_post).unwrap();
156 let post_like = PostLikeForm {
157 post_id: inserted_post.id,
158 user_id: inserted_user.id,
162 let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
164 let comment_form = CommentForm {
165 content: "A test comment".into(),
166 creator_id: inserted_user.id,
167 post_id: inserted_post.id,
178 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
180 let mut comment_like = CommentLikeForm {
181 comment_id: inserted_comment.id,
182 user_id: inserted_user.id,
183 post_id: inserted_post.id,
187 let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
189 let mut child_comment_form = CommentForm {
190 content: "A test comment".into(),
191 creator_id: inserted_user.id,
192 post_id: inserted_post.id,
196 parent_id: Some(inserted_comment.id),
203 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
205 let child_comment_like = CommentLikeForm {
206 comment_id: inserted_child_comment.id,
207 user_id: another_inserted_user.id,
208 post_id: inserted_post.id,
212 let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
214 let user_aggregates_before_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
216 assert_eq!(1, user_aggregates_before_delete.post_count);
217 assert_eq!(1, user_aggregates_before_delete.post_score);
218 assert_eq!(2, user_aggregates_before_delete.comment_count);
219 assert_eq!(2, user_aggregates_before_delete.comment_score);
221 // Remove a post like
222 PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
223 let after_post_like_remove = UserAggregates::read(&conn, inserted_user.id).unwrap();
224 assert_eq!(0, after_post_like_remove.post_score);
226 // Remove a parent comment (the scores should also be removed)
227 Comment::delete(&conn, inserted_comment.id).unwrap();
228 let after_parent_comment_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
229 assert_eq!(0, after_parent_comment_delete.comment_count);
230 assert_eq!(0, after_parent_comment_delete.comment_score);
232 // Add in the two comments again, then delete the post.
233 let new_parent_comment = Comment::create(&conn, &comment_form).unwrap();
234 child_comment_form.parent_id = Some(new_parent_comment.id);
235 Comment::create(&conn, &child_comment_form).unwrap();
236 comment_like.comment_id = new_parent_comment.id;
237 CommentLike::like(&conn, &comment_like).unwrap();
238 let after_comment_add = UserAggregates::read(&conn, inserted_user.id).unwrap();
239 assert_eq!(2, after_comment_add.comment_count);
240 assert_eq!(1, after_comment_add.comment_score);
242 Post::delete(&conn, inserted_post.id).unwrap();
243 let after_post_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
244 assert_eq!(0, after_post_delete.comment_score);
245 assert_eq!(0, after_post_delete.comment_count);
246 assert_eq!(0, after_post_delete.post_score);
247 assert_eq!(0, after_post_delete.post_count);
249 // This should delete all the associated rows, and fire triggers
250 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
251 assert_eq!(1, user_num_deleted);
252 User_::delete(&conn, another_inserted_user.id).unwrap();
254 // Should be none found
255 let after_delete = UserAggregates::read(&conn, inserted_user.id);
256 assert!(after_delete.is_err());