]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/person_aggregates.rs
Moving matrix_user_id to person table. #1438
[lemmy.git] / crates / db_queries / src / aggregates / person_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::{schema::person_aggregates, PersonId};
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "person_aggregates"]
7 pub struct PersonAggregates {
8   pub id: i32,
9   pub person_id: PersonId,
10   pub post_count: i64,
11   pub post_score: i64,
12   pub comment_count: i64,
13   pub comment_score: i64,
14 }
15
16 impl PersonAggregates {
17   pub fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
18     person_aggregates::table
19       .filter(person_aggregates::person_id.eq(person_id))
20       .first::<Self>(conn)
21   }
22 }
23
24 #[cfg(test)]
25 mod tests {
26   use crate::{
27     aggregates::person_aggregates::PersonAggregates,
28     establish_unpooled_connection,
29     Crud,
30     Likeable,
31   };
32   use lemmy_db_schema::source::{
33     comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
34     community::{Community, CommunityForm},
35     person::{Person, PersonForm},
36     post::{Post, PostForm, PostLike, PostLikeForm},
37   };
38   use serial_test::serial;
39
40   #[test]
41   #[serial]
42   fn test_crud() {
43     let conn = establish_unpooled_connection();
44
45     let new_person = PersonForm {
46       name: "thommy_user_agg".into(),
47       preferred_username: None,
48       avatar: None,
49       banner: None,
50       banned: None,
51       deleted: None,
52       published: None,
53       updated: None,
54       actor_id: None,
55       bio: None,
56       local: None,
57       private_key: None,
58       public_key: None,
59       last_refreshed_at: None,
60       inbox_url: None,
61       shared_inbox_url: None,
62       matrix_user_id: None,
63     };
64
65     let inserted_person = Person::create(&conn, &new_person).unwrap();
66
67     let another_person = PersonForm {
68       name: "jerry_user_agg".into(),
69       preferred_username: None,
70       avatar: None,
71       banner: None,
72       banned: None,
73       deleted: None,
74       published: None,
75       updated: None,
76       actor_id: None,
77       bio: None,
78       local: None,
79       private_key: None,
80       public_key: None,
81       last_refreshed_at: None,
82       inbox_url: None,
83       shared_inbox_url: None,
84       matrix_user_id: None,
85     };
86
87     let another_inserted_person = Person::create(&conn, &another_person).unwrap();
88
89     let new_community = CommunityForm {
90       name: "TIL_site_agg".into(),
91       creator_id: inserted_person.id,
92       title: "nada".to_owned(),
93       description: None,
94       nsfw: false,
95       removed: None,
96       deleted: None,
97       updated: None,
98       actor_id: None,
99       local: true,
100       private_key: None,
101       public_key: None,
102       last_refreshed_at: None,
103       published: None,
104       icon: None,
105       banner: None,
106       followers_url: None,
107       inbox_url: None,
108       shared_inbox_url: None,
109     };
110
111     let inserted_community = Community::create(&conn, &new_community).unwrap();
112
113     let new_post = PostForm {
114       name: "A test post".into(),
115       url: None,
116       body: None,
117       creator_id: inserted_person.id,
118       community_id: inserted_community.id,
119       removed: None,
120       deleted: None,
121       locked: None,
122       stickied: None,
123       nsfw: false,
124       updated: None,
125       embed_title: None,
126       embed_description: None,
127       embed_html: None,
128       thumbnail_url: None,
129       ap_id: None,
130       local: true,
131       published: None,
132     };
133
134     let inserted_post = Post::create(&conn, &new_post).unwrap();
135
136     let post_like = PostLikeForm {
137       post_id: inserted_post.id,
138       person_id: inserted_person.id,
139       score: 1,
140     };
141
142     let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
143
144     let comment_form = CommentForm {
145       content: "A test comment".into(),
146       creator_id: inserted_person.id,
147       post_id: inserted_post.id,
148       removed: None,
149       deleted: None,
150       read: None,
151       parent_id: None,
152       published: None,
153       updated: None,
154       ap_id: None,
155       local: true,
156     };
157
158     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
159
160     let mut comment_like = CommentLikeForm {
161       comment_id: inserted_comment.id,
162       person_id: inserted_person.id,
163       post_id: inserted_post.id,
164       score: 1,
165     };
166
167     let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
168
169     let mut child_comment_form = CommentForm {
170       content: "A test comment".into(),
171       creator_id: inserted_person.id,
172       post_id: inserted_post.id,
173       removed: None,
174       deleted: None,
175       read: None,
176       parent_id: Some(inserted_comment.id),
177       published: None,
178       updated: None,
179       ap_id: None,
180       local: true,
181     };
182
183     let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
184
185     let child_comment_like = CommentLikeForm {
186       comment_id: inserted_child_comment.id,
187       person_id: another_inserted_person.id,
188       post_id: inserted_post.id,
189       score: 1,
190     };
191
192     let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
193
194     let person_aggregates_before_delete =
195       PersonAggregates::read(&conn, inserted_person.id).unwrap();
196
197     assert_eq!(1, person_aggregates_before_delete.post_count);
198     assert_eq!(1, person_aggregates_before_delete.post_score);
199     assert_eq!(2, person_aggregates_before_delete.comment_count);
200     assert_eq!(2, person_aggregates_before_delete.comment_score);
201
202     // Remove a post like
203     PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
204     let after_post_like_remove = PersonAggregates::read(&conn, inserted_person.id).unwrap();
205     assert_eq!(0, after_post_like_remove.post_score);
206
207     // Remove a parent comment (the scores should also be removed)
208     Comment::delete(&conn, inserted_comment.id).unwrap();
209     let after_parent_comment_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
210     assert_eq!(0, after_parent_comment_delete.comment_count);
211     assert_eq!(0, after_parent_comment_delete.comment_score);
212
213     // Add in the two comments again, then delete the post.
214     let new_parent_comment = Comment::create(&conn, &comment_form).unwrap();
215     child_comment_form.parent_id = Some(new_parent_comment.id);
216     Comment::create(&conn, &child_comment_form).unwrap();
217     comment_like.comment_id = new_parent_comment.id;
218     CommentLike::like(&conn, &comment_like).unwrap();
219     let after_comment_add = PersonAggregates::read(&conn, inserted_person.id).unwrap();
220     assert_eq!(2, after_comment_add.comment_count);
221     assert_eq!(1, after_comment_add.comment_score);
222
223     Post::delete(&conn, inserted_post.id).unwrap();
224     let after_post_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
225     assert_eq!(0, after_post_delete.comment_score);
226     assert_eq!(0, after_post_delete.comment_count);
227     assert_eq!(0, after_post_delete.post_score);
228     assert_eq!(0, after_post_delete.post_count);
229
230     // This should delete all the associated rows, and fire triggers
231     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
232     assert_eq!(1, person_num_deleted);
233     Person::delete(&conn, another_inserted_person.id).unwrap();
234
235     // Should be none found
236     let after_delete = PersonAggregates::read(&conn, inserted_person.id);
237     assert!(after_delete.is_err());
238   }
239 }