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);
// 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() {
val(&res, "icon_img").await
} else {
@ -85,8 +85,8 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
description: val(&res, "public_description").await,
info: val(&res, "description_html").await.replace("\\", ""),
icon: format_url(icon).await,
members: format_num(members.try_into().unwrap()),
active: format_num(active.try_into().unwrap()),
members: format_num(members.try_into().unwrap_or(0)),
active: format_num(active.try_into().unwrap_or(0)),
};
Ok(sub)

View File

@ -56,17 +56,17 @@ pub async fn profile(req: HttpRequest) -> Result<HttpResponse> {
// USER
async fn user(name: &String) -> Result<User, &'static str> {
// Build the Reddit JSON API url
let url: String = format!("user/{}/about.json", name);
// Build the Reddit JSON API path
let path: String = format!("user/{}/about.json", name);
// 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 req.is_err() {
return Err(req.err().unwrap());
}
// Otherwise, grab the JSON output from the request
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
Ok(User {
name: name.to_string(),
title: nested_val(&res, "subreddit", "title").await,
icon: format_url(nested_val(&res, "subreddit", "icon_img").await).await,
karma: res["data"]["total_karma"].as_i64().unwrap(),
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
pub struct User {
pub name: String,
pub title: String,
pub icon: String,
pub karma: i64,
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()))
}

View File

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

View File

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

View File

@ -2,7 +2,8 @@
{% import "utils.html" as utils %}
{% block title %}
{% if sub.name != "" %}r/{{ sub.name }}: {{ sub.description }}
{% if sub.title != "" %}{{ sub.title }}
{% else if sub.name != "" %}{{ sub.name }}
{% else %}Libreddit{% endif %}
{% endblock %}
@ -66,6 +67,7 @@
<aside>
<div id="subreddit">
<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_description">{{ sub.description }}</p>
<div id="subreddit_details">

View File

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

View File

@ -15,9 +15,9 @@
{%- endmacro %}
{% 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 }}">
{% if root != "/r/" %}
{% if root != "/r/" && !root.is_empty() %}
<div id="inside">
<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>