1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::community_aggregates;
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "community_aggregates"]
7 pub struct CommunityAggregates {
13 pub published: chrono::NaiveDateTime,
14 pub users_active_day: i64,
15 pub users_active_week: i64,
16 pub users_active_month: i64,
17 pub users_active_half_year: i64,
20 impl CommunityAggregates {
21 pub fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
22 community_aggregates::table
23 .filter(community_aggregates::community_id.eq(community_id))
31 aggregates::community_aggregates::CommunityAggregates,
32 establish_unpooled_connection,
38 use lemmy_db_schema::source::{
39 comment::{Comment, CommentForm},
40 community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
41 post::{Post, PostForm},
42 user::{UserForm, User_},
47 let conn = establish_unpooled_connection();
49 let new_user = UserForm {
50 name: "thommy_community_agg".into(),
51 preferred_username: None,
52 password_encrypted: "nope".into(),
62 theme: "browser".into(),
63 default_sort_type: SortType::Hot as i16,
64 default_listing_type: ListingType::Subscribed as i16,
65 lang: "browser".into(),
67 send_notifications_to_email: false,
73 last_refreshed_at: None,
76 let inserted_user = User_::create(&conn, &new_user).unwrap();
78 let another_user = UserForm {
79 name: "jerry_community_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,
105 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
107 let new_community = CommunityForm {
108 name: "TIL_community_agg".into(),
109 creator_id: inserted_user.id,
110 title: "nada".to_owned(),
121 last_refreshed_at: None,
127 let inserted_community = Community::create(&conn, &new_community).unwrap();
129 let another_community = CommunityForm {
130 name: "TIL_community_agg_2".into(),
131 creator_id: inserted_user.id,
132 title: "nada".to_owned(),
143 last_refreshed_at: None,
149 let another_inserted_community = Community::create(&conn, &another_community).unwrap();
151 let first_user_follow = CommunityFollowerForm {
152 community_id: inserted_community.id,
153 user_id: inserted_user.id,
157 CommunityFollower::follow(&conn, &first_user_follow).unwrap();
159 let second_user_follow = CommunityFollowerForm {
160 community_id: inserted_community.id,
161 user_id: another_inserted_user.id,
165 CommunityFollower::follow(&conn, &second_user_follow).unwrap();
167 let another_community_follow = CommunityFollowerForm {
168 community_id: another_inserted_community.id,
169 user_id: inserted_user.id,
173 CommunityFollower::follow(&conn, &another_community_follow).unwrap();
175 let new_post = PostForm {
176 name: "A test post".into(),
179 creator_id: inserted_user.id,
180 community_id: inserted_community.id,
188 embed_description: None,
196 let inserted_post = Post::create(&conn, &new_post).unwrap();
198 let comment_form = CommentForm {
199 content: "A test comment".into(),
200 creator_id: inserted_user.id,
201 post_id: inserted_post.id,
212 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
214 let child_comment_form = CommentForm {
215 content: "A test comment".into(),
216 creator_id: inserted_user.id,
217 post_id: inserted_post.id,
221 parent_id: Some(inserted_comment.id),
228 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
230 let community_aggregates_before_delete =
231 CommunityAggregates::read(&conn, inserted_community.id).unwrap();
233 assert_eq!(2, community_aggregates_before_delete.subscribers);
234 assert_eq!(1, community_aggregates_before_delete.posts);
235 assert_eq!(2, community_aggregates_before_delete.comments);
237 // Test the other community
238 let another_community_aggs =
239 CommunityAggregates::read(&conn, another_inserted_community.id).unwrap();
240 assert_eq!(1, another_community_aggs.subscribers);
241 assert_eq!(0, another_community_aggs.posts);
242 assert_eq!(0, another_community_aggs.comments);
245 CommunityFollower::unfollow(&conn, &second_user_follow).unwrap();
246 let after_unfollow = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
247 assert_eq!(1, after_unfollow.subscribers);
249 // Follow again just for the later tests
250 CommunityFollower::follow(&conn, &second_user_follow).unwrap();
251 let after_follow_again = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
252 assert_eq!(2, after_follow_again.subscribers);
254 // Remove a parent comment (the comment count should also be 0)
255 Post::delete(&conn, inserted_post.id).unwrap();
256 let after_parent_post_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
257 assert_eq!(0, after_parent_post_delete.comments);
258 assert_eq!(0, after_parent_post_delete.posts);
260 // Remove the 2nd user
261 User_::delete(&conn, another_inserted_user.id).unwrap();
262 let after_user_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
263 assert_eq!(1, after_user_delete.subscribers);
265 // This should delete all the associated rows, and fire triggers
266 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
267 assert_eq!(1, user_num_deleted);
269 // Should be none found, since the creator was deleted
270 let after_delete = CommunityAggregates::read(&conn, inserted_community.id);
271 assert!(after_delete.is_err());