From 01f56ea0a642f5433b8c3f6ce6c11bde65242bc0 Mon Sep 17 00:00:00 2001 From: hole-thu Date: Fri, 5 May 2023 22:06:19 +0800 Subject: [PATCH] support sort by n_attention --- .../down.sql | 2 ++ .../2023-05-05-135236_post_add_index_for_attentions/up.sql | 2 ++ src/cache.rs | 1 + src/models.rs | 7 ++++++- src/schema.rs | 6 +++++- 5 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 migrations/postgres/2023-05-05-135236_post_add_index_for_attentions/down.sql create mode 100644 migrations/postgres/2023-05-05-135236_post_add_index_for_attentions/up.sql diff --git a/migrations/postgres/2023-05-05-135236_post_add_index_for_attentions/down.sql b/migrations/postgres/2023-05-05-135236_post_add_index_for_attentions/down.sql new file mode 100644 index 0000000..ffd9ed4 --- /dev/null +++ b/migrations/postgres/2023-05-05-135236_post_add_index_for_attentions/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP INDEX posts_attention_idx diff --git a/migrations/postgres/2023-05-05-135236_post_add_index_for_attentions/up.sql b/migrations/postgres/2023-05-05-135236_post_add_index_for_attentions/up.sql new file mode 100644 index 0000000..9c18b9a --- /dev/null +++ b/migrations/postgres/2023-05-05-135236_post_add_index_for_attentions/up.sql @@ -0,0 +1,2 @@ +-- Your SQL goes here +CREATE INDEX posts_attention_idx ON posts (n_attentions) diff --git a/src/cache.rs b/src/cache.rs index 6d771d2..1def013 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -216,6 +216,7 @@ impl PostListCache { 1 => -p.last_comment_time.timestamp(), 2 => (-p.hot_score).into(), 3 => rand::thread_rng().gen_range(0..i64::MAX), + 4 => (-p.n_attentions).into(), _ => panic!("wrong mode"), }, p.id, diff --git a/src/models.rs b/src/models.rs index b9c948a..cd4ce7a 100644 --- a/src/models.rs +++ b/src/models.rs @@ -256,6 +256,10 @@ impl Post { query = query.filter(posts::is_reported.eq(false)); } + if order_mode == 1 { + query = query.filter(posts::n_comments.gt(0)); + } + if let Some(ri) = room_id { query = query.filter(posts::room_id.eq(ri)); } @@ -265,6 +269,7 @@ impl Post { 1 => query.order(posts::last_comment_time.desc()), 2 => query.order(posts::hot_score.desc()), 3 => query.order(RANDOM), + 4 => query.order(posts::n_attentions.desc()), _ => panic!("Wrong order mode!"), }; @@ -351,7 +356,7 @@ impl Post { pub async fn refresh_cache(&self, rconn: &RdsConn, is_new: bool) { join!( self.set_instance_cache(rconn), - future::join_all((if is_new { 0..4 } else { 1..4 }).map(|mode| async move { + future::join_all((if is_new { 0..5 } else { 1..5 }).map(|mode| async move { PostListCache::init(None, mode, &rconn.clone()) .put(self) .await; diff --git a/src/schema.rs b/src/schema.rs index c7a1a57..ee347e4 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -45,4 +45,8 @@ table! { joinable!(comments -> posts (post_id)); -allow_tables_to_appear_in_same_query!(comments, posts, users,); +allow_tables_to_appear_in_same_query!( + comments, + posts, + users, +);