Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Surface questions over a given threshold #216

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion server/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use ulid::Ulid;
#[allow(unused_imports)]
use tracing::{debug, error, info, trace, warn};

const TOP_N: usize = 20; // TODO: how to make it configurable ?

impl Backend {
pub(super) async fn list(
&self,
Expand Down Expand Up @@ -239,8 +241,26 @@ async fn list_inner(
let exp = (-1. * dt as f64).exp_m1() + 1.;
Score(exp * votes / (1. - exp))
};
questions.sort_by_cached_key(|q| std::cmp::Reverse(score(q)));

let mut answered_hidden = Vec::new();
questions.retain(|item| {
match (
item.get("answered"),
item.get("hidden")
.unwrap_or(&serde_json::Value::Bool(false)),
) {
(None, serde_json::Value::Bool(false)) => true,
(_, _) => {
answered_hidden.push(item.clone());
false
}
}
});
questions.sort_by_key(|item| {
std::cmp::Reverse(item["votes"].as_u64().expect("votes is a number"))
});
questions[TOP_N..].sort_by_cached_key(|q| std::cmp::Reverse(score(q)));
questions.append(&mut answered_hidden);
let max_age = if has_secret {
// hosts should be allowed to see more up-to-date views
"max-age=3"
Expand Down