]> Untitled Git - lemmy.git/blob - lemmy_db/src/aggregates/post_aggregates.rs
Starting to add post_view.
[lemmy.git] / lemmy_db / src / aggregates / post_aggregates.rs
1 use crate::schema::post_aggregates;
2 use diesel::{result::Error, *};
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 newest_comment_time: chrono::NaiveDateTime,
15 }
16
17 impl PostAggregates {
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))
21       .first::<Self>(conn)
22   }
23 }
24
25 // #[cfg(test)]
26 // mod tests {
27 //   use crate::{
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_},
34 //     Crud,
35 //     Followable,
36 //     ListingType,
37 //     SortType,
38 //   };
39
40 //   #[test]
41 //   fn test_crud() {
42 //     let conn = establish_unpooled_connection();
43
44 //     let new_user = UserForm {
45 //       name: "thommy_community_agg".into(),
46 //       preferred_username: None,
47 //       password_encrypted: "nope".into(),
48 //       email: None,
49 //       matrix_user_id: None,
50 //       avatar: None,
51 //       banner: None,
52 //       admin: false,
53 //       banned: Some(false),
54 //       published: None,
55 //       updated: None,
56 //       show_nsfw: 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,
63 //       actor_id: None,
64 //       bio: None,
65 //       local: true,
66 //       private_key: None,
67 //       public_key: None,
68 //       last_refreshed_at: None,
69 //     };
70
71 //     let inserted_user = User_::create(&conn, &new_user).unwrap();
72
73 //     let another_user = UserForm {
74 //       name: "jerry_community_agg".into(),
75 //       preferred_username: None,
76 //       password_encrypted: "nope".into(),
77 //       email: None,
78 //       matrix_user_id: None,
79 //       avatar: None,
80 //       banner: None,
81 //       admin: false,
82 //       banned: Some(false),
83 //       published: None,
84 //       updated: None,
85 //       show_nsfw: 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,
92 //       actor_id: None,
93 //       bio: None,
94 //       local: true,
95 //       private_key: None,
96 //       public_key: None,
97 //       last_refreshed_at: None,
98 //     };
99
100 //     let another_inserted_user = User_::create(&conn, &another_user).unwrap();
101
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,
107 //       category_id: 1,
108 //       nsfw: false,
109 //       removed: None,
110 //       deleted: None,
111 //       updated: None,
112 //       actor_id: None,
113 //       local: true,
114 //       private_key: None,
115 //       public_key: None,
116 //       last_refreshed_at: None,
117 //       published: None,
118 //       icon: None,
119 //       banner: None,
120 //     };
121
122 //     let inserted_community = Community::create(&conn, &new_community).unwrap();
123
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,
129 //       category_id: 1,
130 //       nsfw: false,
131 //       removed: None,
132 //       deleted: None,
133 //       updated: None,
134 //       actor_id: None,
135 //       local: true,
136 //       private_key: None,
137 //       public_key: None,
138 //       last_refreshed_at: None,
139 //       published: None,
140 //       icon: None,
141 //       banner: None,
142 //     };
143
144 //     let another_inserted_community = Community::create(&conn, &another_community).unwrap();
145
146 //     let first_user_follow = CommunityFollowerForm {
147 //       community_id: inserted_community.id,
148 //       user_id: inserted_user.id,
149 //       pending: false,
150 //     };
151
152 //     CommunityFollower::follow(&conn, &first_user_follow).unwrap();
153
154 //     let second_user_follow = CommunityFollowerForm {
155 //       community_id: inserted_community.id,
156 //       user_id: another_inserted_user.id,
157 //       pending: false,
158 //     };
159
160 //     CommunityFollower::follow(&conn, &second_user_follow).unwrap();
161
162 //     let another_community_follow = CommunityFollowerForm {
163 //       community_id: another_inserted_community.id,
164 //       user_id: inserted_user.id,
165 //       pending: false,
166 //     };
167
168 //     CommunityFollower::follow(&conn, &another_community_follow).unwrap();
169
170 //     let new_post = PostForm {
171 //       name: "A test post".into(),
172 //       url: None,
173 //       body: None,
174 //       creator_id: inserted_user.id,
175 //       community_id: inserted_community.id,
176 //       removed: None,
177 //       deleted: None,
178 //       locked: None,
179 //       stickied: None,
180 //       nsfw: false,
181 //       updated: None,
182 //       embed_title: None,
183 //       embed_description: None,
184 //       embed_html: None,
185 //       thumbnail_url: None,
186 //       ap_id: None,
187 //       local: true,
188 //       published: None,
189 //     };
190
191 //     let inserted_post = Post::create(&conn, &new_post).unwrap();
192
193 //     let comment_form = CommentForm {
194 //       content: "A test comment".into(),
195 //       creator_id: inserted_user.id,
196 //       post_id: inserted_post.id,
197 //       removed: None,
198 //       deleted: None,
199 //       read: None,
200 //       parent_id: None,
201 //       published: None,
202 //       updated: None,
203 //       ap_id: None,
204 //       local: true,
205 //     };
206
207 //     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
208
209 //     let child_comment_form = CommentForm {
210 //       content: "A test comment".into(),
211 //       creator_id: inserted_user.id,
212 //       post_id: inserted_post.id,
213 //       removed: None,
214 //       deleted: None,
215 //       read: None,
216 //       parent_id: Some(inserted_comment.id),
217 //       published: None,
218 //       updated: None,
219 //       ap_id: None,
220 //       local: true,
221 //     };
222
223 //     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
224
225 //     let community_aggregates_before_delete =
226 //       CommunityAggregates::read(&conn, inserted_community.id).unwrap();
227
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);
231
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);
238
239 //     // Unfollow test
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);
243
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);
248
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);
254
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);
259
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);
263
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());
267 //   }
268 // }