From 1ee3fcaeab8705e4e0e849ae6b93b45716aa9cc0 Mon Sep 17 00:00:00 2001 From: dull b Date: Fri, 28 Jul 2023 16:57:56 +0000 Subject: [PATCH] PostAggregatesNotInPost --- .../src/aggregates/post_aggregates.rs | 26 ++++++++++++++++++- crates/db_schema/src/aggregates/structs.rs | 19 ++++++++++++++ crates/db_views/src/post_report_view.rs | 10 ++++--- crates/db_views/src/post_view.rs | 10 ++++--- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/crates/db_schema/src/aggregates/post_aggregates.rs b/crates/db_schema/src/aggregates/post_aggregates.rs index 020370825..09353cc57 100644 --- a/crates/db_schema/src/aggregates/post_aggregates.rs +++ b/crates/db_schema/src/aggregates/post_aggregates.rs @@ -1,7 +1,8 @@ use crate::{ - aggregates::structs::PostAggregates, + aggregates::structs::{PostAggregates, PostAggregatesNotInPost}, newtypes::PostId, schema::post_aggregates, + source::post::Post, utils::{functions::hot_rank, get_conn, DbPool}, }; use diesel::{result::Error, ExpressionMethods, QueryDsl}; @@ -33,6 +34,29 @@ impl PostAggregates { } } +impl PostAggregatesNotInPost { + pub fn into_full(self, post: &Post) -> PostAggregates { + PostAggregates { + id: self.id, + comments: self.comments, + score: self.score, + upvotes: self.upvotes, + downvotes: self.downvotes, + newest_comment_time_necro: self.newest_comment_time_necro, + newest_comment_time: self.newest_comment_time, + hot_rank: self.hot_rank, + hot_rank_active: self.hot_rank_active, + controversy_rank: self.controversy_rank, + post_id: post.id, + published: post.published, + featured_community: post.featured_community, + featured_local: post.featured_local, + community_id: post.community_id, + creator_id: post.creator_id, + } + } +} + #[cfg(test)] mod tests { #![allow(clippy::unwrap_used)] diff --git a/crates/db_schema/src/aggregates/structs.rs b/crates/db_schema/src/aggregates/structs.rs index 3b3612bb7..0a87447ec 100644 --- a/crates/db_schema/src/aggregates/structs.rs +++ b/crates/db_schema/src/aggregates/structs.rs @@ -102,6 +102,25 @@ pub struct PostAggregates { pub controversy_rank: f64, } +#[derive(Debug)] +#[cfg_attr(feature = "full", derive(Queryable, Selectable))] +#[cfg_attr(feature = "full", diesel(table_name = post_aggregates))] +/// Data that is in `PostAggregates` and not in `Post` +pub struct PostAggregatesNotInPost { + pub id: i32, + pub comments: i64, + pub score: i64, + pub upvotes: i64, + pub downvotes: i64, + /// A newest comment time, limited to 2 days, to prevent necrobumping + pub newest_comment_time_necro: chrono::NaiveDateTime, + /// The time of the newest comment in the post. + pub newest_comment_time: chrono::NaiveDateTime, + pub hot_rank: i32, + pub hot_rank_active: i32, + pub controversy_rank: f64, +} + #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)] #[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable))] #[cfg_attr(feature = "full", diesel(table_name = person_post_aggregates))] diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index 4ef6067fd..2d31fd0b2 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -7,10 +7,11 @@ use diesel::{ JoinOnDsl, NullableExpressionMethods, QueryDsl, + SelectableHelper, }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aggregates::structs::PostAggregates, + aggregates::structs::PostAggregatesNotInPost, aliases, newtypes::{CommunityId, PersonId, PostReportId}, schema::{ @@ -41,7 +42,7 @@ type PostReportViewTuple = ( Person, Option, Option, - PostAggregates, + PostAggregatesNotInPost, Option, ); @@ -82,7 +83,7 @@ fn queries<'a>() -> Queries< aliases::person1.fields(person::all_columns), community_person_ban::all_columns.nullable(), post_like::score.nullable(), - post_aggregates::all_columns, + PostAggregatesNotInPost::as_select(), aliases::person2.fields(person::all_columns.nullable()), )) }; @@ -207,6 +208,7 @@ impl PostReportQuery { impl JoinView for PostReportView { type JoinTuple = PostReportViewTuple; fn from_tuple(a: Self::JoinTuple) -> Self { + let counts = a.7.into_full(&a.1); Self { post_report: a.0, post: a.1, @@ -215,7 +217,7 @@ impl JoinView for PostReportView { post_creator: a.4, creator_banned_from_community: a.5.is_some(), my_vote: a.6, - counts: a.7, + counts, resolver: a.8, } } diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 9f6d0735f..519463265 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -12,10 +12,11 @@ use diesel::{ NullableExpressionMethods, PgTextExpressionMethods, QueryDsl, + SelectableHelper, }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aggregates::structs::PostAggregates, + aggregates::structs::PostAggregatesNotInPost, newtypes::{CommunityId, LocalUserId, PersonId, PostId}, schema::{ community, @@ -51,7 +52,7 @@ type PostViewTuple = ( Person, Community, Option, - PostAggregates, + PostAggregatesNotInPost, Option, Option, Option, @@ -137,7 +138,7 @@ fn queries<'a>() -> Queries< person::all_columns, community::all_columns, community_person_ban::all_columns.nullable(), - post_aggregates::all_columns, + PostAggregatesNotInPost::as_select(), community_follower::all_columns.nullable(), post_saved::all_columns.nullable(), post_read::all_columns.nullable(), @@ -439,12 +440,13 @@ impl<'a> PostQuery<'a> { impl JoinView for PostView { type JoinTuple = PostViewTuple; fn from_tuple(a: Self::JoinTuple) -> Self { + let counts = a.4.into_full(&a.0); Self { post: a.0, creator: a.1, community: a.2, creator_banned_from_community: a.3.is_some(), - counts: a.4, + counts, subscribed: CommunityFollower::to_subscribed_type(&a.5), saved: a.6.is_some(), read: a.7.is_some(),