]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/community_aggregates.rs
Still continuing on....
[lemmy.git] / crates / db_queries / src / aggregates / community_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::community_aggregates;
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: i32,
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: i32) -> 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     ListingType,
36     SortType,
37   };
38   use lemmy_db_schema::source::{
39     comment::{Comment, CommentForm},
40     community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
41     post::{Post, PostForm},
42     person::{PersonForm, Person},
43   };
44
45   #[test]
46   fn test_crud() {
47     let conn = establish_unpooled_connection();
48
49     let new_person = PersonForm {
50       name: "thommy_community_agg".into(),
51       preferred_username: None,
52       avatar: None,
53       banner: None,
54       banned: Some(false),
55       deleted: false,
56       published: None,
57       updated: None,
58       actor_id: None,
59       bio: None,
60       local: true,
61       private_key: None,
62       public_key: None,
63       last_refreshed_at: None,
64       inbox_url: None,
65       shared_inbox_url: None,
66     };
67
68     let inserted_person = Person::create(&conn, &new_person).unwrap();
69
70     let another_person = PersonForm {
71       name: "jerry_community_agg".into(),
72       preferred_username: None,
73       avatar: None,
74       banner: None,
75       banned: Some(false),
76       deleted: false,
77       published: None,
78       updated: None,
79       actor_id: None,
80       bio: None,
81       local: true,
82       private_key: None,
83       public_key: None,
84       last_refreshed_at: None,
85       inbox_url: None,
86       shared_inbox_url: None,
87     };
88
89     let another_inserted_person = Person::create(&conn, &another_person).unwrap();
90
91     let new_community = CommunityForm {
92       name: "TIL_community_agg".into(),
93       creator_id: inserted_person.id,
94       title: "nada".to_owned(),
95       description: None,
96       nsfw: false,
97       removed: None,
98       deleted: None,
99       updated: None,
100       actor_id: None,
101       local: true,
102       private_key: None,
103       public_key: None,
104       last_refreshed_at: None,
105       published: None,
106       icon: None,
107       banner: None,
108       followers_url: None,
109       inbox_url: None,
110       shared_inbox_url: None,
111     };
112
113     let inserted_community = Community::create(&conn, &new_community).unwrap();
114
115     let another_community = CommunityForm {
116       name: "TIL_community_agg_2".into(),
117       creator_id: inserted_person.id,
118       title: "nada".to_owned(),
119       description: None,
120       nsfw: false,
121       removed: None,
122       deleted: None,
123       updated: None,
124       actor_id: None,
125       local: true,
126       private_key: None,
127       public_key: None,
128       last_refreshed_at: None,
129       published: None,
130       icon: None,
131       banner: None,
132       followers_url: None,
133       inbox_url: None,
134       shared_inbox_url: None,
135     };
136
137     let another_inserted_community = Community::create(&conn, &another_community).unwrap();
138
139     let first_person_follow = CommunityFollowerForm {
140       community_id: inserted_community.id,
141       person_id: inserted_person.id,
142       pending: false,
143     };
144
145     CommunityFollower::follow(&conn, &first_person_follow).unwrap();
146
147     let second_person_follow = CommunityFollowerForm {
148       community_id: inserted_community.id,
149       person_id: another_inserted_person.id,
150       pending: false,
151     };
152
153     CommunityFollower::follow(&conn, &second_person_follow).unwrap();
154
155     let another_community_follow = CommunityFollowerForm {
156       community_id: another_inserted_community.id,
157       person_id: inserted_person.id,
158       pending: false,
159     };
160
161     CommunityFollower::follow(&conn, &another_community_follow).unwrap();
162
163     let new_post = PostForm {
164       name: "A test post".into(),
165       url: None,
166       body: None,
167       creator_id: inserted_person.id,
168       community_id: inserted_community.id,
169       removed: None,
170       deleted: None,
171       locked: None,
172       stickied: None,
173       nsfw: false,
174       updated: None,
175       embed_title: None,
176       embed_description: None,
177       embed_html: None,
178       thumbnail_url: None,
179       ap_id: None,
180       local: true,
181       published: None,
182     };
183
184     let inserted_post = Post::create(&conn, &new_post).unwrap();
185
186     let comment_form = CommentForm {
187       content: "A test comment".into(),
188       creator_id: inserted_person.id,
189       post_id: inserted_post.id,
190       removed: None,
191       deleted: None,
192       read: None,
193       parent_id: None,
194       published: None,
195       updated: None,
196       ap_id: None,
197       local: true,
198     };
199
200     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
201
202     let child_comment_form = CommentForm {
203       content: "A test comment".into(),
204       creator_id: inserted_person.id,
205       post_id: inserted_post.id,
206       removed: None,
207       deleted: None,
208       read: None,
209       parent_id: Some(inserted_comment.id),
210       published: None,
211       updated: None,
212       ap_id: None,
213       local: true,
214     };
215
216     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
217
218     let community_aggregates_before_delete =
219       CommunityAggregates::read(&conn, inserted_community.id).unwrap();
220
221     assert_eq!(2, community_aggregates_before_delete.subscribers);
222     assert_eq!(1, community_aggregates_before_delete.posts);
223     assert_eq!(2, community_aggregates_before_delete.comments);
224
225     // Test the other community
226     let another_community_aggs =
227       CommunityAggregates::read(&conn, another_inserted_community.id).unwrap();
228     assert_eq!(1, another_community_aggs.subscribers);
229     assert_eq!(0, another_community_aggs.posts);
230     assert_eq!(0, another_community_aggs.comments);
231
232     // Unfollow test
233     CommunityFollower::unfollow(&conn, &second_person_follow).unwrap();
234     let after_unfollow = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
235     assert_eq!(1, after_unfollow.subscribers);
236
237     // Follow again just for the later tests
238     CommunityFollower::follow(&conn, &second_person_follow).unwrap();
239     let after_follow_again = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
240     assert_eq!(2, after_follow_again.subscribers);
241
242     // Remove a parent comment (the comment count should also be 0)
243     Post::delete(&conn, inserted_post.id).unwrap();
244     let after_parent_post_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
245     assert_eq!(0, after_parent_post_delete.comments);
246     assert_eq!(0, after_parent_post_delete.posts);
247
248     // Remove the 2nd person
249     Person::delete(&conn, another_inserted_person.id).unwrap();
250     let after_person_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
251     assert_eq!(1, after_person_delete.subscribers);
252
253     // This should delete all the associated rows, and fire triggers
254     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
255     assert_eq!(1, person_num_deleted);
256
257     // Should be none found, since the creator was deleted
258     let after_delete = CommunityAggregates::read(&conn, inserted_community.id);
259     assert!(after_delete.is_err());
260   }
261 }