From 12f0be331b02ff2c9399ededcf138d952bbb62f1 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 20 Feb 2021 13:28:32 +0100 Subject: [PATCH] i18n: fix some untranslatable strings from database --- database/src/fs.rs | 2 ++ database/src/lib.rs | 23 ++++++----------------- dist/templates/index.html.hbs | 2 +- po/hagrid/hagrid.pot | 20 ++++++++++++++++++++ src/delete.rs | 2 +- src/i18n.rs | 1 - src/i18n_helpers.rs | 16 ++++++++++++++++ src/main.rs | 1 + src/web/debug_web.rs | 8 ++++++-- src/web/hkp.rs | 18 ++++++++++++------ src/web/mod.rs | 6 ++++-- src/web/vks_api.rs | 33 +++++++++++++++++++++------------ src/web/vks_web.rs | 7 +++++-- 13 files changed, 95 insertions(+), 44 deletions(-) create mode 100644 src/i18n_helpers.rs diff --git a/database/src/fs.rs b/database/src/fs.rs index 91399af..a9fdc45 100644 --- a/database/src/fs.rs +++ b/database/src/fs.rs @@ -484,6 +484,7 @@ impl Database for Filesystem { ByFingerprint(ref fp) => self.link_by_fingerprint(fp), ByKeyID(ref keyid) => self.link_by_keyid(keyid), ByEmail(ref email) => self.link_by_email(email), + _ => return None }; path.read_link() .ok() @@ -497,6 +498,7 @@ impl Database for Filesystem { ByFingerprint(ref fp) => self.link_by_fingerprint(fp), ByKeyID(ref keyid) => self.link_by_keyid(keyid), ByEmail(ref email) => self.link_by_email(email), + _ => return None }; if path.exists() { diff --git a/database/src/lib.rs b/database/src/lib.rs index 3db6a24..455e8da 100644 --- a/database/src/lib.rs +++ b/database/src/lib.rs @@ -62,6 +62,8 @@ pub enum Query { ByFingerprint(Fingerprint), ByKeyID(KeyID), ByEmail(Email), + InvalidShort(), + Invalid(), } impl FromStr for Query { @@ -73,29 +75,15 @@ impl FromStr for Query { let looks_like_short_key_id = !term.contains('@') && (term.starts_with("0x") && term.len() < 16 || term.len() == 8); if looks_like_short_key_id { - return Err(anyhow!("Search by Short Key ID is not supported, sorry!")); - } - if let Ok(fp) = Fingerprint::from_str(term) { + Ok(InvalidShort()) + } else if let Ok(fp) = Fingerprint::from_str(term) { Ok(ByFingerprint(fp)) } else if let Ok(keyid) = KeyID::from_str(term) { Ok(ByKeyID(keyid)) } else if let Ok(email) = Email::from_str(term) { Ok(ByEmail(email)) } else { - Err(anyhow!("Invalid search query!")) - } - } -} - -impl Query { - pub fn describe_error(&self) -> String { - match self { - Query::ByFingerprint(fpr) => - format!("No key found for fingerprint {}", fpr), - Query::ByKeyID(key_id) => - format!("No key found for key id {}", key_id), - Query::ByEmail(email) => - format!("No key found for e-mail address {}", email), + Ok(Invalid()) } } } @@ -152,6 +140,7 @@ pub trait Database: Sync + Send { ByFingerprint(ref fp) => self.by_fpr(fp), ByKeyID(ref keyid) => self.by_kid(keyid), ByEmail(ref email) => self.by_email(&email), + _ => None, }; match armored { diff --git a/dist/templates/index.html.hbs b/dist/templates/index.html.hbs index 625625d..65af7c2 100644 --- a/dist/templates/index.html.hbs +++ b/dist/templates/index.html.hbs @@ -2,7 +2,7 @@ {{#with page}} {{#with error}} -

Error: {{ this }}

+

{{ text "Error" }}: {{ this }}

{{/with}}
diff --git a/po/hagrid/hagrid.pot b/po/hagrid/hagrid.pot index f8d0e37..116ce4a 100644 --- a/po/hagrid/hagrid.pot +++ b/po/hagrid/hagrid.pot @@ -22,6 +22,26 @@ msgctxt "Subject for manage email" msgid "Manage your key on {domain}" msgstr "" +#: src/i18n_helpers.rs:8 +msgid "No key found for fingerprint {fingerprint}" +msgstr "" + +#: src/i18n_helpers.rs:10 +msgid "No key found for key id {key_id}" +msgstr "" + +#: src/i18n_helpers.rs:12 +msgid "No key found for email address {email}" +msgstr "" + +#: src/i18n_helpers.rs:13 +msgid "Search by Short Key ID is not supported." +msgstr "" + +#: src/i18n_helpers.rs:14 +msgid "Invalid search query." +msgstr "" + #: src/gettext_strings.rs:4 msgid "Error" msgstr "" diff --git a/src/delete.rs b/src/delete.rs index ce136b6..3da0e7d 100644 --- a/src/delete.rs +++ b/src/delete.rs @@ -62,7 +62,7 @@ fn delete(db: &KeyDatabase, query: &Query, all_bindings: bool, mut all: bool) bindings."); all = true; }, - Query::ByEmail(_) => (), + _ => (), } let tpk = db.lookup(&query)?.ok_or_else( diff --git a/src/i18n.rs b/src/i18n.rs index 94b6e27..7501efd 100644 --- a/src/i18n.rs +++ b/src/i18n.rs @@ -99,4 +99,3 @@ impl HelperDef for I18NHelper { Ok(()) } } - diff --git a/src/i18n_helpers.rs b/src/i18n_helpers.rs new file mode 100644 index 0000000..1485b7b --- /dev/null +++ b/src/i18n_helpers.rs @@ -0,0 +1,16 @@ +use rocket_i18n::I18n; +use crate::database::Query; +use gettext_macros::i18n; + +pub fn describe_query_error(i18n: &I18n, q: &Query) -> String { + match q { + Query::ByFingerprint(fpr) => + i18n!(i18n.catalog, "No key found for fingerprint {fingerprint}"; fingerprint = fpr), + Query::ByKeyID(key_id) => + i18n!(i18n.catalog, "No key found for key id {key_id}"; key_id = key_id), + Query::ByEmail(email) => + i18n!(i18n.catalog, "No key found for email address {email}"; email = email), + Query::InvalidShort() => i18n!(i18n.catalog, "Search by Short Key ID is not supported."), + Query::Invalid() => i18n!(i18n.catalog, "Invalid search query."), + } +} diff --git a/src/main.rs b/src/main.rs index b7b190a..030131f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,7 @@ mod rate_limiter; mod dump; mod counters; mod i18n; +mod i18n_helpers; mod gettext_strings; mod web; mod template_helpers; diff --git a/src/web/debug_web.rs b/src/web/debug_web.rs index 9a50445..5310c22 100644 --- a/src/web/debug_web.rs +++ b/src/web/debug_web.rs @@ -1,13 +1,17 @@ use std::io; +use rocket_i18n::I18n; + use crate::dump::{self, Kind}; use crate::web::MyResponse; +use crate::i18n_helpers::describe_query_error; use crate::database::{Database, KeyDatabase, Query}; #[get("/debug?")] pub fn debug_info( db: rocket::State, + i18n: I18n, q: String, ) -> MyResponse { let query = match q.parse::() { @@ -16,12 +20,12 @@ pub fn debug_info( }; let fp = match db.lookup_primary_fingerprint(&query) { Some(fp) => fp, - None => return MyResponse::not_found_plain(query.describe_error()), + None => return MyResponse::not_found_plain(describe_query_error(&i18n, &query)), }; let armored_key = match db.by_fpr(&fp) { Some(armored_key) => armored_key, - None => return MyResponse::not_found_plain(query.describe_error()), + None => return MyResponse::not_found_plain(describe_query_error(&i18n, &query)), }; let mut result = Vec::new(); diff --git a/src/web/hkp.rs b/src/web/hkp.rs index 9d1a06c..84bca03 100644 --- a/src/web/hkp.rs +++ b/src/web/hkp.rs @@ -14,6 +14,7 @@ use crate::database::{Database, Query, KeyDatabase}; use crate::database::types::{Email, Fingerprint, KeyID}; use crate::rate_limiter::RateLimiter; +use crate::i18n_helpers::describe_query_error; use crate::tokens; @@ -216,6 +217,7 @@ fn send_welcome_mail( pub fn pks_lookup( state: rocket::State, db: rocket::State, + i18n: I18n, key: Hkp ) -> MyResponse { let (query, index) = match key { @@ -235,31 +237,35 @@ pub fn pks_lookup( }; if index { - key_to_hkp_index(db, query) + key_to_hkp_index(db, i18n, query) } else { - web::key_to_response_plain(state, db, query) + web::key_to_response_plain(state, db, i18n, query) } } #[get("/pks/internal/index/")] pub fn pks_internal_index( db: rocket::State, + i18n: I18n, query_string: String, ) -> MyResponse { match query_string.parse() { - Ok(query) => key_to_hkp_index(db, query), + Ok(query) => key_to_hkp_index(db, i18n, query), Err(_) => MyResponse::bad_request_plain("Invalid search query!") } } -fn key_to_hkp_index(db: rocket::State, query: Query) - -> MyResponse { +fn key_to_hkp_index( + db: rocket::State, + i18n: I18n, + query: Query, +) -> MyResponse { use sequoia_openpgp::types::RevocationStatus; use sequoia_openpgp::policy::StandardPolicy; let tpk = match db.lookup(&query) { Ok(Some(tpk)) => tpk, - Ok(None) => return MyResponse::not_found_plain(query.describe_error()), + Ok(None) => return MyResponse::not_found_plain(describe_query_error(&i18n, &query)), Err(err) => { return MyResponse::ise(err); } }; let mut out = String::default(); diff --git a/src/web/mod.rs b/src/web/mod.rs index 1c1da02..c193efc 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -21,6 +21,7 @@ use std::path::PathBuf; use crate::mail; use crate::tokens; use crate::counters; +use crate::i18n_helpers::describe_query_error; use crate::template_helpers::TemplateOverrides; use crate::i18n::I18NHelper; use crate::rate_limiter::RateLimiter; @@ -277,12 +278,13 @@ impl RequestOrigin { pub fn key_to_response_plain( state: rocket::State, db: rocket::State, + i18n: I18n, query: Query, ) -> MyResponse { let fp = if let Some(fp) = db.lookup_primary_fingerprint(&query) { fp } else { - return MyResponse::not_found_plain(query.describe_error()); + return MyResponse::not_found_plain(describe_query_error(&i18n, &query)); }; if state.x_accel_redirect { @@ -299,7 +301,7 @@ pub fn key_to_response_plain( return match db.by_fpr(&fp) { Some(armored) => MyResponse::key(armored, &fp.into()), - None => MyResponse::not_found_plain(query.describe_error()), + None => MyResponse::not_found_plain(describe_query_error(&i18n, &query)), } } diff --git a/src/web/vks_api.rs b/src/web/vks_api.rs index bd009f6..7335696 100644 --- a/src/web/vks_api.rs +++ b/src/web/vks_api.rs @@ -140,38 +140,47 @@ pub fn request_verify_fallback( } #[get("/vks/v1/by-fingerprint/")] -pub fn vks_v1_by_fingerprint(state: rocket::State, - db: rocket::State, - fpr: String) -> MyResponse { +pub fn vks_v1_by_fingerprint( + state: rocket::State, + db: rocket::State, + i18n: I18n, + fpr: String, +) -> MyResponse { let query = match fpr.parse::() { Ok(fpr) => Query::ByFingerprint(fpr), Err(_) => return MyResponse::bad_request_plain("malformed fingerprint"), }; - web::key_to_response_plain(state, db, query) + web::key_to_response_plain(state, db, i18n, query) } #[get("/vks/v1/by-email/")] -pub fn vks_v1_by_email(state: rocket::State, - db: rocket::State, - email: String) -> MyResponse { +pub fn vks_v1_by_email( + state: rocket::State, + db: rocket::State, + i18n: I18n, + email: String, +) -> MyResponse { let email = email.replace("%40", "@"); let query = match email.parse::() { Ok(email) => Query::ByEmail(email), Err(_) => return MyResponse::bad_request_plain("malformed e-mail address"), }; - web::key_to_response_plain(state, db, query) + web::key_to_response_plain(state, db, i18n, query) } #[get("/vks/v1/by-keyid/")] -pub fn vks_v1_by_keyid(state: rocket::State, - db: rocket::State, - kid: String) -> MyResponse { +pub fn vks_v1_by_keyid( + state: rocket::State, + db: rocket::State, + i18n: I18n, + kid: String, +) -> MyResponse { let query = match kid.parse::() { Ok(keyid) => Query::ByKeyID(keyid), Err(_) => return MyResponse::bad_request_plain("malformed key id"), }; - web::key_to_response_plain(state, db, query) + web::key_to_response_plain(state, db, i18n, query) } diff --git a/src/web/vks_web.rs b/src/web/vks_web.rs index 83b3ad1..6815c7f 100644 --- a/src/web/vks_web.rs +++ b/src/web/vks_web.rs @@ -15,6 +15,7 @@ use crate::mail; use crate::tokens; use crate::web::{RequestOrigin, MyResponse}; use crate::rate_limiter::RateLimiter; +use crate::i18n_helpers::describe_query_error; use std::io::Read; use std::collections::HashMap; @@ -225,23 +226,25 @@ pub fn process_post_form_data( #[get("/search?")] pub fn search( db: rocket::State, + i18n: I18n, q: String, ) -> MyResponse { match q.parse::() { - Ok(query) => key_to_response(db, q, query), + Ok(query) => key_to_response(db, i18n, q, query), Err(e) => MyResponse::bad_request("index", e), } } fn key_to_response( db: rocket::State, + i18n: I18n, query_string: String, query: Query, ) -> MyResponse { let fp = if let Some(fp) = db.lookup_primary_fingerprint(&query) { fp } else { - return MyResponse::not_found(None, query.describe_error()); + return MyResponse::not_found(None, describe_query_error(&i18n, &query)); }; let context = template::Search{