Add list of moderators to sidebar (#213)

* Added list of moderators to sidebar & added wiki not found message

* Improved code formatting
This commit is contained in:
curlpipe 2021-05-04 18:30:54 +01:00 committed by GitHub
parent 9e4066658c
commit 1211d781d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 3 deletions

View File

@ -148,7 +148,7 @@ pub async fn wiki(req: Request<Body>) -> Result<Response<Body>, 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("<h3>Wiki not found</h3>")),
page,
prefs: Preferences::new(req),
}),
@ -166,8 +166,12 @@ pub async fn sidebar(req: Request<Body>) -> Result<Response<Body>, String> {
match json(path).await {
// If success, receive JSON in response
Ok(response) => template(WikiTemplate {
wiki: format!(
"{}<hr><h1>Moderators</h1><br><ul>{}</ul>",
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<Body>) -> Result<Response<Body>, String> {
}
}
pub async fn moderators(sub: &str) -> Result<Vec<String>, String> {
// Retrieve and format the html for the moderators list
Ok(
moderators_list(sub)
.await?
.iter()
.map(|m| format!("<li><a style=\"color: var(--accent)\" href=\"/u/{name}\">{name}</a></li>", name = m))
.collect(),
)
}
async fn moderators_list(sub: &str) -> Result<Vec<String>, 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::<Vec<_>>()
} else {
vec![]
})
}
// SUBREDDIT
async fn subreddit(sub: &str) -> Result<Subreddit, String> {
// Build the Reddit JSON API url
@ -197,6 +231,7 @@ async fn subreddit(sub: &str) -> Result<Subreddit, String> {
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),

View File

@ -341,6 +341,7 @@ pub struct Subreddit {
pub title: String,
pub description: String,
pub info: String,
pub moderators: Vec<String>,
pub icon: String,
pub members: (String, String),
pub active: (String, String),

View File

@ -99,7 +99,17 @@
</div>
<details class="panel" id="sidebar">
<summary id="sidebar_label">Sidebar</summary>
<div id="sidebar_contents">{{ sub.info }}</div>
<div id="sidebar_contents">
{{ sub.info }}
<hr>
<h2>Moderators</h2>
<br>
<ul>
{% for moderator in sub.moderators %}
<li><a style="color: var(--accent)" href="/u/{{ moderator }}">{{ moderator }}</a></li>
{% endfor %}
</ul>
</div>
</details>
</aside>
{% endif %}