]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/community_aggregates.rs
61abd193c6b1ce427762cb85a1410a1d1772cb04
[lemmy.git] / crates / db_schema / src / aggregates / community_aggregates.rs
1 use crate::{
2   aggregates::structs::CommunityAggregates,
3   newtypes::CommunityId,
4   schema::community_aggregates,
5   utils::{get_conn, DbPool},
6 };
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
9
10 impl CommunityAggregates {
11   pub async fn read(pool: &mut DbPool<'_>, community_id: CommunityId) -> Result<Self, Error> {
12     let conn = &mut get_conn(pool).await?;
13     community_aggregates::table
14       .filter(community_aggregates::community_id.eq(community_id))
15       .first::<Self>(conn)
16       .await
17   }
18 }
19
20 #[cfg(test)]
21 mod tests {
22   use crate::{
23     aggregates::community_aggregates::CommunityAggregates,
24     source::{
25       comment::{Comment, CommentInsertForm},
26       community::{Community, CommunityFollower, CommunityFollowerForm, CommunityInsertForm},
27       instance::Instance,
28       person::{Person, PersonInsertForm},
29       post::{Post, PostInsertForm},
30     },
31     traits::{Crud, Followable},
32     utils::build_db_pool_for_tests,
33   };
34   use serial_test::serial;
35
36   #[tokio::test]
37   #[serial]
38   async fn test_crud() {
39     let pool = &build_db_pool_for_tests().await;
40     let pool = &mut pool.into();
41
42     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
43       .await
44       .unwrap();
45
46     let new_person = PersonInsertForm::builder()
47       .name("thommy_community_agg".into())
48       .public_key("pubkey".to_string())
49       .instance_id(inserted_instance.id)
50       .build();
51
52     let inserted_person = Person::create(pool, &new_person).await.unwrap();
53
54     let another_person = PersonInsertForm::builder()
55       .name("jerry_community_agg".into())
56       .public_key("pubkey".to_string())
57       .instance_id(inserted_instance.id)
58       .build();
59
60     let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
61
62     let new_community = CommunityInsertForm::builder()
63       .name("TIL_community_agg".into())
64       .title("nada".to_owned())
65       .public_key("pubkey".to_string())
66       .instance_id(inserted_instance.id)
67       .build();
68
69     let inserted_community = Community::create(pool, &new_community).await.unwrap();
70
71     let another_community = CommunityInsertForm::builder()
72       .name("TIL_community_agg_2".into())
73       .title("nada".to_owned())
74       .public_key("pubkey".to_string())
75       .instance_id(inserted_instance.id)
76       .build();
77
78     let another_inserted_community = Community::create(pool, &another_community).await.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(pool, &first_person_follow)
87       .await
88       .unwrap();
89
90     let second_person_follow = CommunityFollowerForm {
91       community_id: inserted_community.id,
92       person_id: another_inserted_person.id,
93       pending: false,
94     };
95
96     CommunityFollower::follow(pool, &second_person_follow)
97       .await
98       .unwrap();
99
100     let another_community_follow = CommunityFollowerForm {
101       community_id: another_inserted_community.id,
102       person_id: inserted_person.id,
103       pending: false,
104     };
105
106     CommunityFollower::follow(pool, &another_community_follow)
107       .await
108       .unwrap();
109
110     let new_post = PostInsertForm::builder()
111       .name("A test post".into())
112       .creator_id(inserted_person.id)
113       .community_id(inserted_community.id)
114       .build();
115
116     let inserted_post = Post::create(pool, &new_post).await.unwrap();
117
118     let comment_form = CommentInsertForm::builder()
119       .content("A test comment".into())
120       .creator_id(inserted_person.id)
121       .post_id(inserted_post.id)
122       .build();
123
124     let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
125
126     let child_comment_form = CommentInsertForm::builder()
127       .content("A test comment".into())
128       .creator_id(inserted_person.id)
129       .post_id(inserted_post.id)
130       .build();
131
132     let _inserted_child_comment =
133       Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
134         .await
135         .unwrap();
136
137     let community_aggregates_before_delete = CommunityAggregates::read(pool, inserted_community.id)
138       .await
139       .unwrap();
140
141     assert_eq!(2, community_aggregates_before_delete.subscribers);
142     assert_eq!(1, community_aggregates_before_delete.posts);
143     assert_eq!(2, community_aggregates_before_delete.comments);
144
145     // Test the other community
146     let another_community_aggs = CommunityAggregates::read(pool, another_inserted_community.id)
147       .await
148       .unwrap();
149     assert_eq!(1, another_community_aggs.subscribers);
150     assert_eq!(0, another_community_aggs.posts);
151     assert_eq!(0, another_community_aggs.comments);
152
153     // Unfollow test
154     CommunityFollower::unfollow(pool, &second_person_follow)
155       .await
156       .unwrap();
157     let after_unfollow = CommunityAggregates::read(pool, inserted_community.id)
158       .await
159       .unwrap();
160     assert_eq!(1, after_unfollow.subscribers);
161
162     // Follow again just for the later tests
163     CommunityFollower::follow(pool, &second_person_follow)
164       .await
165       .unwrap();
166     let after_follow_again = CommunityAggregates::read(pool, inserted_community.id)
167       .await
168       .unwrap();
169     assert_eq!(2, after_follow_again.subscribers);
170
171     // Remove a parent post (the comment count should also be 0)
172     Post::delete(pool, inserted_post.id).await.unwrap();
173     let after_parent_post_delete = CommunityAggregates::read(pool, inserted_community.id)
174       .await
175       .unwrap();
176     assert_eq!(0, after_parent_post_delete.comments);
177     assert_eq!(0, after_parent_post_delete.posts);
178
179     // Remove the 2nd person
180     Person::delete(pool, another_inserted_person.id)
181       .await
182       .unwrap();
183     let after_person_delete = CommunityAggregates::read(pool, inserted_community.id)
184       .await
185       .unwrap();
186     assert_eq!(1, after_person_delete.subscribers);
187
188     // This should delete all the associated rows, and fire triggers
189     let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
190     assert_eq!(1, person_num_deleted);
191
192     // Delete the community
193     let community_num_deleted = Community::delete(pool, inserted_community.id)
194       .await
195       .unwrap();
196     assert_eq!(1, community_num_deleted);
197
198     let another_community_num_deleted = Community::delete(pool, another_inserted_community.id)
199       .await
200       .unwrap();
201     assert_eq!(1, another_community_num_deleted);
202
203     // Should be none found, since the creator was deleted
204     let after_delete = CommunityAggregates::read(pool, inserted_community.id).await;
205     assert!(after_delete.is_err());
206   }
207 }