Sort Top by Timeframe

This commit is contained in:
spikecodes 2020-12-29 17:11:47 -08:00
parent 44c4341e67
commit f65ee2eb6a
8 changed files with 76 additions and 43 deletions

View File

@ -8,21 +8,23 @@ use askama::Template;
#[template(path = "popular.html", escape = "none")]
struct PopularTemplate {
posts: Vec<Post>,
sort: String,
sort: (String, String),
ends: (String, String),
}
// RENDER
async fn render(sub_name: String, sort: Option<String>, ends: (Option<String>, Option<String>)) -> Result<HttpResponse> {
async fn render(sort: Option<String>, t: Option<String>, ends: (Option<String>, Option<String>)) -> Result<HttpResponse> {
let sorting = sort.unwrap_or("hot".to_string());
let before = ends.1.clone().unwrap_or(String::new()); // If there is an after, there must be a before
let timeframe = match &t { Some(val) => format!("&t={}", val), None => String::new() };
// Build the Reddit JSON API url
let url = match ends.0 {
Some(val) => format!("r/{}/{}.json?before={}&count=25", sub_name, sorting, val),
Some(val) => format!("r/popular/{}.json?before={}&count=25{}", sorting, val, timeframe),
None => match ends.1 {
Some(val) => format!("r/{}/{}.json?after={}&count=25", sub_name, sorting, val),
None => format!("r/{}/{}.json", sub_name, sorting),
Some(val) => format!("r/popular/{}.json?after={}&count=25{}", sorting, val, timeframe),
None => format!("r/popular/{}.json?{}", sorting, timeframe),
},
};
@ -40,7 +42,7 @@ async fn render(sub_name: String, sort: Option<String>, ends: (Option<String>, O
let s = PopularTemplate {
posts: items.0,
sort: sorting,
sort: (sorting, t.unwrap_or(String::new())),
ends: (before, items.1),
}
.render()
@ -51,5 +53,5 @@ async fn render(sub_name: String, sort: Option<String>, ends: (Option<String>, O
// SERVICES
pub async fn page(params: web::Query<Params>) -> Result<HttpResponse> {
render("popular".to_string(), params.sort.clone(), (params.before.clone(), params.after.clone())).await
render(params.sort.clone(), params.t.clone(), (params.before.clone(), params.after.clone())).await
}

View File

@ -10,26 +10,28 @@ use std::convert::TryInto;
struct SubredditTemplate {
sub: Subreddit,
posts: Vec<Post>,
sort: String,
sort: (String, String),
ends: (String, String),
}
// SERVICES
#[allow(dead_code)]
pub async fn page(web::Path(sub): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
render(sub, params.sort.clone(), (params.before.clone(), params.after.clone())).await
render(sub, params.sort.clone(), params.t.clone(), (params.before.clone(), params.after.clone())).await
}
pub async fn render(sub_name: String, sort: Option<String>, ends: (Option<String>, Option<String>)) -> Result<HttpResponse> {
pub async fn render(sub_name: String, sort: Option<String>, t: Option<String>, ends: (Option<String>, Option<String>)) -> Result<HttpResponse> {
let sorting = sort.unwrap_or("hot".to_string());
let before = ends.1.clone().unwrap_or(String::new()); // If there is an after, there must be a before
let timeframe = match &t { Some(val) => format!("&t={}", val), None => String::new() };
// Build the Reddit JSON API url
let url = match ends.0 {
Some(val) => format!("r/{}/{}.json?before={}&count=25", sub_name, sorting, val),
Some(val) => format!("r/{}/{}.json?before={}&count=25{}", sub_name, sorting, val, timeframe),
None => match ends.1 {
Some(val) => format!("r/{}/{}.json?after={}&count=25", sub_name, sorting, val),
None => format!("r/{}/{}.json", sub_name, sorting),
Some(val) => format!("r/{}/{}.json?after={}&count=25{}", sub_name, sorting, val, timeframe),
None => format!("r/{}/{}.json?{}", sub_name, sorting, timeframe),
},
};
@ -54,7 +56,7 @@ pub async fn render(sub_name: String, sort: Option<String>, ends: (Option<String
let s = SubredditTemplate {
sub: sub,
posts: items.0,
sort: sorting,
sort: (sorting, t.unwrap_or(String::new())),
ends: (before, items.1),
}
.render()

View File

@ -10,21 +10,23 @@ use chrono::{TimeZone, Utc};
struct UserTemplate {
user: User,
posts: Vec<Post>,
sort: String,
sort: (String, String),
ends: (String, String),
}
async fn render(username: String, sort: Option<String>, ends: (Option<String>, Option<String>)) -> Result<HttpResponse> {
async fn render(username: String, sort: Option<String>, t: Option<String>, ends: (Option<String>, Option<String>)) -> Result<HttpResponse> {
let sorting = sort.unwrap_or("new".to_string());
let before = ends.1.clone().unwrap_or(String::new()); // If there is an after, there must be a before
let timeframe = match &t { Some(val) => format!("&t={}", val), None => String::new() };
// Build the Reddit JSON API url
let url = match ends.0 {
Some(val) => format!("user/{}/.json?sort={}&before={}&count=25&raw_json=1", username, sorting, val),
Some(val) => format!("user/{}/.json?sort={}&before={}&count=25&raw_json=1{}", username, sorting, val, timeframe),
None => match ends.1 {
Some(val) => format!("user/{}/.json?sort={}&after={}&count=25&raw_json=1", username, sorting, val),
None => format!("user/{}/.json?sort={}&raw_json=1", username, sorting),
Some(val) => format!("user/{}/.json?sort={}&after={}&count=25&raw_json=1{}", username, sorting, val, timeframe),
None => format!("user/{}/.json?sort={}&raw_json=1{}", username, sorting, timeframe),
},
};
@ -44,7 +46,7 @@ async fn render(username: String, sort: Option<String>, ends: (Option<String>, O
let s = UserTemplate {
user: user.unwrap(),
posts: posts_unwrapped.0,
sort: sorting,
sort: (sorting, t.unwrap_or(String::new())),
ends: (before, posts_unwrapped.1)
}
.render()
@ -55,7 +57,7 @@ async fn render(username: String, sort: Option<String>, ends: (Option<String>, O
// SERVICES
pub async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
render(username, params.sort.clone(), (params.before.clone(), params.after.clone())).await
render(username, params.sort.clone(), params.t.clone(), (params.before.clone(), params.after.clone())).await
}
// USER

View File

@ -66,6 +66,7 @@ pub struct Subreddit {
// Parser for query params, used in sorting (eg. /r/rust/?sort=hot)
#[derive(serde::Deserialize)]
pub struct Params {
pub t: Option<String>,
pub sort: Option<String>,
pub after: Option<String>,
pub before: Option<String>,

View File

@ -138,7 +138,7 @@ aside {
/* Sorting */
#sort {
#sort, #timeframe {
background: var(--outside);
box-shadow: var(--black-contrast);
border: 0;
@ -150,6 +150,11 @@ aside {
appearance: none;
}
#timeframe {
border-radius: 0;
border-left: 4px solid var(--highlighted);
}
#sort_submit {
background: var(--highlighted);
border: 0;
@ -158,7 +163,7 @@ aside {
border-radius: 0 5px 5px 0;
}
#sort:hover { background: var(--foreground); }
#sort:hover, #timeframe:hover { background: var(--foreground); }
#sort_submit:hover { color: var(--accent); }
#sort > div, footer > a {
@ -498,7 +503,7 @@ td, th {
max-width: 100%;
}
#sidebar {
#user, #sidebar {
margin: 20px 0;
}
}

View File

@ -3,11 +3,18 @@
<div id="column_one">
<form>
<select id="sort" name="sort">
<option value="confidence" {% if sort == "confidence" %}selected{% endif %}>Best</option>
<option value="hot" {% if sort == "hot" %}selected{% endif %}>Hot</option>
<option value="new" {% if sort == "new" %}selected{% endif %}>New</option>
<option value="top" {% if sort == "top" %}selected{% endif %}>Top</option>
</select><input id="sort_submit" type="submit" value="&rarr;">
<option value="best" {% if sort.0 == "best" %}selected{% endif %}>Best</option>
<option value="hot" {% if sort.0 == "hot" %}selected{% endif %}>Hot</option>
<option value="new" {% if sort.0 == "new" %}selected{% endif %}>New</option>
<option value="top" {% if sort.0 == "top" %}selected{% endif %}>Top</option>
</select>{% if sort.0 == "top" %}<select id="timeframe" name="t">
<option value="hour" {% if sort.1 == "hour" %}selected{% endif %}>Hour</option>
<option value="day" {% if sort.1 == "day" || sort.1 == "" %}selected{% endif %}>Day</option>
<option value="week" {% if sort.1 == "week" %}selected{% endif %}>Week</option>
<option value="month" {% if sort.1 == "month" %}selected{% endif %}>Month</option>
<option value="year" {% if sort.1 == "year" %}selected{% endif %}>Year</option>
<option value="all" {% if sort.1 == "all" %}selected{% endif %}>All</option>
</select>{% endif %}<input id="sort_submit" type="submit" value="&rarr;">
</form>
{% for post in posts %}
<div class="post">
@ -37,11 +44,11 @@
<footer>
{% if ends.0 != "" %}
<a href="?sort={{ sort }}&before={{ ends.0 }}">PREV</a>
<a href="?sort={{ sort.0 }}{% if sort.0 == "top" %}&t={{ sort.1 }}{% endif %}&before={{ ends.0 }}">PREV</a>
{% endif %}
{% if ends.1 != "" %}
<a href="?sort={{ sort }}&after={{ ends.1 }}">NEXT</a>
<a href="?sort={{ sort.0 }}{% if sort.0 == "top" %}&t={{ sort.1 }}{% endif %}&after={{ ends.1 }}">NEXT</a>
{% endif %}
</footer>
</div>

View File

@ -9,10 +9,17 @@
<div id="column_one">
<form>
<select id="sort" name="sort">
<option value="hot" {% if sort == "hot" %}selected{% endif %}>Hot</option>
<option value="new" {% if sort == "new" %}selected{% endif %}>New</option>
<option value="top" {% if sort == "top" %}selected{% endif %}>Top</option>
</select><input id="sort_submit" type="submit" value="&rarr;">
<option value="hot" {% if sort.0 == "hot" %}selected{% endif %}>Hot</option>
<option value="new" {% if sort.0 == "new" %}selected{% endif %}>New</option>
<option value="top" {% if sort.0 == "top" %}selected{% endif %}>Top</option>
</select>{% if sort.0 == "top" %}<select id="timeframe" name="t">
<option value="hour" {% if sort.1 == "hour" %}selected{% endif %}>Hour</option>
<option value="day" {% if sort.1 == "day" || sort.1 == "" %}selected{% endif %}>Day</option>
<option value="week" {% if sort.1 == "week" %}selected{% endif %}>Week</option>
<option value="month" {% if sort.1 == "month" %}selected{% endif %}>Month</option>
<option value="year" {% if sort.1 == "year" %}selected{% endif %}>Year</option>
<option value="all" {% if sort.1 == "all" %}selected{% endif %}>All</option>
</select>{% endif %}<input id="sort_submit" type="submit" value="&rarr;">
</form>
{% for post in posts %}
<div class="post">
@ -42,11 +49,11 @@
<footer>
{% if ends.0 != "" %}
<a href="?sort={{ sort }}&before={{ ends.0 }}">PREV</a>
<a href="?sort={{ sort.0 }}&before={{ ends.0 }}">PREV</a>
{% endif %}
{% if ends.1 != "" %}
<a href="?sort={{ sort }}&after={{ ends.1 }}">NEXT</a>
<a href="?sort={{ sort.0 }}&after={{ ends.1 }}">NEXT</a>
{% endif %}
</footer>
</div>

View File

@ -5,10 +5,17 @@
<div id="column_one">
<form>
<select id="sort" name="sort">
<option value="hot" {% if sort == "hot" %}selected{% endif %}>Hot</option>
<option value="new" {% if sort == "new" %}selected{% endif %}>New</option>
<option value="top" {% if sort == "top" %}selected{% endif %}>Top</option>
</select><input id="sort_submit" type="submit" value="&rarr;">
<option value="hot" {% if sort.0 == "hot" %}selected{% endif %}>Hot</option>
<option value="new" {% if sort.0 == "new" %}selected{% endif %}>New</option>
<option value="top" {% if sort.0 == "top" %}selected{% endif %}>Top</option>
</select>{% if sort.0 == "top" %}<select id="timeframe" name="t">
<option value="hour" {% if sort.1 == "hour" %}selected{% endif %}>Hour</option>
<option value="day" {% if sort.1 == "day" %}selected{% endif %}>Day</option>
<option value="week" {% if sort.1 == "week" %}selected{% endif %}>Week</option>
<option value="month" {% if sort.1 == "month" %}selected{% endif %}>Month</option>
<option value="year" {% if sort.1 == "year" %}selected{% endif %}>Year</option>
<option value="all" {% if sort.1 == "all" || sort.1 == "" %}selected{% endif %}>All</option>
</select>{% endif %}<input id="sort_submit" type="submit" value="&rarr;">
</form>
{% for post in posts %}
{% if post.title != "Comment" %}
@ -55,11 +62,11 @@
{% endfor %}
<footer>
{% if ends.0 != "" %}
<a href="?sort={{ sort }}&before={{ ends.0 }}">PREV</a>
<a href="?sort={{ sort.0 }}&before={{ ends.0 }}">PREV</a>
{% endif %}
{% if ends.1 != "" %}
<a href="?sort={{ sort }}&after={{ ends.1 }}">NEXT</a>
<a href="?sort={{ sort.0 }}&after={{ ends.1 }}">NEXT</a>
{% endif %}
</footer>
</div>