Refactor Page Titles and Add Subreddit/User Titles

This commit is contained in:
spikecodes 2020-12-31 20:21:56 -08:00
parent 93c1db502d
commit 2d77a91150
8 changed files with 35 additions and 26 deletions

View File

@ -72,7 +72,7 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
let active = res["data"]["accounts_active"].as_u64().unwrap_or(0); let active = res["data"]["accounts_active"].as_u64().unwrap_or(0);
// Fetch subreddit icon either from the community_icon or icon_img value // Fetch subreddit icon either from the community_icon or icon_img value
let community_icon: &str = res["data"]["community_icon"].as_str().unwrap().split("?").collect::<Vec<&str>>()[0]; let community_icon: &str = res["data"]["community_icon"].as_str().unwrap_or("").split("?").collect::<Vec<&str>>()[0];
let icon = if community_icon.is_empty() { let icon = if community_icon.is_empty() {
val(&res, "icon_img").await val(&res, "icon_img").await
} else { } else {
@ -85,8 +85,8 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
description: val(&res, "public_description").await, description: val(&res, "public_description").await,
info: val(&res, "description_html").await.replace("\\", ""), info: val(&res, "description_html").await.replace("\\", ""),
icon: format_url(icon).await, icon: format_url(icon).await,
members: format_num(members.try_into().unwrap()), members: format_num(members.try_into().unwrap_or(0)),
active: format_num(active.try_into().unwrap()), active: format_num(active.try_into().unwrap_or(0)),
}; };
Ok(sub) Ok(sub)

View File

@ -56,17 +56,17 @@ pub async fn profile(req: HttpRequest) -> Result<HttpResponse> {
// USER // USER
async fn user(name: &String) -> Result<User, &'static str> { async fn user(name: &String) -> Result<User, &'static str> {
// Build the Reddit JSON API url // Build the Reddit JSON API path
let url: String = format!("user/{}/about.json", name); let path: String = format!("user/{}/about.json", name);
// Send a request to the url, receive JSON in response // Send a request to the url, receive JSON in response
let req = request(url).await; let req = request(path).await;
// If the Reddit API returns an error, exit this function // If the Reddit API returns an error, exit this function
if req.is_err() { if req.is_err() {
return Err(req.err().unwrap()); return Err(req.err().unwrap());
} }
// Otherwise, grab the JSON output from the request // Otherwise, grab the JSON output from the request
let res = req.unwrap(); let res = req.unwrap();
@ -76,6 +76,7 @@ async fn user(name: &String) -> Result<User, &'static str> {
// Parse the JSON output into a User struct // Parse the JSON output into a User struct
Ok(User { Ok(User {
name: name.to_string(), name: name.to_string(),
title: nested_val(&res, "subreddit", "title").await,
icon: format_url(nested_val(&res, "subreddit", "icon_img").await).await, icon: format_url(nested_val(&res, "subreddit", "icon_img").await).await,
karma: res["data"]["total_karma"].as_i64().unwrap(), karma: res["data"]["total_karma"].as_i64().unwrap(),
created: Utc.timestamp(created, 0).format("%b %e, %Y").to_string(), created: Utc.timestamp(created, 0).format("%b %e, %Y").to_string(),

View File

@ -50,6 +50,7 @@ pub struct Comment {
// User struct containing metadata about user // User struct containing metadata about user
pub struct User { pub struct User {
pub name: String, pub name: String,
pub title: String,
pub icon: String, pub icon: String,
pub karma: i64, pub karma: i64,
pub created: String, pub created: String,
@ -194,8 +195,6 @@ pub async fn fetch_posts(path: String, fallback_title: String) -> Result<(Vec<Po
}); });
} }
dbg!(path);
Ok((posts, res["data"]["after"].as_str().unwrap_or("").to_string())) Ok((posts, res["data"]["after"].as_str().unwrap_or("").to_string()))
} }

View File

@ -44,11 +44,13 @@ nav #version { opacity: 25%; }
main { main {
display: flex; display: flex;
justify-content: center; justify-content: center;
max-width: 750px; max-width: 1000px;
padding: 10px 20px; padding: 10px 20px;
margin: 20px auto; margin: 20px auto;
} }
#column_one { max-width: 750px; }
footer { footer {
display: flex; display: flex;
justify-content: center; justify-content: center;
@ -100,9 +102,9 @@ aside {
border-radius: 5px; border-radius: 5px;
} }
#sidebar, #sidebar_contents { #user *, #subreddit * { text-align: center; }
margin-top: 20px;
} #sidebar, #sidebar_contents { margin-top: 20px; }
#sidebar_label { #sidebar_label {
border: 2px solid var(--highlighted); border: 2px solid var(--highlighted);
@ -118,20 +120,22 @@ aside {
margin: 10px; margin: 10px;
} }
#user_name, #subreddit_name { #user_title, #subreddit_title {
margin-top: 10px; margin: 0 20px;
font-size: 20px;
font-weight: bold;
} }
#user_description, #subreddit_description { #user_description, #subreddit_description {
margin: 10px 20px; margin: 0 20px;
text-align: center;
font-size: 15px; font-size: 15px;
} }
#user_name, #subreddit_name, #user_icon, #subreddit_icon, #user_description, #subreddit_description { margin-bottom: 20px; }
#user_details, #subreddit_details { #user_details, #subreddit_details {
display: grid; display: grid;
grid-template-columns: repeat(2, 1fr); grid-template-columns: repeat(2, 1fr);
margin-top: 15px;
grid-column-gap: 20px; grid-column-gap: 20px;
} }
@ -566,8 +570,8 @@ td, th {
main { flex-direction: column-reverse; } main { flex-direction: column-reverse; }
nav { flex-direction: column; } nav { flex-direction: column; }
aside { aside, #subreddit, #user {
margin: 20px 0 0 0; margin: 0;
max-width: 100%; max-width: 100%;
} }

View File

@ -1,6 +1,8 @@
{% extends "base.html" %} {% extends "base.html" %}
{% import "utils.html" as utils %} {% import "utils.html" as utils %}
{% block title %}Libreddit: search results - {{ query }}{% endblock %}
{% block content %} {% block content %}
<div id="column_one"> <div id="column_one">
<form id="search_sort"> <form id="search_sort">

View File

@ -2,7 +2,8 @@
{% import "utils.html" as utils %} {% import "utils.html" as utils %}
{% block title %} {% block title %}
{% if sub.name != "" %}r/{{ sub.name }}: {{ sub.description }} {% if sub.title != "" %}{{ sub.title }}
{% else if sub.name != "" %}{{ sub.name }}
{% else %}Libreddit{% endif %} {% else %}Libreddit{% endif %}
{% endblock %} {% endblock %}
@ -66,6 +67,7 @@
<aside> <aside>
<div id="subreddit"> <div id="subreddit">
<img id="subreddit_icon" src="{{ sub.icon }}"> <img id="subreddit_icon" src="{{ sub.icon }}">
<p id="subreddit_title">{{ sub.title }}</p>
<p id="subreddit_name">r/{{ sub.name }}</p> <p id="subreddit_name">r/{{ sub.name }}</p>
<p id="subreddit_description">{{ sub.description }}</p> <p id="subreddit_description">{{ sub.description }}</p>
<div id="subreddit_details"> <div id="subreddit_details">

View File

@ -5,7 +5,7 @@
{% call utils::search("".to_owned(), "", "") %} {% call utils::search("".to_owned(), "", "") %}
{% endblock %} {% endblock %}
{% block title %}Libreddit: u/{{ user.name }}{% endblock %} {% block title %}{{ user.name.replace("u/", "") }} (u/{{ user.name }}) - Libreddit{% endblock %}
{% block body %} {% block body %}
<main style="max-width: 1000px;"> <main style="max-width: 1000px;">
<div id="column_one"> <div id="column_one">
@ -72,6 +72,7 @@
<aside> <aside>
<div id="user"> <div id="user">
<img id="user_icon" src="{{ user.icon }}"> <img id="user_icon" src="{{ user.icon }}">
<p id="user_title">{{ user.title }}</p>
<p id="user_name">u/{{ user.name }}</p> <p id="user_name">u/{{ user.name }}</p>
<div id="user_description">{{ user.description }}</div> <div id="user_description">{{ user.description }}</div>
<div id="user_details"> <div id="user_details">

View File

@ -15,9 +15,9 @@
{%- endmacro %} {%- endmacro %}
{% macro search(root, search) -%} {% macro search(root, search) -%}
<form action="{% if root != "/r/" %}{{ root }}{% endif %}/search/" id="searchbox"> <form action="{% if root != "/r/" && !root.is_empty() %}{{ root }}{% endif %}/search/" id="searchbox">
<input id="search" type="text" name="q" placeholder="Search" value="{{ search }}"> <input id="search" type="text" name="q" placeholder="Search" value="{{ search }}">
{% if root != "/r/" %} {% if root != "/r/" && !root.is_empty() %}
<div id="inside"> <div id="inside">
<input type="checkbox" name="restrict_sr" id="restrict_sr" checked="checked" data-com.bitwarden.browser.user-edited="yes"> <input type="checkbox" name="restrict_sr" id="restrict_sr" checked="checked" data-com.bitwarden.browser.user-edited="yes">
<label for="restrict_sr">in {{ root }}</label> <label for="restrict_sr">in {{ root }}</label>