1.1.2 Flairs

This commit is contained in:
spikecodes 2020-11-16 18:49:08 -08:00
parent 3618f8815f
commit 6853d21ea9
8 changed files with 63 additions and 5 deletions

View File

@ -13,6 +13,9 @@ struct PostTemplate {
sort: String, sort: String,
} }
// Post flair with text, background color and foreground color
pub struct Flair(String, String, String);
pub struct Post { pub struct Post {
pub title: String, pub title: String,
pub community: String, pub community: String,
@ -22,6 +25,7 @@ pub struct Post {
pub score: String, pub score: String,
pub media: String, pub media: String,
pub time: String, pub time: String,
pub flair: Flair,
} }
pub struct Comment { pub struct Comment {
@ -39,7 +43,7 @@ async fn render(id: String, sort: String) -> Result<HttpResponse> {
let s = PostTemplate { let s = PostTemplate {
comments: comments, comments: comments,
post: post, post: post,
sort: sort, sort: sort
} }
.render() .render()
.unwrap(); .unwrap();
@ -126,6 +130,11 @@ async fn fetch_post(id: &String) -> Post {
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() }, score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
media: media(post_data).await, media: media(post_data).await,
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(), time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
flair: Flair(
val(post_data, "link_flair_text").await,
val(post_data, "link_flair_background_color").await,
if val(post_data, "link_flair_text_color").await == "dark" { "black".to_string() } else { "white".to_string() }
),
} }
} }

View File

@ -12,6 +12,9 @@ struct SubredditTemplate {
sort: String, sort: String,
} }
// Post flair with text, background color and foreground color
pub struct Flair(pub String, pub String, pub String);
pub struct Post { pub struct Post {
pub title: String, pub title: String,
pub community: String, pub community: String,
@ -20,6 +23,7 @@ pub struct Post {
pub image: String, pub image: String,
pub url: String, pub url: String,
pub time: String, pub time: String,
pub flair: Flair,
} }
pub struct Subreddit { pub struct Subreddit {
@ -106,6 +110,11 @@ pub async fn posts(sub: String, sort: &String) -> Vec<Post> {
image: img, image: img,
url: val(post, "permalink").await, url: val(post, "permalink").await,
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(), time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
flair: Flair(
val(post, "link_flair_text").await,
val(post, "link_flair_background_color").await,
if val(post, "link_flair_text_color").await == "dark" { "black".to_string() } else { "white".to_string() }
),
}); });
} }
posts posts

View File

@ -12,6 +12,9 @@ struct UserTemplate {
sort: String, sort: String,
} }
// Post flair with text, background color and foreground color
pub struct Flair(String, String, String);
pub struct Post { pub struct Post {
pub title: String, pub title: String,
pub community: String, pub community: String,
@ -20,6 +23,7 @@ pub struct Post {
pub image: String, pub image: String,
pub url: String, pub url: String,
pub time: String, pub time: String,
pub flair: Flair,
} }
pub struct User { pub struct User {
@ -99,6 +103,11 @@ async fn posts(sub: String, sort: &String) -> Vec<Post> {
image: img, image: img,
url: post_val(post, "permalink").await, url: post_val(post, "permalink").await,
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(), time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
flair: Flair(
post_val(post, "link_flair_text").await,
post_val(post, "link_flair_background_color").await,
if post_val(post, "link_flair_text_color").await == "dark" { "black".to_string() } else { "white".to_string() }
),
}); });
} }

View File

@ -220,6 +220,15 @@ span {
border: none; border: none;
} }
small {
background: aqua;
color: black;
padding: 5px;
border-radius: 5px;
font-size: 12px;
font-weight: bold;
}
/* Comment */ /* Comment */
.comment { .comment {

View File

@ -37,7 +37,12 @@
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a> <a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
<span style="float: right;">{{ post.time }}</span> <span style="float: right;">{{ post.time }}</span>
</p> </p>
<h3 class="post_title"><a href="{{ post.url }}">{{ post.title }}</a></h3> <h3 class="post_title">
{% if post.flair.0 != "" %}
<small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
</h3>
</div> </div>
<img class="post_thumbnail" src="{{ post.image }}"> <img class="post_thumbnail" src="{{ post.image }}">
</div><br> </div><br>

View File

@ -32,7 +32,12 @@
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a> <a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
<span style="float: right;">{{ post.time }}</span> <span style="float: right;">{{ post.time }}</span>
</p> </p>
<h3 class="post_title">{{ post.title }}</h3> <h3 class="post_title">
{{ post.title }}
{% if post.flair.0 != "" %}
<small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
</h3>
{{ post.media }} {{ post.media }}
<h4 class="post_body">{{ post.body }}</h4> <h4 class="post_body">{{ post.body }}</h4>
</div> </div>

View File

@ -48,7 +48,12 @@
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a> <a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
<span style="float: right;">{{ post.time }}</span> <span style="float: right;">{{ post.time }}</span>
</p> </p>
<h3 class="post_title"><a href="{{ post.url }}">{{ post.title }}</a></h3> <h3 class="post_title">
{% if post.flair.0 != "" %}
<small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
</h3>
</div> </div>
<img class="post_thumbnail" src="{{ post.image }}"> <img class="post_thumbnail" src="{{ post.image }}">
</div><br> </div><br>

View File

@ -48,7 +48,14 @@
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a> <a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
<span style="float: right;">{{ post.time }}</span> <span style="float: right;">{{ post.time }}</span>
</p> </p>
<h3 class="post_title"><a href="{{ post.url }}">{{ post.title }}</a></h3> <h3 class="post_title">
{% if post.flair.0 == "Comment" %}
{% else if post.flair.0 == "" %}
{% else %}
<small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
</h3>
</div> </div>
<img class="post_thumbnail" src="{{ post.image }}"> <img class="post_thumbnail" src="{{ post.image }}">
</div><br> </div><br>