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_},
44 use serial_test::serial;
49 let conn = establish_unpooled_connection();
51 let new_user = UserForm {
52 name: "thommy_community_agg".into(),
53 preferred_username: None,
54 password_encrypted: "nope".into(),
64 theme: "browser".into(),
65 default_sort_type: SortType::Hot as i16,
66 default_listing_type: ListingType::Subscribed as i16,
67 lang: "browser".into(),
69 send_notifications_to_email: false,
75 last_refreshed_at: None,
77 shared_inbox_url: None,
80 let inserted_user = User_::create(&conn, &new_user).unwrap();
82 let another_user = UserForm {
83 name: "jerry_community_agg".into(),
84 preferred_username: None,
85 password_encrypted: "nope".into(),
95 theme: "browser".into(),
96 default_sort_type: SortType::Hot as i16,
97 default_listing_type: ListingType::Subscribed as i16,
98 lang: "browser".into(),
100 send_notifications_to_email: false,
106 last_refreshed_at: None,
108 shared_inbox_url: None,
111 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
113 let new_community = CommunityForm {
114 name: "TIL_community_agg".into(),
115 creator_id: inserted_user.id,
116 title: "nada".to_owned(),
126 last_refreshed_at: None,
132 shared_inbox_url: None,
135 let inserted_community = Community::create(&conn, &new_community).unwrap();
137 let new_post = PostForm {
138 name: "A test post".into(),
141 creator_id: inserted_user.id,
142 community_id: inserted_community.id,
150 embed_description: None,
158 let inserted_post = Post::create(&conn, &new_post).unwrap();
160 let comment_form = CommentForm {
161 content: "A test comment".into(),
162 creator_id: inserted_user.id,
163 post_id: inserted_post.id,
174 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
176 let child_comment_form = CommentForm {
177 content: "A test comment".into(),
178 creator_id: inserted_user.id,
179 post_id: inserted_post.id,
183 parent_id: Some(inserted_comment.id),
190 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
192 let post_like = PostLikeForm {
193 post_id: inserted_post.id,
194 user_id: inserted_user.id,
198 PostLike::like(&conn, &post_like).unwrap();
200 let post_aggs_before_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
202 assert_eq!(2, post_aggs_before_delete.comments);
203 assert_eq!(1, post_aggs_before_delete.score);
204 assert_eq!(1, post_aggs_before_delete.upvotes);
205 assert_eq!(0, post_aggs_before_delete.downvotes);
207 // Add a post dislike from the other user
208 let post_dislike = PostLikeForm {
209 post_id: inserted_post.id,
210 user_id: another_inserted_user.id,
214 PostLike::like(&conn, &post_dislike).unwrap();
216 let post_aggs_after_dislike = PostAggregates::read(&conn, inserted_post.id).unwrap();
218 assert_eq!(2, post_aggs_after_dislike.comments);
219 assert_eq!(0, post_aggs_after_dislike.score);
220 assert_eq!(1, post_aggs_after_dislike.upvotes);
221 assert_eq!(1, post_aggs_after_dislike.downvotes);
223 // Remove the parent comment
224 Comment::delete(&conn, inserted_comment.id).unwrap();
225 let after_comment_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
226 assert_eq!(0, after_comment_delete.comments);
227 assert_eq!(0, after_comment_delete.score);
228 assert_eq!(1, after_comment_delete.upvotes);
229 assert_eq!(1, after_comment_delete.downvotes);
231 // Remove the first post like
232 PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
233 let after_like_remove = PostAggregates::read(&conn, inserted_post.id).unwrap();
234 assert_eq!(0, after_like_remove.comments);
235 assert_eq!(-1, after_like_remove.score);
236 assert_eq!(0, after_like_remove.upvotes);
237 assert_eq!(1, after_like_remove.downvotes);
239 // This should delete all the associated rows, and fire triggers
240 User_::delete(&conn, another_inserted_user.id).unwrap();
241 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
242 assert_eq!(1, user_num_deleted);
244 // Should be none found, since the creator was deleted
245 let after_delete = PostAggregates::read(&conn, inserted_post.id);
246 assert!(after_delete.is_err());