Merge pull request #88 from dyc3/confirmation-details

Parse confirmation descriptions
This commit is contained in:
Carson McManus 2021-08-14 12:33:41 -04:00 committed by GitHub
commit f668004305
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 4 deletions

View file

@ -10,24 +10,28 @@ pub fn demo_confirmation_menu() {
key: 12345, key: 12345,
conf_type: ConfirmationType::Trade, conf_type: ConfirmationType::Trade,
creator: 09870987, creator: 09870987,
description: "example confirmation".into(),
}, },
Confirmation { Confirmation {
id: 1234, id: 1234,
key: 12345, key: 12345,
conf_type: ConfirmationType::MarketSell, conf_type: ConfirmationType::MarketSell,
creator: 09870987, creator: 09870987,
description: "example confirmation".into(),
}, },
Confirmation { Confirmation {
id: 1234, id: 1234,
key: 12345, key: 12345,
conf_type: ConfirmationType::AccountRecovery, conf_type: ConfirmationType::AccountRecovery,
creator: 09870987, creator: 09870987,
description: "example confirmation".into(),
}, },
Confirmation { Confirmation {
id: 1234, id: 1234,
key: 12345, key: 12345,
conf_type: ConfirmationType::Trade, conf_type: ConfirmationType::Trade,
creator: 09870987, creator: 09870987,
description: "example confirmation".into(),
}, },
]); ]);
println!("accept: {}, deny: {}", accept.len(), deny.len()); println!("accept: {}, deny: {}", accept.len(), deny.len());

View file

@ -213,8 +213,14 @@ pub fn prompt_confirmation_menu(
} }
return ( return (
to_accept_idx.iter().map(|i| confirmations[*i]).collect(), to_accept_idx
to_deny_idx.iter().map(|i| confirmations[*i]).collect(), .iter()
.map(|i| confirmations[*i].clone())
.collect(),
to_deny_idx
.iter()
.map(|i| confirmations[*i].clone())
.collect(),
); );
} }

View file

@ -1,17 +1,18 @@
/// A mobile confirmation. There are multiple things that can be confirmed, like trade offers. /// A mobile confirmation. There are multiple things that can be confirmed, like trade offers.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct Confirmation { pub struct Confirmation {
pub id: u64, pub id: u64,
pub key: u64, pub key: u64,
/// Trade offer ID or market transaction ID /// Trade offer ID or market transaction ID
pub creator: u64, pub creator: u64,
pub conf_type: ConfirmationType, pub conf_type: ConfirmationType,
pub description: String,
} }
impl Confirmation { impl Confirmation {
/// Human readable representation of this confirmation. /// Human readable representation of this confirmation.
pub fn description(&self) -> String { pub fn description(&self) -> String {
format!("{:?} id={} key={}", self.conf_type, self.id, self.key) format!("{:?} - {}", self.conf_type, self.description)
} }
} }

View file

@ -3,6 +3,7 @@ use anyhow::Result;
pub use confirmation::{Confirmation, ConfirmationType}; pub use confirmation::{Confirmation, ConfirmationType};
use hmacsha1::hmac_sha1; use hmacsha1::hmac_sha1;
use log::*; use log::*;
use regex::Regex;
use reqwest::{ use reqwest::{
cookie::CookieStore, cookie::CookieStore,
header::{COOKIE, USER_AGENT}, header::{COOKIE, USER_AGENT},
@ -272,8 +273,18 @@ fn parse_confirmations(text: String) -> anyhow::Result<Vec<Confirmation>> {
let fragment = Html::parse_fragment(&text); let fragment = Html::parse_fragment(&text);
let selector = Selector::parse(".mobileconf_list_entry").unwrap(); let selector = Selector::parse(".mobileconf_list_entry").unwrap();
let desc_selector = Selector::parse(".mobileconf_list_entry_description").unwrap();
let mut confirmations = vec![]; let mut confirmations = vec![];
for elem in fragment.select(&selector) { for elem in fragment.select(&selector) {
let desc: String = elem
.select(&desc_selector)
.next()
.unwrap()
.text()
.map(|t| t.trim())
.filter(|t| t.len() > 0)
.collect::<Vec<_>>()
.join(" ");
let conf = Confirmation { let conf = Confirmation {
id: elem.value().attr("data-confid").unwrap().parse()?, id: elem.value().attr("data-confid").unwrap().parse()?,
key: elem.value().attr("data-key").unwrap().parse()?, key: elem.value().attr("data-key").unwrap().parse()?,
@ -284,6 +295,7 @@ fn parse_confirmations(text: String) -> anyhow::Result<Vec<Confirmation>> {
.try_into() .try_into()
.unwrap_or(ConfirmationType::Unknown), .unwrap_or(ConfirmationType::Unknown),
creator: elem.value().attr("data-creator").unwrap().parse()?, creator: elem.value().attr("data-creator").unwrap().parse()?,
description: desc,
}; };
confirmations.push(conf); confirmations.push(conf);
} }
@ -338,6 +350,7 @@ mod tests {
key: 15509106087034649470, key: 15509106087034649470,
conf_type: ConfirmationType::MarketSell, conf_type: ConfirmationType::MarketSell,
creator: 3392884950693131245, creator: 3392884950693131245,
description: "Sell - Summer 2021 - Horror $0.05 ($0.03) 2 minutes ago".into(),
} }
); );
assert_eq!( assert_eq!(
@ -347,6 +360,7 @@ mod tests {
key: 2661901169510258722, key: 2661901169510258722,
conf_type: ConfirmationType::MarketSell, conf_type: ConfirmationType::MarketSell,
creator: 3392884950693130525, creator: 3392884950693130525,
description: "Sell - Summer 2021 - Horror $0.05 ($0.03) 2 minutes ago".into(),
} }
); );
assert_eq!( assert_eq!(
@ -356,6 +370,7 @@ mod tests {
key: 15784514761287735229, key: 15784514761287735229,
conf_type: ConfirmationType::MarketSell, conf_type: ConfirmationType::MarketSell,
creator: 3392884950693129565, creator: 3392884950693129565,
description: "Sell - Summer 2021 - Horror $0.05 ($0.03) 2 minutes ago".into(),
} }
); );
assert_eq!( assert_eq!(
@ -365,6 +380,7 @@ mod tests {
key: 5049250785011653560, key: 5049250785011653560,
conf_type: ConfirmationType::MarketSell, conf_type: ConfirmationType::MarketSell,
creator: 3392884950693128685, creator: 3392884950693128685,
description: "Sell - Summer 2021 - Rogue $0.05 ($0.03) 2 minutes ago".into(),
} }
); );
assert_eq!( assert_eq!(
@ -374,6 +390,24 @@ mod tests {
key: 6133112455066694993, key: 6133112455066694993,
conf_type: ConfirmationType::MarketSell, conf_type: ConfirmationType::MarketSell,
creator: 3392884950693127345, creator: 3392884950693127345,
description: "Sell - Summer 2021 - Horror $0.05 ($0.03) 2 minutes ago".into(),
}
);
}
#[test]
fn test_parse_phone_number_change() {
let text = include_str!("fixtures/confirmations/phone-number-change.html");
let confirmations = parse_confirmations(text.into()).unwrap();
assert_eq!(confirmations.len(), 1);
assert_eq!(
confirmations[0],
Confirmation {
id: 9931444017,
key: 9746021299562127894,
conf_type: ConfirmationType::AccountRecovery,
creator: 2861625242839108895,
description: "Account recovery Just now".into(),
} }
); );
} }