From 2ed4964d0691fb0be7243ed0c27316c77e96c951 Mon Sep 17 00:00:00 2001 From: dadaa Date: Tue, 12 Mar 2024 10:58:59 +0900 Subject: [PATCH] Bug 1884816 - Expose icon information for suggestion as struct --- CHANGELOG.md | 4 + components/suggest/src/db.rs | 30 +- components/suggest/src/lib.rs | 2 +- components/suggest/src/store.rs | 1370 +++++++++++++------------- components/suggest/src/suggest.udl | 14 +- components/suggest/src/suggestion.rs | 15 +- components/suggest/src/yelp.rs | 25 +- 7 files changed, 728 insertions(+), 732 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f236201f2b..df89647cee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ ### Suggest - Improved full keyword display for AMP suggestions +- Introduced icon struct ([#6162](https://github.com/mozilla/application-services/pull/6162)) + +#### ⚠️ Breaking Changes ⚠️ + - Introduced `icon` struct and applied to suggestions needed. [Full Changelog](In progress) diff --git a/components/suggest/src/db.rs b/components/suggest/src/db.rs index 426b1bf098..93da4a5598 100644 --- a/components/suggest/src/db.rs +++ b/components/suggest/src/db.rs @@ -27,7 +27,7 @@ use crate::{ }, schema::{clear_database, SuggestConnectionInitializer, VERSION}, store::{UnparsableRecord, UnparsableRecords}, - suggestion::{cook_raw_suggestion_url, AmpSuggestionType, Suggestion}, + suggestion::{cook_raw_suggestion_url, AmpSuggestionType, Suggestion, SuggestionIcon}, Result, SuggestionQuery, }; @@ -290,7 +290,7 @@ impl<'a> SuggestDao<'a> { amp.iab_category, amp.impression_url, amp.click_url, - i.data AS icon, + i.data AS icon_data, i.mimetype AS icon_mimetype FROM amp_custom_details amp @@ -306,6 +306,12 @@ impl<'a> SuggestDao<'a> { let cooked_url = cook_raw_suggestion_url(&raw_url); let raw_click_url = row.get::<_, String>("click_url")?; let cooked_click_url = cook_raw_suggestion_url(&raw_click_url); + let icon_data = row.get::<_, Option<_>>("icon_data")?; + let icon_mime_type = row.get::<_, Option<_>>("icon_mimetype")?; + let icon = icon_data.map(|data| SuggestionIcon { + data, + mime_type: icon_mime_type.unwrap_or_default(), + }); Ok(Suggestion::Amp { block_id: row.get("block_id")?, @@ -316,8 +322,7 @@ impl<'a> SuggestDao<'a> { raw_url, full_keyword: full_keyword_from_db .unwrap_or_else(|| full_keyword(keyword_lowercased, &keywords)), - icon: row.get("icon")?, - icon_mimetype: row.get("icon_mimetype")?, + icon, impression_url: row.get("impression_url")?, click_url: cooked_click_url, raw_click_url, @@ -368,7 +373,7 @@ impl<'a> SuggestDao<'a> { }, |row| row.get(0), )?; - let (icon, icon_mimetype) = self + let icon = self .conn .try_query_row( "SELECT i.data, i.mimetype @@ -380,21 +385,20 @@ impl<'a> SuggestDao<'a> { ":suggestion_id": suggestion_id }, |row| -> Result<_> { - Ok(( - row.get::<_, Option>>(0)?, - row.get::<_, Option>(1)?, - )) + Ok(Some(SuggestionIcon { + data: row.get::<_, Vec>(0)?, + mime_type: row.get::<_, String>(1)?, + })) }, true, )? - .unwrap_or((None, None)); + .unwrap_or(None); Ok(Suggestion::Wikipedia { title, url: raw_url, full_keyword: full_keyword(keyword_lowercased, &keywords), icon, - icon_mimetype, }) }, )?; @@ -1171,7 +1175,7 @@ impl<'a> SuggestDao<'a> { } /// Inserts or replaces an icon for a suggestion into the database. - pub fn put_icon(&mut self, icon_id: &str, data: &[u8], mimetype: &str) -> Result<()> { + pub fn put_icon(&mut self, icon_id: &str, data: &[u8], mime_type: &str) -> Result<()> { self.conn.execute( "INSERT OR REPLACE INTO icons( id, @@ -1186,7 +1190,7 @@ impl<'a> SuggestDao<'a> { named_params! { ":id": icon_id, ":data": data, - ":mimetype": mimetype, + ":mimetype": mime_type, }, )?; Ok(()) diff --git a/components/suggest/src/lib.rs b/components/suggest/src/lib.rs index 23775b7dec..7260445b89 100644 --- a/components/suggest/src/lib.rs +++ b/components/suggest/src/lib.rs @@ -20,7 +20,7 @@ pub use config::{SuggestGlobalConfig, SuggestProviderConfig}; pub use error::SuggestApiError; pub use provider::SuggestionProvider; pub use store::{SuggestIngestionConstraints, SuggestStore, SuggestStoreBuilder}; -pub use suggestion::{raw_suggestion_url_matches, Suggestion}; +pub use suggestion::{raw_suggestion_url_matches, Suggestion, SuggestionIcon}; pub(crate) type Result = std::result::Result; pub type SuggestApiResult = std::result::Result; diff --git a/components/suggest/src/store.rs b/components/suggest/src/store.rs index 4c925e5482..3561919ca5 100644 --- a/components/suggest/src/store.rs +++ b/components/suggest/src/store.rs @@ -807,7 +807,6 @@ mod tests { url: "https://www.lph-nm.biz", raw_url: "https://www.lph-nm.biz", icon: None, - icon_mimetype: None, full_keyword: "los", block_id: 0, advertiser: "Los Pollos Hermanos", @@ -898,23 +897,23 @@ mod tests { url: "https://www.lasagna.restaurant", raw_url: "https://www.lasagna.restaurant", icon: Some( - [ - 105, - 45, - 97, - 109, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 105, + 45, + 97, + 109, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "lasagna", block_id: 0, @@ -939,23 +938,23 @@ mod tests { url: "https://penne.biz", raw_url: "https://penne.biz", icon: Some( - [ - 105, - 45, - 97, - 109, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 105, + 45, + 97, + 109, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "penne", block_id: 0, @@ -1120,7 +1119,6 @@ mod tests { url: "https://www.lph-nm.biz", raw_url: "https://www.lph-nm.biz", icon: None, - icon_mimetype: None, full_keyword: "los pollos", block_id: 0, advertiser: "Los Pollos Hermanos", @@ -1135,7 +1133,6 @@ mod tests { url: "https://www.lph-nm.biz", raw_url: "https://www.lph-nm.biz", icon: None, - icon_mimetype: None, full_keyword: "los", block_id: 1, advertiser: "Los Pollos Hermanos", @@ -1160,7 +1157,6 @@ mod tests { url: "https://www.lph-nm.biz", raw_url: "https://www.lph-nm.biz", icon: None, - icon_mimetype: None, full_keyword: "los pollos hermanos (restaurant)", block_id: 0, advertiser: "Los Pollos Hermanos", @@ -1175,7 +1171,6 @@ mod tests { url: "https://www.lph-nm.biz", raw_url: "https://www.lph-nm.biz", icon: None, - icon_mimetype: None, full_keyword: "los pollos hermanos", block_id: 1, advertiser: "Los Pollos Hermanos", @@ -1199,7 +1194,6 @@ mod tests { title: "Los Pollos Hermanos - Albuquerque - Wiki", url: "https://www.lph-nm.biz", icon: None, - icon_mimetype: None, full_keyword: "los", }, ] @@ -1217,7 +1211,6 @@ mod tests { url: "https://www.lph-nm.biz", raw_url: "https://www.lph-nm.biz", icon: None, - icon_mimetype: None, full_keyword: "los pollos hermanos (restaurant)", block_id: 0, advertiser: "Los Pollos Hermanos", @@ -1287,7 +1280,6 @@ mod tests { url: "https://www.lasagna.restaurant", raw_url: "https://www.lasagna.restaurant", icon: None, - icon_mimetype: None, full_keyword: "lasagna", block_id: 0, advertiser: "Good Place Eats", @@ -1376,7 +1368,6 @@ mod tests { url: "https://www.lasagna.restaurant", raw_url: "https://www.lasagna.restaurant", icon: None, - icon_mimetype: None, full_keyword: "lasagna", block_id: 0, advertiser: "Good Place Eats", @@ -1462,7 +1453,6 @@ mod tests { url: "https://www.lph-nm.biz", raw_url: "https://www.lph-nm.biz", icon: None, - icon_mimetype: None, full_keyword: "los pollos", block_id: 0, advertiser: "Los Pollos Hermanos", @@ -1486,7 +1476,6 @@ mod tests { url: "https://penne.biz", raw_url: "https://penne.biz", icon: None, - icon_mimetype: None, full_keyword: "penne", block_id: 0, advertiser: "Good Place Eats", @@ -1637,27 +1626,27 @@ mod tests { url: "https://www.lasagna.restaurant", raw_url: "https://www.lasagna.restaurant", icon: Some( - [ - 110, - 101, - 119, - 45, - 108, - 97, - 115, - 97, - 103, - 110, - 97, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 110, + 101, + 119, + 45, + 108, + 97, + 115, + 97, + 103, + 110, + 97, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "lasagna", block_id: 0, @@ -1682,26 +1671,26 @@ mod tests { url: "https://www.lph-nm.biz", raw_url: "https://www.lph-nm.biz", icon: Some( - [ - 110, - 101, - 119, - 45, - 112, - 111, - 108, - 108, - 111, - 115, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 110, + 101, + 119, + 45, + 112, + 111, + 108, + 108, + 111, + 115, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "los", block_id: 0, @@ -2475,23 +2464,23 @@ mod tests { url: "https://www.lasagna.restaurant", raw_url: "https://www.lasagna.restaurant", icon: Some( - [ - 105, - 45, - 97, - 109, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 105, + 45, + 97, + 109, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "lasagna", block_id: 0, @@ -2541,23 +2530,23 @@ mod tests { title: "Multimatch", url: "https://wikipedia.org/Multimatch", icon: Some( - [ - 97, - 108, - 115, - 111, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 97, + 108, + 115, + 111, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "multimatch", }, @@ -2600,23 +2589,23 @@ mod tests { title: "Multimatch", url: "https://wikipedia.org/Multimatch", icon: Some( - [ - 97, - 108, - 115, - 111, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 97, + 108, + 115, + 111, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "multimatch", }, @@ -2672,23 +2661,23 @@ mod tests { url: "https://www.lasagna.restaurant", raw_url: "https://www.lasagna.restaurant", icon: Some( - [ - 105, - 45, - 97, - 109, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 105, + 45, + 97, + 109, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "lasagna", block_id: 0, @@ -2756,23 +2745,23 @@ mod tests { title: "California", url: "https://wikipedia.org/California", icon: Some( - [ - 97, - 108, - 115, - 111, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 97, + 108, + 115, + 111, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "california", }, @@ -2780,23 +2769,23 @@ mod tests { title: "California Institute of Technology", url: "https://wikipedia.org/California_Institute_of_Technology", icon: Some( - [ - 97, - 108, - 115, - 111, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 97, + 108, + 115, + 111, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "california", }, @@ -2816,23 +2805,23 @@ mod tests { title: "California", url: "https://wikipedia.org/California", icon: Some( - [ - 97, - 108, - 115, - 111, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 97, + 108, + 115, + 111, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "california", }, @@ -3033,20 +3022,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=best+spicy+ramen+delivery&find_loc=tokyo", title: "best spicy ramen delivery in tokyo", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: true, @@ -3069,20 +3058,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=BeSt+SpIcY+rAmEn+DeLiVeRy&find_loc=ToKyO", title: "BeSt SpIcY rAmEn DeLiVeRy In ToKyO", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: true, @@ -3105,20 +3094,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=best+ramen+delivery&find_loc=tokyo", title: "best ramen delivery in tokyo", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: true, @@ -3163,20 +3152,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=super+best+ramen+delivery&find_loc=tokyo", title: "super best ramen delivery in tokyo", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: true, @@ -3210,20 +3199,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen+delivery&find_loc=tokyo", title: "ramen delivery in tokyo", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: true, @@ -3246,20 +3235,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen+super+delivery&find_loc=tokyo", title: "ramen super delivery in tokyo", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: true, @@ -3293,20 +3282,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen&find_loc=tokyo", title: "ramen in tokyo", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: true, @@ -3329,20 +3318,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen&find_loc=tokyo", title: "ramen near tokyo", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: true, @@ -3376,20 +3365,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen&find_loc=San+Francisco", title: "ramen in San Francisco", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: true, @@ -3412,20 +3401,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen", title: "ramen in", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: true, @@ -3448,20 +3437,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen+near+by", title: "ramen near by", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -3484,20 +3473,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen+near+me", title: "ramen near me", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -3531,20 +3520,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen", title: "ramen", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -3567,20 +3556,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789", title: "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -3647,20 +3636,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen", title: "ramen", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -3683,20 +3672,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen", title: "ramen", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -3719,20 +3708,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen&find_loc=tokyo", title: "ramen in tokyo", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: true, @@ -3755,20 +3744,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen&find_loc=tokyo", title: "ramen in tokyo", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: true, @@ -3791,20 +3780,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen", title: "ramen", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -3838,20 +3827,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=Spicy+Ramen", title: "Spicy Ramen", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -3874,20 +3863,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=BeSt+Ramen", title: "BeSt Ramen", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -3910,20 +3899,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=BeSt+Spicy+Ramen", title: "BeSt Spicy Ramen", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -3968,20 +3957,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=rats", title: "rats", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -4004,20 +3993,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen", title: "ramen", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -4040,20 +4029,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=raccoon", title: "raccoon", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -4087,20 +4076,20 @@ mod tests { url: "https://www.yelp.com/search?find_desc=best+rats", title: "best rats", icon: Some( - [ - 121, - 101, - 108, - 112, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/svg+xml", + SuggestionIcon { + data: [ + 121, + 101, + 108, + 112, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/svg+xml", + }, ), score: 0.5, has_location_sign: false, @@ -4243,7 +4232,6 @@ mod tests { url: "https://www.lasagna.restaurant", raw_url: "https://www.lasagna.restaurant", icon: None, - icon_mimetype: None, full_keyword: "amp wiki match", block_id: 0, advertiser: "Good Place Eats", @@ -4257,23 +4245,23 @@ mod tests { title: "Multimatch", url: "https://wikipedia.org/Multimatch", icon: Some( - [ - 97, - 108, - 115, - 111, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 97, + 108, + 115, + 111, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "amp wiki match", }, @@ -4282,7 +4270,6 @@ mod tests { url: "https://penne.biz", raw_url: "https://penne.biz", icon: None, - icon_mimetype: None, full_keyword: "amp wiki match", block_id: 0, advertiser: "Good Place Eats", @@ -4315,7 +4302,6 @@ mod tests { url: "https://www.lasagna.restaurant", raw_url: "https://www.lasagna.restaurant", icon: None, - icon_mimetype: None, full_keyword: "amp wiki match", block_id: 0, advertiser: "Good Place Eats", @@ -4329,23 +4315,23 @@ mod tests { title: "Multimatch", url: "https://wikipedia.org/Multimatch", icon: Some( - [ - 97, - 108, - 115, - 111, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 97, + 108, + 115, + 111, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "amp wiki match", }, @@ -4376,23 +4362,23 @@ mod tests { title: "Multimatch", url: "https://wikipedia.org/Multimatch", icon: Some( - [ - 97, - 108, - 115, - 111, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 97, + 108, + 115, + 111, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "pocket wiki match", }, @@ -4699,23 +4685,23 @@ mod tests { url: "https://www.lasagna.restaurant", raw_url: "https://www.lasagna.restaurant", icon: Some( - [ - 97, - 108, - 115, - 111, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 97, + 108, + 115, + 111, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "lasagna", block_id: 0, @@ -4743,23 +4729,23 @@ mod tests { url: "https://www.lasagna.restaurant", raw_url: "https://www.lasagna.restaurant", icon: Some( - [ - 97, - 108, - 115, - 111, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 97, + 108, + 115, + 111, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "lasagna", block_id: 0, @@ -4787,23 +4773,23 @@ mod tests { url: "https://www.lasagna.restaurant", raw_url: "https://www.lasagna.restaurant", icon: Some( - [ - 97, - 108, - 115, - 111, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 97, + 108, + 115, + 111, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "lasagna", block_id: 0, @@ -4819,23 +4805,23 @@ mod tests { url: "https://www.lasagna.restaurant", raw_url: "https://www.lasagna.restaurant", icon: Some( - [ - 97, - 108, - 115, - 111, - 45, - 97, - 110, - 45, - 105, - 99, - 111, - 110, - ], - ), - icon_mimetype: Some( - "image/png", + SuggestionIcon { + data: [ + 97, + 108, + 115, + 111, + 45, + 97, + 110, + 45, + 105, + 99, + 111, + 110, + ], + mime_type: "image/png", + }, ), full_keyword: "lasagna", block_id: 0, @@ -5332,7 +5318,6 @@ mod tests { url: "https://www.lph-nm.biz", raw_url: "https://www.lph-nm.biz", icon: None, - icon_mimetype: None, full_keyword: "los", block_id: 0, advertiser: "Los Pollos Hermanos", @@ -5541,7 +5526,6 @@ mod tests { url: "https://www.yelp.com/search?find_desc=ramen", title: "ramen", icon: None, - icon_mimetype: None, score: 0.5, has_location_sign: false, subject_exact_match: true, diff --git a/components/suggest/src/suggest.udl b/components/suggest/src/suggest.udl index 133cc56f14..011a972055 100644 --- a/components/suggest/src/suggest.udl +++ b/components/suggest/src/suggest.udl @@ -22,6 +22,11 @@ interface SuggestApiError { Other(string reason); }; +dictionary SuggestionIcon { + sequence data; + string mime_type; +}; + enum SuggestionProvider { "Amp", "Pocket", @@ -39,8 +44,7 @@ interface Suggestion { string title, string url, string raw_url, - sequence? icon, - string? icon_mimetype, + SuggestionIcon? icon, string full_keyword, i64 block_id, string advertiser, @@ -59,8 +63,7 @@ interface Suggestion { Wikipedia( string title, string url, - sequence? icon, - string? icon_mimetype, + SuggestionIcon? icon, string full_keyword ); Amo( @@ -76,8 +79,7 @@ interface Suggestion { Yelp( string url, string title, - sequence? icon, - string? icon_mimetype, + SuggestionIcon? icon, f64 score, boolean has_location_sign, boolean subject_exact_match, diff --git a/components/suggest/src/suggestion.rs b/components/suggest/src/suggestion.rs index 02a169df15..39ad1d0ef6 100644 --- a/components/suggest/src/suggestion.rs +++ b/components/suggest/src/suggestion.rs @@ -16,6 +16,12 @@ const TIMESTAMP_TEMPLATE: &str = "%YYYYMMDDHH%"; /// 2 bytes shorter than [`TIMESTAMP_TEMPLATE`]. const TIMESTAMP_LENGTH: usize = 10; +#[derive(Clone, Debug, PartialEq)] +pub struct SuggestionIcon { + pub data: Vec, + pub mime_type: String, +} + /// Suggestion Types for Amp pub(crate) enum AmpSuggestionType { Mobile, @@ -28,8 +34,7 @@ pub enum Suggestion { title: String, url: String, raw_url: String, - icon: Option>, - icon_mimetype: Option, + icon: Option, full_keyword: String, block_id: i64, advertiser: String, @@ -48,8 +53,7 @@ pub enum Suggestion { Wikipedia { title: String, url: String, - icon: Option>, - icon_mimetype: Option, + icon: Option, full_keyword: String, }, Amo { @@ -65,8 +69,7 @@ pub enum Suggestion { Yelp { url: String, title: String, - icon: Option>, - icon_mimetype: Option, + icon: Option, score: f64, has_location_sign: bool, subject_exact_match: bool, diff --git a/components/suggest/src/yelp.rs b/components/suggest/src/yelp.rs index 0eb6c10d56..67e1f579e8 100644 --- a/components/suggest/src/yelp.rs +++ b/components/suggest/src/yelp.rs @@ -12,7 +12,7 @@ use crate::{ db::SuggestDao, provider::SuggestionProvider, rs::{DownloadedYelpSuggestion, SuggestRecordId}, - suggestion::Suggestion, + suggestion::{Suggestion, SuggestionIcon}, Result, SuggestionQuery, }; @@ -147,7 +147,7 @@ impl<'a> SuggestDao<'a> { let Some((subject, subject_exact_match)) = self.find_subject(query_string)? else { return Ok(vec![]); }; - let (icon, icon_mimetype, score) = self.fetch_custom_details()?; + let (icon, score) = self.fetch_custom_details()?; let builder = SuggestionBuilder { subject: &subject, subject_exact_match, @@ -157,7 +157,6 @@ impl<'a> SuggestDao<'a> { location: None, need_location: false, icon, - icon_mimetype, score, }; return Ok(vec![builder.into()]); @@ -189,7 +188,7 @@ impl<'a> SuggestDao<'a> { return Ok(vec![]); }; - let (icon, icon_mimetype, score) = self.fetch_custom_details()?; + let (icon, score) = self.fetch_custom_details()?; let builder = SuggestionBuilder { subject: &subject, subject_exact_match, @@ -199,7 +198,6 @@ impl<'a> SuggestDao<'a> { location, need_location, icon, - icon_mimetype, score, }; Ok(vec![builder.into()]) @@ -208,8 +206,7 @@ impl<'a> SuggestDao<'a> { /// Fetch the custom details for Yelp suggestions. /// It returns the location tuple as follows: /// ( - /// Option>: Icon data. If not found, returns None. - /// Option: Mimetype of the icon data. If not found, returns None. + /// Option: Icon data. If not found, returns None. /// f64: Reflects score field in the yelp_custom_details table. /// ) /// @@ -218,8 +215,8 @@ impl<'a> SuggestDao<'a> { /// on Remote Settings. The following query will perform a table scan against /// `yelp_custom_details` followed by an index search against `icons`, /// which should be fine since there is only one record in the first table. - fn fetch_custom_details(&self) -> Result<(Option>, Option, f64)> { - let result = self.conn.query_row_and_then_cachable( + fn fetch_custom_details(&self) -> Result<(Option, f64)> { + let (icon_data, icon_mime_type, score) = self.conn.query_row_and_then_cachable( r#" SELECT i.data, i.mimetype, y.score @@ -242,7 +239,11 @@ impl<'a> SuggestDao<'a> { true, )?; - Ok(result) + let icon = icon_data.map(|data| SuggestionIcon { + data, + mime_type: icon_mime_type.unwrap_or_default(), + }); + Ok((icon, score)) } /// Find the location information from the given query string. @@ -450,8 +451,7 @@ struct SuggestionBuilder<'a> { location_sign: Option, location: Option, need_location: bool, - icon: Option>, - icon_mimetype: Option, + icon: Option, score: f64, } @@ -501,7 +501,6 @@ impl<'a> From> for Suggestion { url, title, icon: builder.icon, - icon_mimetype: builder.icon_mimetype, score: builder.score, has_location_sign: location_modifier.is_none() && builder.location_sign.is_some(), subject_exact_match: builder.subject_exact_match,