mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-22 20:31:19 +00:00
Calculate initial hot_rank and hot_rank_active for posts and comments from other instances (#3131)
* Calculate initial hot_rank when receiving posts and comments from other instances * Move hot rank update logic into db_schema
This commit is contained in:
parent
d97ff65fe1
commit
e23621c2cd
4 changed files with 40 additions and 2 deletions
|
@ -30,6 +30,7 @@ use lemmy_api_common::{
|
||||||
utils::{check_post_deleted_or_removed, is_mod_or_admin},
|
utils::{check_post_deleted_or_removed, is_mod_or_admin},
|
||||||
};
|
};
|
||||||
use lemmy_db_schema::{
|
use lemmy_db_schema::{
|
||||||
|
aggregates::structs::CommentAggregates,
|
||||||
newtypes::PersonId,
|
newtypes::PersonId,
|
||||||
source::{
|
source::{
|
||||||
comment::{Comment, CommentLike, CommentLikeForm},
|
comment::{Comment, CommentLike, CommentLikeForm},
|
||||||
|
@ -191,6 +192,9 @@ impl ActivityHandler for CreateOrUpdateNote {
|
||||||
};
|
};
|
||||||
CommentLike::like(context.pool(), &like_form).await?;
|
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 do_send_email = self.kind == CreateOrUpdateType::Create;
|
||||||
let post_id = comment.post_id;
|
let post_id = comment.post_id;
|
||||||
let post = Post::read(context.pool(), post_id).await?;
|
let post = Post::read(context.pool(), post_id).await?;
|
||||||
|
|
|
@ -27,6 +27,7 @@ use lemmy_api_common::{
|
||||||
post::{CreatePost, EditPost, PostResponse},
|
post::{CreatePost, EditPost, PostResponse},
|
||||||
};
|
};
|
||||||
use lemmy_db_schema::{
|
use lemmy_db_schema::{
|
||||||
|
aggregates::structs::PostAggregates,
|
||||||
newtypes::PersonId,
|
newtypes::PersonId,
|
||||||
source::{
|
source::{
|
||||||
community::Community,
|
community::Community,
|
||||||
|
@ -187,6 +188,10 @@ impl ActivityHandler for CreateOrUpdatePage {
|
||||||
score: 1,
|
score: 1,
|
||||||
};
|
};
|
||||||
PostLike::like(context.pool(), &like_form).await?;
|
PostLike::like(context.pool(), &like_form).await?;
|
||||||
|
|
||||||
|
// Calculate initial hot_rank for post
|
||||||
|
PostAggregates::update_hot_rank(context.pool(), post.id).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use crate::{
|
||||||
aggregates::structs::CommentAggregates,
|
aggregates::structs::CommentAggregates,
|
||||||
newtypes::CommentId,
|
newtypes::CommentId,
|
||||||
schema::comment_aggregates,
|
schema::comment_aggregates,
|
||||||
utils::{get_conn, DbPool},
|
utils::{functions::hot_rank, get_conn, DbPool},
|
||||||
};
|
};
|
||||||
use diesel::{result::Error, ExpressionMethods, QueryDsl};
|
use diesel::{result::Error, ExpressionMethods, QueryDsl};
|
||||||
use diesel_async::RunQueryDsl;
|
use diesel_async::RunQueryDsl;
|
||||||
|
@ -15,6 +15,19 @@ impl CommentAggregates {
|
||||||
.first::<Self>(conn)
|
.first::<Self>(conn)
|
||||||
.await
|
.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)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -2,7 +2,7 @@ use crate::{
|
||||||
aggregates::structs::PostAggregates,
|
aggregates::structs::PostAggregates,
|
||||||
newtypes::PostId,
|
newtypes::PostId,
|
||||||
schema::post_aggregates,
|
schema::post_aggregates,
|
||||||
utils::{get_conn, DbPool},
|
utils::{functions::hot_rank, get_conn, DbPool},
|
||||||
};
|
};
|
||||||
use diesel::{result::Error, ExpressionMethods, QueryDsl};
|
use diesel::{result::Error, ExpressionMethods, QueryDsl};
|
||||||
use diesel_async::RunQueryDsl;
|
use diesel_async::RunQueryDsl;
|
||||||
|
@ -15,6 +15,22 @@ impl PostAggregates {
|
||||||
.first::<Self>(conn)
|
.first::<Self>(conn)
|
||||||
.await
|
.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)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in a new issue