From 0f7ba3c61dc0491da6a09079c7571af2ce3a873c Mon Sep 17 00:00:00 2001
From: mikupls <93015331+mikupls@users.noreply.github.com>
Date: Mon, 15 Nov 2021 03:39:33 +0100
Subject: [PATCH] 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>
---
src/post.rs | 3 +++
src/settings.rs | 7 ++++++-
src/subreddit.rs | 5 +++++
src/utils.rs | 3 +++
static/style.css | 7 ++++++-
templates/base.html | 6 ++++++
templates/post.html | 1 +
7 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/src/post.rs b/src/post.rs
index 6ddb7be..05a6dde 100644
--- a/src/post.rs
+++ b/src/post.rs
@@ -18,6 +18,7 @@ struct PostTemplate {
sort: String,
prefs: Preferences,
single_thread: bool,
+ url: String,
}
pub async fn item(req: Request
) -> Result, String> {
@@ -54,6 +55,7 @@ pub async fn item(req: Request) -> Result, 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) -> Result, String> {
sort,
prefs: Preferences::new(req),
single_thread,
+ url: url,
})
}
// If the Reddit API returns an error, exit and send error page to user
diff --git a/src/settings.rs b/src/settings.rs
index ef1b1ac..d68e6b4 100644
--- a/src/settings.rs
+++ b/src/settings.rs
@@ -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) -> Result, 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
diff --git a/src/subreddit.rs b/src/subreddit.rs
index 6a4ed10..00b366d 100644
--- a/src/subreddit.rs
+++ b/src/subreddit.rs
@@ -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) -> Result, 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) -> Result, String> {
wiki: rewrite_urls(response["data"]["content_html"].as_str().unwrap_or("Wiki not found
")),
page,
prefs: Preferences::new(req),
+ url: url,
}),
Err(msg) => {
if msg == "quarantined" {
@@ -268,6 +271,7 @@ pub async fn sidebar(req: Request) -> Result, 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) -> Result, String> {
sub,
page: "Sidebar".to_string(),
prefs: Preferences::new(req),
+ url: url,
}),
Err(msg) => {
if msg == "quarantined" {
diff --git a/src/utils.rs b/src/utils.rs
index 268a6aa..675cb12 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -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 {
}
pub async fn error(req: Request, msg: String) -> Result, String> {
+ let url = req.uri().to_string();
let body = ErrorTemplate {
msg,
prefs: Preferences::new(req),
+ url: url,
}
.render()
.unwrap_or_default();
diff --git a/static/style.css b/static/style.css
index 9327e66..569816e 100644
--- a/static/style.css
+++ b/static/style.css
@@ -227,10 +227,15 @@ nav #libreddit {
#settings_link {
opacity: 0.8;
+ margin-left: 10px;
+}
+
+#reddit_link {
+ opacity: 0.8;
}
#code {
- margin-left: 5px;
+ margin-left: 10px;
}
main {
diff --git a/templates/base.html b/templates/base.html
index 6caad37..9d469be 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -35,6 +35,12 @@
{% block search %}{% endblock %}