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