use serde::Serialize;
use crate::golink::GoLink;
pub const WEB_BASE: &str = "https://web.vectorapp.io/#";
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct DeepLinkAction {
pub action_type: String,
pub target: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub call: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub account: Option<String>,
}
fn action(action_type: &str, target: impl Into<String>) -> DeepLinkAction {
DeepLinkAction { action_type: action_type.to_string(), target: target.into(), ..Default::default() }
}
pub fn parse_deep_link(url: &str) -> Option<DeepLinkAction> {
let url = url.trim();
if let Some(link) = GoLink::parse_url(url) {
return Some(action("go", link.payload()));
}
if let Some(payload) = crate::golink::go_payload(url) {
return parse_path(payload).filter(|a| in_app_only(&a.action_type));
}
if let Some((before, frag)) = url.split_once('#') {
if !frag.is_empty() && is_invite_locator(before) {
return Some(action("community_invite", url));
}
}
if let Some(path) = url.strip_prefix(WEB_BASE).or_else(|| url.strip_prefix("vector://")) {
return parse_path(path);
}
let path = url.strip_prefix("https://vectorapp.io/").or_else(|| url.strip_prefix("http://vectorapp.io/"))?;
parse_path(path).filter(|a| !in_app_only(&a.action_type))
}
fn in_app_only(action_type: &str) -> bool {
matches!(action_type, "settings" | "nexus" | "requests")
}
fn parse_path(path: &str) -> Option<DeepLinkAction> {
let path = path.split(['?', '#']).next().unwrap_or_default();
let segments: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
match segments.as_slice() {
["profile", npub, ..] => valid_npub(npub).then(|| action("profile", *npub)),
["dm", npub] => valid_npub(npub).then(|| action("chat", *npub)),
["emojis", "pack", naddr, ..] => {
let naddr = strip_html_suffix(naddr);
valid_naddr(naddr).then(|| action("emoji_pack", naddr))
}
["settings", rest @ ..] => (rest.len() <= 3 && rest.iter().all(|s| settings_id(s))).then(|| action("settings", rest.join("/"))),
["nexus"] => Some(action("nexus", "")),
["nexus", app] => crate::webxdc::is_safe_app_id(app).then(|| action("nexus", *app)),
["requests"] => Some(action("requests", "")),
_ => None,
}
}
fn valid_npub(npub: &str) -> bool {
npub.starts_with("npub1") && npub.len() == 63
}
fn valid_naddr(naddr: &str) -> bool {
naddr.starts_with("naddr1") && naddr.len() >= 30
}
fn settings_id(s: &str) -> bool {
s.len() <= 32 && s.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
}
fn is_invite_locator(before: &str) -> bool {
match before.rsplit_once("/invite/") {
Some((_, naddr)) => valid_naddr(strip_html_suffix(naddr.trim_end_matches('/'))),
None => false,
}
}
fn strip_html_suffix(s: &str) -> &str {
s.strip_suffix(".html").unwrap_or(s)
}
#[cfg(test)]
mod tests {
use super::*;
const NADDR: &str = "naddr1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq";
fn npub() -> String {
format!("npub1{}", "q".repeat(58))
}
fn opens(url: &str) -> Option<(String, String)> {
parse_deep_link(url).map(|a| (a.action_type, a.target))
}
#[test]
fn every_place_opens_from_the_app_scheme_the_website_and_vector_web() {
let npub = npub();
let places = [
(format!("profile/{npub}"), "profile", npub.clone()),
(format!("dm/{npub}"), "chat", npub.clone()),
(format!("emojis/pack/{NADDR}"), "emoji_pack", NADDR.to_string()),
("settings".to_string(), "settings", String::new()),
("settings/network".to_string(), "settings", "network".to_string()),
("settings/network/relays".to_string(), "settings", "network/relays".to_string()),
("settings/appearance/display/composer".to_string(), "settings", "appearance/display/composer".to_string()),
("nexus".to_string(), "nexus", String::new()),
("nexus/chess.v2_beta".to_string(), "nexus", "chess.v2_beta".to_string()),
("requests".to_string(), "requests", String::new()),
];
for (path, kind, target) in places {
let web_link = if in_app_only(kind) { format!("https://vectorapp.io/go#{path}") } else { format!("https://vectorapp.io/{path}") };
for url in [format!("vector://{path}"), web_link, format!("{WEB_BASE}{path}")] {
assert_eq!(opens(&url), Some((kind.to_string(), target.clone())), "{url}");
}
}
}
#[test]
fn the_website_keeps_its_paths_for_places_it_has_no_page_for() {
for path in ["settings/privacy", "nexus/chess", "requests"] {
assert_eq!(parse_deep_link(&format!("https://vectorapp.io/{path}")), None, "{path}");
assert!(parse_deep_link(&format!("vector://go#{path}")).is_some(), "{path}");
assert!(parse_deep_link(&format!("{WEB_BASE}go/{path}")).is_some(), "{path}");
}
assert_eq!(parse_deep_link(&format!("https://vectorapp.io/go#profile/{}", npub())), None, "a go link carries only in-app places");
assert!(crate::golink::is_go_url("https://vectorapp.io/go#settings/privacy"), "and is never fetched for a preview");
}
#[test]
fn a_place_that_names_something_malformed_opens_nothing() {
for url in [
"vector://profile/notanpub".to_string(),
format!("vector://dm/{}/extra", npub()),
"vector://emojis/pack/naddr1short".to_string(),
"vector://settings/network/relays/more/deeper".to_string(),
"vector://settings/Network".to_string(),
"vector://settings/../danger".to_string(),
"vector://nexus/../../etc".to_string(),
"vector://nexus/a/b".to_string(),
"vector://requests/x".to_string(),
"vector://unknown".to_string(),
format!("https://evil.example/profile/{}", npub()),
format!("https://web.vectorapp.io/profile/{}", npub()),
] {
assert_eq!(parse_deep_link(&url), None, "{url}");
}
}
#[test]
fn a_query_or_fragment_after_a_place_is_ignored() {
assert_eq!(opens("vector://settings/network?from=docs#top"), Some(("settings".into(), "network".into())));
assert_eq!(opens(&format!("https://vectorapp.io/emojis/pack/{NADDR}.html")), Some(("emoji_pack".into(), NADDR.into())));
}
#[test]
fn a_fragment_only_invite_link_is_not_an_invite() {
for url in ["vector://invite#AgEs3q1MZz0", "https://vectorapp.io/invite#AgEs3q1MZz0"] {
assert!(parse_deep_link(url).map(|a| a.action_type) != Some("community_invite".into()), "{url}");
}
}
#[test]
fn v2_naddr_invite_parses_from_every_host() {
for url in [
format!("vector://invite/{NADDR}#BAHs3q1MZz0aBcDeF"),
format!("https://vectorapp.io/invite/{NADDR}#BAHs3q1MZz0aBcDeF"),
format!("https://armada.buzz/invite/{NADDR}#BAHs3q1MZz0aBcDeF"),
] {
let action = parse_deep_link(&url).unwrap_or_else(|| panic!("{url}"));
assert_eq!(action.action_type, "community_invite");
assert_eq!(action.target, url, "join flow re-parses the full URL");
}
}
#[test]
fn invite_without_fragment_is_not_an_invite() {
assert!(parse_deep_link("vector://invite").is_none());
}
#[test]
fn invite_path_that_is_not_an_naddr_is_not_an_invite() {
assert!(parse_deep_link("https://vectorapp.io/invite/npub1abc#frag").is_none());
assert!(parse_deep_link("https://evil.example/invite/naddr1#frag").is_none());
}
#[test]
fn fragment_on_non_invite_paths_does_not_hijack() {
assert!(parse_deep_link("vector://profile/notanpub#x").is_none());
let npub = npub();
assert_eq!(opens(&format!("vector://profile/{npub}")), Some(("profile".into(), npub)));
}
#[test]
fn a_vector_link_opens_from_every_carrier() {
let payload = format!("c/{}/{}", "a".repeat(64), "b".repeat(64));
for url in [format!("https://vectorapp.io/go#{payload}"), format!("vector://go#{payload}"), format!("{WEB_BASE}go/{payload}")] {
assert_eq!(opens(&url), Some(("go".into(), payload.clone())), "{url}");
}
assert!(parse_deep_link("https://vectorapp.io/go#c/not-an-id").is_none());
assert!(parse_deep_link("https://vectorapp.io/go").is_none());
}
}