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