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)
39 aggregates::post_aggregates::PostAggregates,
41 comment::{Comment, CommentInsertForm, CommentUpdateForm},
42 community::{Community, CommunityInsertForm},
44 person::{Person, PersonInsertForm},
45 post::{Post, PostInsertForm, PostLike, PostLikeForm},
47 traits::{Crud, Likeable},
48 utils::build_db_pool_for_tests,
50 use serial_test::serial;
54 async fn test_crud() {
55 let pool = &build_db_pool_for_tests().await;
56 let pool = &mut pool.into();
58 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
62 let new_person = PersonInsertForm::builder()
63 .name("thommy_community_agg".into())
64 .public_key("pubkey".to_string())
65 .instance_id(inserted_instance.id)
68 let inserted_person = Person::create(pool, &new_person).await.unwrap();
70 let another_person = PersonInsertForm::builder()
71 .name("jerry_community_agg".into())
72 .public_key("pubkey".to_string())
73 .instance_id(inserted_instance.id)
76 let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
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)
85 let inserted_community = Community::create(pool, &new_community).await.unwrap();
87 let new_post = PostInsertForm::builder()
88 .name("A test post".into())
89 .creator_id(inserted_person.id)
90 .community_id(inserted_community.id)
93 let inserted_post = Post::create(pool, &new_post).await.unwrap();
95 let comment_form = CommentInsertForm::builder()
96 .content("A test comment".into())
97 .creator_id(inserted_person.id)
98 .post_id(inserted_post.id)
101 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
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)
109 let inserted_child_comment =
110 Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
114 let post_like = PostLikeForm {
115 post_id: inserted_post.id,
116 person_id: inserted_person.id,
120 PostLike::like(pool, &post_like).await.unwrap();
122 let post_aggs_before_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
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);
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,
136 PostLike::like(pool, &post_dislike).await.unwrap();
138 let post_aggs_after_dislike = PostAggregates::read(pool, inserted_post.id).await.unwrap();
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);
145 // Remove the comments
146 Comment::delete(pool, inserted_comment.id).await.unwrap();
147 Comment::delete(pool, inserted_child_comment.id)
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);
156 // Remove the first post like
157 PostLike::remove(pool, inserted_person.id, inserted_post.id)
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);
166 // This should delete all the associated rows, and fire triggers
167 Person::delete(pool, another_inserted_person.id)
170 let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
171 assert_eq!(1, person_num_deleted);
173 // Delete the community
174 let community_num_deleted = Community::delete(pool, inserted_community.id)
177 assert_eq!(1, community_num_deleted);
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());
183 Instance::delete(pool, inserted_instance.id).await.unwrap();
188 async fn test_soft_delete() {
189 let pool = &build_db_pool_for_tests().await;
190 let pool = &mut pool.into();
192 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
196 let new_person = PersonInsertForm::builder()
197 .name("thommy_community_agg".into())
198 .public_key("pubkey".to_string())
199 .instance_id(inserted_instance.id)
202 let inserted_person = Person::create(pool, &new_person).await.unwrap();
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)
211 let inserted_community = Community::create(pool, &new_community).await.unwrap();
213 let new_post = PostInsertForm::builder()
214 .name("A test post".into())
215 .creator_id(inserted_person.id)
216 .community_id(inserted_community.id)
219 let inserted_post = Post::create(pool, &new_post).await.unwrap();
221 let comment_form = CommentInsertForm::builder()
222 .content("A test comment".into())
223 .creator_id(inserted_person.id)
224 .post_id(inserted_post.id)
227 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
229 let post_aggregates_before = PostAggregates::read(pool, inserted_post.id).await.unwrap();
230 assert_eq!(1, post_aggregates_before.comments);
235 &CommentUpdateForm::builder().removed(Some(true)).build(),
240 let post_aggregates_after_remove = PostAggregates::read(pool, inserted_post.id).await.unwrap();
241 assert_eq!(0, post_aggregates_after_remove.comments);
246 &CommentUpdateForm::builder().removed(Some(false)).build(),
254 &CommentUpdateForm::builder().deleted(Some(true)).build(),
259 let post_aggregates_after_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
260 assert_eq!(0, post_aggregates_after_delete.comments);
265 &CommentUpdateForm::builder().removed(Some(true)).build(),
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);
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)
280 Instance::delete(pool, inserted_instance.id).await.unwrap();