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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 2 deletions

View File

@ -18,6 +18,7 @@ struct PostTemplate {
sort: String, sort: String,
prefs: Preferences, prefs: Preferences,
single_thread: bool, single_thread: bool,
url: String,
} }
pub async fn item(req: Request<Body>) -> Result<Response<Body>, 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 // Parse the JSON into Post and Comment structs
let post = parse_post(&response[0]).await; let post = parse_post(&response[0]).await;
let comments = parse_comments(&response[1], &post.permalink, &post.author.name, highlighted_comment); 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 // Use the Post and Comment structs to generate a website to show users
template(PostTemplate { template(PostTemplate {
@ -62,6 +64,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
sort, sort,
prefs: Preferences::new(req), prefs: Preferences::new(req),
single_thread, single_thread,
url: url,
}) })
} }
// If the Reddit API returns an error, exit and send error page to user // 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")] #[template(path = "settings.html")]
struct SettingsTemplate { struct SettingsTemplate {
prefs: Preferences, prefs: Preferences,
url: String,
} }
// CONSTANTS // CONSTANTS
@ -35,7 +36,11 @@ const PREFS: [&str; 10] = [
// Retrieve cookies from request "Cookie" header // Retrieve cookies from request "Cookie" header
pub async fn get(req: Request<Body>) -> Result<Response<Body>, String> { 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 // Set cookies using response "Set-Cookie" header

View File

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

View File

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

View File

@ -227,10 +227,15 @@ nav #libreddit {
#settings_link { #settings_link {
opacity: 0.8; opacity: 0.8;
margin-left: 10px;
}
#reddit_link {
opacity: 0.8;
} }
#code { #code {
margin-left: 5px; margin-left: 10px;
} }
main { main {

View File

@ -35,6 +35,12 @@
</div> </div>
{% block search %}{% endblock %} {% block search %}{% endblock %}
<div id="links"> <div id="links">
<a id="reddit_link" href="https://www.reddit.com{{ url }}">
<span>reddit</span>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M23 12.0737C23 10.7308 21.9222 9.64226 20.5926 9.64226C19.9435 9.64226 19.3557 9.90274 18.923 10.3244C17.2772 9.12492 15.0099 8.35046 12.4849 8.26135L13.5814 3.05002L17.1643 3.8195C17.2081 4.73947 17.9539 5.47368 18.8757 5.47368C19.8254 5.47368 20.5951 4.69626 20.5951 3.73684C20.5951 2.77769 19.8254 2 18.8758 2C18.2001 2 17.6214 2.39712 17.3404 2.96952L13.3393 2.11066C13.2279 2.08679 13.1116 2.10858 13.016 2.17125C12.9204 2.23393 12.8533 2.33235 12.8295 2.44491L11.6051 8.25987C9.04278 8.33175 6.73904 9.10729 5.07224 10.3201C4.63988 9.90099 4.05398 9.64226 3.40757 9.64226C2.0781 9.64226 1 10.7308 1 12.0737C1 13.0618 1.58457 13.9105 2.4225 14.2909C2.38466 14.5342 2.36545 14.78 2.36505 15.0263C2.36505 18.7673 6.67626 21.8 11.9945 21.8C17.3131 21.8 21.6243 18.7673 21.6243 15.0263C21.6243 14.7794 21.6043 14.5359 21.5678 14.2957C22.4109 13.9175 23 13.0657 23 12.0737Z"/>
</svg>
</a>
<a id="settings_link" href="/settings"> <a id="settings_link" href="/settings">
<span>settings</span> <span>settings</span>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">

View File

@ -55,6 +55,7 @@
</p> </p>
<!-- POST MEDIA --> <!-- POST MEDIA -->
<!-- post_type: {{ post.post_type }} -->
{% if post.post_type == "image" %} {% if post.post_type == "image" %}
<a href="{{ post.media.url }}" class="post_media_image" > <a href="{{ post.media.url }}" class="post_media_image" >
<svg <svg