From add7efea3cd0639a736c539f595d6afdcf8ed6cd Mon Sep 17 00:00:00 2001 From: robrobinbin Date: Tue, 12 Jan 2021 18:53:10 +0100 Subject: [PATCH 01/12] Update style.css --- static/style.css | 1 + 1 file changed, 1 insertion(+) diff --git a/static/style.css b/static/style.css index 20c5712..568e4f9 100644 --- a/static/style.css +++ b/static/style.css @@ -779,6 +779,7 @@ input[type="submit"] { margin-top: 10px; border-radius: 5px; box-shadow: var(--shadow); + overflow: auto; } .md table { From 374f53eb320844be14d13bf637afc33f2077bb22 Mon Sep 17 00:00:00 2001 From: robrobinbin <> Date: Tue, 12 Jan 2021 19:59:32 +0100 Subject: [PATCH 02/12] Relative timestamps for recent posts --- src/utils.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 443e6d0..88c1224 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -7,7 +7,7 @@ use base64::encode; use regex::Regex; use serde_json::from_str; use std::collections::HashMap; -use time::OffsetDateTime; +use time::{OffsetDateTime, Duration}; use url::Url; // @@ -185,6 +185,18 @@ pub async fn media(data: &serde_json::Value) -> (String, String) { (post_type.to_string(), url) } +pub fn time(unix_time: i64) -> String { + let time = OffsetDateTime::from_unix_timestamp(unix_time); + let time_delta = OffsetDateTime::now_utc() - time; + if time_delta > Duration::days(1) { + time.format("%b %d '%y") // %b %e '%y + } else if time_delta.whole_hours() > 0 { + format!("{}h ago", time_delta.whole_hours()) + } else { + format!("{}m ago", time_delta.whole_minutes()) + } +} + // // JSON PARSING // @@ -227,8 +239,8 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec Result<(Vec Date: Tue, 12 Jan 2021 20:02:35 +0100 Subject: [PATCH 03/12] whitespace --- src/utils.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 88c1224..2d4100c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -239,8 +239,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec Date: Tue, 12 Jan 2021 20:03:54 +0100 Subject: [PATCH 04/12] whitespace --- src/utils.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils.rs b/src/utils.rs index 2d4100c..0003cf0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -240,6 +240,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec Date: Tue, 12 Jan 2021 22:43:03 +0100 Subject: [PATCH 05/12] Start richtext flairs --- src/post.rs | 32 +++++++++++------------ src/proxy.rs | 2 ++ src/utils.rs | 56 ++++++++++++++++++++++++++++++---------- static/style.css | 9 +++++++ templates/post.html | 14 +++------- templates/search.html | 8 ++---- templates/subreddit.html | 4 +-- templates/user.html | 12 ++++----- templates/utils.html | 16 +++++++++++- 9 files changed, 97 insertions(+), 56 deletions(-) diff --git a/src/post.rs b/src/post.rs index f84d424..f6199d1 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::{cookie, error, format_num, format_url, media, param, prefs, request, rewrite_url, val, Comment, Flags, Flair, Post, Preferences}; +use crate::utils::{cookie, error, format_num, format_url, media, parse_rich_flair, param, prefs, request, rewrite_url, val, Comment, Flags, Flair, Post, Preferences}; use actix_web::{HttpRequest, HttpResponse}; use async_recursion::async_recursion; @@ -82,25 +82,25 @@ async fn parse_post(json: &serde_json::Value) -> Post { community: val(post, "subreddit"), body: rewrite_url(&val(post, "selftext_html")), author: val(post, "author"), - author_flair: Flair( - val(post, "author_flair_text"), - val(post, "author_flair_background_color"), - val(post, "author_flair_text_color"), - ), + author_flair: Flair{ + flair_parts: parse_rich_flair(post["data"]["author_flair_richtext"].as_array().unwrap()), + background_color: val(post, "author_flair_background_color"), + foreground_color: val(post, "author_flair_text_color"), + }, permalink: val(post, "permalink"), score: format_num(score), upvote_ratio: ratio as i64, post_type, thumbnail: format_url(val(post, "thumbnail").as_str()), - flair: Flair( - val(post, "link_flair_text"), - val(post, "link_flair_background_color"), - if val(post, "link_flair_text_color") == "dark" { + flair: Flair{ + flair_parts: parse_rich_flair(post["data"]["link_flair_richtext"].as_array().unwrap()), + background_color: val(post, "link_flair_background_color"), + foreground_color: if val(post, "link_flair_text_color") == "dark" { "black".to_string() } else { "white".to_string() }, - ), + }, flags: Flags { nsfw: post["data"]["over_18"].as_bool().unwrap_or(false), stickied: post["data"]["stickied"].as_bool().unwrap_or(false), @@ -145,11 +145,11 @@ async fn parse_comments(json: &serde_json::Value) -> Vec { score: format_num(score), time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"), replies, - flair: Flair( - val(&comment, "author_flair_text"), - val(&comment, "author_flair_background_color"), - val(&comment, "author_flair_text_color"), - ), + flair: Flair{ + flair_parts: parse_rich_flair(comment["data"]["author_flair_richtext"].as_array().unwrap()), + background_color: val(&comment, "author_flair_background_color"), + foreground_color: val(&comment, "author_flair_text_color"), + }, }); } diff --git a/src/proxy.rs b/src/proxy.rs index 0f9dc36..c874b1e 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -8,6 +8,8 @@ pub async fn handler(web::Path(b64): web::Path) -> Result // THUMBNAILS "a.thumbs.redditmedia.com", "b.thumbs.redditmedia.com", + // EMOJI + "emoji.redditmedia.com", // ICONS "styles.redditmedia.com", "www.redditstatic.com", diff --git a/src/utils.rs b/src/utils.rs index 443e6d0..b049e53 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,7 +5,7 @@ use actix_web::{cookie::Cookie, HttpRequest, HttpResponse, Result}; use askama::Template; use base64::encode; use regex::Regex; -use serde_json::from_str; +use serde_json::{from_str, Value}; use std::collections::HashMap; use time::OffsetDateTime; use url::Url; @@ -13,8 +13,18 @@ use url::Url; // // STRUCTS // -// Post flair with text, background color and foreground color -pub struct Flair(pub String, pub String, pub String); +// Post flair with content, background color and foreground color +pub struct Flair{ + pub flair_parts: Vec, + pub background_color: String, + pub foreground_color: String, +} + +pub struct FlairPart{ + pub flair_part_type: String, + pub value: String, +} + // Post flags with nsfw and stickied pub struct Flags { pub nsfw: bool, @@ -185,6 +195,26 @@ pub async fn media(data: &serde_json::Value) -> (String, String) { (post_type.to_string(), url) } +pub fn parse_rich_flair(rich_flair: &Vec) -> Vec { + let mut result: Vec = Vec::new(); + for part in rich_flair { + let flair_part_type = part["e"].as_str().unwrap_or_default().to_string(); + let value = if flair_part_type == "text" { + part["t"].as_str().unwrap_or_default().to_string() + + } else if flair_part_type == "emoji" { + format_url(part["u"].as_str().unwrap_or_default()) + } else { + "".to_string() + }; + result.push(FlairPart { + flair_part_type, + value, + }); + } + result +} + // // JSON PARSING // @@ -238,26 +268,26 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec
u/{{ item.author }} - {% if item.flair.0 != "" %} - {{ item.flair.0 }} - {% endif %} + {% call utils::render_flair(item.flair) %} {{ item.time }}

{{ item.body }}

@@ -50,17 +48,13 @@ r/{{ post.community }} - {% if post.author_flair.0 != "" %} - {{ post.author_flair.0 }} - {% endif %} + {% call utils::render_flair(post.author_flair) %} {{ post.time }}

{{ post.title }} - {% if post.flair.0 != "" %} - {{ post.flair.0 }} - {% endif %} + {% call utils::render_flair(post.flair) %} @@ -119,4 +113,4 @@ {%- endfor %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/search.html b/templates/search.html index f97a4e9..2862194 100644 --- a/templates/search.html +++ b/templates/search.html @@ -34,16 +34,12 @@ r/{{ post.community }} - {% if post.author_flair.0 != "" %} - {{ post.author_flair.0 }} - {% endif %} + {% call utils::render_flair(post.author_flair) %} {{ post.time }}

- {% if post.flair.0 != "" %} - {{ post.flair.0 }} - {% endif %} + {% call utils::render_flair(post.flair) %} {{ post.title }}

diff --git a/templates/subreddit.html b/templates/subreddit.html index fcfce7a..abfa12b 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -46,9 +46,7 @@ {{ post.time }}

- {% if post.flair.0 != "" %} - {{ post.flair.0 }} - {% endif %} + {% call utils::render_flair(post.flair) %} {{ post.title }}

diff --git a/templates/user.html b/templates/user.html index f1afd45..6725894 100644 --- a/templates/user.html +++ b/templates/user.html @@ -32,17 +32,15 @@

r/{{ post.community }} - {% if post.author_flair.0 != "" %} - {{ post.author_flair.0 }} - {% endif %} + {% call utils::render_flair(post.author_flair) %} {{ post.time }}

- {% if post.flair.0 == "Comment" %} - {% else if post.flair.0 == "" %} + {% if post.flair.background_color == "Comment" %} + {% else if post.flair.background_color == "" %} {% else %} - {{ post.flair.0 }} + {% call utils::render_flair(post.flair) %} {% endif %} {{ post.title }}

@@ -102,4 +100,4 @@
-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/utils.html b/templates/utils.html index a34d987..0a0b726 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -25,4 +25,18 @@ {% endif %} -{%- endmacro %} \ No newline at end of file +{%- endmacro %} + +{% macro render_flair(flair) -%} +{% if flair.flair_parts.len() > 0 %} + +{% for flair_part in flair.flair_parts %} +{% if flair_part.flair_part_type == "emoji" %} + +{% else if flair_part.flair_part_type == "text" %} + {{ flair_part.value }} +{% endif %} +{% endfor %} + +{% endif %} +{%- endmacro %} From a4d77926b6f7a011bcd35408a0471e24efd80432 Mon Sep 17 00:00:00 2001 From: robrobinbin <> Date: Tue, 12 Jan 2021 23:34:16 +0100 Subject: [PATCH 06/12] Rich flairs --- templates/post.html | 12 +++++++++--- templates/search.html | 8 ++++++-- templates/subreddit.html | 4 +++- templates/user.html | 6 ++++-- templates/utils.html | 6 +----- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/templates/post.html b/templates/post.html index 944c4f3..e9c7a5b 100644 --- a/templates/post.html +++ b/templates/post.html @@ -22,7 +22,9 @@
u/{{ item.author }} - {% call utils::render_flair(item.flair) %} + {% if item.flair.flair_parts.len() > 0 %} + {% call utils::render_flair(item.flair.flair_parts) %} + {% endif %} {{ item.time }}

{{ item.body }}

@@ -48,13 +50,17 @@ r/{{ post.community }} - {% call utils::render_flair(post.author_flair) %} + {% if post.author_flair.flair_parts.len() > 0 %} + {% call utils::render_flair(post.author_flair.flair_parts) %} + {% endif %} {{ post.time }}

{{ post.title }} - {% call utils::render_flair(post.flair) %} + {% if post.flair.flair_parts.len() > 0 %} + {% call utils::render_flair(post.flair.flair_parts) %} + {% endif %} diff --git a/templates/search.html b/templates/search.html index 2862194..fdb5689 100644 --- a/templates/search.html +++ b/templates/search.html @@ -34,12 +34,16 @@ r/{{ post.community }} - {% call utils::render_flair(post.author_flair) %} + {% if post.author_flair.flair_parts.len() > 0 %} + {% call utils::render_flair(post.author_flair.flair_parts) %} + {% endif %} {{ post.time }}

- {% call utils::render_flair(post.flair) %} + {% if post.flair.flair_parts.len() > 0 %} + {% call utils::render_flair(post.flair.flair_parts) %} + {% endif %} {{ post.title }}

diff --git a/templates/subreddit.html b/templates/subreddit.html index abfa12b..734d295 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -46,7 +46,9 @@ {{ post.time }}

- {% call utils::render_flair(post.flair) %} + {% if post.flair.flair_parts.len() > 0 %} + {% call utils::render_flair(post.flair.flair_parts) %} + {% endif %} {{ post.title }}

diff --git a/templates/user.html b/templates/user.html index 6725894..070e253 100644 --- a/templates/user.html +++ b/templates/user.html @@ -32,7 +32,9 @@

r/{{ post.community }} - {% call utils::render_flair(post.author_flair) %} + {% if post.author_flair.flair_parts.len() > 0 %} + {% call utils::render_flair(post.author_flair.flair_parts) %} + {% endif %} {{ post.time }}

@@ -40,7 +42,7 @@ {% if post.flair.background_color == "Comment" %} {% else if post.flair.background_color == "" %} {% else %} - {% call utils::render_flair(post.flair) %} + {% call utils::render_flair(post.flair.flair_parts) %} {% endif %} {{ post.title }}

diff --git a/templates/utils.html b/templates/utils.html index 0a0b726..5665246 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -28,15 +28,11 @@ {%- endmacro %} {% macro render_flair(flair) -%} -{% if flair.flair_parts.len() > 0 %} - -{% for flair_part in flair.flair_parts %} +{% for flair_part in flair %} {% if flair_part.flair_part_type == "emoji" %} {% else if flair_part.flair_part_type == "text" %} {{ flair_part.value }} {% endif %} {% endfor %} - -{% endif %} {%- endmacro %} From d2314580a9faa77c4db586f77ea0495efc6730bd Mon Sep 17 00:00:00 2001 From: robrobinbin <> Date: Tue, 12 Jan 2021 23:50:50 +0100 Subject: [PATCH 07/12] Rich flairs --- templates/utils.html | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/templates/utils.html b/templates/utils.html index 5665246..968f198 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -28,11 +28,5 @@ {%- endmacro %} {% macro render_flair(flair) -%} -{% for flair_part in flair %} -{% if flair_part.flair_part_type == "emoji" %} - -{% else if flair_part.flair_part_type == "text" %} - {{ flair_part.value }} -{% endif %} -{% endfor %} +{% for flair_part in flair %}{% if flair_part.flair_part_type == "emoji" %}{% else if flair_part.flair_part_type == "text" %}{{ flair_part.value }}{% endif %}{% endfor %} {%- endmacro %} From 41c4661bbb03f8c6fa6f7d1face21906bbfa2376 Mon Sep 17 00:00:00 2001 From: robrobinbin <> Date: Tue, 12 Jan 2021 23:55:35 +0100 Subject: [PATCH 08/12] Rich flairs --- static/style.css | 1 + 1 file changed, 1 insertion(+) diff --git a/static/style.css b/static/style.css index cdbd8ba..1f38067 100644 --- a/static/style.css +++ b/static/style.css @@ -506,6 +506,7 @@ input[type="submit"]:hover { color: var(--accent); } background-size: contain; background-position: 50% 50%; background-repeat: no-repeat; + vertical-align: middle; } .nsfw { From b0540d2c573c1b05694ba7ebd2cf8e3939444c4a Mon Sep 17 00:00:00 2001 From: robrobinbin <> Date: Wed, 13 Jan 2021 00:10:06 +0100 Subject: [PATCH 09/12] Rich flairs --- src/post.rs | 6 +++--- src/utils.rs | 36 +++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/post.rs b/src/post.rs index f6199d1..9100e0c 100644 --- a/src/post.rs +++ b/src/post.rs @@ -83,7 +83,7 @@ async fn parse_post(json: &serde_json::Value) -> Post { body: rewrite_url(&val(post, "selftext_html")), author: val(post, "author"), author_flair: Flair{ - flair_parts: parse_rich_flair(post["data"]["author_flair_richtext"].as_array().unwrap()), + flair_parts: parse_rich_flair(post["data"]["author_flair_richtext"].as_array()), background_color: val(post, "author_flair_background_color"), foreground_color: val(post, "author_flair_text_color"), }, @@ -93,7 +93,7 @@ async fn parse_post(json: &serde_json::Value) -> Post { post_type, thumbnail: format_url(val(post, "thumbnail").as_str()), flair: Flair{ - flair_parts: parse_rich_flair(post["data"]["link_flair_richtext"].as_array().unwrap()), + flair_parts: parse_rich_flair(post["data"]["link_flair_richtext"].as_array()), background_color: val(post, "link_flair_background_color"), foreground_color: if val(post, "link_flair_text_color") == "dark" { "black".to_string() @@ -146,7 +146,7 @@ async fn parse_comments(json: &serde_json::Value) -> Vec { time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"), replies, flair: Flair{ - flair_parts: parse_rich_flair(comment["data"]["author_flair_richtext"].as_array().unwrap()), + flair_parts: parse_rich_flair(comment["data"]["author_flair_richtext"].as_array()), background_color: val(&comment, "author_flair_background_color"), foreground_color: val(&comment, "author_flair_text_color"), }, diff --git a/src/utils.rs b/src/utils.rs index b049e53..2524d16 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -195,22 +195,24 @@ pub async fn media(data: &serde_json::Value) -> (String, String) { (post_type.to_string(), url) } -pub fn parse_rich_flair(rich_flair: &Vec) -> Vec { +pub fn parse_rich_flair(rich_flair: Option<&Vec>) -> Vec { let mut result: Vec = Vec::new(); - for part in rich_flair { - let flair_part_type = part["e"].as_str().unwrap_or_default().to_string(); - let value = if flair_part_type == "text" { - part["t"].as_str().unwrap_or_default().to_string() - - } else if flair_part_type == "emoji" { - format_url(part["u"].as_str().unwrap_or_default()) - } else { - "".to_string() - }; - result.push(FlairPart { - flair_part_type, - value, - }); + if !rich_flair.is_none() { + for part in rich_flair.unwrap() { + let flair_part_type = part["e"].as_str().unwrap_or_default().to_string(); + let value = if flair_part_type == "text" { + part["t"].as_str().unwrap_or_default().to_string() + + } else if flair_part_type == "emoji" { + format_url(part["u"].as_str().unwrap_or_default()) + } else { + "".to_string() + }; + result.push(FlairPart { + flair_part_type, + value, + }); + } } result } @@ -269,7 +271,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec Result<(Vec Date: Tue, 12 Jan 2021 19:52:02 -0800 Subject: [PATCH 10/12] Fix GIFs --- Cargo.lock | 28 ++++++++++++++-------------- src/post.rs | 2 +- src/utils.rs | 11 ++++++++--- static/style.css | 2 +- templates/post.html | 2 +- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3c3865f..df32cb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -498,9 +498,9 @@ checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" [[package]] name = "bytes" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad1f8e949d755f9d79112b5bb46938e0ef9d3804a0b16dfab13aafcaa5f0fa72" +checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" [[package]] name = "bytestring" @@ -508,7 +508,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90706ba19e97b90786e19dc0d5e2abd80008d99d4c0c5d1ad0b5e72cec7c494d" dependencies = [ - "bytes 1.0.0", + "bytes 1.0.1", ] [[package]] @@ -752,7 +752,7 @@ dependencies = [ "futures-sink", "futures-task", "memchr", - "pin-project-lite 0.2.3", + "pin-project-lite 0.2.4", "pin-utils", "proc-macro-hack", "proc-macro-nested", @@ -856,7 +856,7 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7245cd7449cc792608c3c8a9eaf69bd4eabbabf802713748fd739c98b82f0747" dependencies = [ - "bytes 1.0.0", + "bytes 1.0.1", "fnv", "itoa", ] @@ -1013,9 +1013,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.11" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" +checksum = "fcf3805d4480bb5b86070dcfeb9e2cb2ebc148adb753c5cca5f884d1d65a42b2" dependencies = [ "cfg-if 0.1.10", ] @@ -1244,9 +1244,9 @@ checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b" [[package]] name = "pin-project-lite" -version = "0.2.3" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba36e0a6cc5a4c645073f4984f1ed55d09f5857d4de7c14550baa81a39ef5a17" +checksum = "439697af366c49a6d0a010c56a0d97685bc140ce0d377b13a2ea2aa42d64a827" [[package]] name = "pin-utils" @@ -1472,18 +1472,18 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.118" +version = "1.0.119" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800" +checksum = "9bdd36f49e35b61d49efd8aa7fc068fd295961fd2286d0b2ee9a4c7a14e99cc3" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.118" +version = "1.0.119" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df" +checksum = "552954ce79a059ddd5fd68c271592374bd15cab2274970380c000118aeffe1cd" dependencies = [ "proc-macro2", "quote", @@ -1805,7 +1805,7 @@ checksum = "9f47026cdc4080c07e49b37087de021820269d996f581aac150ef9e5583eefe3" dependencies = [ "cfg-if 1.0.0", "log", - "pin-project-lite 0.2.3", + "pin-project-lite 0.2.4", "tracing-core", ] diff --git a/src/post.rs b/src/post.rs index f84d424..eb4c412 100644 --- a/src/post.rs +++ b/src/post.rs @@ -117,7 +117,7 @@ async fn parse_comments(json: &serde_json::Value) -> Vec { // Separate the comment JSON into a Vector of comments let comment_data = match json["data"]["children"].as_array() { Some(f) => f.to_owned(), - None => { let v = Vec::new(); v } + None => Vec::new(), }; let mut comments: Vec = Vec::new(); diff --git a/src/utils.rs b/src/utils.rs index 0003cf0..2dd5505 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -169,10 +169,15 @@ pub async fn media(data: &serde_json::Value) -> (String, String) { format_url(data["secure_media"]["reddit_video"]["fallback_url"].as_str().unwrap_or_default()) } else if data["post_hint"].as_str().unwrap_or("") == "image" { let preview = data["preview"]["images"][0].clone(); - post_type = "image"; match preview["variants"]["mp4"].as_object() { - Some(gif) => format_url(gif["source"]["url"].as_str().unwrap_or_default()), - None => format_url(preview["source"]["url"].as_str().unwrap_or_default()), + Some(gif) => { + post_type = "gif"; + format_url(gif["source"]["url"].as_str().unwrap_or_default()) + }, + None => { + post_type = "image"; + format_url(preview["source"]["url"].as_str().unwrap_or_default()) + }, } } else if data["is_self"].as_bool().unwrap_or_default() { post_type = "self"; diff --git a/static/style.css b/static/style.css index 568e4f9..702c252 100644 --- a/static/style.css +++ b/static/style.css @@ -223,7 +223,7 @@ aside { /* Sorting and Search */ .search_label { - max-width: 100px; + max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; diff --git a/templates/post.html b/templates/post.html index f5f92fe..1a410df 100644 --- a/templates/post.html +++ b/templates/post.html @@ -66,7 +66,7 @@ {% if post.post_type == "image" %} - {% else if post.post_type == "video" %} + {% else if post.post_type == "video" || post.post_type == "gif" %} {% else if post.post_type == "link" %} {{ post.media }} From 96607256fc48c541b2eb00f448b746e6998ae190 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Tue, 12 Jan 2021 20:18:20 -0800 Subject: [PATCH 11/12] Add Favicon --- src/main.rs | 9 +++++---- static/favicon.ico | Bin 0 -> 789 bytes templates/base.html | 1 + templates/post.html | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 static/favicon.ico diff --git a/src/main.rs b/src/main.rs index 07a351e..b5520c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ // Import Crates -use actix_web::{get, middleware, web, App, HttpResponse, HttpServer}; // dev::Service +use actix_web::{middleware, web, App, HttpResponse, HttpServer}; // dev::Service // Reference local files mod post; @@ -21,9 +21,10 @@ async fn robots() -> HttpResponse { .body(include_str!("../static/robots.txt")) } -#[get("/favicon.ico")] async fn favicon() -> HttpResponse { - HttpResponse::Ok().body("") + HttpResponse::Ok() + .header("Cache-Control", "public, max-age=1209600, s-maxage=86400") + .body(include_bytes!("../static/favicon.ico").as_ref()) } #[actix_web::main] @@ -64,7 +65,7 @@ async fn main() -> std::io::Result<()> { .default_service(web::get().to(|| utils::error("Nothing here".to_string()))) // GENERAL SERVICES .route("/style.css/", web::get().to(style)) - .route("/favicon.ico/", web::get().to(HttpResponse::Ok)) + .route("/favicon.ico/", web::get().to(favicon)) .route("/robots.txt/", web::get().to(robots)) // SETTINGS SERVICE .route("/settings/", web::get().to(settings::get)) diff --git a/static/favicon.ico b/static/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..4ba6bfc25096d08d91936ac3f0473b64463ddf4c GIT binary patch literal 789 zcmV+w1M2*VP)z=qa37iL`vLEH<>1Ct`s9%fNvOaVv( z%UbKhh6Ff{(_xHB0W%^J2}2wpqy(%0i(2ce&;o4R?h%o1K%x=i03jLReNZ)O1lYFy zMnt{?FPlMLCE^Si*IMt^72r5dFYu$;FgQ5qk2^a% z`2!S2M*MeHKF`(I7{p=#l)JhDs~{o?@8_Ncu)yDHGl4Tg;wqW+$pNHXzAJsktRrin zdFa{{_aFm5M>F_5h2(I|3|Ou~_8#`uag6#bU8uDC2PfwWhQO*yd?X z>2S3@ztM`7Qb)#^ap3HU$j5Kr4QkE;&$kcbma19E;M(!s+kv*A?2C(wY&;(S3Ooab zf`o=d3HSnh&|3crN_djXDQ;s+1IykHtBZRDRNy$yjqABFrdvenUeA?Mp-$PqD7XmE T`ywb;00000NkvXXu0mjffyr@+ literal 0 HcmV?d00001 diff --git a/templates/base.html b/templates/base.html index 4ac0cb1..b01e205 100644 --- a/templates/base.html +++ b/templates/base.html @@ -8,6 +8,7 @@ + {% endblock %} diff --git a/templates/post.html b/templates/post.html index 1a410df..4493f5a 100644 --- a/templates/post.html +++ b/templates/post.html @@ -27,7 +27,7 @@ {% endif %} {{ item.time }} -

{{ item.body }}

+
{{ item.body }}
{%- endmacro %} From d9affcdefc3ed979fa3fc7d31e8cea165bc51143 Mon Sep 17 00:00:00 2001 From: robrobinbin <> Date: Wed, 13 Jan 2021 08:23:48 +0100 Subject: [PATCH 12/12] Rich flairs --- src/post.rs | 6 +++--- src/utils.rs | 13 +++++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/post.rs b/src/post.rs index 9100e0c..6f0f38f 100644 --- a/src/post.rs +++ b/src/post.rs @@ -83,7 +83,7 @@ async fn parse_post(json: &serde_json::Value) -> Post { body: rewrite_url(&val(post, "selftext_html")), author: val(post, "author"), author_flair: Flair{ - flair_parts: parse_rich_flair(post["data"]["author_flair_richtext"].as_array()), + flair_parts: parse_rich_flair(val(post, "author_flair_type"), post["data"]["author_flair_richtext"].as_array(), post["data"]["author_flair_text"].as_str()), background_color: val(post, "author_flair_background_color"), foreground_color: val(post, "author_flair_text_color"), }, @@ -93,7 +93,7 @@ async fn parse_post(json: &serde_json::Value) -> Post { post_type, thumbnail: format_url(val(post, "thumbnail").as_str()), flair: Flair{ - flair_parts: parse_rich_flair(post["data"]["link_flair_richtext"].as_array()), + flair_parts: parse_rich_flair(val(post, "link_flair_type"), post["data"]["link_flair_richtext"].as_array(), post["data"]["link_flair_text"].as_str()), background_color: val(post, "link_flair_background_color"), foreground_color: if val(post, "link_flair_text_color") == "dark" { "black".to_string() @@ -146,7 +146,7 @@ async fn parse_comments(json: &serde_json::Value) -> Vec { time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"), replies, flair: Flair{ - flair_parts: parse_rich_flair(comment["data"]["author_flair_richtext"].as_array()), + flair_parts: parse_rich_flair(val(&comment, "author_flair_type"), comment["data"]["author_flair_richtext"].as_array(), comment["data"]["author_flair_text"].as_str()), background_color: val(&comment, "author_flair_background_color"), foreground_color: val(&comment, "author_flair_text_color"), }, diff --git a/src/utils.rs b/src/utils.rs index 2524d16..5725435 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -195,9 +195,9 @@ pub async fn media(data: &serde_json::Value) -> (String, String) { (post_type.to_string(), url) } -pub fn parse_rich_flair(rich_flair: Option<&Vec>) -> Vec { +pub fn parse_rich_flair(flair_type: String, rich_flair: Option<&Vec>, text_flair: Option<&str>) -> Vec { let mut result: Vec = Vec::new(); - if !rich_flair.is_none() { + if flair_type == "richtext" && !rich_flair.is_none() { for part in rich_flair.unwrap() { let flair_part_type = part["e"].as_str().unwrap_or_default().to_string(); let value = if flair_part_type == "text" { @@ -213,6 +213,11 @@ pub fn parse_rich_flair(rich_flair: Option<&Vec>) -> Vec { value, }); } + } else if flair_type == "text" && !text_flair.is_none() { + result.push(FlairPart { + flair_part_type: "text".to_string(), + value: text_flair.unwrap().to_string(), + }); } result } @@ -271,7 +276,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec Result<(Vec