]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/site_aggregates.rs
Removing the site creator, adding leave_admin. Fixes #1808 (#2052)
[lemmy.git] / crates / db_schema / src / aggregates / site_aggregates.rs
1 use crate::schema::site_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 = "site_aggregates"]
9 pub struct SiteAggregates {
10   pub id: i32,
11   pub site_id: i32,
12   pub users: i64,
13   pub posts: i64,
14   pub comments: i64,
15   pub communities: i64,
16   pub users_active_day: i64,
17   pub users_active_week: i64,
18   pub users_active_month: i64,
19   pub users_active_half_year: i64,
20 }
21
22 impl SiteAggregates {
23   pub fn read(conn: &PgConnection) -> Result<Self, Error> {
24     site_aggregates::table.first::<Self>(conn)
25   }
26 }
27
28 #[cfg(test)]
29 mod tests {
30   use crate::{
31     aggregates::site_aggregates::SiteAggregates,
32     establish_unpooled_connection,
33     source::{
34       comment::{Comment, CommentForm},
35       community::{Community, CommunityForm},
36       person::{Person, PersonForm},
37       post::{Post, PostForm},
38       site::{Site, SiteForm},
39     },
40     traits::Crud,
41   };
42   use serial_test::serial;
43
44   #[test]
45   #[serial]
46   fn test_crud() {
47     let conn = establish_unpooled_connection();
48
49     let new_person = PersonForm {
50       name: "thommy_site_agg".into(),
51       ..PersonForm::default()
52     };
53
54     let inserted_person = Person::create(&conn, &new_person).unwrap();
55
56     let site_form = SiteForm {
57       name: "test_site".into(),
58       sidebar: None,
59       description: None,
60       icon: None,
61       banner: None,
62       enable_downvotes: None,
63       open_registration: None,
64       enable_nsfw: None,
65       updated: None,
66       community_creation_admin_only: Some(false),
67       require_email_verification: None,
68       require_application: None,
69       application_question: None,
70       private_instance: None,
71     };
72
73     Site::create(&conn, &site_form).unwrap();
74
75     let new_community = CommunityForm {
76       name: "TIL_site_agg".into(),
77       title: "nada".to_owned(),
78       ..CommunityForm::default()
79     };
80
81     let inserted_community = Community::create(&conn, &new_community).unwrap();
82
83     let new_post = PostForm {
84       name: "A test post".into(),
85       creator_id: inserted_person.id,
86       community_id: inserted_community.id,
87       ..PostForm::default()
88     };
89
90     // Insert two of those posts
91     let inserted_post = Post::create(&conn, &new_post).unwrap();
92     let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
93
94     let comment_form = CommentForm {
95       content: "A test comment".into(),
96       creator_id: inserted_person.id,
97       post_id: inserted_post.id,
98       ..CommentForm::default()
99     };
100
101     // Insert two of those comments
102     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
103
104     let child_comment_form = CommentForm {
105       content: "A test comment".into(),
106       creator_id: inserted_person.id,
107       post_id: inserted_post.id,
108       parent_id: Some(inserted_comment.id),
109       ..CommentForm::default()
110     };
111
112     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
113
114     let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
115
116     assert_eq!(1, site_aggregates_before_delete.users);
117     assert_eq!(1, site_aggregates_before_delete.communities);
118     assert_eq!(2, site_aggregates_before_delete.posts);
119     assert_eq!(2, site_aggregates_before_delete.comments);
120
121     // Try a post delete
122     Post::delete(&conn, inserted_post.id).unwrap();
123     let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
124     assert_eq!(1, site_aggregates_after_post_delete.posts);
125     assert_eq!(0, site_aggregates_after_post_delete.comments);
126
127     // This shouuld delete all the associated rows, and fire triggers
128     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
129     assert_eq!(1, person_num_deleted);
130
131     // Delete the community
132     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
133     assert_eq!(1, community_num_deleted);
134
135     // Site should still exist, it can without a site creator.
136     let after_delete_creator = SiteAggregates::read(&conn);
137     assert!(after_delete_creator.is_ok());
138
139     Site::delete(&conn, 1).unwrap();
140     let after_delete_site = SiteAggregates::read(&conn);
141     assert!(after_delete_site.is_err());
142   }
143 }