]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/site_aggregates.rs
Adding some recurring lemmy tasks. (#1386)
[lemmy.git] / crates / db_queries / src / aggregates / site_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::site_aggregates;
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "site_aggregates"]
7 pub struct SiteAggregates {
8   pub id: i32,
9   pub site_id: i32,
10   pub users: i64,
11   pub posts: i64,
12   pub comments: i64,
13   pub communities: i64,
14   pub users_active_day: i64,
15   pub users_active_week: i64,
16   pub users_active_month: i64,
17   pub users_active_half_year: i64,
18 }
19
20 impl SiteAggregates {
21   pub fn read(conn: &PgConnection) -> Result<Self, Error> {
22     site_aggregates::table.first::<Self>(conn)
23   }
24 }
25
26 #[cfg(test)]
27 mod tests {
28   use crate::{
29     aggregates::site_aggregates::SiteAggregates,
30     establish_unpooled_connection,
31     Crud,
32     ListingType,
33     SortType,
34   };
35   use lemmy_db_schema::source::{
36     comment::{Comment, CommentForm},
37     community::{Community, CommunityForm},
38     post::{Post, PostForm},
39     site::{Site, SiteForm},
40     user::{UserForm, User_},
41   };
42
43   #[test]
44   fn test_crud() {
45     let conn = establish_unpooled_connection();
46
47     let new_user = UserForm {
48       name: "thommy_site_agg".into(),
49       preferred_username: None,
50       password_encrypted: "nope".into(),
51       email: None,
52       matrix_user_id: None,
53       avatar: None,
54       banner: None,
55       admin: false,
56       banned: Some(false),
57       published: None,
58       updated: None,
59       show_nsfw: false,
60       theme: "browser".into(),
61       default_sort_type: SortType::Hot as i16,
62       default_listing_type: ListingType::Subscribed as i16,
63       lang: "browser".into(),
64       show_avatars: true,
65       send_notifications_to_email: false,
66       actor_id: None,
67       bio: None,
68       local: true,
69       private_key: None,
70       public_key: None,
71       last_refreshed_at: None,
72     };
73
74     let inserted_user = User_::create(&conn, &new_user).unwrap();
75
76     let site_form = SiteForm {
77       name: "test_site".into(),
78       description: None,
79       icon: None,
80       banner: None,
81       creator_id: inserted_user.id,
82       enable_downvotes: true,
83       open_registration: true,
84       enable_nsfw: true,
85       updated: None,
86     };
87
88     Site::create(&conn, &site_form).unwrap();
89
90     let new_community = CommunityForm {
91       name: "TIL_site_agg".into(),
92       creator_id: inserted_user.id,
93       title: "nada".to_owned(),
94       description: None,
95       category_id: 1,
96       nsfw: false,
97       removed: None,
98       deleted: None,
99       updated: None,
100       actor_id: None,
101       local: true,
102       private_key: None,
103       public_key: None,
104       last_refreshed_at: None,
105       published: None,
106       icon: None,
107       banner: None,
108     };
109
110     let inserted_community = Community::create(&conn, &new_community).unwrap();
111
112     let new_post = PostForm {
113       name: "A test post".into(),
114       url: None,
115       body: None,
116       creator_id: inserted_user.id,
117       community_id: inserted_community.id,
118       removed: None,
119       deleted: None,
120       locked: None,
121       stickied: None,
122       nsfw: false,
123       updated: None,
124       embed_title: None,
125       embed_description: None,
126       embed_html: None,
127       thumbnail_url: None,
128       ap_id: None,
129       local: true,
130       published: None,
131     };
132
133     // Insert two of those posts
134     let inserted_post = Post::create(&conn, &new_post).unwrap();
135     let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
136
137     let comment_form = CommentForm {
138       content: "A test comment".into(),
139       creator_id: inserted_user.id,
140       post_id: inserted_post.id,
141       removed: None,
142       deleted: None,
143       read: None,
144       parent_id: None,
145       published: None,
146       updated: None,
147       ap_id: None,
148       local: true,
149     };
150
151     // Insert two of those comments
152     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
153
154     let child_comment_form = CommentForm {
155       content: "A test comment".into(),
156       creator_id: inserted_user.id,
157       post_id: inserted_post.id,
158       removed: None,
159       deleted: None,
160       read: None,
161       parent_id: Some(inserted_comment.id),
162       published: None,
163       updated: None,
164       ap_id: None,
165       local: true,
166     };
167
168     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
169
170     let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
171
172     assert_eq!(1, site_aggregates_before_delete.users);
173     assert_eq!(1, site_aggregates_before_delete.communities);
174     assert_eq!(2, site_aggregates_before_delete.posts);
175     assert_eq!(2, site_aggregates_before_delete.comments);
176
177     // Try a post delete
178     Post::delete(&conn, inserted_post.id).unwrap();
179     let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
180     assert_eq!(1, site_aggregates_after_post_delete.posts);
181     assert_eq!(0, site_aggregates_after_post_delete.comments);
182
183     // This shouuld delete all the associated rows, and fire triggers
184     let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
185     assert_eq!(1, user_num_deleted);
186
187     let after_delete = SiteAggregates::read(&conn);
188     assert!(after_delete.is_err());
189   }
190 }