Skip to content

Commit bc92578

Browse files
authored
Add rich link previews (#1334)
1 parent 826d735 commit bc92578

12 files changed

Lines changed: 1787 additions & 7 deletions

File tree

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
use std::time::Duration;
2+
3+
use futures_util::StreamExt;
4+
use reqwest::{
5+
header::{ACCEPT, CONTENT_TYPE, USER_AGENT},
6+
redirect::Policy,
7+
};
8+
use url::Url;
9+
10+
const MAX_TITLE_FETCH_BYTES: usize = 256 * 1024;
11+
const TITLE_FETCH_TIMEOUT: Duration = Duration::from_secs(4);
12+
13+
#[tauri::command]
14+
pub async fn fetch_link_preview_title(href: String) -> Result<Option<String>, String> {
15+
let url = Url::parse(href.trim()).map_err(|error| format!("invalid URL: {error}"))?;
16+
if !is_supported_google_link(&url) {
17+
return Ok(None);
18+
}
19+
20+
let client = reqwest::Client::builder()
21+
.redirect(Policy::none())
22+
.pool_idle_timeout(Duration::from_secs(10))
23+
.pool_max_idle_per_host(1)
24+
.build()
25+
.map_err(|error| format!("link preview title client failed: {error}"))?;
26+
27+
let request = client
28+
.get(url.as_str())
29+
.header(
30+
ACCEPT,
31+
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
32+
)
33+
.header(USER_AGENT, "Buzz Desktop link preview");
34+
35+
let response = tokio::time::timeout(TITLE_FETCH_TIMEOUT, request.send())
36+
.await
37+
.map_err(|_| "link preview title request timed out".to_string())?
38+
.map_err(|error| format!("link preview title request failed: {error}"))?;
39+
40+
if !response.status().is_success() {
41+
return Ok(None);
42+
}
43+
44+
let is_html = response
45+
.headers()
46+
.get(CONTENT_TYPE)
47+
.and_then(|value| value.to_str().ok())
48+
.map(|value| value.to_ascii_lowercase().contains("text/html"))
49+
.unwrap_or(true);
50+
if !is_html {
51+
return Ok(None);
52+
}
53+
54+
let body = read_limited_text(response).await?;
55+
Ok(extract_google_title(&body))
56+
}
57+
58+
fn is_supported_google_link(url: &Url) -> bool {
59+
if url.scheme() != "https" {
60+
return false;
61+
}
62+
63+
let Some(host) = url.host_str().map(|host| host.to_ascii_lowercase()) else {
64+
return false;
65+
};
66+
let segments = url
67+
.path_segments()
68+
.map(|segments| segments.collect::<Vec<_>>())
69+
.unwrap_or_default();
70+
71+
match host.trim_start_matches("www.") {
72+
"docs.google.com" => {
73+
matches!(
74+
segments.as_slice(),
75+
["document", "d", _, ..]
76+
| ["spreadsheets", "d", _, ..]
77+
| ["presentation", "d", _, ..]
78+
)
79+
}
80+
"drive.google.com" => {
81+
matches!(segments.as_slice(), ["file", "d", _, ..])
82+
|| matches!(segments.as_slice(), ["drive", "folders", _, ..])
83+
|| (segments.first() == Some(&"open")
84+
&& url.query_pairs().any(|(key, _)| key == "id"))
85+
}
86+
_ => false,
87+
}
88+
}
89+
90+
async fn read_limited_text(response: reqwest::Response) -> Result<String, String> {
91+
let mut stream = response.bytes_stream();
92+
let mut bytes = Vec::new();
93+
94+
while let Some(chunk) = stream.next().await {
95+
let chunk = chunk.map_err(|error| format!("reading title response failed: {error}"))?;
96+
if bytes.len() + chunk.len() > MAX_TITLE_FETCH_BYTES {
97+
let remaining = MAX_TITLE_FETCH_BYTES.saturating_sub(bytes.len());
98+
bytes.extend_from_slice(&chunk[..remaining]);
99+
break;
100+
}
101+
bytes.extend_from_slice(&chunk);
102+
}
103+
104+
Ok(String::from_utf8_lossy(&bytes).into_owned())
105+
}
106+
107+
fn extract_google_title(html: &str) -> Option<String> {
108+
extract_meta_title(html)
109+
.or_else(|| extract_title_tag(html))
110+
.and_then(|title| normalize_google_title(&title))
111+
}
112+
113+
fn extract_meta_title(html: &str) -> Option<String> {
114+
let lower = html.to_ascii_lowercase();
115+
let mut search_from = 0;
116+
117+
while let Some(relative_start) = lower[search_from..].find("<meta") {
118+
let start = search_from + relative_start;
119+
let Some(relative_end) = lower[start..].find('>') else {
120+
break;
121+
};
122+
let end = start + relative_end + 1;
123+
let tag = &html[start..end];
124+
let lower_tag = &lower[start..end];
125+
126+
if lower_tag.contains("og:title") || lower_tag.contains("twitter:title") {
127+
if let Some(content) = attr_value(tag, "content") {
128+
return Some(content);
129+
}
130+
}
131+
132+
search_from = end;
133+
}
134+
135+
None
136+
}
137+
138+
fn extract_title_tag(html: &str) -> Option<String> {
139+
let lower = html.to_ascii_lowercase();
140+
let start = lower.find("<title")?;
141+
let content_start = start + lower[start..].find('>')? + 1;
142+
let content_end = content_start + lower[content_start..].find("</title>")?;
143+
Some(html[content_start..content_end].to_string())
144+
}
145+
146+
fn attr_value(tag: &str, attr: &str) -> Option<String> {
147+
let lower = tag.to_ascii_lowercase();
148+
let attr = attr.to_ascii_lowercase();
149+
let mut search_from = 0;
150+
151+
while let Some(relative_start) = lower[search_from..].find(&attr) {
152+
let name_start = search_from + relative_start;
153+
let name_end = name_start + attr.len();
154+
let before = lower[..name_start].chars().last();
155+
let after = lower[name_end..].chars().next();
156+
let has_name_boundary = !matches!(before, Some(c) if c.is_ascii_alphanumeric() || c == '-' || c == '_')
157+
&& !matches!(after, Some(c) if c.is_ascii_alphanumeric() || c == '-' || c == '_');
158+
159+
if has_name_boundary {
160+
let lower_rest = &lower[name_end..];
161+
let equals_offset = lower_rest.find('=')?;
162+
let value_start = name_end + equals_offset + 1;
163+
let value = tag[value_start..].trim_start();
164+
let quote = value.chars().next()?;
165+
166+
if quote == '"' || quote == '\'' {
167+
let value_body = &value[quote.len_utf8()..];
168+
let value_end = value_body.find(quote)?;
169+
return Some(decode_html_entities(&value_body[..value_end]));
170+
}
171+
172+
let value_end = value
173+
.find(|c: char| c.is_ascii_whitespace() || c == '>')
174+
.unwrap_or(value.len());
175+
return Some(decode_html_entities(&value[..value_end]));
176+
}
177+
178+
search_from = name_end;
179+
}
180+
181+
None
182+
}
183+
184+
fn normalize_google_title(raw_title: &str) -> Option<String> {
185+
let mut title = decode_html_entities(raw_title)
186+
.split_whitespace()
187+
.collect::<Vec<_>>()
188+
.join(" ");
189+
190+
for suffix in [
191+
" - Google Docs",
192+
" - Google Sheets",
193+
" - Google Slides",
194+
" - Google Drive",
195+
] {
196+
if let Some(stripped) = title.strip_suffix(suffix) {
197+
title = stripped.trim().to_string();
198+
break;
199+
}
200+
}
201+
202+
match title.as_str() {
203+
""
204+
| "Document"
205+
| "Spreadsheet"
206+
| "Presentation"
207+
| "Drive file"
208+
| "Drive folder"
209+
| "Google Docs"
210+
| "Google Sheets"
211+
| "Google Slides"
212+
| "Google Drive"
213+
| "Sign in - Google Accounts" => None,
214+
_ => Some(title.chars().take(180).collect()),
215+
}
216+
}
217+
218+
fn decode_html_entities(value: &str) -> String {
219+
let mut decoded = value
220+
.replace("&amp;", "&")
221+
.replace("&quot;", "\"")
222+
.replace("&#39;", "'")
223+
.replace("&#x27;", "'")
224+
.replace("&apos;", "'")
225+
.replace("&lt;", "<")
226+
.replace("&gt;", ">");
227+
228+
while let Some(start) = decoded.find("&#") {
229+
let Some(relative_end) = decoded[start..].find(';') else {
230+
break;
231+
};
232+
let end = start + relative_end + 1;
233+
let entity = &decoded[start + 2..end - 1];
234+
let parsed = if let Some(hex) = entity
235+
.strip_prefix('x')
236+
.or_else(|| entity.strip_prefix('X'))
237+
{
238+
u32::from_str_radix(hex, 16).ok()
239+
} else {
240+
entity.parse::<u32>().ok()
241+
};
242+
243+
let Some(ch) = parsed.and_then(char::from_u32) else {
244+
break;
245+
};
246+
decoded.replace_range(start..end, &ch.to_string());
247+
}
248+
249+
decoded
250+
}
251+
252+
#[cfg(test)]
253+
mod tests {
254+
use super::{extract_google_title, is_supported_google_link};
255+
use url::Url;
256+
257+
#[test]
258+
fn title_prefers_open_graph_title() {
259+
let html = r#"
260+
<html>
261+
<head>
262+
<meta property="og:title" content="Composer links &amp; previews - Google Docs">
263+
<title>Fallback - Google Docs</title>
264+
</head>
265+
</html>
266+
"#;
267+
268+
assert_eq!(
269+
extract_google_title(html).as_deref(),
270+
Some("Composer links & previews")
271+
);
272+
}
273+
274+
#[test]
275+
fn title_ignores_generic_google_titles() {
276+
assert_eq!(
277+
extract_google_title("<title>Sign in - Google Accounts</title>"),
278+
None
279+
);
280+
assert_eq!(extract_google_title("<title>Google Docs</title>"), None);
281+
}
282+
283+
#[test]
284+
fn supported_urls_are_google_file_links_only() {
285+
assert!(is_supported_google_link(
286+
&Url::parse("https://docs.google.com/document/d/abc/edit").unwrap()
287+
));
288+
assert!(is_supported_google_link(
289+
&Url::parse("https://docs.google.com/spreadsheets/d/abc/edit").unwrap()
290+
));
291+
assert!(is_supported_google_link(
292+
&Url::parse("https://drive.google.com/file/d/abc/view").unwrap()
293+
));
294+
assert!(!is_supported_google_link(
295+
&Url::parse("https://example.com/document/d/abc/edit").unwrap()
296+
));
297+
assert!(!is_supported_google_link(
298+
&Url::parse("http://docs.google.com/document/d/abc/edit").unwrap()
299+
));
300+
}
301+
}

‎desktop/src-tauri/src/commands/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod export_util;
1111
mod identity;
1212
mod identity_archive;
1313
mod legacy_storage;
14+
mod link_preview;
1415
mod media;
1516
mod media_download;
1617
mod media_transcode;
@@ -41,6 +42,7 @@ pub use engrams::*;
4142
pub use identity::*;
4243
pub use identity_archive::*;
4344
pub use legacy_storage::*;
45+
pub use link_preview::*;
4446
pub use media::*;
4547
pub use media_download::*;
4648
#[cfg(feature = "mesh-llm")]

‎desktop/src-tauri/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ pub fn run() {
445445
get_relay_ws_url,
446446
get_relay_http_url,
447447
get_media_proxy_port,
448+
fetch_link_preview_title,
448449
discover_acp_providers,
449450
install_acp_runtime,
450451
discover_managed_agent_prereqs,

‎desktop/src/features/messages/lib/linkInteractionExtension.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ type LinkInteractionExtensionOptions = {
1414

1515
export const linkInteractionKey = new PluginKey("linkInteraction");
1616

17+
const PASTED_LINK_AT_END_RE =
18+
/(?:^|\s)((?:https?:\/\/|www\.)[^\s]+|(?:github\.com|linear\.app|drive\.google\.com|docs\.google\.com)\/[^\s]+)$/i;
19+
20+
function isPasteEndingWithLink(text: string): boolean {
21+
const trimmedEnd = text.trimEnd();
22+
if (!trimmedEnd || trimmedEnd.length !== text.length) return false;
23+
return PASTED_LINK_AT_END_RE.test(trimmedEnd);
24+
}
25+
1726
/**
1827
* Centralises composer link interactions that depend on ProseMirror editor
1928
* state: click interception, click-vs-drag selection preservation, and active
@@ -27,6 +36,8 @@ export function createLinkInteractionExtension({
2736
name: "linkInteraction",
2837

2938
addProseMirrorPlugins() {
39+
let suppressSelectionCardUntil = 0;
40+
3041
return [
3142
new Plugin({
3243
key: linkInteractionKey,
@@ -42,11 +53,22 @@ export function createLinkInteractionExtension({
4253
) {
4354
return;
4455
}
45-
notifyLinkSelection(nextState, getSelectionChangeHandler);
56+
notifyLinkSelection(nextState, getSelectionChangeHandler, {
57+
suppressLinkedSelection:
58+
Date.now() < suppressSelectionCardUntil,
59+
});
4660
},
4761
};
4862
},
4963
props: {
64+
handlePaste(_view, event) {
65+
const pastedText =
66+
event.clipboardData?.getData("text/plain") ?? "";
67+
if (isPasteEndingWithLink(pastedText)) {
68+
suppressSelectionCardUntil = Date.now() + 500;
69+
}
70+
return false;
71+
},
5072
handleDOMEvents: {
5173
// Native anchor default can still win in the WebView before
5274
// ProseMirror's semantic click hook runs, so intercept editor
@@ -98,14 +120,16 @@ export function createLinkInteractionExtension({
98120
function notifyLinkSelection(
99121
state: EditorState,
100122
getSelectionChangeHandler: LinkInteractionExtensionOptions["getSelectionChangeHandler"],
123+
options?: { suppressLinkedSelection?: boolean },
101124
) {
102125
const handler = getSelectionChangeHandler();
103126
if (!handler) return;
104127
if (!state.selection.empty) {
105128
handler(null);
106129
return;
107130
}
108-
handler(resolveLinkAt(state, state.selection.from));
131+
const info = resolveLinkAt(state, state.selection.from);
132+
handler(options?.suppressLinkedSelection && info ? null : info);
109133
}
110134

111135
function handleLinkClick({

0 commit comments

Comments
 (0)