Start richtext flairs
This commit is contained in:
parent
065d82a5f5
commit
bbe7024323
32
src/post.rs
32
src/post.rs
@ -1,5 +1,5 @@
|
|||||||
// CRATES
|
// 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 actix_web::{HttpRequest, HttpResponse};
|
||||||
|
|
||||||
use async_recursion::async_recursion;
|
use async_recursion::async_recursion;
|
||||||
@ -82,25 +82,25 @@ async fn parse_post(json: &serde_json::Value) -> Post {
|
|||||||
community: val(post, "subreddit"),
|
community: val(post, "subreddit"),
|
||||||
body: rewrite_url(&val(post, "selftext_html")),
|
body: rewrite_url(&val(post, "selftext_html")),
|
||||||
author: val(post, "author"),
|
author: val(post, "author"),
|
||||||
author_flair: Flair(
|
author_flair: Flair{
|
||||||
val(post, "author_flair_text"),
|
flair_parts: parse_rich_flair(post["data"]["author_flair_richtext"].as_array().unwrap()),
|
||||||
val(post, "author_flair_background_color"),
|
background_color: val(post, "author_flair_background_color"),
|
||||||
val(post, "author_flair_text_color"),
|
foreground_color: val(post, "author_flair_text_color"),
|
||||||
),
|
},
|
||||||
permalink: val(post, "permalink"),
|
permalink: val(post, "permalink"),
|
||||||
score: format_num(score),
|
score: format_num(score),
|
||||||
upvote_ratio: ratio as i64,
|
upvote_ratio: ratio as i64,
|
||||||
post_type,
|
post_type,
|
||||||
thumbnail: format_url(val(post, "thumbnail").as_str()),
|
thumbnail: format_url(val(post, "thumbnail").as_str()),
|
||||||
flair: Flair(
|
flair: Flair{
|
||||||
val(post, "link_flair_text"),
|
flair_parts: parse_rich_flair(post["data"]["link_flair_richtext"].as_array().unwrap()),
|
||||||
val(post, "link_flair_background_color"),
|
background_color: val(post, "link_flair_background_color"),
|
||||||
if val(post, "link_flair_text_color") == "dark" {
|
foreground_color: if val(post, "link_flair_text_color") == "dark" {
|
||||||
"black".to_string()
|
"black".to_string()
|
||||||
} else {
|
} else {
|
||||||
"white".to_string()
|
"white".to_string()
|
||||||
},
|
},
|
||||||
),
|
},
|
||||||
flags: Flags {
|
flags: Flags {
|
||||||
nsfw: post["data"]["over_18"].as_bool().unwrap_or(false),
|
nsfw: post["data"]["over_18"].as_bool().unwrap_or(false),
|
||||||
stickied: post["data"]["stickied"].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<Comment> {
|
|||||||
score: format_num(score),
|
score: format_num(score),
|
||||||
time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"),
|
time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"),
|
||||||
replies,
|
replies,
|
||||||
flair: Flair(
|
flair: Flair{
|
||||||
val(&comment, "author_flair_text"),
|
flair_parts: parse_rich_flair(comment["data"]["author_flair_richtext"].as_array().unwrap()),
|
||||||
val(&comment, "author_flair_background_color"),
|
background_color: val(&comment, "author_flair_background_color"),
|
||||||
val(&comment, "author_flair_text_color"),
|
foreground_color: val(&comment, "author_flair_text_color"),
|
||||||
),
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,8 @@ pub async fn handler(web::Path(b64): web::Path<String>) -> Result<HttpResponse>
|
|||||||
// THUMBNAILS
|
// THUMBNAILS
|
||||||
"a.thumbs.redditmedia.com",
|
"a.thumbs.redditmedia.com",
|
||||||
"b.thumbs.redditmedia.com",
|
"b.thumbs.redditmedia.com",
|
||||||
|
// EMOJI
|
||||||
|
"emoji.redditmedia.com",
|
||||||
// ICONS
|
// ICONS
|
||||||
"styles.redditmedia.com",
|
"styles.redditmedia.com",
|
||||||
"www.redditstatic.com",
|
"www.redditstatic.com",
|
||||||
|
56
src/utils.rs
56
src/utils.rs
@ -5,7 +5,7 @@ use actix_web::{cookie::Cookie, HttpRequest, HttpResponse, Result};
|
|||||||
use askama::Template;
|
use askama::Template;
|
||||||
use base64::encode;
|
use base64::encode;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde_json::from_str;
|
use serde_json::{from_str, Value};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
@ -13,8 +13,18 @@ use url::Url;
|
|||||||
//
|
//
|
||||||
// STRUCTS
|
// STRUCTS
|
||||||
//
|
//
|
||||||
// Post flair with text, background color and foreground color
|
// Post flair with content, background color and foreground color
|
||||||
pub struct Flair(pub String, pub String, pub String);
|
pub struct Flair{
|
||||||
|
pub flair_parts: Vec<FlairPart>,
|
||||||
|
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
|
// Post flags with nsfw and stickied
|
||||||
pub struct Flags {
|
pub struct Flags {
|
||||||
pub nsfw: bool,
|
pub nsfw: bool,
|
||||||
@ -185,6 +195,26 @@ pub async fn media(data: &serde_json::Value) -> (String, String) {
|
|||||||
(post_type.to_string(), url)
|
(post_type.to_string(), url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_rich_flair(rich_flair: &Vec<Value>) -> Vec<FlairPart> {
|
||||||
|
let mut result: Vec<FlairPart> = 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
|
// JSON PARSING
|
||||||
//
|
//
|
||||||
@ -238,26 +268,26 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
|
|||||||
community: val(post, "subreddit"),
|
community: val(post, "subreddit"),
|
||||||
body: rewrite_url(&val(post, "body_html")),
|
body: rewrite_url(&val(post, "body_html")),
|
||||||
author: val(post, "author"),
|
author: val(post, "author"),
|
||||||
author_flair: Flair(
|
author_flair: Flair{
|
||||||
val(post, "author_flair_text"),
|
flair_parts: parse_rich_flair(post["data"]["author_flair_richtext"].as_array().unwrap()),
|
||||||
val(post, "author_flair_background_color"),
|
background_color: val(post, "author_flair_background_color"),
|
||||||
val(post, "author_flair_text_color"),
|
foreground_color: val(post, "author_flair_text_color"),
|
||||||
),
|
},
|
||||||
score: format_num(score),
|
score: format_num(score),
|
||||||
upvote_ratio: ratio as i64,
|
upvote_ratio: ratio as i64,
|
||||||
post_type,
|
post_type,
|
||||||
thumbnail: format_url(val(post, "thumbnail").as_str()),
|
thumbnail: format_url(val(post, "thumbnail").as_str()),
|
||||||
media,
|
media,
|
||||||
domain: val(post, "domain"),
|
domain: val(post, "domain"),
|
||||||
flair: Flair(
|
flair: Flair{
|
||||||
val(post, "link_flair_text"),
|
flair_parts: parse_rich_flair(post["data"]["link_flair_richtext"].as_array().unwrap()),
|
||||||
val(post, "link_flair_background_color"),
|
background_color: val(post, "link_flair_background_color"),
|
||||||
if val(post, "link_flair_text_color") == "dark" {
|
foreground_color: if val(post, "link_flair_text_color") == "dark" {
|
||||||
"black".to_string()
|
"black".to_string()
|
||||||
} else {
|
} else {
|
||||||
"white".to_string()
|
"white".to_string()
|
||||||
},
|
},
|
||||||
),
|
},
|
||||||
flags: Flags {
|
flags: Flags {
|
||||||
nsfw: post["data"]["over_18"].as_bool().unwrap_or_default(),
|
nsfw: post["data"]["over_18"].as_bool().unwrap_or_default(),
|
||||||
stickied: post["data"]["stickied"].as_bool().unwrap_or_default(),
|
stickied: post["data"]["stickied"].as_bool().unwrap_or_default(),
|
||||||
|
@ -499,6 +499,15 @@ input[type="submit"]:hover { color: var(--accent); }
|
|||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.emoji {
|
||||||
|
width: 1em;
|
||||||
|
height: 1em;
|
||||||
|
display: inline-block;
|
||||||
|
background-size: contain;
|
||||||
|
background-position: 50% 50%;
|
||||||
|
background-repeat: no-repeat;
|
||||||
|
}
|
||||||
|
|
||||||
.nsfw {
|
.nsfw {
|
||||||
color: var(--nsfw);
|
color: var(--nsfw);
|
||||||
margin-top: 20px;
|
margin-top: 20px;
|
||||||
|
@ -22,9 +22,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<details class="comment_right" open>
|
<details class="comment_right" open>
|
||||||
<summary class="comment_data"><a class="comment_author {% if item.author == post.author %}op{% endif %}" href="/u/{{ item.author }}">u/{{ item.author }}</a>
|
<summary class="comment_data"><a class="comment_author {% if item.author == post.author %}op{% endif %}" href="/u/{{ item.author }}">u/{{ item.author }}</a>
|
||||||
{% if item.flair.0 != "" %}
|
{% call utils::render_flair(item.flair) %}
|
||||||
<small class="author_flair">{{ item.flair.0 }}</small>
|
|
||||||
{% endif %}
|
|
||||||
<span class="datetime">{{ item.time }}</span>
|
<span class="datetime">{{ item.time }}</span>
|
||||||
</summary>
|
</summary>
|
||||||
<p class="comment_body">{{ item.body }}</p>
|
<p class="comment_body">{{ item.body }}</p>
|
||||||
@ -50,17 +48,13 @@
|
|||||||
<a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a>
|
<a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a>
|
||||||
<span class="dot">•</span>
|
<span class="dot">•</span>
|
||||||
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
|
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
|
||||||
{% if post.author_flair.0 != "" %}
|
{% call utils::render_flair(post.author_flair) %}
|
||||||
<small class="author_flair">{{ post.author_flair.0 }}</small>
|
|
||||||
{% endif %}
|
|
||||||
<span class="dot">•</span>
|
<span class="dot">•</span>
|
||||||
<span class="datetime">{{ post.time }}</span>
|
<span class="datetime">{{ post.time }}</span>
|
||||||
</p>
|
</p>
|
||||||
<a href="{{ post.permalink }}" class="post_title">
|
<a href="{{ post.permalink }}" class="post_title">
|
||||||
{{ post.title }}
|
{{ post.title }}
|
||||||
{% if post.flair.0 != "" %}
|
{% call utils::render_flair(post.flair) %}
|
||||||
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
|
|
||||||
{% endif %}
|
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<!-- POST MEDIA -->
|
<!-- POST MEDIA -->
|
||||||
|
@ -34,16 +34,12 @@
|
|||||||
<a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a>
|
<a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a>
|
||||||
<span class="dot">•</span>
|
<span class="dot">•</span>
|
||||||
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
|
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
|
||||||
{% if post.author_flair.0 != "" %}
|
{% call utils::render_flair(post.author_flair) %}
|
||||||
<small class="author_flair">{{ post.author_flair.0 }}</small>
|
|
||||||
{% endif %}
|
|
||||||
<span class="dot">•</span>
|
<span class="dot">•</span>
|
||||||
<span class="datetime">{{ post.time }}</span>
|
<span class="datetime">{{ post.time }}</span>
|
||||||
</p>
|
</p>
|
||||||
<p class="post_title">
|
<p class="post_title">
|
||||||
{% if post.flair.0 != "" %}
|
{% call utils::render_flair(post.flair) %}
|
||||||
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
|
|
||||||
{% endif %}
|
|
||||||
<a href="{{ post.permalink }}">{{ post.title }}</a>
|
<a href="{{ post.permalink }}">{{ post.title }}</a>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
@ -46,9 +46,7 @@
|
|||||||
<span class="datetime">{{ post.time }}</span>
|
<span class="datetime">{{ post.time }}</span>
|
||||||
</p>
|
</p>
|
||||||
<p class="post_title">
|
<p class="post_title">
|
||||||
{% if post.flair.0 != "" %}
|
{% call utils::render_flair(post.flair) %}
|
||||||
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
|
|
||||||
{% endif %}
|
|
||||||
<a href="{{ post.permalink }}">{{ post.title }}</a>
|
<a href="{{ post.permalink }}">{{ post.title }}</a>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
@ -32,17 +32,15 @@
|
|||||||
<div class="post_text">
|
<div class="post_text">
|
||||||
<p class="post_header">
|
<p class="post_header">
|
||||||
<a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a>
|
<a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a>
|
||||||
{% if post.author_flair.0 != "" %}
|
{% call utils::render_flair(post.author_flair) %}
|
||||||
<small class="author_flair">{{ post.author_flair.0 }}</small>
|
|
||||||
{% endif %}
|
|
||||||
<span class="dot">•</span>
|
<span class="dot">•</span>
|
||||||
<span class="datetime">{{ post.time }}</span>
|
<span class="datetime">{{ post.time }}</span>
|
||||||
</p>
|
</p>
|
||||||
<p class="post_title">
|
<p class="post_title">
|
||||||
{% if post.flair.0 == "Comment" %}
|
{% if post.flair.background_color == "Comment" %}
|
||||||
{% else if post.flair.0 == "" %}
|
{% else if post.flair.background_color == "" %}
|
||||||
{% else %}
|
{% else %}
|
||||||
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
|
{% call utils::render_flair(post.flair) %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<a href="{{ post.permalink }}">{{ post.title }}</a>
|
<a href="{{ post.permalink }}">{{ post.title }}</a>
|
||||||
</p>
|
</p>
|
||||||
|
@ -26,3 +26,17 @@
|
|||||||
<input type="submit" value="→">
|
<input type="submit" value="→">
|
||||||
</form>
|
</form>
|
||||||
{%- endmacro %}
|
{%- endmacro %}
|
||||||
|
|
||||||
|
{% macro render_flair(flair) -%}
|
||||||
|
{% if flair.flair_parts.len() > 0 %}
|
||||||
|
<small class="post_flair" style="color:{{ flair.foreground_color }}; background:{{ flair.background_color }}">
|
||||||
|
{% for flair_part in flair.flair_parts %}
|
||||||
|
{% if flair_part.flair_part_type == "emoji" %}
|
||||||
|
<span class="emoji" style="background-image:url('{{ flair_part.value }}')"></span>
|
||||||
|
{% else if flair_part.flair_part_type == "text" %}
|
||||||
|
<span>{{ flair_part.value }}</span>
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
</small>
|
||||||
|
{% endif %}
|
||||||
|
{%- endmacro %}
|
||||||
|
Loading…
Reference in New Issue
Block a user