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 preferred_username: None,
63 last_refreshed_at: None,
65 shared_inbox_url: None,
69 let inserted_person = Person::create(&conn, &new_person).unwrap();
71 let another_person = PersonForm {
72 name: "jerry_community_agg".into(),
73 preferred_username: None,
85 last_refreshed_at: None,
87 shared_inbox_url: None,
91 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
93 let new_community = CommunityForm {
94 name: "TIL_community_agg".into(),
95 creator_id: inserted_person.id,
96 title: "nada".to_owned(),
106 last_refreshed_at: None,
112 shared_inbox_url: None,
115 let inserted_community = Community::create(&conn, &new_community).unwrap();
117 let another_community = CommunityForm {
118 name: "TIL_community_agg_2".into(),
119 creator_id: inserted_person.id,
120 title: "nada".to_owned(),
130 last_refreshed_at: None,
136 shared_inbox_url: None,
139 let another_inserted_community = Community::create(&conn, &another_community).unwrap();
141 let first_person_follow = CommunityFollowerForm {
142 community_id: inserted_community.id,
143 person_id: inserted_person.id,
147 CommunityFollower::follow(&conn, &first_person_follow).unwrap();
149 let second_person_follow = CommunityFollowerForm {
150 community_id: inserted_community.id,
151 person_id: another_inserted_person.id,
155 CommunityFollower::follow(&conn, &second_person_follow).unwrap();
157 let another_community_follow = CommunityFollowerForm {
158 community_id: another_inserted_community.id,
159 person_id: inserted_person.id,
163 CommunityFollower::follow(&conn, &another_community_follow).unwrap();
165 let new_post = PostForm {
166 name: "A test post".into(),
169 creator_id: inserted_person.id,
170 community_id: inserted_community.id,
178 embed_description: None,
186 let inserted_post = Post::create(&conn, &new_post).unwrap();
188 let comment_form = CommentForm {
189 content: "A test comment".into(),
190 creator_id: inserted_person.id,
191 post_id: inserted_post.id,
202 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
204 let child_comment_form = CommentForm {
205 content: "A test comment".into(),
206 creator_id: inserted_person.id,
207 post_id: inserted_post.id,
211 parent_id: Some(inserted_comment.id),
218 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
220 let community_aggregates_before_delete =
221 CommunityAggregates::read(&conn, inserted_community.id).unwrap();
223 assert_eq!(2, community_aggregates_before_delete.subscribers);
224 assert_eq!(1, community_aggregates_before_delete.posts);
225 assert_eq!(2, community_aggregates_before_delete.comments);
227 // Test the other community
228 let another_community_aggs =
229 CommunityAggregates::read(&conn, another_inserted_community.id).unwrap();
230 assert_eq!(1, another_community_aggs.subscribers);
231 assert_eq!(0, another_community_aggs.posts);
232 assert_eq!(0, another_community_aggs.comments);
235 CommunityFollower::unfollow(&conn, &second_person_follow).unwrap();
236 let after_unfollow = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
237 assert_eq!(1, after_unfollow.subscribers);
239 // Follow again just for the later tests
240 CommunityFollower::follow(&conn, &second_person_follow).unwrap();
241 let after_follow_again = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
242 assert_eq!(2, after_follow_again.subscribers);
244 // Remove a parent comment (the comment count should also be 0)
245 Post::delete(&conn, inserted_post.id).unwrap();
246 let after_parent_post_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
247 assert_eq!(0, after_parent_post_delete.comments);
248 assert_eq!(0, after_parent_post_delete.posts);
250 // Remove the 2nd person
251 Person::delete(&conn, another_inserted_person.id).unwrap();
252 let after_person_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
253 assert_eq!(1, after_person_delete.subscribers);
255 // This should delete all the associated rows, and fire triggers
256 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
257 assert_eq!(1, person_num_deleted);
259 // Should be none found, since the creator was deleted
260 let after_delete = CommunityAggregates::read(&conn, inserted_community.id);
261 assert!(after_delete.is_err());