From 565b50646f6db84e243da43adb5b86c266b4e616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20Karsl=C4=B1?= Date: Thu, 25 Apr 2024 14:01:06 +0300 Subject: [PATCH] added geo_filter query param to /r/popular endpoint --- src/subreddit.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/subreddit.rs b/src/subreddit.rs index 569f84c..4df81b4 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -7,6 +7,8 @@ use askama::Template; use cookie::Cookie; use hyper::{Body, Request, Response}; +use once_cell::sync::Lazy; +use regex::Regex; use time::{Duration, OffsetDateTime}; // STRUCTS @@ -50,10 +52,13 @@ struct WallTemplate { url: String, } +static GEO_FILTER_MATCH: Lazy = Lazy::new(|| Regex::new(r"geo_filter=(?\w+)").unwrap()); + // SERVICES pub async fn community(req: Request) -> Result, String> { // Build Reddit API path let root = req.uri().path() == "/"; + let query = req.uri().query().unwrap_or_default().to_string(); let subscribed = setting(&req, "subscriptions"); let front_page = setting(&req, "front_page"); let post_sort = req.cookie("post_sort").map_or_else(|| "hot".to_string(), |c| c.value().to_string()); @@ -107,7 +112,11 @@ pub async fn community(req: Request) -> Result, String> { let mut params = String::from("&raw_json=1"); if sub_name == "popular" { - params.push_str("&geo_filter=GLOBAL"); + let geo_filter = match GEO_FILTER_MATCH.captures(&query) { + Some(geo_filter) => geo_filter["region"].to_string(), + None => "GLOBAL".to_owned(), + }; + params.push_str(&format!("&geo_filter={geo_filter}")); } let path = format!("/r/{sub_name}/{sort}.json?{}{params}", req.uri().query().unwrap_or_default());