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,
38 use lemmy_db_schema::source::{
39 comment::{Comment, CommentForm},
40 community::{Community, CommunityForm},
41 post::{Post, PostForm, PostLike, PostLikeForm},
42 user::{UserForm, User_},
47 let conn = establish_unpooled_connection();
49 let new_user = UserForm {
50 name: "thommy_community_agg".into(),
51 preferred_username: None,
52 password_encrypted: "nope".into(),
62 theme: "browser".into(),
63 default_sort_type: SortType::Hot as i16,
64 default_listing_type: ListingType::Subscribed as i16,
65 lang: "browser".into(),
67 send_notifications_to_email: false,
73 last_refreshed_at: None,
75 shared_inbox_url: None,
78 let inserted_user = User_::create(&conn, &new_user).unwrap();
80 let another_user = UserForm {
81 name: "jerry_community_agg".into(),
82 preferred_username: None,
83 password_encrypted: "nope".into(),
93 theme: "browser".into(),
94 default_sort_type: SortType::Hot as i16,
95 default_listing_type: ListingType::Subscribed as i16,
96 lang: "browser".into(),
98 send_notifications_to_email: false,
104 last_refreshed_at: None,
106 shared_inbox_url: None,
109 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
111 let new_community = CommunityForm {
112 name: "TIL_community_agg".into(),
113 creator_id: inserted_user.id,
114 title: "nada".to_owned(),
124 last_refreshed_at: None,
130 shared_inbox_url: None,
133 let inserted_community = Community::create(&conn, &new_community).unwrap();
135 let new_post = PostForm {
136 name: "A test post".into(),
139 creator_id: inserted_user.id,
140 community_id: inserted_community.id,
148 embed_description: None,
156 let inserted_post = Post::create(&conn, &new_post).unwrap();
158 let comment_form = CommentForm {
159 content: "A test comment".into(),
160 creator_id: inserted_user.id,
161 post_id: inserted_post.id,
172 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
174 let child_comment_form = CommentForm {
175 content: "A test comment".into(),
176 creator_id: inserted_user.id,
177 post_id: inserted_post.id,
181 parent_id: Some(inserted_comment.id),
188 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
190 let post_like = PostLikeForm {
191 post_id: inserted_post.id,
192 user_id: inserted_user.id,
196 PostLike::like(&conn, &post_like).unwrap();
198 let post_aggs_before_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
200 assert_eq!(2, post_aggs_before_delete.comments);
201 assert_eq!(1, post_aggs_before_delete.score);
202 assert_eq!(1, post_aggs_before_delete.upvotes);
203 assert_eq!(0, post_aggs_before_delete.downvotes);
205 // Add a post dislike from the other user
206 let post_dislike = PostLikeForm {
207 post_id: inserted_post.id,
208 user_id: another_inserted_user.id,
212 PostLike::like(&conn, &post_dislike).unwrap();
214 let post_aggs_after_dislike = PostAggregates::read(&conn, inserted_post.id).unwrap();
216 assert_eq!(2, post_aggs_after_dislike.comments);
217 assert_eq!(0, post_aggs_after_dislike.score);
218 assert_eq!(1, post_aggs_after_dislike.upvotes);
219 assert_eq!(1, post_aggs_after_dislike.downvotes);
221 // Remove the parent comment
222 Comment::delete(&conn, inserted_comment.id).unwrap();
223 let after_comment_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
224 assert_eq!(0, after_comment_delete.comments);
225 assert_eq!(0, after_comment_delete.score);
226 assert_eq!(1, after_comment_delete.upvotes);
227 assert_eq!(1, after_comment_delete.downvotes);
229 // Remove the first post like
230 PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
231 let after_like_remove = PostAggregates::read(&conn, inserted_post.id).unwrap();
232 assert_eq!(0, after_like_remove.comments);
233 assert_eq!(-1, after_like_remove.score);
234 assert_eq!(0, after_like_remove.upvotes);
235 assert_eq!(1, after_like_remove.downvotes);
237 // This should delete all the associated rows, and fire triggers
238 User_::delete(&conn, another_inserted_user.id).unwrap();
239 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
240 assert_eq!(1, user_num_deleted);
242 // Should be none found, since the creator was deleted
243 let after_delete = PostAggregates::read(&conn, inserted_post.id);
244 assert!(after_delete.is_err());