Add "open in reddit" button to all pages (#304)

* Pass the url parameter to all templates. Add a reddit_link to the navbar, which opens the current url on reddit.

* Add icon for reddit link

Co-authored-by: spikecodes <19519553+spikecodes@users.noreply.github.com>
This commit is contained in:
mikupls
2021-11-15 03:39:33 +01:00
committed by GitHub
parent 2486347b14
commit 0f7ba3c61d
7 changed files with 30 additions and 2 deletions

View File

@ -18,6 +18,7 @@ struct PostTemplate {
sort: String,
prefs: Preferences,
single_thread: bool,
url: String,
}
pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
@ -54,6 +55,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
// Parse the JSON into Post and Comment structs
let post = parse_post(&response[0]).await;
let comments = parse_comments(&response[1], &post.permalink, &post.author.name, highlighted_comment);
let url = req.uri().to_string();
// Use the Post and Comment structs to generate a website to show users
template(PostTemplate {
@ -62,6 +64,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
sort,
prefs: Preferences::new(req),
single_thread,
url: url,
})
}
// If the Reddit API returns an error, exit and send error page to user

View File

@ -14,6 +14,7 @@ use time::{Duration, OffsetDateTime};
#[template(path = "settings.html")]
struct SettingsTemplate {
prefs: Preferences,
url: String,
}
// CONSTANTS
@ -35,7 +36,11 @@ const PREFS: [&str; 10] = [
// Retrieve cookies from request "Cookie" header
pub async fn get(req: Request<Body>) -> Result<Response<Body>, String> {
template(SettingsTemplate { prefs: Preferences::new(req) })
let url = req.uri().to_string();
template(SettingsTemplate {
prefs: Preferences::new(req),
url: url,
})
}
// Set cookies using response "Set-Cookie" header

View File

@ -26,6 +26,7 @@ struct WikiTemplate {
wiki: String,
page: String,
prefs: Preferences,
url: String,
}
#[derive(Template)]
@ -239,6 +240,7 @@ pub async fn wiki(req: Request<Body>) -> Result<Response<Body>, String> {
let page = req.param("page").unwrap_or_else(|| "index".to_string());
let path: String = format!("/r/{}/wiki/{}.json?raw_json=1", sub, page);
let url = req.uri().to_string();
match json(path, quarantined).await {
Ok(response) => template(WikiTemplate {
@ -246,6 +248,7 @@ pub async fn wiki(req: Request<Body>) -> Result<Response<Body>, String> {
wiki: rewrite_urls(response["data"]["content_html"].as_str().unwrap_or("<h3>Wiki not found</h3>")),
page,
prefs: Preferences::new(req),
url: url,
}),
Err(msg) => {
if msg == "quarantined" {
@ -268,6 +271,7 @@ pub async fn sidebar(req: Request<Body>) -> Result<Response<Body>, String> {
// Build the Reddit JSON API url
let path: String = format!("/r/{}/about.json?raw_json=1", sub);
let url = req.uri().to_string();
// Send a request to the url
match json(path, quarantined).await {
@ -282,6 +286,7 @@ pub async fn sidebar(req: Request<Body>) -> Result<Response<Body>, String> {
sub,
page: "Sidebar".to_string(),
prefs: Preferences::new(req),
url: url,
}),
Err(msg) => {
if msg == "quarantined" {

View File

@ -341,6 +341,7 @@ pub struct Comment {
pub struct ErrorTemplate {
pub msg: String,
pub prefs: Preferences,
pub url: String,
}
#[derive(Default)]
@ -606,9 +607,11 @@ pub fn redirect(path: String) -> Response<Body> {
}
pub async fn error(req: Request<Body>, msg: String) -> Result<Response<Body>, String> {
let url = req.uri().to_string();
let body = ErrorTemplate {
msg,
prefs: Preferences::new(req),
url: url,
}
.render()
.unwrap_or_default();