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