2 aggregates::structs::PostAggregates,
4 schema::post_aggregates,
5 utils::{functions::hot_rank, get_conn, DbPool},
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
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))
19 pub async fn update_hot_rank(pool: &mut DbPool<'_>, post_id: PostId) -> Result<Self, Error> {
20 let conn = &mut get_conn(pool).await?;
22 diesel::update(post_aggregates::table)
23 .filter(post_aggregates::post_id.eq(post_id))
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,
31 .get_result::<Self>(conn)
38 #![allow(clippy::unwrap_used)]
39 #![allow(clippy::indexing_slicing)]
42 aggregates::post_aggregates::PostAggregates,
44 comment::{Comment, CommentInsertForm, CommentUpdateForm},
45 community::{Community, CommunityInsertForm},
47 person::{Person, PersonInsertForm},
48 post::{Post, PostInsertForm, PostLike, PostLikeForm},
50 traits::{Crud, Likeable},
51 utils::build_db_pool_for_tests,
53 use serial_test::serial;
57 async fn test_crud() {
58 let pool = &build_db_pool_for_tests().await;
59 let pool = &mut pool.into();
61 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
65 let new_person = PersonInsertForm::builder()
66 .name("thommy_community_agg".into())
67 .public_key("pubkey".to_string())
68 .instance_id(inserted_instance.id)
71 let inserted_person = Person::create(pool, &new_person).await.unwrap();
73 let another_person = PersonInsertForm::builder()
74 .name("jerry_community_agg".into())
75 .public_key("pubkey".to_string())
76 .instance_id(inserted_instance.id)
79 let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
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)
88 let inserted_community = Community::create(pool, &new_community).await.unwrap();
90 let new_post = PostInsertForm::builder()
91 .name("A test post".into())
92 .creator_id(inserted_person.id)
93 .community_id(inserted_community.id)
96 let inserted_post = Post::create(pool, &new_post).await.unwrap();
98 let comment_form = CommentInsertForm::builder()
99 .content("A test comment".into())
100 .creator_id(inserted_person.id)
101 .post_id(inserted_post.id)
104 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
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)
112 let inserted_child_comment =
113 Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
117 let post_like = PostLikeForm {
118 post_id: inserted_post.id,
119 person_id: inserted_person.id,
123 PostLike::like(pool, &post_like).await.unwrap();
125 let post_aggs_before_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
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);
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,
139 PostLike::like(pool, &post_dislike).await.unwrap();
141 let post_aggs_after_dislike = PostAggregates::read(pool, inserted_post.id).await.unwrap();
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);
148 // Remove the comments
149 Comment::delete(pool, inserted_comment.id).await.unwrap();
150 Comment::delete(pool, inserted_child_comment.id)
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);
159 // Remove the first post like
160 PostLike::remove(pool, inserted_person.id, inserted_post.id)
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);
169 // This should delete all the associated rows, and fire triggers
170 Person::delete(pool, another_inserted_person.id)
173 let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
174 assert_eq!(1, person_num_deleted);
176 // Delete the community
177 let community_num_deleted = Community::delete(pool, inserted_community.id)
180 assert_eq!(1, community_num_deleted);
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());
186 Instance::delete(pool, inserted_instance.id).await.unwrap();
191 async fn test_soft_delete() {
192 let pool = &build_db_pool_for_tests().await;
193 let pool = &mut pool.into();
195 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
199 let new_person = PersonInsertForm::builder()
200 .name("thommy_community_agg".into())
201 .public_key("pubkey".to_string())
202 .instance_id(inserted_instance.id)
205 let inserted_person = Person::create(pool, &new_person).await.unwrap();
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)
214 let inserted_community = Community::create(pool, &new_community).await.unwrap();
216 let new_post = PostInsertForm::builder()
217 .name("A test post".into())
218 .creator_id(inserted_person.id)
219 .community_id(inserted_community.id)
222 let inserted_post = Post::create(pool, &new_post).await.unwrap();
224 let comment_form = CommentInsertForm::builder()
225 .content("A test comment".into())
226 .creator_id(inserted_person.id)
227 .post_id(inserted_post.id)
230 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
232 let post_aggregates_before = PostAggregates::read(pool, inserted_post.id).await.unwrap();
233 assert_eq!(1, post_aggregates_before.comments);
238 &CommentUpdateForm::builder().removed(Some(true)).build(),
243 let post_aggregates_after_remove = PostAggregates::read(pool, inserted_post.id).await.unwrap();
244 assert_eq!(0, post_aggregates_after_remove.comments);
249 &CommentUpdateForm::builder().removed(Some(false)).build(),
257 &CommentUpdateForm::builder().deleted(Some(true)).build(),
262 let post_aggregates_after_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
263 assert_eq!(0, post_aggregates_after_delete.comments);
268 &CommentUpdateForm::builder().removed(Some(true)).build(),
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);
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)
283 Instance::delete(pool, inserted_instance.id).await.unwrap();