Browse Source

support sort by n_attention

master
hole-thu 2 years ago
parent
commit
01f56ea0a6
  1. 2
      migrations/postgres/2023-05-05-135236_post_add_index_for_attentions/down.sql
  2. 2
      migrations/postgres/2023-05-05-135236_post_add_index_for_attentions/up.sql
  3. 1
      src/cache.rs
  4. 7
      src/models.rs
  5. 6
      src/schema.rs

2
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

2
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)

1
src/cache.rs

@ -216,6 +216,7 @@ impl PostListCache {
1 => -p.last_comment_time.timestamp(), 1 => -p.last_comment_time.timestamp(),
2 => (-p.hot_score).into(), 2 => (-p.hot_score).into(),
3 => rand::thread_rng().gen_range(0..i64::MAX), 3 => rand::thread_rng().gen_range(0..i64::MAX),
4 => (-p.n_attentions).into(),
_ => panic!("wrong mode"), _ => panic!("wrong mode"),
}, },
p.id, p.id,

7
src/models.rs

@ -256,6 +256,10 @@ impl Post {
query = query.filter(posts::is_reported.eq(false)); 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 { if let Some(ri) = room_id {
query = query.filter(posts::room_id.eq(ri)); query = query.filter(posts::room_id.eq(ri));
} }
@ -265,6 +269,7 @@ impl Post {
1 => query.order(posts::last_comment_time.desc()), 1 => query.order(posts::last_comment_time.desc()),
2 => query.order(posts::hot_score.desc()), 2 => query.order(posts::hot_score.desc()),
3 => query.order(RANDOM), 3 => query.order(RANDOM),
4 => query.order(posts::n_attentions.desc()),
_ => panic!("Wrong order mode!"), _ => panic!("Wrong order mode!"),
}; };
@ -351,7 +356,7 @@ impl Post {
pub async fn refresh_cache(&self, rconn: &RdsConn, is_new: bool) { pub async fn refresh_cache(&self, rconn: &RdsConn, is_new: bool) {
join!( join!(
self.set_instance_cache(rconn), 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()) PostListCache::init(None, mode, &rconn.clone())
.put(self) .put(self)
.await; .await;

6
src/schema.rs

@ -45,4 +45,8 @@ table! {
joinable!(comments -> posts (post_id)); 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,
);

Loading…
Cancel
Save