]> Untitled Git - lemmy.git/blobdiff - crates/db_views/src/post_view.rs
implement language tags for site/community in db and api (#2434)
[lemmy.git] / crates / db_views / src / post_view.rs
index 778cf98a3d4ddb158ed51e986e11027eb6784eee..9db1d095c9ef935d5bdb0740bed7be193c0488fb 100644 (file)
@@ -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<PostRead>,
   Option<PersonBlock>,
   Option<i16>,
+  i64,
 );
 
+sql_function!(fn coalesce(x: sql_types::Nullable<sql_types::BigInt>, 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::<PostViewTuple>(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::<Vec<Self>>()
   }
@@ -425,11 +454,11 @@ mod tests {
     aggregates::structs::PostAggregates,
     newtypes::LanguageId,
     source::{
+      actor_language::LocalUserLanguage,
       community::*,
       community_block::{CommunityBlock, CommunityBlockForm},
       language::Language,
       local_user::{LocalUser, LocalUserForm},
-      local_user_language::LocalUserLanguage,
       person::*,
       person_block::{PersonBlock, PersonBlockForm},
       post::*,
@@ -720,12 +749,7 @@ mod tests {
     assert_eq!(3, post_listings_all.len());
 
     let french_id = Language::read_id_from_code(conn, "fr").unwrap();
-    LocalUserLanguage::update_user_languages(
-      conn,
-      Some(vec![french_id]),
-      data.inserted_local_user.id,
-    )
-    .unwrap();
+    LocalUserLanguage::update(conn, vec![french_id], data.inserted_local_user.id).unwrap();
 
     let post_listing_french = PostQuery::builder()
       .conn(conn)
@@ -740,9 +764,9 @@ mod tests {
     assert_eq!(french_id, post_listing_french[0].post.language_id);
 
     let undetermined_id = Language::read_id_from_code(conn, "und").unwrap();
-    LocalUserLanguage::update_user_languages(
+    LocalUserLanguage::update(
       conn,
-      Some(vec![french_id, undetermined_id]),
+      vec![french_id, undetermined_id],
       data.inserted_local_user.id,
     )
     .unwrap();
@@ -806,6 +830,7 @@ mod tests {
         language_id: LanguageId(47),
       },
       my_vote: None,
+      unread_comments: 0,
       creator: PersonSafe {
         id: inserted_person.id,
         name: inserted_person.name.clone(),