]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/community_aggregates.rs
c668c15ac8a4fa50d1fe989e4a5cc2debad9cd39
[lemmy.git] / crates / db_queries / src / aggregates / community_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::{schema::community_aggregates, CommunityId};
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "community_aggregates"]
7 pub struct CommunityAggregates {
8   pub id: i32,
9   pub community_id: CommunityId,
10   pub subscribers: i64,
11   pub posts: i64,
12   pub comments: i64,
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,
18 }
19
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))
24       .first::<Self>(conn)
25   }
26 }
27
28 #[cfg(test)]
29 mod tests {
30   use crate::{
31     aggregates::community_aggregates::CommunityAggregates,
32     establish_unpooled_connection,
33     Crud,
34     Followable,
35   };
36   use lemmy_db_schema::source::{
37     comment::{Comment, CommentForm},
38     community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
39     person::{Person, PersonForm},
40     post::{Post, PostForm},
41   };
42   use serial_test::serial;
43
44   #[test]
45   #[serial]
46   fn test_crud() {
47     let conn = establish_unpooled_connection();
48
49     let new_person = PersonForm {
50       name: "thommy_community_agg".into(),
51       ..PersonForm::default()
52     };
53
54     let inserted_person = Person::create(&conn, &new_person).unwrap();
55
56     let another_person = PersonForm {
57       name: "jerry_community_agg".into(),
58       ..PersonForm::default()
59     };
60
61     let another_inserted_person = Person::create(&conn, &another_person).unwrap();
62
63     let new_community = CommunityForm {
64       name: "TIL_community_agg".into(),
65       title: "nada".to_owned(),
66       ..CommunityForm::default()
67     };
68
69     let inserted_community = Community::create(&conn, &new_community).unwrap();
70
71     let another_community = CommunityForm {
72       name: "TIL_community_agg_2".into(),
73       title: "nada".to_owned(),
74       ..CommunityForm::default()
75     };
76
77     let another_inserted_community = Community::create(&conn, &another_community).unwrap();
78
79     let first_person_follow = CommunityFollowerForm {
80       community_id: inserted_community.id,
81       person_id: inserted_person.id,
82       pending: false,
83     };
84
85     CommunityFollower::follow(&conn, &first_person_follow).unwrap();
86
87     let second_person_follow = CommunityFollowerForm {
88       community_id: inserted_community.id,
89       person_id: another_inserted_person.id,
90       pending: false,
91     };
92
93     CommunityFollower::follow(&conn, &second_person_follow).unwrap();
94
95     let another_community_follow = CommunityFollowerForm {
96       community_id: another_inserted_community.id,
97       person_id: inserted_person.id,
98       pending: false,
99     };
100
101     CommunityFollower::follow(&conn, &another_community_follow).unwrap();
102
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()
108     };
109
110     let inserted_post = Post::create(&conn, &new_post).unwrap();
111
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()
117     };
118
119     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
120
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()
127     };
128
129     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
130
131     let community_aggregates_before_delete =
132       CommunityAggregates::read(&conn, inserted_community.id).unwrap();
133
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);
137
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);
144
145     // Unfollow test
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);
149
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);
154
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);
160
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);
165
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);
169
170     // Delete the community
171     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
172     assert_eq!(1, community_num_deleted);
173
174     let another_community_num_deleted =
175       Community::delete(&conn, another_inserted_community.id).unwrap();
176     assert_eq!(1, another_community_num_deleted);
177
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());
181   }
182 }