]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/structs.rs
make shebangs posix compliant (#2974)
[lemmy.git] / crates / db_schema / src / aggregates / structs.rs
1 use crate::newtypes::{CommentId, CommunityId, PersonId, PostId, SiteId};
2 #[cfg(feature = "full")]
3 use crate::schema::{
4   comment_aggregates,
5   community_aggregates,
6   person_aggregates,
7   person_post_aggregates,
8   post_aggregates,
9   site_aggregates,
10 };
11 use serde::{Deserialize, Serialize};
12 #[cfg(feature = "full")]
13 use ts_rs::TS;
14
15 #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
16 #[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable, TS))]
17 #[cfg_attr(feature = "full", diesel(table_name = comment_aggregates))]
18 #[cfg_attr(feature = "full", diesel(belongs_to(crate::source::comment::Comment)))]
19 #[cfg_attr(feature = "full", ts(export))]
20 /// Aggregate data for a comment.
21 pub struct CommentAggregates {
22   pub id: i32,
23   pub comment_id: CommentId,
24   pub score: i64,
25   pub upvotes: i64,
26   pub downvotes: i64,
27   pub published: chrono::NaiveDateTime,
28   /// The total number of children in this comment branch.
29   pub child_count: i32,
30 }
31
32 #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
33 #[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable, TS))]
34 #[cfg_attr(feature = "full", diesel(table_name = community_aggregates))]
35 #[cfg_attr(
36   feature = "full",
37   diesel(belongs_to(crate::source::community::Community))
38 )]
39 #[cfg_attr(feature = "full", ts(export))]
40 /// Aggregate data for a community.
41 pub struct CommunityAggregates {
42   pub id: i32,
43   pub community_id: CommunityId,
44   pub subscribers: i64,
45   pub posts: i64,
46   pub comments: i64,
47   pub published: chrono::NaiveDateTime,
48   /// The number of users with any activity in the last day.
49   pub users_active_day: i64,
50   /// The number of users with any activity in the last week.
51   pub users_active_week: i64,
52   /// The number of users with any activity in the last month.
53   pub users_active_month: i64,
54   /// The number of users with any activity in the last year.
55   pub users_active_half_year: i64,
56 }
57
58 #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone, Default)]
59 #[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable, TS))]
60 #[cfg_attr(feature = "full", diesel(table_name = person_aggregates))]
61 #[cfg_attr(feature = "full", diesel(belongs_to(crate::source::person::Person)))]
62 #[cfg_attr(feature = "full", ts(export))]
63 /// Aggregate data for a person.
64 pub struct PersonAggregates {
65   pub id: i32,
66   pub person_id: PersonId,
67   pub post_count: i64,
68   pub post_score: i64,
69   pub comment_count: i64,
70   pub comment_score: i64,
71 }
72
73 #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
74 #[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable, TS))]
75 #[cfg_attr(feature = "full", diesel(table_name = post_aggregates))]
76 #[cfg_attr(feature = "full", diesel(belongs_to(crate::source::post::Post)))]
77 #[cfg_attr(feature = "full", ts(export))]
78 /// Aggregate data for a post.
79 pub struct PostAggregates {
80   pub id: i32,
81   pub post_id: PostId,
82   pub comments: i64,
83   pub score: i64,
84   pub upvotes: i64,
85   pub downvotes: i64,
86   pub published: chrono::NaiveDateTime,
87   /// A newest comment time, limited to 2 days, to prevent necrobumping  
88   pub newest_comment_time_necro: chrono::NaiveDateTime,
89   /// The time of the newest comment in the post.
90   pub newest_comment_time: chrono::NaiveDateTime,
91   /// If the post is featured on the community.
92   pub featured_community: bool,
93   /// If the post is featured on the site / to local.
94   pub featured_local: bool,
95 }
96
97 #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
98 #[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable))]
99 #[cfg_attr(feature = "full", diesel(table_name = person_post_aggregates))]
100 #[cfg_attr(feature = "full", diesel(belongs_to(crate::source::person::Person)))]
101 /// Aggregate data for a person's post.
102 pub struct PersonPostAggregates {
103   pub id: i32,
104   pub person_id: PersonId,
105   pub post_id: PostId,
106   /// The number of comments they've read on that post.
107   ///
108   /// This is updated to the current post comment count every time they view a post.
109   pub read_comments: i64,
110   pub published: chrono::NaiveDateTime,
111 }
112
113 #[derive(Clone, Default)]
114 #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
115 #[cfg_attr(feature = "full", diesel(table_name = person_post_aggregates))]
116 pub struct PersonPostAggregatesForm {
117   pub person_id: PersonId,
118   pub post_id: PostId,
119   pub read_comments: i64,
120   pub published: Option<chrono::NaiveDateTime>,
121 }
122
123 #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
124 #[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable, TS))]
125 #[cfg_attr(feature = "full", diesel(table_name = site_aggregates))]
126 #[cfg_attr(feature = "full", diesel(belongs_to(crate::source::site::Site)))]
127 #[cfg_attr(feature = "full", ts(export))]
128 /// Aggregate data for a site.
129 pub struct SiteAggregates {
130   pub id: i32,
131   pub site_id: SiteId,
132   pub users: i64,
133   pub posts: i64,
134   pub comments: i64,
135   pub communities: i64,
136   /// The number of users with any activity in the last day.
137   pub users_active_day: i64,
138   /// The number of users with any activity in the last week.
139   pub users_active_week: i64,
140   /// The number of users with any activity in the last month.
141   pub users_active_month: i64,
142   /// The number of users with any activity in the last half year.
143   pub users_active_half_year: i64,
144 }