From 9dc027c0c004aa987cdcecee3ec3b6c3bd6c7d04 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 07:14:12 +0000 Subject: [PATCH] link previews: scan only the links tried Every link was collected before the first three were tried, and a run of bare https:// names one link per byte, each running to the end of the text: 160 KB of it is 20,000 copies, 1.6 GB in all. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Qq6RetJnzXvEeXAvSsgvs1 --- crates/vector-web/src/chat_ops.rs | 10 ++++++---- src-tauri/src/message/mod.rs | 5 ++--- src-tauri/src/util.rs | 8 +++++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/crates/vector-web/src/chat_ops.rs b/crates/vector-web/src/chat_ops.rs index efda81bce..3ba31e557 100644 --- a/crates/vector-web/src/chat_ops.rs +++ b/crates/vector-web/src/chat_ops.rs @@ -364,11 +364,13 @@ async fn get_messages_around_id(chat_id: &str, target: &str, context_before: usi const INVITE_PREFIXES: [&str; 2] = ["https://vectorapp.io/invite", "https://www.vectorapp.io/invite"]; -fn extract_https_urls(text: &str) -> Vec { +/// The first `max`: a run of bare `https://` names one link per byte, each running to the end. +fn extract_https_urls(text: &str, max: usize) -> Vec { const DELIMITERS: &[u8] = b" \t\n\r\"<>)]}|"; let mut urls = Vec::new(); let mut from = 0; - while let Some(i) = text[from..].find("https://") { + while urls.len() < max { + let Some(i) = text[from..].find("https://") else { break }; let start = from + i; let rest = text[start..].as_bytes(); let mut end = rest.iter().position(|b| DELIMITERS.contains(b)).unwrap_or(rest.len()); @@ -436,8 +438,8 @@ async fn site_metadata(url: &str) -> Result { async fn fetch_msg_metadata(chat_id: String, msg_id: String) -> bool { let Some((_, message)) = VectorCore.get_message(&msg_id).await else { return false }; - let urls = extract_https_urls(&vector_core::net::strip_md_link_claims(&message.content)); - for url in urls.into_iter().take(3) { + let urls = extract_https_urls(&vector_core::net::strip_md_link_claims(&message.content), 3); + for url in urls { if INVITE_PREFIXES.iter().any(|p| url.starts_with(p)) || vector_core::golink::is_go_url(&url) { continue; } diff --git a/src-tauri/src/message/mod.rs b/src-tauri/src/message/mod.rs index e1757dba3..cbbc3594a 100644 --- a/src-tauri/src/message/mod.rs +++ b/src-tauri/src/message/mod.rs @@ -118,13 +118,12 @@ pub async fn fetch_msg_metadata(chat_id: String, msg_id: String) -> bool { // only: [https://trusted.com](https://evil.io) must never preview the claimed // site while the click goes elsewhere. const MAX_URLS_TO_TRY: usize = 3; - let urls = util::extract_https_urls(&vector_core::net::strip_md_link_claims(&text)); + let urls = util::extract_https_urls(&vector_core::net::strip_md_link_claims(&text), MAX_URLS_TO_TRY); if urls.is_empty() { return false; } - // Only try the first few URLs - for url in urls.into_iter().take(MAX_URLS_TO_TRY) { + for url in urls { // A link to a Nostr post, article or video gets its own card from relays. if vector_core::nostr_embed::EmbedRef::from_url(&url).is_some() { continue; diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs index 38ce1c1bf..c6229811a 100644 --- a/src-tauri/src/util.rs +++ b/src-tauri/src/util.rs @@ -29,12 +29,14 @@ pub use vector_core::simd::hex::{ pub use crate::simd::has_alpha_transparency; -/// Extract all HTTPS URLs from a string -pub fn extract_https_urls(text: &str) -> Vec { +/// The first `max` HTTPS URLs in a string. Bounded, since a run of bare `https://` names one +/// URL per byte, each running to the end of the text. +pub fn extract_https_urls(text: &str, max: usize) -> Vec { let mut urls = Vec::new(); let mut start_idx = 0; - while let Some(https_idx) = text[start_idx..].find("https://") { + while urls.len() < max { + let Some(https_idx) = text[start_idx..].find("https://") else { break }; let abs_start = start_idx + https_idx; let url_text = &text[abs_start..];