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