]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/post_aggregates.rs
fa8b69255c755e88cac92cecc46b5714b73ac82a
[lemmy.git] / crates / db_queries / src / aggregates / post_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::post_aggregates;
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "post_aggregates"]
7 pub struct PostAggregates {
8   pub id: i32,
9   pub post_id: i32,
10   pub comments: i64,
11   pub score: i64,
12   pub upvotes: i64,
13   pub downvotes: i64,
14   pub stickied: bool,
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,
18 }
19
20 impl PostAggregates {
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))
24       .first::<Self>(conn)
25   }
26 }
27
28 #[cfg(test)]
29 mod tests {
30   use crate::{
31     aggregates::post_aggregates::PostAggregates,
32     establish_unpooled_connection,
33     Crud,
34     Likeable,
35     ListingType,
36     SortType,
37   };
38   use lemmy_db_schema::source::{
39     comment::{Comment, CommentForm},
40     community::{Community, CommunityForm},
41     post::{Post, PostForm, PostLike, PostLikeForm},
42     user::{UserForm, User_},
43   };
44
45   #[test]
46   fn test_crud() {
47     let conn = establish_unpooled_connection();
48
49     let new_user = UserForm {
50       name: "thommy_community_agg".into(),
51       preferred_username: None,
52       password_encrypted: "nope".into(),
53       email: None,
54       matrix_user_id: None,
55       avatar: None,
56       banner: None,
57       admin: false,
58       banned: Some(false),
59       published: None,
60       updated: None,
61       show_nsfw: false,
62       theme: "browser".into(),
63       default_sort_type: SortType::Hot as i16,
64       default_listing_type: ListingType::Subscribed as i16,
65       lang: "browser".into(),
66       show_avatars: true,
67       send_notifications_to_email: false,
68       actor_id: None,
69       bio: None,
70       local: true,
71       private_key: None,
72       public_key: None,
73       last_refreshed_at: None,
74       inbox_url: None,
75       shared_inbox_url: None,
76     };
77
78     let inserted_user = User_::create(&conn, &new_user).unwrap();
79
80     let another_user = UserForm {
81       name: "jerry_community_agg".into(),
82       preferred_username: None,
83       password_encrypted: "nope".into(),
84       email: None,
85       matrix_user_id: None,
86       avatar: None,
87       banner: None,
88       admin: false,
89       banned: Some(false),
90       published: None,
91       updated: None,
92       show_nsfw: false,
93       theme: "browser".into(),
94       default_sort_type: SortType::Hot as i16,
95       default_listing_type: ListingType::Subscribed as i16,
96       lang: "browser".into(),
97       show_avatars: true,
98       send_notifications_to_email: false,
99       actor_id: None,
100       bio: None,
101       local: true,
102       private_key: None,
103       public_key: None,
104       last_refreshed_at: None,
105       inbox_url: None,
106       shared_inbox_url: None,
107     };
108
109     let another_inserted_user = User_::create(&conn, &another_user).unwrap();
110
111     let new_community = CommunityForm {
112       name: "TIL_community_agg".into(),
113       creator_id: inserted_user.id,
114       title: "nada".to_owned(),
115       description: None,
116       nsfw: false,
117       removed: None,
118       deleted: None,
119       updated: None,
120       actor_id: None,
121       local: true,
122       private_key: None,
123       public_key: None,
124       last_refreshed_at: None,
125       published: None,
126       icon: None,
127       banner: None,
128       followers_url: None,
129       inbox_url: None,
130       shared_inbox_url: None,
131     };
132
133     let inserted_community = Community::create(&conn, &new_community).unwrap();
134
135     let new_post = PostForm {
136       name: "A test post".into(),
137       url: None,
138       body: None,
139       creator_id: inserted_user.id,
140       community_id: inserted_community.id,
141       removed: None,
142       deleted: None,
143       locked: None,
144       stickied: None,
145       nsfw: false,
146       updated: None,
147       embed_title: None,
148       embed_description: None,
149       embed_html: None,
150       thumbnail_url: None,
151       ap_id: None,
152       local: true,
153       published: None,
154     };
155
156     let inserted_post = Post::create(&conn, &new_post).unwrap();
157
158     let comment_form = CommentForm {
159       content: "A test comment".into(),
160       creator_id: inserted_user.id,
161       post_id: inserted_post.id,
162       removed: None,
163       deleted: None,
164       read: None,
165       parent_id: None,
166       published: None,
167       updated: None,
168       ap_id: None,
169       local: true,
170     };
171
172     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
173
174     let child_comment_form = CommentForm {
175       content: "A test comment".into(),
176       creator_id: inserted_user.id,
177       post_id: inserted_post.id,
178       removed: None,
179       deleted: None,
180       read: None,
181       parent_id: Some(inserted_comment.id),
182       published: None,
183       updated: None,
184       ap_id: None,
185       local: true,
186     };
187
188     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
189
190     let post_like = PostLikeForm {
191       post_id: inserted_post.id,
192       user_id: inserted_user.id,
193       score: 1,
194     };
195
196     PostLike::like(&conn, &post_like).unwrap();
197
198     let post_aggs_before_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
199
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);
204
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,
209       score: -1,
210     };
211
212     PostLike::like(&conn, &post_dislike).unwrap();
213
214     let post_aggs_after_dislike = PostAggregates::read(&conn, inserted_post.id).unwrap();
215
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);
220
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);
228
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);
236
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);
241
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());
245   }
246 }