diff --git a/src/subreddit.rs b/src/subreddit.rs index c26abdc..4630074 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -148,7 +148,7 @@ pub async fn wiki(req: Request) -> Result, String> { match json(path).await { Ok(response) => template(WikiTemplate { sub, - wiki: rewrite_urls(response["data"]["content_html"].as_str().unwrap_or_default()), + wiki: rewrite_urls(response["data"]["content_html"].as_str().unwrap_or("

Wiki not found

")), page, prefs: Preferences::new(req), }), @@ -166,8 +166,12 @@ pub async fn sidebar(req: Request) -> Result, String> { match json(path).await { // If success, receive JSON in response Ok(response) => template(WikiTemplate { + wiki: format!( + "{}

Moderators


", + rewrite_urls(&val(&response, "description_html").replace("\\", "")), + moderators(&sub).await?.join(""), + ), sub, - wiki: rewrite_urls(&val(&response, "description_html").replace("\\", "")), page: "Sidebar".to_string(), prefs: Preferences::new(req), }), @@ -175,6 +179,36 @@ pub async fn sidebar(req: Request) -> Result, String> { } } +pub async fn moderators(sub: &str) -> Result, String> { + // Retrieve and format the html for the moderators list + Ok( + moderators_list(sub) + .await? + .iter() + .map(|m| format!("
  • {name}
  • ", name = m)) + .collect(), + ) +} + +async fn moderators_list(sub: &str) -> Result, String> { + // Build the moderator list URL + let path: String = format!("/r/{}/about/moderators.json?raw_json=1", sub); + + // Retrieve response + let response = json(path).await?["data"]["children"].clone(); + Ok(if let Some(response) = response.as_array() { + // Traverse json tree and format into list of strings + response + .iter() + .map(|m| m["name"].as_str().unwrap_or("")) + .filter(|m| !m.is_empty()) + .map(|m| m.to_string()) + .collect::>() + } else { + vec![] + }) +} + // SUBREDDIT async fn subreddit(sub: &str) -> Result { // Build the Reddit JSON API url @@ -197,6 +231,7 @@ async fn subreddit(sub: &str) -> Result { title: esc!(&res, "title"), description: esc!(&res, "public_description"), info: rewrite_urls(&val(&res, "description_html").replace("\\", "")), + moderators: moderators_list(sub).await?, icon: format_url(&icon), members: format_num(members), active: format_num(active), diff --git a/src/utils.rs b/src/utils.rs index 1134000..ccf062f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -341,6 +341,7 @@ pub struct Subreddit { pub title: String, pub description: String, pub info: String, + pub moderators: Vec, pub icon: String, pub members: (String, String), pub active: (String, String), diff --git a/templates/subreddit.html b/templates/subreddit.html index b00c511..ff9dbff 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -99,7 +99,17 @@ {% endif %}