diff --git a/crates/db_schema/src/aggregates/comment_aggregates.rs b/crates/db_schema/src/aggregates/comment_aggregates.rs index e081d1a1e..c8789924f 100644 --- a/crates/db_schema/src/aggregates/comment_aggregates.rs +++ b/crates/db_schema/src/aggregates/comment_aggregates.rs @@ -1,7 +1,8 @@ use crate::{ - aggregates::structs::CommentAggregates, + aggregates::structs::{CommentAggregates, CommentAggregatesNotInComment}, newtypes::CommentId, schema::comment_aggregates, + source::comment::Comment, utils::{functions::hot_rank, get_conn, DbPool}, }; use diesel::{result::Error, ExpressionMethods, QueryDsl}; @@ -33,6 +34,22 @@ impl CommentAggregates { } } +impl CommentAggregatesNotInComment { + pub fn into_full(self, comment: &Comment) -> CommentAggregates { + CommentAggregates { + id: self.id, + score: self.score, + upvotes: self.upvotes, + downvotes: self.downvotes, + child_count: self.child_count, + hot_rank: self.hot_rank, + controversy_rank: self.controversy_rank, + comment_id: comment.id, + published: comment.published, + } + } +} + #[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 0a87447ec..f7d949fcd 100644 --- a/crates/db_schema/src/aggregates/structs.rs +++ b/crates/db_schema/src/aggregates/structs.rs @@ -31,6 +31,21 @@ pub struct CommentAggregates { pub controversy_rank: f64, } +#[derive(Debug)] +#[cfg_attr(feature = "full", derive(Queryable, Selectable))] +#[cfg_attr(feature = "full", diesel(table_name = comment_aggregates))] +/// Data that is in `CommentAggregates` and not in `Comment` +pub struct CommentAggregatesNotInComment { + pub id: i32, + pub score: i64, + pub upvotes: i64, + pub downvotes: i64, + /// The total number of children in this comment branch. + pub child_count: i32, + pub hot_rank: i32, + pub controversy_rank: f64, +} + #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)] #[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable, TS))] #[cfg_attr(feature = "full", diesel(table_name = community_aggregates))] diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index a92b38063..3ee3934b5 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -8,10 +8,11 @@ use diesel::{ JoinOnDsl, NullableExpressionMethods, QueryDsl, + SelectableHelper, }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aggregates::structs::CommentAggregates, + aggregates::structs::CommentAggregatesNotInComment, aliases, newtypes::{CommentReportId, CommunityId, PersonId}, schema::{ @@ -70,7 +71,7 @@ fn queries<'a>() -> Queries< community::all_columns, person::all_columns, aliases::person1.fields(person::all_columns), - comment_aggregates::all_columns, + CommentAggregatesNotInComment::as_select(), community_person_ban::all_columns.nullable(), comment_like::score.nullable(), aliases::person2.fields(person::all_columns).nullable(), @@ -227,13 +228,14 @@ impl JoinView for CommentReportView { Community, Person, Person, - CommentAggregates, + CommentAggregatesNotInComment, Option, Option, Option, ); fn from_tuple(a: Self::JoinTuple) -> Self { + let counts = a.6.into_full(&a.1); Self { comment_report: a.0, comment: a.1, @@ -241,7 +243,7 @@ impl JoinView for CommentReportView { community: a.3, creator: a.4, comment_creator: a.5, - counts: a.6, + counts, creator_banned_from_community: a.7.is_some(), my_vote: a.8, resolver: a.9, diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 26787cceb..f126bfd2a 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -8,11 +8,12 @@ use diesel::{ NullableExpressionMethods, PgTextExpressionMethods, QueryDsl, + SelectableHelper, }; use diesel_async::RunQueryDsl; use diesel_ltree::{nlevel, subpath, Ltree, LtreeExtensions}; use lemmy_db_schema::{ - aggregates::structs::CommentAggregates, + aggregates::structs::CommentAggregatesNotInComment, newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId}, schema::{ comment, @@ -46,7 +47,7 @@ type CommentViewTuple = ( Person, Post, Community, - CommentAggregates, + CommentAggregatesNotInComment, Option, Option, Option, @@ -108,7 +109,7 @@ fn queries<'a>() -> Queries< person::all_columns, post::all_columns, community::all_columns, - comment_aggregates::all_columns, + CommentAggregatesNotInComment::as_select(), community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), comment_saved::all_columns.nullable(), @@ -326,12 +327,13 @@ impl<'a> CommentQuery<'a> { impl JoinView for CommentView { type JoinTuple = CommentViewTuple; fn from_tuple(a: Self::JoinTuple) -> Self { + let counts = a.4.into_full(&a.0); Self { comment: a.0, creator: a.1, post: a.2, community: a.3, - counts: a.4, + counts, creator_banned_from_community: a.5.is_some(), subscribed: CommunityFollower::to_subscribed_type(&a.6), saved: a.7.is_some(), diff --git a/crates/db_views_actor/src/comment_reply_view.rs b/crates/db_views_actor/src/comment_reply_view.rs index 869345200..3c2ce07fc 100644 --- a/crates/db_views_actor/src/comment_reply_view.rs +++ b/crates/db_views_actor/src/comment_reply_view.rs @@ -7,10 +7,11 @@ use diesel::{ JoinOnDsl, NullableExpressionMethods, QueryDsl, + SelectableHelper, }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aggregates::structs::CommentAggregates, + aggregates::structs::CommentAggregatesNotInComment, aliases, newtypes::{CommentReplyId, PersonId}, schema::{ @@ -46,7 +47,7 @@ type CommentReplyViewTuple = ( Post, Community, Person, - CommentAggregates, + CommentAggregatesNotInComment, Option, Option, Option, @@ -111,7 +112,7 @@ fn queries<'a>() -> Queries< post::all_columns, community::all_columns, aliases::person1.fields(person::all_columns), - comment_aggregates::all_columns, + CommentAggregatesNotInComment::as_select(), community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), comment_saved::all_columns.nullable(), @@ -218,6 +219,7 @@ impl CommentReplyQuery { impl JoinView for CommentReplyView { type JoinTuple = CommentReplyViewTuple; fn from_tuple(a: Self::JoinTuple) -> Self { + let counts = a.6.into_full(&a.1); Self { comment_reply: a.0, comment: a.1, @@ -225,7 +227,7 @@ impl JoinView for CommentReplyView { post: a.3, community: a.4, recipient: a.5, - counts: a.6, + counts, creator_banned_from_community: a.7.is_some(), subscribed: CommunityFollower::to_subscribed_type(&a.8), saved: a.9.is_some(), diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index 6528ab5da..08dc9c092 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -8,10 +8,11 @@ use diesel::{ JoinOnDsl, NullableExpressionMethods, QueryDsl, + SelectableHelper, }; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - aggregates::structs::CommentAggregates, + aggregates::structs::CommentAggregatesNotInComment, aliases, newtypes::{PersonId, PersonMentionId}, schema::{ @@ -47,7 +48,7 @@ type PersonMentionViewTuple = ( Post, Community, Person, - CommentAggregates, + CommentAggregatesNotInComment, Option, Option, Option, @@ -107,7 +108,7 @@ fn queries<'a>() -> Queries< post::all_columns, community::all_columns, aliases::person1.fields(person::all_columns), - comment_aggregates::all_columns, + CommentAggregatesNotInComment::as_select(), community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), comment_saved::all_columns.nullable(), @@ -235,6 +236,7 @@ impl PersonMentionQuery { impl JoinView for PersonMentionView { type JoinTuple = PersonMentionViewTuple; fn from_tuple(a: Self::JoinTuple) -> Self { + let counts = a.6.into_full(&a.1); Self { person_mention: a.0, comment: a.1, @@ -242,7 +244,7 @@ impl JoinView for PersonMentionView { post: a.3, community: a.4, recipient: a.5, - counts: a.6, + counts, creator_banned_from_community: a.7.is_some(), subscribed: CommunityFollower::to_subscribed_type(&a.8), saved: a.9.is_some(),