| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
*/ |
| 5 |
|
| 6 |
use chrono::Local; |
| 7 |
|
| 8 |
use crate::{db::DEFAULT_SUGGESTION_SCORE, geoname::Geoname}; |
| 9 |
|
| 10 |
/// The template parameter for a timestamp in a "raw" sponsored suggestion URL. |
| 11 |
const TIMESTAMP_TEMPLATE: &str = "%YYYYMMDDHH%"; |
| 12 |
|
| 13 |
/// The length, in bytes, of a timestamp in a "cooked" sponsored suggestion URL. |
| 14 |
/// |
| 15 |
/// Cooked timestamps don't include the leading or trailing `%`, so this is |
| 16 |
/// 2 bytes shorter than [`TIMESTAMP_TEMPLATE`]. |
| 17 |
const TIMESTAMP_LENGTH: usize = 10; |
| 18 |
|
| 19 |
/// Subject type for Yelp suggestion. |
| 20 |
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, uniffi::Enum)] |
| 21 |
#[repr(u8)] |
| 22 |
pub enum YelpSubjectType { |
| 23 |
// Service such as sushi, ramen, yoga etc. |
| 24 |
Service = 0, |
| 25 |
// Specific business such as the shop name. |
| 26 |
Business = 1, |
| 27 |
} |
| 28 |
|
| 29 |
/// A suggestion from the database to show in the address bar. |
| 30 |
#[derive(Clone, Debug, PartialEq, uniffi::Enum)] |
| 31 |
pub enum Suggestion { |
| 32 |
Amp { |
| 33 |
title: String, |
| 34 |
url: String, |
| 35 |
raw_url: String, |
| 36 |
icon: Option<Vec<u8>>, |
| 37 |
icon_mimetype: Option<String>, |
| 38 |
full_keyword: String, |
| 39 |
block_id: i64, |
| 40 |
advertiser: String, |
| 41 |
iab_category: String, |
| 42 |
impression_url: String, |
| 43 |
click_url: String, |
| 44 |
raw_click_url: String, |
| 45 |
score: f64, |
| 46 |
fts_match_info: Option<FtsMatchInfo>, |
| 47 |
}, |
| 48 |
Pocket { |
| 49 |
title: String, |
| 50 |
url: String, |
| 51 |
score: f64, |
| 52 |
is_top_pick: bool, |
| 53 |
}, |
| 54 |
Wikipedia { |
| 55 |
title: String, |
| 56 |
url: String, |
| 57 |
icon: Option<Vec<u8>>, |
| 58 |
icon_mimetype: Option<String>, |
| 59 |
full_keyword: String, |
| 60 |
}, |
| 61 |
Amo { |
| 62 |
title: String, |
| 63 |
url: String, |
| 64 |
icon_url: String, |
| 65 |
description: String, |
| 66 |
rating: Option<String>, |
| 67 |
number_of_ratings: i64, |
| 68 |
guid: String, |
| 69 |
score: f64, |
| 70 |
}, |
| 71 |
Yelp { |
| 72 |
url: String, |
| 73 |
title: String, |
| 74 |
icon: Option<Vec<u8>>, |
| 75 |
icon_mimetype: Option<String>, |
| 76 |
score: f64, |
| 77 |
has_location_sign: bool, |
| 78 |
subject_exact_match: bool, |
| 79 |
subject_type: YelpSubjectType, |
| 80 |
location_param: String, |
| 81 |
}, |
| 82 |
Mdn { |
| 83 |
title: String, |
| 84 |
url: String, |
| 85 |
description: String, |
| 86 |
score: f64, |
| 87 |
}, |
| 88 |
Weather { |
| 89 |
city: Option<Geoname>, |
| 90 |
score: f64, |
| 91 |
}, |
| 92 |
Fakespot { |
| 93 |
fakespot_grade: String, |
| 94 |
product_id: String, |
| 95 |
rating: f64, |
| 96 |
title: String, |
| 97 |
total_reviews: i64, |
| 98 |
url: String, |
| 99 |
icon: Option<Vec<u8>>, |
| 100 |
icon_mimetype: Option<String>, |
| 101 |
score: f64, |
| 102 |
// Details about the FTS match. For performance reasons, this is only calculated for the |
| 103 |
// result with the highest score. We assume that only one that will be shown to the user |
| 104 |
// and therefore the only one we'll collect metrics for. |
| 105 |
match_info: Option<FtsMatchInfo>, |
| 106 |
}, |
| 107 |
Dynamic { |
| 108 |
suggestion_type: String, |
| 109 |
data: Option<serde_json::Value>, |
| 110 |
/// This value is optionally defined in the suggestion's remote settings |
| 111 |
/// data and is an opaque token used for dismissing the suggestion in |
| 112 |
/// lieu of a URL. If `Some`, the suggestion can be dismissed by passing |
| 113 |
/// the wrapped string to [crate::SuggestStore::dismiss_suggestion]. |
| 114 |
dismissal_key: Option<String>, |
| 115 |
score: f64, |
| 116 |
}, |
| 117 |
} |
| 118 |
|
| 119 |
/// Additional data about how an FTS match was made |
| 120 |
#[derive(Debug, Clone, PartialEq, uniffi::Record)] |
| 121 |
pub struct FtsMatchInfo { |
| 122 |
/// Was this a prefix match (`water b` matched against `water bottle`) |
| 123 |
pub prefix: bool, |
| 124 |
/// Did the match require stemming? (`run shoes` matched against `running shoes`) |
| 125 |
pub stemming: bool, |
| 126 |
} |
| 127 |
|
| 128 |
impl PartialOrd for Suggestion { |
| 129 |
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
| 130 |
Some(self.cmp(other)) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
impl Ord for Suggestion { |
| 135 |
fn cmp(&self, other: &Self) -> std::cmp::Ordering { |
| 136 |
other |
| 137 |
.score() |
| 138 |
.partial_cmp(&self.score()) |
| 139 |
.unwrap_or(std::cmp::Ordering::Equal) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
impl Suggestion { |
| 144 |
/// Get the suggestion's dismissal key, which should be stored in the |
| 145 |
/// `dismissed_suggestions` table when the suggestion is dismissed. Some |
| 146 |
/// suggestions may not have dismissal keys and cannot be dismissed. |
| 147 |
pub fn dismissal_key(&self) -> Option<&str> { |
| 148 |
match self { |
| 149 |
Self::Amp { full_keyword, .. } => { |
| 150 |
if !full_keyword.is_empty() { |
| 151 |
Some(full_keyword) |
| 152 |
} else { |
| 153 |
self.raw_url() |
| 154 |
} |
| 155 |
} |
| 156 |
Self::Dynamic { dismissal_key, .. } => dismissal_key.as_deref(), |
| 157 |
Self::Pocket { .. } |
| 158 |
| Self::Wikipedia { .. } |
| 159 |
| Self::Amo { .. } |
| 160 |
| Self::Yelp { .. } |
| 161 |
| Self::Mdn { .. } |
| 162 |
| Self::Weather { .. } |
| 163 |
| Self::Fakespot { .. } => self.raw_url(), |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/// Get the URL for this suggestion, if present |
| 168 |
pub fn url(&self) -> Option<&str> { |
| 169 |
match self { |
| 170 |
Self::Amp { url, .. } |
| 171 |
| Self::Pocket { url, .. } |
| 172 |
| Self::Wikipedia { url, .. } |
| 173 |
| Self::Amo { url, .. } |
| 174 |
| Self::Yelp { url, .. } |
| 175 |
| Self::Mdn { url, .. } |
| 176 |
| Self::Fakespot { url, .. } => Some(url), |
| 177 |
Self::Weather { .. } | Self::Dynamic { .. } => None, |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/// Get the raw URL for this suggestion, if present |
| 182 |
/// |
| 183 |
/// This is the same as `url` except for Amp. In that case, `url` is the URL after being |
| 184 |
/// "cooked" using template interpolation, while `raw_url` is the URL template. |
| 185 |
pub fn raw_url(&self) -> Option<&str> { |
| 186 |
match self { |
| 187 |
Self::Amp { raw_url, .. } => Some(raw_url), |
| 188 |
Self::Pocket { .. } |
| 189 |
| Self::Wikipedia { .. } |
| 190 |
| Self::Amo { .. } |
| 191 |
| Self::Yelp { .. } |
| 192 |
| Self::Mdn { .. } |
| 193 |
| Self::Weather { .. } |
| 194 |
| Self::Fakespot { .. } |
| 195 |
| Self::Dynamic { .. } => self.url(), |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
pub fn title(&self) -> &str { |
| 200 |
match self { |
| 201 |
Self::Amp { title, .. } |
| 202 |
| Self::Pocket { title, .. } |
| 203 |
| Self::Wikipedia { title, .. } |
| 204 |
| Self::Amo { title, .. } |
| 205 |
| Self::Yelp { title, .. } |
| 206 |
| Self::Mdn { title, .. } |
| 207 |
| Self::Fakespot { title, .. } => title, |
| 208 |
_ => "untitled", |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
pub fn icon_data(&self) -> Option<&[u8]> { |
| 213 |
match self { |
| 214 |
Self::Amp { icon, .. } |
| 215 |
| Self::Wikipedia { icon, .. } |
| 216 |
| Self::Yelp { icon, .. } |
| 217 |
| Self::Fakespot { icon, .. } => icon.as_deref(), |
| 218 |
_ => None, |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
pub fn score(&self) -> f64 { |
| 223 |
match self { |
| 224 |
Self::Amp { score, .. } |
| 225 |
| Self::Pocket { score, .. } |
| 226 |
| Self::Amo { score, .. } |
| 227 |
| Self::Yelp { score, .. } |
| 228 |
| Self::Mdn { score, .. } |
| 229 |
| Self::Weather { score, .. } |
| 230 |
| Self::Fakespot { score, .. } |
| 231 |
| Self::Dynamic { score, .. } => *score, |
| 232 |
Self::Wikipedia { .. } => DEFAULT_SUGGESTION_SCORE, |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
pub fn fts_match_info(&self) -> Option<&FtsMatchInfo> { |
| 237 |
match self { |
| 238 |
Self::Fakespot { match_info, .. } => match_info.as_ref(), |
| 239 |
_ => None, |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
#[cfg(test)] |
| 245 |
/// Testing utilitise |
| 246 |
impl Suggestion { |
| 247 |
pub fn with_fakespot_keyword_bonus(mut self) -> Self { |
| 248 |
match &mut self { |
| 249 |
Self::Fakespot { score, .. } => { |
| 250 |
*score += 0.01; |
| 251 |
} |
| 252 |
_ => panic!("Not Suggestion::Fakespot"), |
| 253 |
} |
| 254 |
self |
| 255 |
} |
| 256 |
|
| 257 |
pub fn with_fakespot_product_type_bonus(mut self, bonus: f64) -> Self { |
| 258 |
match &mut self { |
| 259 |
Self::Fakespot { score, .. } => { |
| 260 |
*score += 0.001 * bonus; |
| 261 |
} |
| 262 |
_ => panic!("Not Suggestion::Fakespot"), |
| 263 |
} |
| 264 |
self |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
impl Eq for Suggestion {} |
| 269 |
/// Replaces all template parameters in a "raw" sponsored suggestion URL, |
| 270 |
/// producing a "cooked" URL with real values. |
| 271 |
pub(crate) fn cook_raw_suggestion_url(raw_url: &str) -> String { |
| 272 |
let timestamp = Local::now().format("%Y%m%d%H").to_string(); |
| 273 |
debug_assert!(timestamp.len() == TIMESTAMP_LENGTH); |
| 274 |
// "Raw" sponsored suggestion URLs must not contain more than one timestamp |
| 275 |
// template parameter, so we replace just the first occurrence. |
| 276 |
raw_url.replacen(TIMESTAMP_TEMPLATE, ×tamp, 1) |
| 277 |
} |
| 278 |
|
| 279 |
/// Determines whether a "raw" sponsored suggestion URL is equivalent to a |
| 280 |
/// "cooked" URL. The two URLs are equivalent if they are identical except for |
| 281 |
/// their replaced template parameters, which can be different. |
| 282 |
#[uniffi::export] |
| 283 |
pub fn raw_suggestion_url_matches(raw_url: &str, cooked_url: &str) -> bool { |
| 284 |
let Some((raw_url_prefix, raw_url_suffix)) = raw_url.split_once(TIMESTAMP_TEMPLATE) else { |
| 285 |
return raw_url == cooked_url; |
| 286 |
}; |
| 287 |
let (Some(cooked_url_prefix), Some(cooked_url_suffix)) = ( |
| 288 |
cooked_url.get(..raw_url_prefix.len()), |
| 289 |
cooked_url.get(raw_url_prefix.len() + TIMESTAMP_LENGTH..), |
| 290 |
) else { |
| 291 |
return false; |
| 292 |
}; |
| 293 |
if raw_url_prefix != cooked_url_prefix || raw_url_suffix != cooked_url_suffix { |
| 294 |
return false; |
| 295 |
} |
| 296 |
let maybe_timestamp = |
| 297 |
&cooked_url[raw_url_prefix.len()..raw_url_prefix.len() + TIMESTAMP_LENGTH]; |
| 298 |
maybe_timestamp.bytes().all(|b| b.is_ascii_digit()) |
| 299 |
} |
| 300 |
|
| 301 |
#[cfg(test)] |
| 302 |
mod tests { |
| 303 |
use super::*; |
| 304 |
|
| 305 |
#[test] |
| 306 |
fn cook_url_with_template_parameters() { |
| 307 |
let raw_url_with_one_timestamp = "https://example.com?a=%YYYYMMDDHH%"; |
| 308 |
let cooked_url_with_one_timestamp = cook_raw_suggestion_url(raw_url_with_one_timestamp); |
| 309 |
assert_eq!( |
| 310 |
cooked_url_with_one_timestamp.len(), |
| 311 |
raw_url_with_one_timestamp.len() - 2 |
| 312 |
); |
| 313 |
assert_ne!(raw_url_with_one_timestamp, cooked_url_with_one_timestamp); |
| 314 |
|
| 315 |
let raw_url_with_trailing_segment = "https://example.com?a=%YYYYMMDDHH%&b=c"; |
| 316 |
let cooked_url_with_trailing_segment = |
| 317 |
cook_raw_suggestion_url(raw_url_with_trailing_segment); |
| 318 |
assert_eq!( |
| 319 |
cooked_url_with_trailing_segment.len(), |
| 320 |
raw_url_with_trailing_segment.len() - 2 |
| 321 |
); |
| 322 |
assert_ne!( |
| 323 |
raw_url_with_trailing_segment, |
| 324 |
cooked_url_with_trailing_segment |
| 325 |
); |
| 326 |
} |
| 327 |
|
| 328 |
#[test] |
| 329 |
fn cook_url_without_template_parameters() { |
| 330 |
let raw_url_without_timestamp = "https://example.com?b=c"; |
| 331 |
let cooked_url_without_timestamp = cook_raw_suggestion_url(raw_url_without_timestamp); |
| 332 |
assert_eq!(raw_url_without_timestamp, cooked_url_without_timestamp); |
| 333 |
} |
| 334 |
|
| 335 |
#[test] |
| 336 |
fn url_with_template_parameters_matches() { |
| 337 |
let raw_url_with_one_timestamp = "https://example.com?a=%YYYYMMDDHH%"; |
| 338 |
let raw_url_with_trailing_segment = "https://example.com?a=%YYYYMMDDHH%&b=c"; |
| 339 |
|
| 340 |
// Equivalent, except for their replaced template parameters. |
| 341 |
assert!(raw_suggestion_url_matches( |
| 342 |
raw_url_with_one_timestamp, |
| 343 |
"https://example.com?a=0000000000" |
| 344 |
)); |
| 345 |
assert!(raw_suggestion_url_matches( |
| 346 |
raw_url_with_trailing_segment, |
| 347 |
"https://example.com?a=1111111111&b=c" |
| 348 |
)); |
| 349 |
|
| 350 |
// Different lengths. |
| 351 |
assert!(!raw_suggestion_url_matches( |
| 352 |
raw_url_with_one_timestamp, |
| 353 |
"https://example.com?a=1234567890&c=d" |
| 354 |
)); |
| 355 |
assert!(!raw_suggestion_url_matches( |
| 356 |
raw_url_with_one_timestamp, |
| 357 |
"https://example.com?a=123456789" |
| 358 |
)); |
| 359 |
assert!(!raw_suggestion_url_matches( |
| 360 |
raw_url_with_trailing_segment, |
| 361 |
"https://example.com?a=0987654321" |
| 362 |
)); |
| 363 |
assert!(!raw_suggestion_url_matches( |
| 364 |
raw_url_with_trailing_segment, |
| 365 |
"https://example.com?a=0987654321&b=c&d=e" |
| 366 |
)); |
| 367 |
|
| 368 |
// Different query parameter names. |
| 369 |
assert!(!raw_suggestion_url_matches( |
| 370 |
raw_url_with_one_timestamp, // `a`. |
| 371 |
"https://example.com?b=4444444444" // `b`. |
| 372 |
)); |
| 373 |
assert!(!raw_suggestion_url_matches( |
| 374 |
raw_url_with_trailing_segment, // `a&b`. |
| 375 |
"https://example.com?a=5555555555&c=c" // `a&c`. |
| 376 |
)); |
| 377 |
|
| 378 |
// Not a timestamp. |
| 379 |
assert!(!raw_suggestion_url_matches( |
| 380 |
raw_url_with_one_timestamp, |
| 381 |
"https://example.com?a=bcdefghijk" |
| 382 |
)); |
| 383 |
assert!(!raw_suggestion_url_matches( |
| 384 |
raw_url_with_trailing_segment, |
| 385 |
"https://example.com?a=bcdefghijk&b=c" |
| 386 |
)); |
| 387 |
} |
| 388 |
|
| 389 |
#[test] |
| 390 |
fn url_without_template_parameters_matches() { |
| 391 |
let raw_url_without_timestamp = "https://example.com?b=c"; |
| 392 |
|
| 393 |
assert!(raw_suggestion_url_matches( |
| 394 |
raw_url_without_timestamp, |
| 395 |
"https://example.com?b=c" |
| 396 |
)); |
| 397 |
assert!(!raw_suggestion_url_matches( |
| 398 |
raw_url_without_timestamp, |
| 399 |
"http://example.com" |
| 400 |
)); |
| 401 |
assert!(!raw_suggestion_url_matches( |
| 402 |
raw_url_without_timestamp, // `a`. |
| 403 |
"http://example.com?a=c" // `b`. |
| 404 |
)); |
| 405 |
assert!(!raw_suggestion_url_matches( |
| 406 |
raw_url_without_timestamp, |
| 407 |
"https://example.com?b=c&d=e" |
| 408 |
)); |
| 409 |
} |
| 410 |
} |