PostAggregatesNotInPost

pull/3755/head
dull b 2023-07-28 16:57:56 +00:00
parent 543bd99900
commit 1ee3fcaeab
4 changed files with 56 additions and 9 deletions

View File

@ -1,7 +1,8 @@
use crate::{ use crate::{
aggregates::structs::PostAggregates, aggregates::structs::{PostAggregates, PostAggregatesNotInPost},
newtypes::PostId, newtypes::PostId,
schema::post_aggregates, schema::post_aggregates,
source::post::Post,
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,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)] #[cfg(test)]
mod tests { mod tests {
#![allow(clippy::unwrap_used)] #![allow(clippy::unwrap_used)]

View File

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

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::PostAggregates, aggregates::structs::PostAggregatesNotInPost,
aliases, aliases,
newtypes::{CommunityId, PersonId, PostReportId}, newtypes::{CommunityId, PersonId, PostReportId},
schema::{ schema::{
@ -41,7 +42,7 @@ type PostReportViewTuple = (
Person, Person,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
Option<i16>, Option<i16>,
PostAggregates, PostAggregatesNotInPost,
Option<Person>, Option<Person>,
); );
@ -82,7 +83,7 @@ fn queries<'a>() -> Queries<
aliases::person1.fields(person::all_columns), aliases::person1.fields(person::all_columns),
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
post_like::score.nullable(), post_like::score.nullable(),
post_aggregates::all_columns, PostAggregatesNotInPost::as_select(),
aliases::person2.fields(person::all_columns.nullable()), aliases::person2.fields(person::all_columns.nullable()),
)) ))
}; };
@ -207,6 +208,7 @@ impl PostReportQuery {
impl JoinView for PostReportView { impl JoinView for PostReportView {
type JoinTuple = PostReportViewTuple; type JoinTuple = PostReportViewTuple;
fn from_tuple(a: Self::JoinTuple) -> Self { fn from_tuple(a: Self::JoinTuple) -> Self {
let counts = a.7.into_full(&a.1);
Self { Self {
post_report: a.0, post_report: a.0,
post: a.1, post: a.1,
@ -215,7 +217,7 @@ impl JoinView for PostReportView {
post_creator: a.4, post_creator: a.4,
creator_banned_from_community: a.5.is_some(), creator_banned_from_community: a.5.is_some(),
my_vote: a.6, my_vote: a.6,
counts: a.7, counts,
resolver: a.8, resolver: a.8,
} }
} }

View File

@ -12,10 +12,11 @@ use diesel::{
NullableExpressionMethods, NullableExpressionMethods,
PgTextExpressionMethods, PgTextExpressionMethods,
QueryDsl, QueryDsl,
SelectableHelper,
}; };
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::PostAggregates, aggregates::structs::PostAggregatesNotInPost,
newtypes::{CommunityId, LocalUserId, PersonId, PostId}, newtypes::{CommunityId, LocalUserId, PersonId, PostId},
schema::{ schema::{
community, community,
@ -51,7 +52,7 @@ type PostViewTuple = (
Person, Person,
Community, Community,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
PostAggregates, PostAggregatesNotInPost,
Option<CommunityFollower>, Option<CommunityFollower>,
Option<PostSaved>, Option<PostSaved>,
Option<PostRead>, Option<PostRead>,
@ -137,7 +138,7 @@ fn queries<'a>() -> Queries<
person::all_columns, person::all_columns,
community::all_columns, community::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
post_aggregates::all_columns, PostAggregatesNotInPost::as_select(),
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
post_saved::all_columns.nullable(), post_saved::all_columns.nullable(),
post_read::all_columns.nullable(), post_read::all_columns.nullable(),
@ -439,12 +440,13 @@ impl<'a> PostQuery<'a> {
impl JoinView for PostView { impl JoinView for PostView {
type JoinTuple = PostViewTuple; type JoinTuple = PostViewTuple;
fn from_tuple(a: Self::JoinTuple) -> Self { fn from_tuple(a: Self::JoinTuple) -> Self {
let counts = a.4.into_full(&a.0);
Self { Self {
post: a.0, post: a.0,
creator: a.1, creator: a.1,
community: a.2, community: a.2,
creator_banned_from_community: a.3.is_some(), creator_banned_from_community: a.3.is_some(),
counts: a.4, counts,
subscribed: CommunityFollower::to_subscribed_type(&a.5), subscribed: CommunityFollower::to_subscribed_type(&a.5),
saved: a.6.is_some(), saved: a.6.is_some(),
read: a.7.is_some(), read: a.7.is_some(),