filter out users with an empty profile

This commit is contained in:
Armaël Guéneau 2024-11-27 19:36:47 +01:00
parent 862ccfa455
commit f50141ef23
2 changed files with 33 additions and 7 deletions

View file

@ -57,6 +57,15 @@ struct Db {
} }
impl UserData { impl UserData {
fn is_empty(&self) -> bool {
self.full_name.is_none()
&& self.location.is_none()
&& self.website.is_none()
&& self.description.is_none()
&& self.repos.is_empty()
&& self.issues.is_empty()
}
fn to_tokens(&self) -> Vec<String> { fn to_tokens(&self) -> Vec<String> {
let mut text = String::new(); let mut text = String::new();
let mut add = |s: &str| { let mut add = |s: &str| {
@ -226,6 +235,14 @@ async fn scrape_users(forge: &Forgejo) -> anyhow::Result<Vec<forgejo_api::struct
async fn get_users_data(forge: &Forgejo) -> anyhow::Result<HashMap<UserId, UserData>> { async fn get_users_data(forge: &Forgejo) -> anyhow::Result<HashMap<UserId, UserData>> {
let mut data = HashMap::new(); let mut data = HashMap::new();
let discard_empty = |o: Option<String>| {
match o {
None => None,
Some(s) if s.is_empty() => None,
Some(s) => Some(s),
}
};
eprintln!("Fetching users..."); eprintln!("Fetching users...");
for user in scrape_users(&forge).await? { for user in scrape_users(&forge).await? {
let Some(id) = user.id else { let Some(id) = user.id else {
@ -248,10 +265,10 @@ async fn get_users_data(forge: &Forgejo) -> anyhow::Result<HashMap<UserId, UserD
UserData { UserData {
login, login,
email, email,
full_name: user.full_name, full_name: discard_empty(user.full_name),
location: user.location, location: discard_empty(user.location),
website: user.website, website: discard_empty(user.website),
description: user.description, description: discard_empty(user.description),
repos: Vec::new(), repos: Vec::new(),
issues: Vec::new(), issues: Vec::new(),
}, },
@ -289,7 +306,7 @@ async fn get_users_data(forge: &Forgejo) -> anyhow::Result<HashMap<UserId, UserD
RepoId(id), RepoId(id),
RepoData { RepoData {
name: repo_name, name: repo_name,
description: repo.description, description: discard_empty(repo.description),
}, },
)); ));
} }
@ -328,6 +345,12 @@ async fn get_users_data(forge: &Forgejo) -> anyhow::Result<HashMap<UserId, UserD
)); ));
} }
// discard users with an entirely empty profile: there is nothing useful we
// can say about them
let data = data
.into_iter()
.filter(|(_, user)| !user.is_empty())
.collect();
Ok(data) Ok(data)
} }
@ -409,7 +432,8 @@ async fn index(data: web::Data<AppState>, q: web::Query<SortSetting>) -> impl Re
let db = &data.db.lock().unwrap(); let db = &data.db.lock().unwrap();
eprintln!("scoring users..."); eprintln!("scoring users...");
let mut users: Vec<_> = db.unclassified_users() let mut users: Vec<_> = db
.unclassified_users()
.into_iter() .into_iter()
.map(|(id, u)| (id, u, *db.score.get(id).unwrap())) .map(|(id, u)| (id, u, *db.score.get(id).unwrap()))
.collect(); .collect();

View file

@ -1,5 +1,7 @@
{% macro compact(name, desc) %} {% macro compact(name, desc) %}
{% if desc | length <= 150 %} {% if not desc %}
{{ name }}
{% elif desc | length <= 150 %}
{{ name }} | {{ desc }} {{ name }} | {{ desc }}
{% else %} {% else %}
<details> <details>