1 use diesel::{result::Error, *};
2 use lemmy_db_schema::{schema::post_aggregates, PostId};
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "post_aggregates"]
7 pub struct PostAggregates {
15 pub published: chrono::NaiveDateTime,
16 pub newest_comment_time_necro: chrono::NaiveDateTime, // A newest comment time, limited to 2 days, to prevent necrobumping
17 pub newest_comment_time: chrono::NaiveDateTime,
21 pub fn read(conn: &PgConnection, post_id: PostId) -> Result<Self, Error> {
22 post_aggregates::table
23 .filter(post_aggregates::post_id.eq(post_id))
31 aggregates::post_aggregates::PostAggregates,
32 establish_unpooled_connection,
36 use lemmy_db_schema::source::{
37 comment::{Comment, CommentForm},
38 community::{Community, CommunityForm},
39 person::{Person, PersonForm},
40 post::{Post, PostForm, PostLike, PostLikeForm},
42 use serial_test::serial;
47 let conn = establish_unpooled_connection();
49 let new_person = PersonForm {
50 name: "thommy_community_agg".into(),
51 preferred_username: None,
63 last_refreshed_at: None,
65 shared_inbox_url: None,
69 let inserted_person = Person::create(&conn, &new_person).unwrap();
71 let another_person = PersonForm {
72 name: "jerry_community_agg".into(),
73 preferred_username: None,
85 last_refreshed_at: None,
87 shared_inbox_url: None,
91 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
93 let new_community = CommunityForm {
94 name: "TIL_community_agg".into(),
95 creator_id: inserted_person.id,
96 title: "nada".to_owned(),
106 last_refreshed_at: None,
112 shared_inbox_url: None,
115 let inserted_community = Community::create(&conn, &new_community).unwrap();
117 let new_post = PostForm {
118 name: "A test post".into(),
121 creator_id: inserted_person.id,
122 community_id: inserted_community.id,
130 embed_description: None,
138 let inserted_post = Post::create(&conn, &new_post).unwrap();
140 let comment_form = CommentForm {
141 content: "A test comment".into(),
142 creator_id: inserted_person.id,
143 post_id: inserted_post.id,
154 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
156 let child_comment_form = CommentForm {
157 content: "A test comment".into(),
158 creator_id: inserted_person.id,
159 post_id: inserted_post.id,
163 parent_id: Some(inserted_comment.id),
170 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
172 let post_like = PostLikeForm {
173 post_id: inserted_post.id,
174 person_id: inserted_person.id,
178 PostLike::like(&conn, &post_like).unwrap();
180 let post_aggs_before_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
182 assert_eq!(2, post_aggs_before_delete.comments);
183 assert_eq!(1, post_aggs_before_delete.score);
184 assert_eq!(1, post_aggs_before_delete.upvotes);
185 assert_eq!(0, post_aggs_before_delete.downvotes);
187 // Add a post dislike from the other person
188 let post_dislike = PostLikeForm {
189 post_id: inserted_post.id,
190 person_id: another_inserted_person.id,
194 PostLike::like(&conn, &post_dislike).unwrap();
196 let post_aggs_after_dislike = PostAggregates::read(&conn, inserted_post.id).unwrap();
198 assert_eq!(2, post_aggs_after_dislike.comments);
199 assert_eq!(0, post_aggs_after_dislike.score);
200 assert_eq!(1, post_aggs_after_dislike.upvotes);
201 assert_eq!(1, post_aggs_after_dislike.downvotes);
203 // Remove the parent comment
204 Comment::delete(&conn, inserted_comment.id).unwrap();
205 let after_comment_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
206 assert_eq!(0, after_comment_delete.comments);
207 assert_eq!(0, after_comment_delete.score);
208 assert_eq!(1, after_comment_delete.upvotes);
209 assert_eq!(1, after_comment_delete.downvotes);
211 // Remove the first post like
212 PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
213 let after_like_remove = PostAggregates::read(&conn, inserted_post.id).unwrap();
214 assert_eq!(0, after_like_remove.comments);
215 assert_eq!(-1, after_like_remove.score);
216 assert_eq!(0, after_like_remove.upvotes);
217 assert_eq!(1, after_like_remove.downvotes);
219 // This should delete all the associated rows, and fire triggers
220 Person::delete(&conn, another_inserted_person.id).unwrap();
221 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
222 assert_eq!(1, person_num_deleted);
224 // Should be none found, since the creator was deleted
225 let after_delete = PostAggregates::read(&conn, inserted_post.id);
226 assert!(after_delete.is_err());