User Flairs

This commit is contained in:
spikecodes 2020-12-20 11:29:23 -08:00
parent 1678245750
commit ef3820a2e1
10 changed files with 64 additions and 31 deletions

2
Cargo.lock generated
View File

@ -1015,7 +1015,7 @@ checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb"
[[package]]
name = "libreddit"
version = "0.2.0"
version = "0.2.1"
dependencies = [
"actix-web",
"askama",

View File

@ -3,7 +3,7 @@ name = "libreddit"
description = " Alternative private front-end to Reddit"
license = "AGPL-3.0"
repository = "https://github.com/spikecodes/libreddit"
version = "0.2.0"
version = "0.2.1"
authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"]
edition = "2018"

View File

@ -31,8 +31,8 @@ Libreddit hopes to provide an easier way to browse Reddit, without the ads, trac
Libreddit currently implements most of Reddit's functionalities but still lacks a few features that are being worked on below.
### In Progress
- User flairs
- Searching
- Multireddits
### How does it compare to Teddit?

View File

@ -17,12 +17,15 @@ struct PostTemplate {
sort: String,
}
async fn render(id: String, sort: String) -> Result<HttpResponse> {
async fn render(id: String, sort: Option<String>) -> Result<HttpResponse> {
// Log the post ID being fetched
dbg!(&id);
// Handling sort paramater
let sorting: String = sort.unwrap_or("confidence".to_string());
// Build the Reddit JSON API url
let url: String = format!("https://reddit.com/{}.json?sort={}", id, sort);
let url: String = format!("https://reddit.com/{}.json?sort={}", id, sorting);
// Send a request to the url, receive JSON in response
let req = request(url).await;
@ -48,7 +51,7 @@ async fn render(id: String, sort: String) -> Result<HttpResponse> {
let s = PostTemplate {
comments: comments.unwrap(),
post: post.unwrap(),
sort: sort,
sort: sorting,
}
.render()
.unwrap();
@ -57,17 +60,11 @@ async fn render(id: String, sort: String) -> Result<HttpResponse> {
// SERVICES
pub async fn short(web::Path(id): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
match &params.sort {
Some(sort) => render(id, sort.to_string()).await,
None => render(id, "confidence".to_string()).await,
}
render(id, params.sort.clone()).await
}
pub async fn page(web::Path((_sub, id)): web::Path<(String, String)>, params: web::Query<Params>) -> Result<HttpResponse> {
match &params.sort {
Some(sort) => render(id, sort.to_string()).await,
None => render(id, "confidence".to_string()).await,
}
render(id, params.sort.clone()).await
}
// UTILITIES
@ -118,6 +115,11 @@ async fn parse_post(json: serde_json::Value) -> Result<Post, &'static str> {
community: val(post_data, "subreddit").await,
body: markdown_to_html(post_data["data"]["selftext"].as_str().unwrap()).await,
author: val(post_data, "author").await,
author_flair: Flair(
val(post_data, "author_flair_text").await,
val(post_data, "author_flair_background_color").await,
val(post_data, "author_flair_text_color").await,
),
url: val(post_data, "permalink").await,
score: format_num(score),
post_type: media.0,
@ -131,7 +133,7 @@ async fn parse_post(json: serde_json::Value) -> Result<Post, &'static str> {
} else {
"white".to_string()
},
),
)
};
Ok(post)
@ -167,6 +169,11 @@ async fn parse_comments(json: serde_json::Value) -> Result<Vec<Comment>, &'stati
score: format_num(score),
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
replies: replies,
flair: Flair(
val(comment, "author_flair_text").await,
val(comment, "author_flair_background_color").await,
val(comment, "author_flair_text_color").await,
),
});
}

View File

@ -22,6 +22,7 @@ pub struct Post {
pub community: String,
pub body: String,
pub author: String,
pub author_flair: Flair,
pub url: String,
pub score: String,
pub post_type: String,
@ -35,6 +36,7 @@ pub struct Post {
pub struct Comment {
pub body: String,
pub author: String,
pub flair: Flair,
pub score: String,
pub time: String,
pub replies: Vec<Comment>,
@ -147,6 +149,11 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Pos
community: val(post, "subreddit").await,
body: val(post, "body").await,
author: val(post, "author").await,
author_flair: Flair(
val(post, "author_flair_text").await,
val(post, "author_flair_background_color").await,
val(post, "author_flair_text_color").await,
),
score: format_num(score),
post_type: "link".to_string(),
media: img,

View File

@ -253,7 +253,7 @@ a:not(.post_right):hover {
display: none;
}
small {
.post_flair {
background: aqua;
color: black;
padding: 5px;
@ -292,6 +292,16 @@ small {
opacity: 0.9;
}
.author_flair {
background: var(--highlighted);
color: white;
padding: 5px;
margin-right: 5px;
border-radius: 5px;
font-size: 12px;
font-weight: bold;
}
.comment_upvote {
margin-top: 0.5em;
border-radius: 5px 5px 0px 0px;

View File

@ -15,14 +15,15 @@
<div class="post_right">
<h4>
<b><a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a></b>
&bull;
Posted by
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
&bull; <a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
{% if post.author_flair.0 != "" %}
<small class="author_flair">{{ post.author_flair.0 }}</small>
{% endif %}
<span class="datetime" style="float: right;">{{ post.time }}</span>
</h4>
<h3 class="post_title">
{% if post.flair.0 != "" %}
<small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
</h3>

View File

@ -14,7 +14,11 @@
</div>
<details class="comment_right" open>
<summary class="comment_data">
<a class="comment_author" href="/u/{{ item.author }}">u/{{ item.author }}</a><span class="datetime">{{ item.time }}</span>
<a class="comment_author" href="/u/{{ item.author }}">u/{{ item.author }}</a>
{% if item.flair.0 != "" %}
<small class="author_flair">{{ item.flair.0 }}</small>
{% endif %}
&bull; <span class="datetime">{{ item.time }}</span>
</summary>
<h4 class="comment_body">{{ item.body }}</h4>
@ -29,14 +33,16 @@
<h4>
<b><a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a></b>
&bull;
Posted by
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
{% if post.author_flair.0 != "" %}
<small class="author_flair">{{ post.author_flair.0 }}</small>
{% endif %}
<span class="datetime">{{ post.time }}</span>
</h4>
<a href="{{ post.url }}" class="post_title">
{{ post.title }}
{% if post.flair.0 != "" %}
<small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
</a>
{% if post.post_type == "image" %}

View File

@ -27,14 +27,15 @@
<div class="post_right">
<h4>
<b><a class="post_subreddit" href="/r/{{ post.community }}">r/{{ sub.name }}</a></b>
&bull;
Posted by
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
&bull; <a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
{% if post.author_flair.0 != "" %}
<small class="author_flair">{{ post.author_flair.0 }}</small>
{% endif %}
<span class="datetime">{{ post.time }}</span>
</h4>
<h3 class="post_title">
{% if post.flair.0 != "" %}
<small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
</h3>

View File

@ -27,16 +27,17 @@
<div class="post_right">
<h4>
<b><a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a></b>
&bull;
Posted by
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
&bull; <a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
{% if post.author_flair.0 != "" %}
<small class="author_flair">{{ post.author_flair.0 }}</small>
{% endif %}
<span class="datetime" style="float: right;">{{ post.time }}</span>
</h4>
<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>
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
</h3>