From 138f8320e9c180c493be5a7aadd1ca5f00e91adf Mon Sep 17 00:00:00 2001
From: spikecodes <19519553+spikecodes@users.noreply.github.com>
Date: Sun, 17 Jan 2021 12:58:12 -0800
Subject: [PATCH 1/3] Create media struct
---
src/main.rs | 13 ++++++------
src/post.rs | 14 ++++++-------
src/utils.rs | 43 +++++++++++++++++++++-------------------
templates/post.html | 6 +++---
templates/search.html | 10 +++++-----
templates/subreddit.html | 10 +++++-----
templates/user.html | 10 +++++-----
7 files changed, 55 insertions(+), 51 deletions(-)
diff --git a/src/main.rs b/src/main.rs
index 2d10985..a0f5f37 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,8 @@
// Import Crates
-use actix_web::{App, HttpResponse, HttpServer, dev::{Service, ServiceResponse}, middleware, web};
+use actix_web::{
+ dev::{Service, ServiceResponse},
+ middleware, web, App, HttpResponse, HttpServer,
+};
use futures::future::FutureExt;
// Reference local files
@@ -50,12 +53,10 @@ async fn main() -> std::io::Result<()> {
.wrap_fn(move |req, srv| {
let secure = req.connection_info().scheme() == "https";
let https_url = format!("https://{}{}", req.connection_info().host(), req.uri().to_string());
- srv.call(req).map(move |res: Result | {
+ srv.call(req).map(move |res: Result| {
if force_https && !secure {
- let redirect: ServiceResponse = ServiceResponse::new(
- res.unwrap().request().clone(),
- HttpResponse::Found().header("Location", https_url).finish()
- );
+ let redirect: ServiceResponse =
+ ServiceResponse::new(res.unwrap().request().clone(), HttpResponse::Found().header("Location", https_url).finish());
Ok(redirect)
} else {
res
diff --git a/src/post.rs b/src/post.rs
index b61490b..08d942c 100644
--- a/src/post.rs
+++ b/src/post.rs
@@ -72,7 +72,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
let ratio: f64 = post["data"]["upvote_ratio"].as_f64().unwrap_or(1.0) * 100.0;
// Determine the type of media along with the media URL
- let (post_type, media, width, height) = media(&post["data"]).await;
+ let (post_type, media) = media(&post["data"]).await;
// Build a post using data parsed from Reddit post API
Post {
@@ -97,7 +97,12 @@ async fn parse_post(json: &serde_json::Value) -> Post {
score: format_num(score),
upvote_ratio: ratio as i64,
post_type,
- thumbnail: format_url(val(post, "thumbnail").as_str()),
+ media,
+ thumbnail: Media {
+ url: format_url(val(post, "thumbnail").as_str()),
+ width: post["data"]["thumbnail_width"].as_i64().unwrap_or_default(),
+ height: post["data"]["thumbnail_height"].as_i64().unwrap_or_default(),
+ },
flair: Flair {
flair_parts: parse_rich_flair(
val(post, "link_flair_type"),
@@ -115,15 +120,10 @@ async fn parse_post(json: &serde_json::Value) -> Post {
nsfw: post["data"]["over_18"].as_bool().unwrap_or(false),
stickied: post["data"]["stickied"].as_bool().unwrap_or(false),
},
- media,
domain: val(post, "domain"),
rel_time,
created,
comments: format_num(post["data"]["num_comments"].as_i64().unwrap_or_default()),
- media_width: width,
- media_height: height,
- thumbnail_width: post["data"]["thumbnail_width"].as_i64().unwrap_or_default(),
- thumbnail_height: post["data"]["thumbnail_height"].as_i64().unwrap_or_default(),
}
}
diff --git a/src/utils.rs b/src/utils.rs
index 5d44ac9..4804ca6 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -39,6 +39,12 @@ pub struct Flags {
pub stickied: bool,
}
+pub struct Media {
+ pub url: String,
+ pub width: i64,
+ pub height: i64,
+}
+
// Post containing content, metadata and media
pub struct Post {
pub id: String,
@@ -52,16 +58,12 @@ pub struct Post {
pub post_type: String,
pub flair: Flair,
pub flags: Flags,
- pub thumbnail: String,
- pub media: String,
+ pub thumbnail: Media,
+ pub media: Media,
pub domain: String,
pub rel_time: String,
pub created: String,
pub comments: String,
- pub media_width: i64,
- pub media_height: i64,
- pub thumbnail_width: i64,
- pub thumbnail_height: i64,
}
// Comment with content, post, score and data/time that it was posted
@@ -183,7 +185,7 @@ pub fn format_num(num: i64) -> String {
}
}
-pub async fn media(data: &Value) -> (String, String, i64, i64) {
+pub async fn media(data: &Value) -> (String, Media) {
let post_type: &str;
// If post is a video, return the video
let url = if data["preview"]["reddit_video_preview"]["fallback_url"].is_string() {
@@ -214,14 +216,15 @@ pub async fn media(data: &Value) -> (String, String, i64, i64) {
post_type = "link";
data["url"].as_str().unwrap_or_default().to_string()
};
-
- let (width, height) = if post_type == "image" {
- (data["preview"]["images"][0]["source"]["width"].as_i64().unwrap_or_default(), data["preview"]["images"][0]["source"]["height"].as_i64().unwrap_or_default())
- } else {
- (0, 0)
- };
- (post_type.to_string(), url, width, height)
+ (
+ post_type.to_string(),
+ Media {
+ url,
+ width: data["preview"]["images"][0]["source"]["width"].as_i64().unwrap_or_default(),
+ height: data["preview"]["images"][0]["source"]["height"].as_i64().unwrap_or_default(),
+ },
+ )
}
pub fn parse_rich_flair(flair_type: String, rich_flair: Option<&Vec>, text_flair: Option<&str>) -> Vec {
@@ -317,7 +320,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec Result<(Vec Result<(Vec
{% if post.post_type == "image" %}
-
+
{% else if post.post_type == "video" || post.post_type == "gif" %}
-
+
{% else if post.post_type == "link" %}
- {{ post.media }}
+ {{ post.media.url }}
{% endif %}
diff --git a/templates/search.html b/templates/search.html
index 69edf23..079b52f 100644
--- a/templates/search.html
+++ b/templates/search.html
@@ -53,16 +53,16 @@
{{ post.title }}{% if post.flags.nsfw %} NSFW{% endif %}
- {% if (prefs.layout == "" || prefs.layout == "card") && post.post_type == "image" %}
-
+ {% if (prefs.layout.is_empty() || prefs.layout == "card") && post.post_type == "image" %}
+
{% else if post.post_type != "self" %}
-
- {% if post.thumbnail == "" %}
+
+ {% if post.thumbnail.url.is_empty() %}
{% else %}
-
+
{% endif %}
{% if post.post_type == "link" %}{{ post.domain }}{% else %}{{ post.post_type }}{% endif %}
diff --git a/templates/subreddit.html b/templates/subreddit.html
index 3f1adb2..c34f134 100644
--- a/templates/subreddit.html
+++ b/templates/subreddit.html
@@ -47,16 +47,16 @@
{{ post.title }}{% if post.flags.nsfw %} NSFW{% endif %}
- {% if (prefs.layout == "" || prefs.layout == "card") && post.post_type == "image" %}
-
+ {% if (prefs.layout.is_empty() || prefs.layout == "card") && post.post_type == "image" %}
+
{% else if post.post_type != "self" %}
-
- {% if post.thumbnail == "" %}
+
+ {% if post.thumbnail.url.is_empty() %}
{% else %}
-
+
{% endif %}
{% if post.post_type == "link" %}{{ post.domain }}{% else %}{{ post.post_type }}{% endif %}
diff --git a/templates/user.html b/templates/user.html
index 4d8561e..8e23236 100644
--- a/templates/user.html
+++ b/templates/user.html
@@ -38,16 +38,16 @@
{{ post.title }}{% if post.flags.nsfw %} NSFW{% endif %}
- {% if (prefs.layout == "" || prefs.layout == "card") && post.post_type == "image" %}
-
+ {% if (prefs.layout.is_empty() || prefs.layout == "card") && post.post_type == "image" %}
+
{% else if post.post_type != "self" %}
-
- {% if post.thumbnail == "" %}
+
+ {% if post.thumbnail.url.is_empty() %}
{% else %}
-
+
{% endif %}
{% if post.post_type == "link" %}{{ post.domain }}{% else %}{{ post.post_type }}{% endif %}
From 21763c51cd742102c01dbca0e12ebd512099110e Mon Sep 17 00:00:00 2001
From: spikecodes <19519553+spikecodes@users.noreply.github.com>
Date: Sun, 17 Jan 2021 12:59:40 -0800
Subject: [PATCH 2/3] Make number formatting inclusive
---
src/utils.rs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/utils.rs b/src/utils.rs
index 4804ca6..371f18d 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -176,9 +176,9 @@ pub fn rewrite_url(text: &str) -> String {
// Append `m` and `k` for millions and thousands respectively
pub fn format_num(num: i64) -> String {
- if num > 1_000_000 {
+ if num >= 1_000_000 {
format!("{}m", num / 1_000_000)
- } else if num > 1000 {
+ } else if num >= 1000 {
format!("{}k", num / 1_000)
} else {
num.to_string()
From 801216dfe9c4ee70c6b9a91c870af0da39c6daab Mon Sep 17 00:00:00 2001
From: spikecodes <19519553+spikecodes@users.noreply.github.com>
Date: Sun, 17 Jan 2021 13:24:44 -0800
Subject: [PATCH 3/3] Include code link on mobile
---
static/style.css | 27 +++++++++++++++++----------
templates/base.html | 2 +-
2 files changed, 18 insertions(+), 11 deletions(-)
diff --git a/static/style.css b/static/style.css
index 2d3b34e..4503ae5 100644
--- a/static/style.css
+++ b/static/style.css
@@ -32,23 +32,30 @@ body {
}
nav {
- display: flex;
+ display: grid;
+ grid-template-areas: "logo searchbox code";
+
justify-content: space-between;
align-items: center;
+
color: var(--accent);
background: var(--outside);
- padding: 5px 15px;
- font-size: 20px;
- min-height: 40px;
- position: fixed;
- width: calc(100% - 30px);
box-shadow: var(--shadow);
- top: 0;
+
+ font-size: 20px;
+
z-index: 1;
+ top: 0;
+ padding: 5px 15px;
+ min-height: 40px;
+ width: calc(100% - 30px);
+ position: fixed;
}
nav * { color: var(--text); }
-nav #reddit { color: var(--accent); }
+nav #reddit, #code { color: var(--accent); }
+nav #logo { grid-area: logo; }
+nav #code { grid-area: code; }
nav #version { opacity: 25%; }
#settings_link {
@@ -246,6 +253,7 @@ select, #search {
}
#searchbox {
+ grid-area: searchbox;
display: flex;
box-shadow: var(--shadow);
border-radius: 5px;
@@ -939,7 +947,7 @@ td, th {
}
nav {
- flex-direction: column;
+ grid-template-areas: 'logo code' 'searchbox searchbox';
padding: 10px;
width: calc(100% - 20px);
}
@@ -952,5 +960,4 @@ td, th {
#user, #sidebar { margin: 20px 0; }
#logo { margin: 5px auto; }
#searchbox { width: 100%; }
- #github { display: none; }
}
diff --git a/templates/base.html b/templates/base.html
index b01e205..84f50ca 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -26,7 +26,7 @@
settings
{% block search %}{% endblock %}
- GITHUB
+ code