|
|
|
@ -99,8 +99,27 @@ pub async fn subscriptions(req: Request<Body>) -> Result<Response<Body>, String>
|
|
|
|
|
|
|
|
|
|
let mut sub_list = Preferences::new(req).subscriptions;
|
|
|
|
|
|
|
|
|
|
// Retrieve list of posts for these subreddits to extract display names
|
|
|
|
|
let display = json(format!("/r/{}/hot.json?raw_json=1", sub)).await?;
|
|
|
|
|
let display_lookup: Vec<(String, &str)> = display["data"]["children"].as_array().unwrap().iter().map(|post| {
|
|
|
|
|
let display_name = post["data"]["subreddit"].as_str().unwrap();
|
|
|
|
|
(display_name.to_lowercase(), display_name)
|
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
|
|
// Find each subreddit name (separated by '+') in sub parameter
|
|
|
|
|
for part in sub.split('+') {
|
|
|
|
|
// Retrieve display name for the subreddit
|
|
|
|
|
let display;
|
|
|
|
|
let part = if let Some(&(_, display)) = display_lookup.iter().find(|x| x.0 == part.to_lowercase()) {
|
|
|
|
|
// This is already known, doesn't require seperate request
|
|
|
|
|
display
|
|
|
|
|
} else {
|
|
|
|
|
// This subreddit display name isn't known, retrieve it
|
|
|
|
|
let path: String = format!("/r/{}/about.json?raw_json=1", part);
|
|
|
|
|
display = json(path).await?;
|
|
|
|
|
display["data"]["display_name"].as_str().ok_or_else(|| "Failed to query subreddit name".to_string())?
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Modify sub list based on action
|
|
|
|
|
if action.contains(&"subscribe".to_string()) && !sub_list.contains(&part.to_owned()) {
|
|
|
|
|
// Add each sub name to the subscribed list
|
|
|
|
|