CommentAggregatesNotInComment

This commit is contained in:
dull b 2023-07-28 19:25:36 +00:00
parent 1ee3fcaeab
commit 603aede7ce
6 changed files with 57 additions and 17 deletions

View file

@ -1,7 +1,8 @@
use crate::{ use crate::{
aggregates::structs::CommentAggregates, aggregates::structs::{CommentAggregates, CommentAggregatesNotInComment},
newtypes::CommentId, newtypes::CommentId,
schema::comment_aggregates, schema::comment_aggregates,
source::comment::Comment,
utils::{functions::hot_rank, get_conn, DbPool}, utils::{functions::hot_rank, get_conn, DbPool},
}; };
use diesel::{result::Error, ExpressionMethods, QueryDsl}; 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)] #[cfg(test)]
mod tests { mod tests {
#![allow(clippy::unwrap_used)] #![allow(clippy::unwrap_used)]

View file

@ -31,6 +31,21 @@ pub struct CommentAggregates {
pub controversy_rank: f64, 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)] #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable, TS))] #[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = community_aggregates))] #[cfg_attr(feature = "full", diesel(table_name = community_aggregates))]

View file

@ -8,10 +8,11 @@ use diesel::{
JoinOnDsl, JoinOnDsl,
NullableExpressionMethods, NullableExpressionMethods,
QueryDsl, QueryDsl,
SelectableHelper,
}; };
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::CommentAggregates, aggregates::structs::CommentAggregatesNotInComment,
aliases, aliases,
newtypes::{CommentReportId, CommunityId, PersonId}, newtypes::{CommentReportId, CommunityId, PersonId},
schema::{ schema::{
@ -70,7 +71,7 @@ fn queries<'a>() -> Queries<
community::all_columns, community::all_columns,
person::all_columns, person::all_columns,
aliases::person1.fields(person::all_columns), aliases::person1.fields(person::all_columns),
comment_aggregates::all_columns, CommentAggregatesNotInComment::as_select(),
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
comment_like::score.nullable(), comment_like::score.nullable(),
aliases::person2.fields(person::all_columns).nullable(), aliases::person2.fields(person::all_columns).nullable(),
@ -227,13 +228,14 @@ impl JoinView for CommentReportView {
Community, Community,
Person, Person,
Person, Person,
CommentAggregates, CommentAggregatesNotInComment,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
Option<i16>, Option<i16>,
Option<Person>, Option<Person>,
); );
fn from_tuple(a: Self::JoinTuple) -> Self { fn from_tuple(a: Self::JoinTuple) -> Self {
let counts = a.6.into_full(&a.1);
Self { Self {
comment_report: a.0, comment_report: a.0,
comment: a.1, comment: a.1,
@ -241,7 +243,7 @@ impl JoinView for CommentReportView {
community: a.3, community: a.3,
creator: a.4, creator: a.4,
comment_creator: a.5, comment_creator: a.5,
counts: a.6, counts,
creator_banned_from_community: a.7.is_some(), creator_banned_from_community: a.7.is_some(),
my_vote: a.8, my_vote: a.8,
resolver: a.9, resolver: a.9,

View file

@ -8,11 +8,12 @@ use diesel::{
NullableExpressionMethods, NullableExpressionMethods,
PgTextExpressionMethods, PgTextExpressionMethods,
QueryDsl, QueryDsl,
SelectableHelper,
}; };
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use diesel_ltree::{nlevel, subpath, Ltree, LtreeExtensions}; use diesel_ltree::{nlevel, subpath, Ltree, LtreeExtensions};
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::CommentAggregates, aggregates::structs::CommentAggregatesNotInComment,
newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId}, newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId},
schema::{ schema::{
comment, comment,
@ -46,7 +47,7 @@ type CommentViewTuple = (
Person, Person,
Post, Post,
Community, Community,
CommentAggregates, CommentAggregatesNotInComment,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
Option<CommunityFollower>, Option<CommunityFollower>,
Option<CommentSaved>, Option<CommentSaved>,
@ -108,7 +109,7 @@ fn queries<'a>() -> Queries<
person::all_columns, person::all_columns,
post::all_columns, post::all_columns,
community::all_columns, community::all_columns,
comment_aggregates::all_columns, CommentAggregatesNotInComment::as_select(),
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
comment_saved::all_columns.nullable(), comment_saved::all_columns.nullable(),
@ -326,12 +327,13 @@ impl<'a> CommentQuery<'a> {
impl JoinView for CommentView { impl JoinView for CommentView {
type JoinTuple = CommentViewTuple; type JoinTuple = CommentViewTuple;
fn from_tuple(a: Self::JoinTuple) -> Self { fn from_tuple(a: Self::JoinTuple) -> Self {
let counts = a.4.into_full(&a.0);
Self { Self {
comment: a.0, comment: a.0,
creator: a.1, creator: a.1,
post: a.2, post: a.2,
community: a.3, community: a.3,
counts: a.4, counts,
creator_banned_from_community: a.5.is_some(), creator_banned_from_community: a.5.is_some(),
subscribed: CommunityFollower::to_subscribed_type(&a.6), subscribed: CommunityFollower::to_subscribed_type(&a.6),
saved: a.7.is_some(), saved: a.7.is_some(),

View file

@ -7,10 +7,11 @@ use diesel::{
JoinOnDsl, JoinOnDsl,
NullableExpressionMethods, NullableExpressionMethods,
QueryDsl, QueryDsl,
SelectableHelper,
}; };
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::CommentAggregates, aggregates::structs::CommentAggregatesNotInComment,
aliases, aliases,
newtypes::{CommentReplyId, PersonId}, newtypes::{CommentReplyId, PersonId},
schema::{ schema::{
@ -46,7 +47,7 @@ type CommentReplyViewTuple = (
Post, Post,
Community, Community,
Person, Person,
CommentAggregates, CommentAggregatesNotInComment,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
Option<CommunityFollower>, Option<CommunityFollower>,
Option<CommentSaved>, Option<CommentSaved>,
@ -111,7 +112,7 @@ fn queries<'a>() -> Queries<
post::all_columns, post::all_columns,
community::all_columns, community::all_columns,
aliases::person1.fields(person::all_columns), aliases::person1.fields(person::all_columns),
comment_aggregates::all_columns, CommentAggregatesNotInComment::as_select(),
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
comment_saved::all_columns.nullable(), comment_saved::all_columns.nullable(),
@ -218,6 +219,7 @@ impl CommentReplyQuery {
impl JoinView for CommentReplyView { impl JoinView for CommentReplyView {
type JoinTuple = CommentReplyViewTuple; type JoinTuple = CommentReplyViewTuple;
fn from_tuple(a: Self::JoinTuple) -> Self { fn from_tuple(a: Self::JoinTuple) -> Self {
let counts = a.6.into_full(&a.1);
Self { Self {
comment_reply: a.0, comment_reply: a.0,
comment: a.1, comment: a.1,
@ -225,7 +227,7 @@ impl JoinView for CommentReplyView {
post: a.3, post: a.3,
community: a.4, community: a.4,
recipient: a.5, recipient: a.5,
counts: a.6, counts,
creator_banned_from_community: a.7.is_some(), creator_banned_from_community: a.7.is_some(),
subscribed: CommunityFollower::to_subscribed_type(&a.8), subscribed: CommunityFollower::to_subscribed_type(&a.8),
saved: a.9.is_some(), saved: a.9.is_some(),

View file

@ -8,10 +8,11 @@ use diesel::{
JoinOnDsl, JoinOnDsl,
NullableExpressionMethods, NullableExpressionMethods,
QueryDsl, QueryDsl,
SelectableHelper,
}; };
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::CommentAggregates, aggregates::structs::CommentAggregatesNotInComment,
aliases, aliases,
newtypes::{PersonId, PersonMentionId}, newtypes::{PersonId, PersonMentionId},
schema::{ schema::{
@ -47,7 +48,7 @@ type PersonMentionViewTuple = (
Post, Post,
Community, Community,
Person, Person,
CommentAggregates, CommentAggregatesNotInComment,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
Option<CommunityFollower>, Option<CommunityFollower>,
Option<CommentSaved>, Option<CommentSaved>,
@ -107,7 +108,7 @@ fn queries<'a>() -> Queries<
post::all_columns, post::all_columns,
community::all_columns, community::all_columns,
aliases::person1.fields(person::all_columns), aliases::person1.fields(person::all_columns),
comment_aggregates::all_columns, CommentAggregatesNotInComment::as_select(),
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
comment_saved::all_columns.nullable(), comment_saved::all_columns.nullable(),
@ -235,6 +236,7 @@ impl PersonMentionQuery {
impl JoinView for PersonMentionView { impl JoinView for PersonMentionView {
type JoinTuple = PersonMentionViewTuple; type JoinTuple = PersonMentionViewTuple;
fn from_tuple(a: Self::JoinTuple) -> Self { fn from_tuple(a: Self::JoinTuple) -> Self {
let counts = a.6.into_full(&a.1);
Self { Self {
person_mention: a.0, person_mention: a.0,
comment: a.1, comment: a.1,
@ -242,7 +244,7 @@ impl JoinView for PersonMentionView {
post: a.3, post: a.3,
community: a.4, community: a.4,
recipient: a.5, recipient: a.5,
counts: a.6, counts,
creator_banned_from_community: a.7.is_some(), creator_banned_from_community: a.7.is_some(),
subscribed: CommunityFollower::to_subscribed_type(&a.8), subscribed: CommunityFollower::to_subscribed_type(&a.8),
saved: a.9.is_some(), saved: a.9.is_some(),