From 99fcbf77f60cf2b5618083342fd062b04e0b241e Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Sat, 3 Dec 2022 15:08:59 +0000 Subject: [PATCH] Hide icons that don't exist --- quickpeep/src/web/searcher.rs | 17 ++++++++++++++--- quickpeep/templates/search.html.askama | 4 +++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/quickpeep/src/web/searcher.rs b/quickpeep/src/web/searcher.rs index e7fb491..469c460 100644 --- a/quickpeep/src/web/searcher.rs +++ b/quickpeep/src/web/searcher.rs @@ -4,7 +4,9 @@ use crate::webutil::{internal_error, TemplatedHtml}; use askama::Template; use axum::extract::{Extension, Query}; use axum::response::IntoResponse; +use quickpeep_index::auxiliary::icon_store::IconStore; use serde::Deserialize; +use std::sync::Arc; #[derive(Clone, Template)] #[template(path = "search.html.askama")] @@ -17,7 +19,7 @@ pub struct SearchTemplate { #[derive(Clone)] pub struct SearchResult { - pub favicon_url: String, + pub favicon_url: Option, pub url: String, pub title: String, pub excerpt: String, @@ -49,8 +51,9 @@ pub async fn search_search( web_config: Extension, index_access: Extension, params: Query, + icon_store: Extension>, ) -> impl IntoResponse { - search_search_inner(web_config, index_access, params) + search_search_inner(web_config, index_access, params, icon_store) .await .map_err(internal_error) } @@ -59,14 +62,22 @@ pub async fn search_search_inner( Extension(web_config): Extension, Extension(index_access): Extension, Query(params): Query, + Extension(icon_store): Extension>, ) -> anyhow::Result { let raw_results = index_access.backend.query(params.q.clone())?; let mut results = Vec::with_capacity(raw_results.len()); + let txn = icon_store.env.ro_txn()?; + for search_doc in raw_results { + let favicon_url_hash: [u8; 8] = search_doc.favicon_url_hash.to_le_bytes(); + let favicon_url = icon_store + .icons + .get(&txn, &favicon_url_hash)? + .map(|_| format!("{:016x}", search_doc.favicon_url_hash)); results.push(SearchResult { - favicon_url: format!("{:16x}", search_doc.favicon_url_hash), + favicon_url, url: search_doc.url, title: search_doc.title, excerpt: search_doc.excerpt, diff --git a/quickpeep/templates/search.html.askama b/quickpeep/templates/search.html.askama index 10af462..44cb95d 100644 --- a/quickpeep/templates/search.html.askama +++ b/quickpeep/templates/search.html.askama @@ -42,7 +42,9 @@
    {%- for result in results %}
  • - + {%- if result.favicon_url.is_some() -%} + + {%- endif -%}
    {{- result.excerpt|safe -}}