Skip to content

Commit

Permalink
Merge pull request #102 from ismailkarsli/main
Browse files Browse the repository at this point in the history
added geo_filter query param to /r/popular endpoint
  • Loading branch information
sigaloid authored May 29, 2024
2 parents 26aa374 + 565b506 commit 8c67d33
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/subreddit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,10 +52,13 @@ struct WallTemplate {
url: String,
}

static GEO_FILTER_MATCH: Lazy<Regex> = Lazy::new(|| Regex::new(r"geo_filter=(?<region>\w+)").unwrap());

// SERVICES
pub async fn community(req: Request<Body>) -> Result<Response<Body>, 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());
Expand Down Expand Up @@ -107,7 +112,11 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, 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());
Expand Down

0 comments on commit 8c67d33

Please sign in to comment.