]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/community_aggregates.rs
Merge crates db_schema and db_queries
[lemmy.git] / crates / db_schema / src / aggregates / community_aggregates.rs
1 use crate::{newtypes::CommunityId, schema::community_aggregates};
2 use diesel::{result::Error, *};
3 use serde::{Deserialize, Serialize};
4
5 #[derive(
6   Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize, Clone,
7 )]
8 #[table_name = "community_aggregates"]
9 pub struct CommunityAggregates {
10   pub id: i32,
11   pub community_id: CommunityId,
12   pub subscribers: i64,
13   pub posts: i64,
14   pub comments: i64,
15   pub published: chrono::NaiveDateTime,
16   pub users_active_day: i64,
17   pub users_active_week: i64,
18   pub users_active_month: i64,
19   pub users_active_half_year: i64,
20 }
21
22 impl CommunityAggregates {
23   pub fn read(conn: &PgConnection, community_id: CommunityId) -> Result<Self, Error> {
24     community_aggregates::table
25       .filter(community_aggregates::community_id.eq(community_id))
26       .first::<Self>(conn)
27   }
28 }
29
30 #[cfg(test)]
31 mod tests {
32   use crate::{
33     aggregates::community_aggregates::CommunityAggregates,
34     establish_unpooled_connection,
35     source::{
36       comment::{Comment, CommentForm},
37       community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
38       person::{Person, PersonForm},
39       post::{Post, PostForm},
40     },
41     traits::{Crud, Followable},
42   };
43   use serial_test::serial;
44
45   #[test]
46   #[serial]
47   fn test_crud() {
48     let conn = establish_unpooled_connection();
49
50     let new_person = PersonForm {
51       name: "thommy_community_agg".into(),
52       ..PersonForm::default()
53     };
54
55     let inserted_person = Person::create(&conn, &new_person).unwrap();
56
57     let another_person = PersonForm {
58       name: "jerry_community_agg".into(),
59       ..PersonForm::default()
60     };
61
62     let another_inserted_person = Person::create(&conn, &another_person).unwrap();
63
64     let new_community = CommunityForm {
65       name: "TIL_community_agg".into(),
66       title: "nada".to_owned(),
67       ..CommunityForm::default()
68     };
69
70     let inserted_community = Community::create(&conn, &new_community).unwrap();
71
72     let another_community = CommunityForm {
73       name: "TIL_community_agg_2".into(),
74       title: "nada".to_owned(),
75       ..CommunityForm::default()
76     };
77
78     let another_inserted_community = Community::create(&conn, &another_community).unwrap();
79
80     let first_person_follow = CommunityFollowerForm {
81       community_id: inserted_community.id,
82       person_id: inserted_person.id,
83       pending: false,
84     };
85
86     CommunityFollower::follow(&conn, &first_person_follow).unwrap();
87
88     let second_person_follow = CommunityFollowerForm {
89       community_id: inserted_community.id,
90       person_id: another_inserted_person.id,
91       pending: false,
92     };
93
94     CommunityFollower::follow(&conn, &second_person_follow).unwrap();
95
96     let another_community_follow = CommunityFollowerForm {
97       community_id: another_inserted_community.id,
98       person_id: inserted_person.id,
99       pending: false,
100     };
101
102     CommunityFollower::follow(&conn, &another_community_follow).unwrap();
103
104     let new_post = PostForm {
105       name: "A test post".into(),
106       creator_id: inserted_person.id,
107       community_id: inserted_community.id,
108       ..PostForm::default()
109     };
110
111     let inserted_post = Post::create(&conn, &new_post).unwrap();
112
113     let comment_form = CommentForm {
114       content: "A test comment".into(),
115       creator_id: inserted_person.id,
116       post_id: inserted_post.id,
117       ..CommentForm::default()
118     };
119
120     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
121
122     let child_comment_form = CommentForm {
123       content: "A test comment".into(),
124       creator_id: inserted_person.id,
125       post_id: inserted_post.id,
126       parent_id: Some(inserted_comment.id),
127       ..CommentForm::default()
128     };
129
130     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
131
132     let community_aggregates_before_delete =
133       CommunityAggregates::read(&conn, inserted_community.id).unwrap();
134
135     assert_eq!(2, community_aggregates_before_delete.subscribers);
136     assert_eq!(1, community_aggregates_before_delete.posts);
137     assert_eq!(2, community_aggregates_before_delete.comments);
138
139     // Test the other community
140     let another_community_aggs =
141       CommunityAggregates::read(&conn, another_inserted_community.id).unwrap();
142     assert_eq!(1, another_community_aggs.subscribers);
143     assert_eq!(0, another_community_aggs.posts);
144     assert_eq!(0, another_community_aggs.comments);
145
146     // Unfollow test
147     CommunityFollower::unfollow(&conn, &second_person_follow).unwrap();
148     let after_unfollow = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
149     assert_eq!(1, after_unfollow.subscribers);
150
151     // Follow again just for the later tests
152     CommunityFollower::follow(&conn, &second_person_follow).unwrap();
153     let after_follow_again = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
154     assert_eq!(2, after_follow_again.subscribers);
155
156     // Remove a parent comment (the comment count should also be 0)
157     Post::delete(&conn, inserted_post.id).unwrap();
158     let after_parent_post_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
159     assert_eq!(0, after_parent_post_delete.comments);
160     assert_eq!(0, after_parent_post_delete.posts);
161
162     // Remove the 2nd person
163     Person::delete(&conn, another_inserted_person.id).unwrap();
164     let after_person_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
165     assert_eq!(1, after_person_delete.subscribers);
166
167     // This should delete all the associated rows, and fire triggers
168     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
169     assert_eq!(1, person_num_deleted);
170
171     // Delete the community
172     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
173     assert_eq!(1, community_num_deleted);
174
175     let another_community_num_deleted =
176       Community::delete(&conn, another_inserted_community.id).unwrap();
177     assert_eq!(1, another_community_num_deleted);
178
179     // Should be none found, since the creator was deleted
180     let after_delete = CommunityAggregates::read(&conn, inserted_community.id);
181     assert!(after_delete.is_err());
182   }
183 }