1 use crate::schema::post_aggregates;
2 use diesel::{result::Error, *};
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "post_aggregates"]
7 pub struct PostAggregates {
14 pub newest_comment_time: chrono::NaiveDateTime,
18 pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
19 post_aggregates::table
20 .filter(post_aggregates::post_id.eq(post_id))
28 // aggregates::community_aggregates::CommunityAggregates,
29 // comment::{Comment, CommentForm},
30 // community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
31 // post::{Post, PostForm},
32 // tests::establish_unpooled_connection,
33 // user::{UserForm, User_},
42 // let conn = establish_unpooled_connection();
44 // let new_user = UserForm {
45 // name: "thommy_community_agg".into(),
46 // preferred_username: None,
47 // password_encrypted: "nope".into(),
49 // matrix_user_id: None,
53 // banned: Some(false),
57 // theme: "browser".into(),
58 // default_sort_type: SortType::Hot as i16,
59 // default_listing_type: ListingType::Subscribed as i16,
60 // lang: "browser".into(),
61 // show_avatars: true,
62 // send_notifications_to_email: false,
68 // last_refreshed_at: None,
71 // let inserted_user = User_::create(&conn, &new_user).unwrap();
73 // let another_user = UserForm {
74 // name: "jerry_community_agg".into(),
75 // preferred_username: None,
76 // password_encrypted: "nope".into(),
78 // matrix_user_id: None,
82 // banned: Some(false),
86 // theme: "browser".into(),
87 // default_sort_type: SortType::Hot as i16,
88 // default_listing_type: ListingType::Subscribed as i16,
89 // lang: "browser".into(),
90 // show_avatars: true,
91 // send_notifications_to_email: false,
97 // last_refreshed_at: None,
100 // let another_inserted_user = User_::create(&conn, &another_user).unwrap();
102 // let new_community = CommunityForm {
103 // name: "TIL_community_agg".into(),
104 // creator_id: inserted_user.id,
105 // title: "nada".to_owned(),
106 // description: None,
114 // private_key: None,
116 // last_refreshed_at: None,
122 // let inserted_community = Community::create(&conn, &new_community).unwrap();
124 // let another_community = CommunityForm {
125 // name: "TIL_community_agg_2".into(),
126 // creator_id: inserted_user.id,
127 // title: "nada".to_owned(),
128 // description: None,
136 // private_key: None,
138 // last_refreshed_at: None,
144 // let another_inserted_community = Community::create(&conn, &another_community).unwrap();
146 // let first_user_follow = CommunityFollowerForm {
147 // community_id: inserted_community.id,
148 // user_id: inserted_user.id,
152 // CommunityFollower::follow(&conn, &first_user_follow).unwrap();
154 // let second_user_follow = CommunityFollowerForm {
155 // community_id: inserted_community.id,
156 // user_id: another_inserted_user.id,
160 // CommunityFollower::follow(&conn, &second_user_follow).unwrap();
162 // let another_community_follow = CommunityFollowerForm {
163 // community_id: another_inserted_community.id,
164 // user_id: inserted_user.id,
168 // CommunityFollower::follow(&conn, &another_community_follow).unwrap();
170 // let new_post = PostForm {
171 // name: "A test post".into(),
174 // creator_id: inserted_user.id,
175 // community_id: inserted_community.id,
182 // embed_title: None,
183 // embed_description: None,
185 // thumbnail_url: None,
191 // let inserted_post = Post::create(&conn, &new_post).unwrap();
193 // let comment_form = CommentForm {
194 // content: "A test comment".into(),
195 // creator_id: inserted_user.id,
196 // post_id: inserted_post.id,
207 // let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
209 // let child_comment_form = CommentForm {
210 // content: "A test comment".into(),
211 // creator_id: inserted_user.id,
212 // post_id: inserted_post.id,
216 // parent_id: Some(inserted_comment.id),
223 // let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
225 // let community_aggregates_before_delete =
226 // CommunityAggregates::read(&conn, inserted_community.id).unwrap();
228 // assert_eq!(2, community_aggregates_before_delete.subscribers);
229 // assert_eq!(1, community_aggregates_before_delete.posts);
230 // assert_eq!(2, community_aggregates_before_delete.comments);
232 // // Test the other community
233 // let another_community_aggs =
234 // CommunityAggregates::read(&conn, another_inserted_community.id).unwrap();
235 // assert_eq!(1, another_community_aggs.subscribers);
236 // assert_eq!(0, another_community_aggs.posts);
237 // assert_eq!(0, another_community_aggs.comments);
240 // CommunityFollower::unfollow(&conn, &second_user_follow).unwrap();
241 // let after_unfollow = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
242 // assert_eq!(1, after_unfollow.subscribers);
244 // // Follow again just for the later tests
245 // CommunityFollower::follow(&conn, &second_user_follow).unwrap();
246 // let after_follow_again = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
247 // assert_eq!(2, after_follow_again.subscribers);
249 // // Remove a parent comment (the comment count should also be 0)
250 // Post::delete(&conn, inserted_post.id).unwrap();
251 // let after_parent_post_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
252 // assert_eq!(0, after_parent_post_delete.comments);
253 // assert_eq!(0, after_parent_post_delete.posts);
255 // // Remove the 2nd user
256 // User_::delete(&conn, another_inserted_user.id).unwrap();
257 // let after_user_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
258 // assert_eq!(1, after_user_delete.subscribers);
260 // // This should delete all the associated rows, and fire triggers
261 // let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
262 // assert_eq!(1, user_num_deleted);
264 // // Should be none found, since the creator was deleted
265 // let after_delete = CommunityAggregates::read(&conn, inserted_community.id);
266 // assert!(after_delete.is_err());