]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/post_aggregates.rs
Cache & Optimize Woodpecker CI (#3450)
[lemmy.git] / crates / db_schema / src / aggregates / post_aggregates.rs
1 use crate::{
2   aggregates::structs::PostAggregates,
3   newtypes::PostId,
4   schema::post_aggregates,
5   utils::{functions::hot_rank, get_conn, DbPool},
6 };
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
9
10 impl PostAggregates {
11   pub async fn read(pool: &mut DbPool<'_>, post_id: PostId) -> Result<Self, Error> {
12     let conn = &mut get_conn(pool).await?;
13     post_aggregates::table
14       .filter(post_aggregates::post_id.eq(post_id))
15       .first::<Self>(conn)
16       .await
17   }
18
19   pub async fn update_hot_rank(pool: &mut DbPool<'_>, post_id: PostId) -> Result<Self, Error> {
20     let conn = &mut get_conn(pool).await?;
21
22     diesel::update(post_aggregates::table)
23       .filter(post_aggregates::post_id.eq(post_id))
24       .set((
25         post_aggregates::hot_rank.eq(hot_rank(post_aggregates::score, post_aggregates::published)),
26         post_aggregates::hot_rank_active.eq(hot_rank(
27           post_aggregates::score,
28           post_aggregates::newest_comment_time_necro,
29         )),
30       ))
31       .get_result::<Self>(conn)
32       .await
33   }
34 }
35
36 #[cfg(test)]
37 mod tests {
38   #![allow(clippy::unwrap_used)]
39   #![allow(clippy::indexing_slicing)]
40
41   use crate::{
42     aggregates::post_aggregates::PostAggregates,
43     source::{
44       comment::{Comment, CommentInsertForm, CommentUpdateForm},
45       community::{Community, CommunityInsertForm},
46       instance::Instance,
47       person::{Person, PersonInsertForm},
48       post::{Post, PostInsertForm, PostLike, PostLikeForm},
49     },
50     traits::{Crud, Likeable},
51     utils::build_db_pool_for_tests,
52   };
53   use serial_test::serial;
54
55   #[tokio::test]
56   #[serial]
57   async fn test_crud() {
58     let pool = &build_db_pool_for_tests().await;
59     let pool = &mut pool.into();
60
61     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
62       .await
63       .unwrap();
64
65     let new_person = PersonInsertForm::builder()
66       .name("thommy_community_agg".into())
67       .public_key("pubkey".to_string())
68       .instance_id(inserted_instance.id)
69       .build();
70
71     let inserted_person = Person::create(pool, &new_person).await.unwrap();
72
73     let another_person = PersonInsertForm::builder()
74       .name("jerry_community_agg".into())
75       .public_key("pubkey".to_string())
76       .instance_id(inserted_instance.id)
77       .build();
78
79     let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
80
81     let new_community = CommunityInsertForm::builder()
82       .name("TIL_community_agg".into())
83       .title("nada".to_owned())
84       .public_key("pubkey".to_string())
85       .instance_id(inserted_instance.id)
86       .build();
87
88     let inserted_community = Community::create(pool, &new_community).await.unwrap();
89
90     let new_post = PostInsertForm::builder()
91       .name("A test post".into())
92       .creator_id(inserted_person.id)
93       .community_id(inserted_community.id)
94       .build();
95
96     let inserted_post = Post::create(pool, &new_post).await.unwrap();
97
98     let comment_form = CommentInsertForm::builder()
99       .content("A test comment".into())
100       .creator_id(inserted_person.id)
101       .post_id(inserted_post.id)
102       .build();
103
104     let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
105
106     let child_comment_form = CommentInsertForm::builder()
107       .content("A test comment".into())
108       .creator_id(inserted_person.id)
109       .post_id(inserted_post.id)
110       .build();
111
112     let inserted_child_comment =
113       Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
114         .await
115         .unwrap();
116
117     let post_like = PostLikeForm {
118       post_id: inserted_post.id,
119       person_id: inserted_person.id,
120       score: 1,
121     };
122
123     PostLike::like(pool, &post_like).await.unwrap();
124
125     let post_aggs_before_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
126
127     assert_eq!(2, post_aggs_before_delete.comments);
128     assert_eq!(1, post_aggs_before_delete.score);
129     assert_eq!(1, post_aggs_before_delete.upvotes);
130     assert_eq!(0, post_aggs_before_delete.downvotes);
131
132     // Add a post dislike from the other person
133     let post_dislike = PostLikeForm {
134       post_id: inserted_post.id,
135       person_id: another_inserted_person.id,
136       score: -1,
137     };
138
139     PostLike::like(pool, &post_dislike).await.unwrap();
140
141     let post_aggs_after_dislike = PostAggregates::read(pool, inserted_post.id).await.unwrap();
142
143     assert_eq!(2, post_aggs_after_dislike.comments);
144     assert_eq!(0, post_aggs_after_dislike.score);
145     assert_eq!(1, post_aggs_after_dislike.upvotes);
146     assert_eq!(1, post_aggs_after_dislike.downvotes);
147
148     // Remove the comments
149     Comment::delete(pool, inserted_comment.id).await.unwrap();
150     Comment::delete(pool, inserted_child_comment.id)
151       .await
152       .unwrap();
153     let after_comment_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
154     assert_eq!(0, after_comment_delete.comments);
155     assert_eq!(0, after_comment_delete.score);
156     assert_eq!(1, after_comment_delete.upvotes);
157     assert_eq!(1, after_comment_delete.downvotes);
158
159     // Remove the first post like
160     PostLike::remove(pool, inserted_person.id, inserted_post.id)
161       .await
162       .unwrap();
163     let after_like_remove = PostAggregates::read(pool, inserted_post.id).await.unwrap();
164     assert_eq!(0, after_like_remove.comments);
165     assert_eq!(-1, after_like_remove.score);
166     assert_eq!(0, after_like_remove.upvotes);
167     assert_eq!(1, after_like_remove.downvotes);
168
169     // This should delete all the associated rows, and fire triggers
170     Person::delete(pool, another_inserted_person.id)
171       .await
172       .unwrap();
173     let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
174     assert_eq!(1, person_num_deleted);
175
176     // Delete the community
177     let community_num_deleted = Community::delete(pool, inserted_community.id)
178       .await
179       .unwrap();
180     assert_eq!(1, community_num_deleted);
181
182     // Should be none found, since the creator was deleted
183     let after_delete = PostAggregates::read(pool, inserted_post.id).await;
184     assert!(after_delete.is_err());
185
186     Instance::delete(pool, inserted_instance.id).await.unwrap();
187   }
188
189   #[tokio::test]
190   #[serial]
191   async fn test_soft_delete() {
192     let pool = &build_db_pool_for_tests().await;
193     let pool = &mut pool.into();
194
195     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
196       .await
197       .unwrap();
198
199     let new_person = PersonInsertForm::builder()
200       .name("thommy_community_agg".into())
201       .public_key("pubkey".to_string())
202       .instance_id(inserted_instance.id)
203       .build();
204
205     let inserted_person = Person::create(pool, &new_person).await.unwrap();
206
207     let new_community = CommunityInsertForm::builder()
208       .name("TIL_community_agg".into())
209       .title("nada".to_owned())
210       .public_key("pubkey".to_string())
211       .instance_id(inserted_instance.id)
212       .build();
213
214     let inserted_community = Community::create(pool, &new_community).await.unwrap();
215
216     let new_post = PostInsertForm::builder()
217       .name("A test post".into())
218       .creator_id(inserted_person.id)
219       .community_id(inserted_community.id)
220       .build();
221
222     let inserted_post = Post::create(pool, &new_post).await.unwrap();
223
224     let comment_form = CommentInsertForm::builder()
225       .content("A test comment".into())
226       .creator_id(inserted_person.id)
227       .post_id(inserted_post.id)
228       .build();
229
230     let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
231
232     let post_aggregates_before = PostAggregates::read(pool, inserted_post.id).await.unwrap();
233     assert_eq!(1, post_aggregates_before.comments);
234
235     Comment::update(
236       pool,
237       inserted_comment.id,
238       &CommentUpdateForm::builder().removed(Some(true)).build(),
239     )
240     .await
241     .unwrap();
242
243     let post_aggregates_after_remove = PostAggregates::read(pool, inserted_post.id).await.unwrap();
244     assert_eq!(0, post_aggregates_after_remove.comments);
245
246     Comment::update(
247       pool,
248       inserted_comment.id,
249       &CommentUpdateForm::builder().removed(Some(false)).build(),
250     )
251     .await
252     .unwrap();
253
254     Comment::update(
255       pool,
256       inserted_comment.id,
257       &CommentUpdateForm::builder().deleted(Some(true)).build(),
258     )
259     .await
260     .unwrap();
261
262     let post_aggregates_after_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
263     assert_eq!(0, post_aggregates_after_delete.comments);
264
265     Comment::update(
266       pool,
267       inserted_comment.id,
268       &CommentUpdateForm::builder().removed(Some(true)).build(),
269     )
270     .await
271     .unwrap();
272
273     let post_aggregates_after_delete_remove =
274       PostAggregates::read(pool, inserted_post.id).await.unwrap();
275     assert_eq!(0, post_aggregates_after_delete_remove.comments);
276
277     Comment::delete(pool, inserted_comment.id).await.unwrap();
278     Post::delete(pool, inserted_post.id).await.unwrap();
279     Person::delete(pool, inserted_person.id).await.unwrap();
280     Community::delete(pool, inserted_community.id)
281       .await
282       .unwrap();
283     Instance::delete(pool, inserted_instance.id).await.unwrap();
284   }
285 }