1pub const LINK_BASE: &str = "https://vectorapp.io/go#";
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum GoLink {
13 Community { community: String },
14 Channel { community: String, channel: String },
15 ChannelMessage { community: String, channel: String, message: String },
16 Message { message: String },
17}
18
19fn id(s: &str) -> Option<String> {
20 (s.len() == 64 && s.bytes().all(|b| b.is_ascii_hexdigit())).then(|| s.to_ascii_lowercase())
21}
22
23impl GoLink {
24 pub fn parse_payload(payload: &str) -> Option<Self> {
26 let mut parts = payload.trim_end_matches('/').split('/');
27 let link = match parts.next()? {
28 "c" => {
29 let community = id(parts.next()?)?;
30 match (parts.next().map(id), parts.next().map(id)) {
31 (None, _) => GoLink::Community { community },
32 (Some(channel), None) => GoLink::Channel { community, channel: channel? },
33 (Some(channel), Some(message)) => GoLink::ChannelMessage { community, channel: channel?, message: message? },
34 }
35 }
36 "m" => GoLink::Message { message: id(parts.next()?)? },
37 _ => return None,
38 };
39 parts.next().is_none().then_some(link)
40 }
41
42 pub fn parse_url(url: &str) -> Option<Self> {
44 Self::parse_payload(go_payload(url)?)
45 }
46
47 pub fn payload(&self) -> String {
48 match self {
49 GoLink::Community { community } => format!("c/{community}"),
50 GoLink::Channel { community, channel } => format!("c/{community}/{channel}"),
51 GoLink::ChannelMessage { community, channel, message } => format!("c/{community}/{channel}/{message}"),
52 GoLink::Message { message } => format!("m/{message}"),
53 }
54 }
55
56 pub fn url(&self) -> String {
57 format!("{LINK_BASE}{}", self.payload())
58 }
59}
60
61pub fn go_payload(url: &str) -> Option<&str> {
64 let (locator, payload) = url.trim().split_once('#')?;
65 match locator.trim_end_matches('/') {
66 "https://vectorapp.io/go" | "http://vectorapp.io/go" | "https://www.vectorapp.io/go" | "vector://go" => Some(payload),
67 "https://web.vectorapp.io" => payload.strip_prefix("go/"),
68 _ => None,
69 }
70}
71
72pub fn is_go_url(url: &str) -> bool {
75 let locator = url.split(['#', '?']).next().unwrap_or("").trim_end_matches('/');
76 matches!(locator, "https://vectorapp.io/go" | "http://vectorapp.io/go" | "https://www.vectorapp.io/go")
77 || url.starts_with("https://web.vectorapp.io/#go/")
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 const A: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
85 const B: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
86 const C: &str = "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc";
87
88 #[test]
89 fn every_shape_round_trips_through_its_url() {
90 let links = [
91 GoLink::Community { community: A.into() },
92 GoLink::Channel { community: A.into(), channel: B.into() },
93 GoLink::ChannelMessage { community: A.into(), channel: B.into(), message: C.into() },
94 GoLink::Message { message: C.into() },
95 ];
96 for link in links {
97 assert_eq!(GoLink::parse_url(&link.url()), Some(link.clone()));
98 assert_eq!(GoLink::parse_url(&format!("vector://go#{}", link.payload())), Some(link.clone()));
99 assert_eq!(GoLink::parse_url(&format!("https://web.vectorapp.io/#go/{}", link.payload())), Some(link.clone()));
100 }
101 }
102
103 #[test]
104 fn ids_must_be_whole_hex_and_shapes_exact() {
105 let upper = format!("https://vectorapp.io/go#c/{}", A.to_uppercase());
106 assert_eq!(GoLink::parse_url(&upper), Some(GoLink::Community { community: A.into() }), "case folds");
107 for bad in [
108 format!("https://vectorapp.io/go#c/{}", &A[1..]),
109 format!("https://vectorapp.io/go#c/{A}/{B}/{C}/{A}"),
110 format!("https://vectorapp.io/go#m/{A}/{B}"),
111 format!("https://vectorapp.io/go#x/{A}"),
112 format!("https://vectorapp.io/go#c/{}g", &A[1..]),
113 format!("https://evil.example/go#c/{A}"),
114 format!("https://vectorapp.io/invite#c/{A}"),
115 format!("https://web.vectorapp.io/#c/{A}"),
116 ] {
117 assert_eq!(GoLink::parse_url(&bad), None, "{bad}");
118 }
119 }
120
121 #[test]
122 fn only_vector_links_skip_previews() {
123 assert!(is_go_url(&format!("https://vectorapp.io/go#c/{A}")));
124 assert!(is_go_url("https://vectorapp.io/go"));
125 assert!(is_go_url(&format!("https://web.vectorapp.io/#go/m/{A}")));
126 assert!(!is_go_url("https://vectorapp.io/"));
127 assert!(!is_go_url("https://vectorapp.io/gopher"));
128 assert!(!is_go_url("https://example.com/go#c/x"));
129 }
130}