]> Untitled Git - lemmy.git/commitdiff
Calculate initial hot_rank and hot_rank_active for posts and comments from other...
authorSander Saarend <sander@saarend.com>
Tue, 20 Jun 2023 08:52:51 +0000 (11:52 +0300)
committerGitHub <noreply@github.com>
Tue, 20 Jun 2023 08:52:51 +0000 (10:52 +0200)
* Calculate initial hot_rank when receiving posts and comments from other instances

* Move hot rank update logic into db_schema

crates/apub/src/activities/create_or_update/comment.rs
crates/apub/src/activities/create_or_update/post.rs
crates/db_schema/src/aggregates/comment_aggregates.rs
crates/db_schema/src/aggregates/post_aggregates.rs

index 814ec0f87dc68e3ca1eb05dda61a1e591dd04522..e9e5673355510f1fb4b08c5c759627f651ceb95a 100644 (file)
@@ -30,6 +30,7 @@ use lemmy_api_common::{
   utils::{check_post_deleted_or_removed, is_mod_or_admin},
 };
 use lemmy_db_schema::{
+  aggregates::structs::CommentAggregates,
   newtypes::PersonId,
   source::{
     comment::{Comment, CommentLike, CommentLikeForm},
@@ -191,6 +192,9 @@ impl ActivityHandler for CreateOrUpdateNote {
     };
     CommentLike::like(context.pool(), &like_form).await?;
 
+    // Calculate initial hot_rank
+    CommentAggregates::update_hot_rank(context.pool(), comment.id).await?;
+
     let do_send_email = self.kind == CreateOrUpdateType::Create;
     let post_id = comment.post_id;
     let post = Post::read(context.pool(), post_id).await?;
index f2923bfbdcbb0d4d125a2ecef33b1345a8b1d301..916111fc5488fcd3b682636ea09bf4f8a340635d 100644 (file)
@@ -27,6 +27,7 @@ use lemmy_api_common::{
   post::{CreatePost, EditPost, PostResponse},
 };
 use lemmy_db_schema::{
+  aggregates::structs::PostAggregates,
   newtypes::PersonId,
   source::{
     community::Community,
@@ -187,6 +188,10 @@ impl ActivityHandler for CreateOrUpdatePage {
       score: 1,
     };
     PostLike::like(context.pool(), &like_form).await?;
+
+    // Calculate initial hot_rank for post
+    PostAggregates::update_hot_rank(context.pool(), post.id).await?;
+
     Ok(())
   }
 }
index f56fd8cbe50b6777aef32bfee83bf764a79ed78e..5a5550a2ec2920f83049946a7cd9e9ce2d0d18c1 100644 (file)
@@ -2,7 +2,7 @@ use crate::{
   aggregates::structs::CommentAggregates,
   newtypes::CommentId,
   schema::comment_aggregates,
-  utils::{get_conn, DbPool},
+  utils::{functions::hot_rank, get_conn, DbPool},
 };
 use diesel::{result::Error, ExpressionMethods, QueryDsl};
 use diesel_async::RunQueryDsl;
@@ -15,6 +15,19 @@ impl CommentAggregates {
       .first::<Self>(conn)
       .await
   }
+
+  pub async fn update_hot_rank(pool: &DbPool, comment_id: CommentId) -> Result<Self, Error> {
+    let conn = &mut get_conn(pool).await?;
+
+    diesel::update(comment_aggregates::table)
+      .filter(comment_aggregates::comment_id.eq(comment_id))
+      .set(comment_aggregates::hot_rank.eq(hot_rank(
+        comment_aggregates::score,
+        comment_aggregates::published,
+      )))
+      .get_result::<Self>(conn)
+      .await
+  }
 }
 
 #[cfg(test)]
index 848f2014b3cb02f3b76af39534f7a20078e445f7..176f694a7038fd61c805ce21a6c34d985371cd65 100644 (file)
@@ -2,7 +2,7 @@ use crate::{
   aggregates::structs::PostAggregates,
   newtypes::PostId,
   schema::post_aggregates,
-  utils::{get_conn, DbPool},
+  utils::{functions::hot_rank, get_conn, DbPool},
 };
 use diesel::{result::Error, ExpressionMethods, QueryDsl};
 use diesel_async::RunQueryDsl;
@@ -15,6 +15,22 @@ impl PostAggregates {
       .first::<Self>(conn)
       .await
   }
+
+  pub async fn update_hot_rank(pool: &DbPool, post_id: PostId) -> Result<Self, Error> {
+    let conn = &mut get_conn(pool).await?;
+
+    diesel::update(post_aggregates::table)
+      .filter(post_aggregates::post_id.eq(post_id))
+      .set((
+        post_aggregates::hot_rank.eq(hot_rank(post_aggregates::score, post_aggregates::published)),
+        post_aggregates::hot_rank_active.eq(hot_rank(
+          post_aggregates::score,
+          post_aggregates::newest_comment_time_necro,
+        )),
+      ))
+      .get_result::<Self>(conn)
+      .await
+  }
 }
 
 #[cfg(test)]