1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::post_aggregates;
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: i32) -> 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,
35 use lemmy_db_schema::source::{
36 comment::{Comment, CommentForm},
37 community::{Community, CommunityForm},
38 post::{Post, PostForm, PostLike, PostLikeForm},
39 person::{PersonForm, Person},
44 let conn = establish_unpooled_connection();
46 let new_person = PersonForm {
47 name: "thommy_community_agg".into(),
48 preferred_username: None,
60 last_refreshed_at: None,
62 shared_inbox_url: None,
65 let inserted_person = Person::create(&conn, &new_person).unwrap();
67 let another_person = PersonForm {
68 name: "jerry_community_agg".into(),
69 preferred_username: None,
81 last_refreshed_at: None,
83 shared_inbox_url: None,
86 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
88 let new_community = CommunityForm {
89 name: "TIL_community_agg".into(),
90 creator_id: inserted_person.id,
91 title: "nada".to_owned(),
101 last_refreshed_at: None,
107 shared_inbox_url: None,
110 let inserted_community = Community::create(&conn, &new_community).unwrap();
112 let new_post = PostForm {
113 name: "A test post".into(),
116 creator_id: inserted_person.id,
117 community_id: inserted_community.id,
125 embed_description: None,
133 let inserted_post = Post::create(&conn, &new_post).unwrap();
135 let comment_form = CommentForm {
136 content: "A test comment".into(),
137 creator_id: inserted_person.id,
138 post_id: inserted_post.id,
149 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
151 let child_comment_form = CommentForm {
152 content: "A test comment".into(),
153 creator_id: inserted_person.id,
154 post_id: inserted_post.id,
158 parent_id: Some(inserted_comment.id),
165 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
167 let post_like = PostLikeForm {
168 post_id: inserted_post.id,
169 person_id: inserted_person.id,
173 PostLike::like(&conn, &post_like).unwrap();
175 let post_aggs_before_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
177 assert_eq!(2, post_aggs_before_delete.comments);
178 assert_eq!(1, post_aggs_before_delete.score);
179 assert_eq!(1, post_aggs_before_delete.upvotes);
180 assert_eq!(0, post_aggs_before_delete.downvotes);
182 // Add a post dislike from the other person
183 let post_dislike = PostLikeForm {
184 post_id: inserted_post.id,
185 person_id: another_inserted_person.id,
189 PostLike::like(&conn, &post_dislike).unwrap();
191 let post_aggs_after_dislike = PostAggregates::read(&conn, inserted_post.id).unwrap();
193 assert_eq!(2, post_aggs_after_dislike.comments);
194 assert_eq!(0, post_aggs_after_dislike.score);
195 assert_eq!(1, post_aggs_after_dislike.upvotes);
196 assert_eq!(1, post_aggs_after_dislike.downvotes);
198 // Remove the parent comment
199 Comment::delete(&conn, inserted_comment.id).unwrap();
200 let after_comment_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
201 assert_eq!(0, after_comment_delete.comments);
202 assert_eq!(0, after_comment_delete.score);
203 assert_eq!(1, after_comment_delete.upvotes);
204 assert_eq!(1, after_comment_delete.downvotes);
206 // Remove the first post like
207 PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
208 let after_like_remove = PostAggregates::read(&conn, inserted_post.id).unwrap();
209 assert_eq!(0, after_like_remove.comments);
210 assert_eq!(-1, after_like_remove.score);
211 assert_eq!(0, after_like_remove.upvotes);
212 assert_eq!(1, after_like_remove.downvotes);
214 // This should delete all the associated rows, and fire triggers
215 Person::delete(&conn, another_inserted_person.id).unwrap();
216 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
217 assert_eq!(1, person_num_deleted);
219 // Should be none found, since the creator was deleted
220 let after_delete = PostAggregates::read(&conn, inserted_post.id);
221 assert!(after_delete.is_err());