Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions crates/vector-web/src/chat_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
/// 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<String> {
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());
Expand Down Expand Up @@ -436,8 +438,8 @@ async fn site_metadata(url: &str) -> Result<SiteMetadata, String> {

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;
}
Expand Down
5 changes: 2 additions & 3 deletions src-tauri/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src-tauri/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
/// 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<String> {
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..];

Expand Down
Loading