Add "hide nsfw" option

This commit is contained in:
spikecodes 2021-01-08 17:35:04 -08:00
parent 3d142afd03
commit b13874d0db
10 changed files with 98 additions and 75 deletions

View file

@ -1,5 +1,5 @@
// CRATES
use crate::utils::{cookie, error, fetch_posts, format_url, nested_val, param, request, Post, User};
use crate::utils::{error, fetch_posts, format_url, nested_val, param, prefs, request, Post, Preferences, User};
use actix_web::{HttpRequest, HttpResponse, Result};
use askama::Template;
use time::OffsetDateTime;
@ -12,7 +12,7 @@ struct UserTemplate {
posts: Vec<Post>,
sort: (String, String),
ends: (String, String),
layout: String,
prefs: Preferences,
}
// FUNCTIONS
@ -35,7 +35,7 @@ pub async fn profile(req: HttpRequest) -> HttpResponse {
posts,
sort: (sort, param(&path, "t")),
ends: (param(&path, "after"), after),
layout: cookie(req, "layout"),
prefs: prefs(req),
}
.render()
.unwrap();
@ -51,29 +51,25 @@ async fn user(name: &str) -> Result<User, &'static str> {
// Build the Reddit JSON API path
let path: String = format!("user/{}/about.json", name);
let res;
// Send a request to the url
match request(&path).await {
// If success, receive JSON in response
Ok(response) => {
res = response;
Ok(res) => {
// Grab creation date as unix timestamp
let created: i64 = res["data"]["created"].as_f64().unwrap_or(0.0).round() as i64;
// Parse the JSON output into a User struct
Ok(User {
name: name.to_string(),
title: nested_val(&res, "subreddit", "title"),
icon: format_url(nested_val(&res, "subreddit", "icon_img")),
karma: res["data"]["total_karma"].as_i64().unwrap_or(0),
created: OffsetDateTime::from_unix_timestamp(created).format("%b %d '%y"),
banner: nested_val(&res, "subreddit", "banner_img"),
description: nested_val(&res, "subreddit", "public_description"),
})
}
// If the Reddit API returns an error, exit this function
Err(msg) => return Err(msg),
}
// Grab creation date as unix timestamp
let created: i64 = res["data"]["created"].as_f64().unwrap_or(0.0).round() as i64;
// Parse the JSON output into a User struct
Ok(User {
name: name.to_string(),
title: nested_val(&res, "subreddit", "title"),
icon: format_url(nested_val(&res, "subreddit", "icon_img")),
karma: res["data"]["total_karma"].as_i64().unwrap_or(0),
created: OffsetDateTime::from_unix_timestamp(created).format("%b %d '%y"),
banner: nested_val(&res, "subreddit", "banner_img"),
description: nested_val(&res, "subreddit", "public_description"),
})
}