]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/post_aggregates.rs
176f694a7038fd61c805ce21a6c34d985371cd65
[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},
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 }