From 9e434e7db6deccd2c6db7bb63a906453e9f963a6 Mon Sep 17 00:00:00 2001 From: gmnsii <95436780+gmnsii@users.noreply.github.com> Date: Sat, 31 Dec 2022 19:57:42 -0800 Subject: [PATCH] Search - add support for raw reddit links (#663) * Search - add support for raw reddit links If a search query starts with 'https://www.reddit.com/' or 'https://old.reddit.com/', this prefix will be truncated and the query will be processed normally. For example, a search query 'https://www.reddit.com/r/rust' will redirect to r/rust. * Search - support a wider variety of reddit links. Add once cell dependency for static regex support (avoid compiling the same regex multiple times). All search queries are now matched against a regex (provided by @Daniel-Valentine) that determines if it is a reddit link. If it is, the prefix specifying the reddit instance will be truncated from the query that will then be processed normally. For example, the query 'https://www.reddit.com/r/rust' will be treated the same way as the query 'r/rust'. --- Cargo.lock | 1 + Cargo.toml | 1 + src/search.rs | 8 +++++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 2865f62..6945ebe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -677,6 +677,7 @@ dependencies = [ "hyper-rustls", "libflate", "lipsum", + "once_cell", "percent-encoding", "regex", "route-recognizer", diff --git a/Cargo.toml b/Cargo.toml index 157ed31..c1b2548 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ url = "2.3.1" rust-embed = { version = "6.4.2", features = ["include-exclude"] } libflate = "1.2.0" brotli = { version = "3.3.4", features = ["std"] } +once_cell = "1.16.0" [dev-dependencies] lipsum = "0.8.2" diff --git a/src/search.rs b/src/search.rs index 9fbe77a..87965c3 100644 --- a/src/search.rs +++ b/src/search.rs @@ -7,6 +7,8 @@ use crate::{ }; use askama::Template; use hyper::{Body, Request, Response}; +use once_cell::sync::Lazy; +use regex::Regex; // STRUCTS struct SearchParams { @@ -47,11 +49,15 @@ struct SearchTemplate { no_posts: bool, } +// Regex matched against search queries to determine if they are reddit urls. +static REDDIT_URL_MATCH: Lazy = Lazy::new(|| Regex::new(r"^https?://([^\./]+\.)*reddit.com/").unwrap()); + // SERVICES pub async fn find(req: Request) -> Result, String> { let nsfw_results = if setting(&req, "show_nsfw") == "on" { "&include_over_18=on" } else { "" }; let path = format!("{}.json?{}{}&raw_json=1", req.uri().path(), req.uri().query().unwrap_or_default(), nsfw_results); - let query = param(&path, "q").unwrap_or_default(); + let mut query = param(&path, "q").unwrap_or_default(); + query = REDDIT_URL_MATCH.replace(&query, "").to_string(); if query.is_empty() { return Ok(redirect("/".to_string()));