PostAggregatesNotInPost

This commit is contained in:
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::{
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)]

View file

@ -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))]

View file

@ -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<CommunityPersonBan>,
Option<i16>,
PostAggregates,
PostAggregatesNotInPost,
Option<Person>,
);
@ -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,
}
}

View file

@ -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<CommunityPersonBan>,
PostAggregates,
PostAggregatesNotInPost,
Option<CommunityFollower>,
Option<PostSaved>,
Option<PostRead>,
@ -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(),