]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/comment_aggregates.rs
Cache & Optimize Woodpecker CI (#3450)
[lemmy.git] / crates / db_schema / src / aggregates / comment_aggregates.rs
1 use crate::{
2   aggregates::structs::CommentAggregates,
3   newtypes::CommentId,
4   schema::comment_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 CommentAggregates {
11   pub async fn read(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result<Self, Error> {
12     let conn = &mut get_conn(pool).await?;
13     comment_aggregates::table
14       .filter(comment_aggregates::comment_id.eq(comment_id))
15       .first::<Self>(conn)
16       .await
17   }
18
19   pub async fn update_hot_rank(
20     pool: &mut DbPool<'_>,
21     comment_id: CommentId,
22   ) -> Result<Self, Error> {
23     let conn = &mut get_conn(pool).await?;
24
25     diesel::update(comment_aggregates::table)
26       .filter(comment_aggregates::comment_id.eq(comment_id))
27       .set(comment_aggregates::hot_rank.eq(hot_rank(
28         comment_aggregates::score,
29         comment_aggregates::published,
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::comment_aggregates::CommentAggregates,
43     source::{
44       comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm},
45       community::{Community, CommunityInsertForm},
46       instance::Instance,
47       person::{Person, PersonInsertForm},
48       post::{Post, PostInsertForm},
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_comment_agg".into())
67       .public_key("pubkey".into())
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_comment_agg".into())
75       .public_key("pubkey".into())
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_comment_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 comment_like = CommentLikeForm {
118       comment_id: inserted_comment.id,
119       post_id: inserted_post.id,
120       person_id: inserted_person.id,
121       score: 1,
122     };
123
124     CommentLike::like(pool, &comment_like).await.unwrap();
125
126     let comment_aggs_before_delete = CommentAggregates::read(pool, inserted_comment.id)
127       .await
128       .unwrap();
129
130     assert_eq!(1, comment_aggs_before_delete.score);
131     assert_eq!(1, comment_aggs_before_delete.upvotes);
132     assert_eq!(0, comment_aggs_before_delete.downvotes);
133
134     // Add a post dislike from the other person
135     let comment_dislike = CommentLikeForm {
136       comment_id: inserted_comment.id,
137       post_id: inserted_post.id,
138       person_id: another_inserted_person.id,
139       score: -1,
140     };
141
142     CommentLike::like(pool, &comment_dislike).await.unwrap();
143
144     let comment_aggs_after_dislike = CommentAggregates::read(pool, inserted_comment.id)
145       .await
146       .unwrap();
147
148     assert_eq!(0, comment_aggs_after_dislike.score);
149     assert_eq!(1, comment_aggs_after_dislike.upvotes);
150     assert_eq!(1, comment_aggs_after_dislike.downvotes);
151
152     // Remove the first comment like
153     CommentLike::remove(pool, inserted_person.id, inserted_comment.id)
154       .await
155       .unwrap();
156     let after_like_remove = CommentAggregates::read(pool, inserted_comment.id)
157       .await
158       .unwrap();
159     assert_eq!(-1, after_like_remove.score);
160     assert_eq!(0, after_like_remove.upvotes);
161     assert_eq!(1, after_like_remove.downvotes);
162
163     // Remove the parent post
164     Post::delete(pool, inserted_post.id).await.unwrap();
165
166     // Should be none found, since the post was deleted
167     let after_delete = CommentAggregates::read(pool, inserted_comment.id).await;
168     assert!(after_delete.is_err());
169
170     // This should delete all the associated rows, and fire triggers
171     Person::delete(pool, another_inserted_person.id)
172       .await
173       .unwrap();
174     let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
175     assert_eq!(1, person_num_deleted);
176
177     // Delete the community
178     let community_num_deleted = Community::delete(pool, inserted_community.id)
179       .await
180       .unwrap();
181     assert_eq!(1, community_num_deleted);
182
183     Instance::delete(pool, inserted_instance.id).await.unwrap();
184   }
185 }