]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/post_aggregates.rs
Dont upsert Instance row every apub fetch (#2771)
[lemmy.git] / crates / db_schema / src / aggregates / post_aggregates.rs
1 use crate::{
2   aggregates::structs::PostAggregates,
3   newtypes::PostId,
4   schema::post_aggregates,
5   utils::{get_conn, DbPool},
6 };
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
9
10 impl PostAggregates {
11   pub async fn read(pool: &DbPool, post_id: PostId) -> Result<Self, Error> {
12     let conn = &mut get_conn(pool).await?;
13     post_aggregates::table
14       .filter(post_aggregates::post_id.eq(post_id))
15       .first::<Self>(conn)
16       .await
17   }
18 }
19
20 #[cfg(test)]
21 mod tests {
22   use crate::{
23     aggregates::post_aggregates::PostAggregates,
24     source::{
25       comment::{Comment, CommentInsertForm},
26       community::{Community, CommunityInsertForm},
27       instance::Instance,
28       person::{Person, PersonInsertForm},
29       post::{Post, PostInsertForm, PostLike, PostLikeForm},
30     },
31     traits::{Crud, Likeable},
32     utils::build_db_pool_for_tests,
33   };
34   use serial_test::serial;
35
36   #[tokio::test]
37   #[serial]
38   async fn test_crud() {
39     let pool = &build_db_pool_for_tests().await;
40
41     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
42       .await
43       .unwrap();
44
45     let new_person = PersonInsertForm::builder()
46       .name("thommy_community_agg".into())
47       .public_key("pubkey".to_string())
48       .instance_id(inserted_instance.id)
49       .build();
50
51     let inserted_person = Person::create(pool, &new_person).await.unwrap();
52
53     let another_person = PersonInsertForm::builder()
54       .name("jerry_community_agg".into())
55       .public_key("pubkey".to_string())
56       .instance_id(inserted_instance.id)
57       .build();
58
59     let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
60
61     let new_community = CommunityInsertForm::builder()
62       .name("TIL_community_agg".into())
63       .title("nada".to_owned())
64       .public_key("pubkey".to_string())
65       .instance_id(inserted_instance.id)
66       .build();
67
68     let inserted_community = Community::create(pool, &new_community).await.unwrap();
69
70     let new_post = PostInsertForm::builder()
71       .name("A test post".into())
72       .creator_id(inserted_person.id)
73       .community_id(inserted_community.id)
74       .build();
75
76     let inserted_post = Post::create(pool, &new_post).await.unwrap();
77
78     let comment_form = CommentInsertForm::builder()
79       .content("A test comment".into())
80       .creator_id(inserted_person.id)
81       .post_id(inserted_post.id)
82       .build();
83
84     let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
85
86     let child_comment_form = CommentInsertForm::builder()
87       .content("A test comment".into())
88       .creator_id(inserted_person.id)
89       .post_id(inserted_post.id)
90       .build();
91
92     let inserted_child_comment =
93       Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
94         .await
95         .unwrap();
96
97     let post_like = PostLikeForm {
98       post_id: inserted_post.id,
99       person_id: inserted_person.id,
100       score: 1,
101     };
102
103     PostLike::like(pool, &post_like).await.unwrap();
104
105     let post_aggs_before_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
106
107     assert_eq!(2, post_aggs_before_delete.comments);
108     assert_eq!(1, post_aggs_before_delete.score);
109     assert_eq!(1, post_aggs_before_delete.upvotes);
110     assert_eq!(0, post_aggs_before_delete.downvotes);
111
112     // Add a post dislike from the other person
113     let post_dislike = PostLikeForm {
114       post_id: inserted_post.id,
115       person_id: another_inserted_person.id,
116       score: -1,
117     };
118
119     PostLike::like(pool, &post_dislike).await.unwrap();
120
121     let post_aggs_after_dislike = PostAggregates::read(pool, inserted_post.id).await.unwrap();
122
123     assert_eq!(2, post_aggs_after_dislike.comments);
124     assert_eq!(0, post_aggs_after_dislike.score);
125     assert_eq!(1, post_aggs_after_dislike.upvotes);
126     assert_eq!(1, post_aggs_after_dislike.downvotes);
127
128     // Remove the comments
129     Comment::delete(pool, inserted_comment.id).await.unwrap();
130     Comment::delete(pool, inserted_child_comment.id)
131       .await
132       .unwrap();
133     let after_comment_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
134     assert_eq!(0, after_comment_delete.comments);
135     assert_eq!(0, after_comment_delete.score);
136     assert_eq!(1, after_comment_delete.upvotes);
137     assert_eq!(1, after_comment_delete.downvotes);
138
139     // Remove the first post like
140     PostLike::remove(pool, inserted_person.id, inserted_post.id)
141       .await
142       .unwrap();
143     let after_like_remove = PostAggregates::read(pool, inserted_post.id).await.unwrap();
144     assert_eq!(0, after_like_remove.comments);
145     assert_eq!(-1, after_like_remove.score);
146     assert_eq!(0, after_like_remove.upvotes);
147     assert_eq!(1, after_like_remove.downvotes);
148
149     // This should delete all the associated rows, and fire triggers
150     Person::delete(pool, another_inserted_person.id)
151       .await
152       .unwrap();
153     let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
154     assert_eq!(1, person_num_deleted);
155
156     // Delete the community
157     let community_num_deleted = Community::delete(pool, inserted_community.id)
158       .await
159       .unwrap();
160     assert_eq!(1, community_num_deleted);
161
162     // Should be none found, since the creator was deleted
163     let after_delete = PostAggregates::read(pool, inserted_post.id).await;
164     assert!(after_delete.is_err());
165
166     Instance::delete(pool, inserted_instance.id).await.unwrap();
167   }
168 }