X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fdb_views%2Fsrc%2Fpost_view.rs;h=bba59ac690b9d945ca4d302609c9a8c9d6d47e35;hb=0aeb78b8f39f777cd5d1af292258897964e58f2a;hp=778cf98a3d4ddb158ed51e986e11027eb6784eee;hpb=f2537ba7db39b338617e07f36c5cd6b11f59795e;p=lemmy.git diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 778cf98a..bba59ac6 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -11,6 +11,7 @@ use lemmy_db_schema::{ local_user_language, person, person_block, + person_post_aggregates, post, post_aggregates, post_like, @@ -43,8 +44,11 @@ type PostViewTuple = ( Option, Option, Option, + i64, ); +sql_function!(fn coalesce(x: sql_types::Nullable, y: sql_types::BigInt) -> sql_types::BigInt); + impl PostView { pub fn read( conn: &mut PgConnection, @@ -64,6 +68,7 @@ impl PostView { read, creator_blocked, post_like, + unread_comments, ) = post::table .find(post_id) .inner_join(person::table) @@ -116,6 +121,13 @@ impl PostView { .and(post_like::person_id.eq(person_id_join)), ), ) + .left_join( + person_post_aggregates::table.on( + post::id + .eq(person_post_aggregates::post_id) + .and(person_post_aggregates::person_id.eq(person_id_join)), + ), + ) .select(( post::all_columns, Person::safe_columns_tuple(), @@ -127,6 +139,10 @@ impl PostView { post_read::all_columns.nullable(), person_block::all_columns.nullable(), post_like::score.nullable(), + coalesce( + post_aggregates::comments.nullable() - person_post_aggregates::read_comments.nullable(), + post_aggregates::comments, + ), )) .first::(conn)?; @@ -149,6 +165,7 @@ impl PostView { read: read.is_some(), creator_blocked: creator_blocked.is_some(), my_vote, + unread_comments, }) } } @@ -237,6 +254,13 @@ impl<'a> PostQuery<'a> { .and(post_like::person_id.eq(person_id_join)), ), ) + .left_join( + person_post_aggregates::table.on( + post::id + .eq(person_post_aggregates::post_id) + .and(person_post_aggregates::person_id.eq(person_id_join)), + ), + ) .left_join( local_user_language::table.on( post::language_id @@ -255,6 +279,10 @@ impl<'a> PostQuery<'a> { post_read::all_columns.nullable(), person_block::all_columns.nullable(), post_like::score.nullable(), + coalesce( + post_aggregates::comments.nullable() - person_post_aggregates::read_comments.nullable(), + post_aggregates::comments, + ), )) .into_boxed(); @@ -412,6 +440,7 @@ impl ViewToVec for PostView { read: a.7.is_some(), creator_blocked: a.8.is_some(), my_vote: a.9, + unread_comments: a.10, }) .collect::>() } @@ -806,6 +835,7 @@ mod tests { language_id: LanguageId(47), }, my_vote: None, + unread_comments: 0, creator: PersonSafe { id: inserted_person.id, name: inserted_person.name.clone(),