]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/community_aggregates.rs
Moving matrix_user_id to person table. #1438
[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       preferred_username: None,
52       avatar: None,
53       banner: None,
54       banned: None,
55       deleted: None,
56       published: None,
57       updated: None,
58       actor_id: None,
59       bio: None,
60       local: None,
61       private_key: None,
62       public_key: None,
63       last_refreshed_at: None,
64       inbox_url: None,
65       shared_inbox_url: None,
66       matrix_user_id: None,
67     };
68
69     let inserted_person = Person::create(&conn, &new_person).unwrap();
70
71     let another_person = PersonForm {
72       name: "jerry_community_agg".into(),
73       preferred_username: None,
74       avatar: None,
75       banner: None,
76       banned: None,
77       deleted: None,
78       published: None,
79       updated: None,
80       actor_id: None,
81       bio: None,
82       local: None,
83       private_key: None,
84       public_key: None,
85       last_refreshed_at: None,
86       inbox_url: None,
87       shared_inbox_url: None,
88       matrix_user_id: None,
89     };
90
91     let another_inserted_person = Person::create(&conn, &another_person).unwrap();
92
93     let new_community = CommunityForm {
94       name: "TIL_community_agg".into(),
95       creator_id: inserted_person.id,
96       title: "nada".to_owned(),
97       description: None,
98       nsfw: false,
99       removed: None,
100       deleted: None,
101       updated: None,
102       actor_id: None,
103       local: true,
104       private_key: None,
105       public_key: None,
106       last_refreshed_at: None,
107       published: None,
108       icon: None,
109       banner: None,
110       followers_url: None,
111       inbox_url: None,
112       shared_inbox_url: None,
113     };
114
115     let inserted_community = Community::create(&conn, &new_community).unwrap();
116
117     let another_community = CommunityForm {
118       name: "TIL_community_agg_2".into(),
119       creator_id: inserted_person.id,
120       title: "nada".to_owned(),
121       description: None,
122       nsfw: false,
123       removed: None,
124       deleted: None,
125       updated: None,
126       actor_id: None,
127       local: true,
128       private_key: None,
129       public_key: None,
130       last_refreshed_at: None,
131       published: None,
132       icon: None,
133       banner: None,
134       followers_url: None,
135       inbox_url: None,
136       shared_inbox_url: None,
137     };
138
139     let another_inserted_community = Community::create(&conn, &another_community).unwrap();
140
141     let first_person_follow = CommunityFollowerForm {
142       community_id: inserted_community.id,
143       person_id: inserted_person.id,
144       pending: false,
145     };
146
147     CommunityFollower::follow(&conn, &first_person_follow).unwrap();
148
149     let second_person_follow = CommunityFollowerForm {
150       community_id: inserted_community.id,
151       person_id: another_inserted_person.id,
152       pending: false,
153     };
154
155     CommunityFollower::follow(&conn, &second_person_follow).unwrap();
156
157     let another_community_follow = CommunityFollowerForm {
158       community_id: another_inserted_community.id,
159       person_id: inserted_person.id,
160       pending: false,
161     };
162
163     CommunityFollower::follow(&conn, &another_community_follow).unwrap();
164
165     let new_post = PostForm {
166       name: "A test post".into(),
167       url: None,
168       body: None,
169       creator_id: inserted_person.id,
170       community_id: inserted_community.id,
171       removed: None,
172       deleted: None,
173       locked: None,
174       stickied: None,
175       nsfw: false,
176       updated: None,
177       embed_title: None,
178       embed_description: None,
179       embed_html: None,
180       thumbnail_url: None,
181       ap_id: None,
182       local: true,
183       published: None,
184     };
185
186     let inserted_post = Post::create(&conn, &new_post).unwrap();
187
188     let comment_form = CommentForm {
189       content: "A test comment".into(),
190       creator_id: inserted_person.id,
191       post_id: inserted_post.id,
192       removed: None,
193       deleted: None,
194       read: None,
195       parent_id: None,
196       published: None,
197       updated: None,
198       ap_id: None,
199       local: true,
200     };
201
202     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
203
204     let child_comment_form = CommentForm {
205       content: "A test comment".into(),
206       creator_id: inserted_person.id,
207       post_id: inserted_post.id,
208       removed: None,
209       deleted: None,
210       read: None,
211       parent_id: Some(inserted_comment.id),
212       published: None,
213       updated: None,
214       ap_id: None,
215       local: true,
216     };
217
218     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
219
220     let community_aggregates_before_delete =
221       CommunityAggregates::read(&conn, inserted_community.id).unwrap();
222
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);
226
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);
233
234     // Unfollow test
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);
238
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);
243
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);
249
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);
254
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);
258
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());
262   }
263 }