//! Profile metadata extraction from found pages, plus pivot handles.
pub mod html;
pub mod json;
pub mod links;
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::db::Site;
use crate::engine::http::Exchange;
use crate::util;
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Profile {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bio: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub avatar: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub location: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub website: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub joined: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub followers: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub following: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
/// External links found on the profile (social hosts only).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub links: Vec<String>,
/// Handles worth pivoting on.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub handles: Vec<String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub extra: BTreeMap<String, String>,
}
impl Profile {
pub fn is_empty(&self) -> bool {
self.name.is_none()
&& self.bio.is_none()
&& self.avatar.is_none()
&& self.location.is_none()
&& self.website.is_none()
&& self.joined.is_none()
&& self.followers.is_none()
&& self.following.is_none()
&& self.email.is_none()
&& self.links.is_empty()
&& self.handles.is_empty()
&& self.extra.is_empty()
}
/// Compact one-line description for terminal output.
pub fn summary(&self, max_bio: usize) -> String {
let mut parts: Vec<String> = Vec::new();
if let Some(n) = &self.name {
parts.push(n.clone());
}
if let Some(b) = &self.bio {
parts.push(format!("“{}”", util::truncate(b, max_bio)));
}
if let Some(l) = &self.location {
parts.push(format!("📍 {l}"));
}
if let Some(f) = self.followers {
parts.push(format!("{} followers", util::humanize_count(f)));
}
if let Some(j) = &self.joined {
parts.push(format!("joined {j}"));
}
if let Some(e) = &self.email {
parts.push(e.clone());
}
if let Some(w) = &self.website {
parts.push(w.clone());
}
parts.join(" · ")
}
/// Fields worth showing, in display order.
pub fn fields(&self) -> Vec<(&'static str, String)> {
let mut v = Vec::new();
if let Some(x) = &self.name {
v.push(("name", x.clone()));
}
if let Some(x) = &self.bio {
v.push(("bio", x.clone()));
}
if let Some(x) = &self.location {
v.push(("location", x.clone()));
}
if let Some(x) = &self.website {
v.push(("website", x.clone()));
}
if let Some(x) = &self.email {
v.push(("email", x.clone()));
}
if let Some(x) = self.followers {
v.push(("followers", x.to_string()));
}
if let Some(x) = self.following {
v.push(("following", x.to_string()));
}
if let Some(x) = &self.joined {
v.push(("joined", x.clone()));
}
if let Some(x) = &self.avatar {
v.push(("avatar", x.clone()));
}
v
}
}
/// Extract whatever the response reveals about the account holder.
pub fn extract(site: &Site, ex: &Exchange, account: &str) -> Option<Profile> {
let body = ex.body.as_str();
if body.trim().is_empty() {
return None;
}
let ct = ex
.content_type
.as_deref()
.unwrap_or("")
.to_ascii_lowercase();
let trimmed = body.trim_start();
let is_json = ct.contains("json")
|| ((trimmed.starts_with('{') || trimmed.starts_with('[')) && !ct.contains("html"));
let (mut profile, raw_links, strong_links) = if is_json {
let (p, l) = json::extract(body, account);
(p, l, Vec::new())
} else {
html::extract(body)
};
let self_host = site.host();
let (links, mut handles) = links::pivot(&raw_links, &strong_links, &self_host, account);
profile.links = links;
// A site's own social accounts (opensea on opensea.io, ifttt on ifttt.com) are chrome, not pivots.
let brand = self_host
.split('.')
.next()
.unwrap_or("")
.to_ascii_lowercase();
if brand.len() >= 4 {
handles.retain(|h| {
!h.to_ascii_lowercase()
.replace(['_', '-', '.'], "")
.contains(&brand)
});
}
if let Some(bio) = &profile.bio {
for h in links::mentions(bio) {
if !h.eq_ignore_ascii_case(account)
&& !handles.iter().any(|x| x.eq_ignore_ascii_case(&h))
{
handles.push(h);
}
}
}
handles.truncate(20);
profile.handles = handles;
if profile.is_empty() {
None
} else {
Some(profile)
}
}