1 use diesel::{result::Error, *};
2 use lemmy_db_schema::{schema::community_aggregates, CommunityId};
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "community_aggregates"]
7 pub struct CommunityAggregates {
9 pub community_id: CommunityId,
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: CommunityId) -> 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,
36 use lemmy_db_schema::source::{
37 comment::{Comment, CommentForm},
38 community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
39 person::{Person, PersonForm},
40 post::{Post, PostForm},
42 use serial_test::serial;
47 let conn = establish_unpooled_connection();
49 let new_person = PersonForm {
50 name: "thommy_community_agg".into(),
51 ..PersonForm::default()
54 let inserted_person = Person::create(&conn, &new_person).unwrap();
56 let another_person = PersonForm {
57 name: "jerry_community_agg".into(),
58 ..PersonForm::default()
61 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
63 let new_community = CommunityForm {
64 name: "TIL_community_agg".into(),
65 title: "nada".to_owned(),
66 ..CommunityForm::default()
69 let inserted_community = Community::create(&conn, &new_community).unwrap();
71 let another_community = CommunityForm {
72 name: "TIL_community_agg_2".into(),
73 title: "nada".to_owned(),
74 ..CommunityForm::default()
77 let another_inserted_community = Community::create(&conn, &another_community).unwrap();
79 let first_person_follow = CommunityFollowerForm {
80 community_id: inserted_community.id,
81 person_id: inserted_person.id,
85 CommunityFollower::follow(&conn, &first_person_follow).unwrap();
87 let second_person_follow = CommunityFollowerForm {
88 community_id: inserted_community.id,
89 person_id: another_inserted_person.id,
93 CommunityFollower::follow(&conn, &second_person_follow).unwrap();
95 let another_community_follow = CommunityFollowerForm {
96 community_id: another_inserted_community.id,
97 person_id: inserted_person.id,
101 CommunityFollower::follow(&conn, &another_community_follow).unwrap();
103 let new_post = PostForm {
104 name: "A test post".into(),
105 creator_id: inserted_person.id,
106 community_id: inserted_community.id,
107 ..PostForm::default()
110 let inserted_post = Post::create(&conn, &new_post).unwrap();
112 let comment_form = CommentForm {
113 content: "A test comment".into(),
114 creator_id: inserted_person.id,
115 post_id: inserted_post.id,
116 ..CommentForm::default()
119 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
121 let child_comment_form = CommentForm {
122 content: "A test comment".into(),
123 creator_id: inserted_person.id,
124 post_id: inserted_post.id,
125 parent_id: Some(inserted_comment.id),
126 ..CommentForm::default()
129 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
131 let community_aggregates_before_delete =
132 CommunityAggregates::read(&conn, inserted_community.id).unwrap();
134 assert_eq!(2, community_aggregates_before_delete.subscribers);
135 assert_eq!(1, community_aggregates_before_delete.posts);
136 assert_eq!(2, community_aggregates_before_delete.comments);
138 // Test the other community
139 let another_community_aggs =
140 CommunityAggregates::read(&conn, another_inserted_community.id).unwrap();
141 assert_eq!(1, another_community_aggs.subscribers);
142 assert_eq!(0, another_community_aggs.posts);
143 assert_eq!(0, another_community_aggs.comments);
146 CommunityFollower::unfollow(&conn, &second_person_follow).unwrap();
147 let after_unfollow = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
148 assert_eq!(1, after_unfollow.subscribers);
150 // Follow again just for the later tests
151 CommunityFollower::follow(&conn, &second_person_follow).unwrap();
152 let after_follow_again = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
153 assert_eq!(2, after_follow_again.subscribers);
155 // Remove a parent comment (the comment count should also be 0)
156 Post::delete(&conn, inserted_post.id).unwrap();
157 let after_parent_post_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
158 assert_eq!(0, after_parent_post_delete.comments);
159 assert_eq!(0, after_parent_post_delete.posts);
161 // Remove the 2nd person
162 Person::delete(&conn, another_inserted_person.id).unwrap();
163 let after_person_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
164 assert_eq!(1, after_person_delete.subscribers);
166 // This should delete all the associated rows, and fire triggers
167 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
168 assert_eq!(1, person_num_deleted);
170 // Delete the community
171 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
172 assert_eq!(1, community_num_deleted);
174 let another_community_num_deleted =
175 Community::delete(&conn, another_inserted_community.id).unwrap();
176 assert_eq!(1, another_community_num_deleted);
178 // Should be none found, since the creator was deleted
179 let after_delete = CommunityAggregates::read(&conn, inserted_community.id);
180 assert!(after_delete.is_err());