]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/person_aggregates.rs
Merge pull request #1850 from LemmyNet/refactor-apub
[lemmy.git] / crates / db_schema / src / aggregates / person_aggregates.rs
1 use crate::{newtypes::PersonId, schema::person_aggregates};
2 use diesel::{result::Error, *};
3 use serde::{Deserialize, Serialize};
4
5 #[derive(
6   Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize, Clone,
7 )]
8 #[table_name = "person_aggregates"]
9 pub struct PersonAggregates {
10   pub id: i32,
11   pub person_id: PersonId,
12   pub post_count: i64,
13   pub post_score: i64,
14   pub comment_count: i64,
15   pub comment_score: i64,
16 }
17
18 impl PersonAggregates {
19   pub fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
20     person_aggregates::table
21       .filter(person_aggregates::person_id.eq(person_id))
22       .first::<Self>(conn)
23   }
24 }
25
26 #[cfg(test)]
27 mod tests {
28   use crate::{
29     aggregates::person_aggregates::PersonAggregates,
30     establish_unpooled_connection,
31     source::{
32       comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
33       community::{Community, CommunityForm},
34       person::{Person, PersonForm},
35       post::{Post, PostForm, PostLike, PostLikeForm},
36     },
37     traits::{Crud, Likeable},
38   };
39   use serial_test::serial;
40
41   #[test]
42   #[serial]
43   fn test_crud() {
44     let conn = establish_unpooled_connection();
45
46     let new_person = PersonForm {
47       name: "thommy_user_agg".into(),
48       ..PersonForm::default()
49     };
50
51     let inserted_person = Person::create(&conn, &new_person).unwrap();
52
53     let another_person = PersonForm {
54       name: "jerry_user_agg".into(),
55       ..PersonForm::default()
56     };
57
58     let another_inserted_person = Person::create(&conn, &another_person).unwrap();
59
60     let new_community = CommunityForm {
61       name: "TIL_site_agg".into(),
62       title: "nada".to_owned(),
63       ..CommunityForm::default()
64     };
65
66     let inserted_community = Community::create(&conn, &new_community).unwrap();
67
68     let new_post = PostForm {
69       name: "A test post".into(),
70       creator_id: inserted_person.id,
71       community_id: inserted_community.id,
72       ..PostForm::default()
73     };
74
75     let inserted_post = Post::create(&conn, &new_post).unwrap();
76
77     let post_like = PostLikeForm {
78       post_id: inserted_post.id,
79       person_id: inserted_person.id,
80       score: 1,
81     };
82
83     let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
84
85     let comment_form = CommentForm {
86       content: "A test comment".into(),
87       creator_id: inserted_person.id,
88       post_id: inserted_post.id,
89       ..CommentForm::default()
90     };
91
92     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
93
94     let mut comment_like = CommentLikeForm {
95       comment_id: inserted_comment.id,
96       person_id: inserted_person.id,
97       post_id: inserted_post.id,
98       score: 1,
99     };
100
101     let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
102
103     let mut child_comment_form = CommentForm {
104       content: "A test comment".into(),
105       creator_id: inserted_person.id,
106       post_id: inserted_post.id,
107       parent_id: Some(inserted_comment.id),
108       ..CommentForm::default()
109     };
110
111     let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
112
113     let child_comment_like = CommentLikeForm {
114       comment_id: inserted_child_comment.id,
115       person_id: another_inserted_person.id,
116       post_id: inserted_post.id,
117       score: 1,
118     };
119
120     let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
121
122     let person_aggregates_before_delete =
123       PersonAggregates::read(&conn, inserted_person.id).unwrap();
124
125     assert_eq!(1, person_aggregates_before_delete.post_count);
126     assert_eq!(1, person_aggregates_before_delete.post_score);
127     assert_eq!(2, person_aggregates_before_delete.comment_count);
128     assert_eq!(2, person_aggregates_before_delete.comment_score);
129
130     // Remove a post like
131     PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
132     let after_post_like_remove = PersonAggregates::read(&conn, inserted_person.id).unwrap();
133     assert_eq!(0, after_post_like_remove.post_score);
134
135     // Remove a parent comment (the scores should also be removed)
136     Comment::delete(&conn, inserted_comment.id).unwrap();
137     let after_parent_comment_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
138     assert_eq!(0, after_parent_comment_delete.comment_count);
139     assert_eq!(0, after_parent_comment_delete.comment_score);
140
141     // Add in the two comments again, then delete the post.
142     let new_parent_comment = Comment::create(&conn, &comment_form).unwrap();
143     child_comment_form.parent_id = Some(new_parent_comment.id);
144     Comment::create(&conn, &child_comment_form).unwrap();
145     comment_like.comment_id = new_parent_comment.id;
146     CommentLike::like(&conn, &comment_like).unwrap();
147     let after_comment_add = PersonAggregates::read(&conn, inserted_person.id).unwrap();
148     assert_eq!(2, after_comment_add.comment_count);
149     assert_eq!(1, after_comment_add.comment_score);
150
151     Post::delete(&conn, inserted_post.id).unwrap();
152     let after_post_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
153     assert_eq!(0, after_post_delete.comment_score);
154     assert_eq!(0, after_post_delete.comment_count);
155     assert_eq!(0, after_post_delete.post_score);
156     assert_eq!(0, after_post_delete.post_count);
157
158     // This should delete all the associated rows, and fire triggers
159     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
160     assert_eq!(1, person_num_deleted);
161     Person::delete(&conn, another_inserted_person.id).unwrap();
162
163     // Delete the community
164     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
165     assert_eq!(1, community_num_deleted);
166
167     // Should be none found
168     let after_delete = PersonAggregates::read(&conn, inserted_person.id);
169     assert!(after_delete.is_err());
170   }
171 }