]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/comment_aggregates.rs
Moving matrix_user_id to person table. #1438
[lemmy.git] / crates / db_queries / src / aggregates / comment_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::{schema::comment_aggregates, CommentId};
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "comment_aggregates"]
7 pub struct CommentAggregates {
8   pub id: i32,
9   pub comment_id: CommentId,
10   pub score: i64,
11   pub upvotes: i64,
12   pub downvotes: i64,
13   pub published: chrono::NaiveDateTime,
14 }
15
16 impl CommentAggregates {
17   pub fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
18     comment_aggregates::table
19       .filter(comment_aggregates::comment_id.eq(comment_id))
20       .first::<Self>(conn)
21   }
22 }
23
24 #[cfg(test)]
25 mod tests {
26   use crate::{
27     aggregates::comment_aggregates::CommentAggregates,
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},
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_comment_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_comment_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_comment_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 comment_form = CommentForm {
137       content: "A test comment".into(),
138       creator_id: inserted_person.id,
139       post_id: inserted_post.id,
140       removed: None,
141       deleted: None,
142       read: None,
143       parent_id: None,
144       published: None,
145       updated: None,
146       ap_id: None,
147       local: true,
148     };
149
150     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
151
152     let child_comment_form = CommentForm {
153       content: "A test comment".into(),
154       creator_id: inserted_person.id,
155       post_id: inserted_post.id,
156       removed: None,
157       deleted: None,
158       read: None,
159       parent_id: Some(inserted_comment.id),
160       published: None,
161       updated: None,
162       ap_id: None,
163       local: true,
164     };
165
166     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
167
168     let comment_like = CommentLikeForm {
169       comment_id: inserted_comment.id,
170       post_id: inserted_post.id,
171       person_id: inserted_person.id,
172       score: 1,
173     };
174
175     CommentLike::like(&conn, &comment_like).unwrap();
176
177     let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
178
179     assert_eq!(1, comment_aggs_before_delete.score);
180     assert_eq!(1, comment_aggs_before_delete.upvotes);
181     assert_eq!(0, comment_aggs_before_delete.downvotes);
182
183     // Add a post dislike from the other person
184     let comment_dislike = CommentLikeForm {
185       comment_id: inserted_comment.id,
186       post_id: inserted_post.id,
187       person_id: another_inserted_person.id,
188       score: -1,
189     };
190
191     CommentLike::like(&conn, &comment_dislike).unwrap();
192
193     let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
194
195     assert_eq!(0, comment_aggs_after_dislike.score);
196     assert_eq!(1, comment_aggs_after_dislike.upvotes);
197     assert_eq!(1, comment_aggs_after_dislike.downvotes);
198
199     // Remove the first comment like
200     CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
201     let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
202     assert_eq!(-1, after_like_remove.score);
203     assert_eq!(0, after_like_remove.upvotes);
204     assert_eq!(1, after_like_remove.downvotes);
205
206     // Remove the parent post
207     Post::delete(&conn, inserted_post.id).unwrap();
208
209     // Should be none found, since the post was deleted
210     let after_delete = CommentAggregates::read(&conn, inserted_comment.id);
211     assert!(after_delete.is_err());
212
213     // This should delete all the associated rows, and fire triggers
214     Person::delete(&conn, another_inserted_person.id).unwrap();
215     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
216     assert_eq!(1, person_num_deleted);
217   }
218 }