Add Pages to User Profiles
This commit is contained in:
parent
bec5c78709
commit
68495fb280
27
src/user.rs
27
src/user.rs
@ -11,11 +11,22 @@ struct UserTemplate {
|
|||||||
user: User,
|
user: User,
|
||||||
posts: Vec<Post>,
|
posts: Vec<Post>,
|
||||||
sort: String,
|
sort: String,
|
||||||
|
ends: (String, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn render(username: String, sort: String) -> Result<HttpResponse> {
|
async fn render(username: String, sort: Option<String>, ends: (Option<String>, Option<String>)) -> Result<HttpResponse> {
|
||||||
|
let sorting = sort.unwrap_or("new".to_string());
|
||||||
|
|
||||||
|
let before = ends.1.clone().unwrap_or(String::new()); // If there is an after, there must be a before
|
||||||
|
|
||||||
// Build the Reddit JSON API url
|
// Build the Reddit JSON API url
|
||||||
let url: String = format!("user/{}/.json?sort={}", username, sort);
|
let url = match ends.0 {
|
||||||
|
Some(val) => format!("user/{}/.json?sort={}&before={}&count=25", username, sorting, val),
|
||||||
|
None => match ends.1 {
|
||||||
|
Some(val) => format!("user/{}/.json?sort={}&after={}&count=25", username, sorting, val),
|
||||||
|
None => format!("user/{}/.json?sort={}", username, sorting),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
let user = user(&username).await;
|
let user = user(&username).await;
|
||||||
let posts = fetch_posts(url, "Comment".to_string()).await;
|
let posts = fetch_posts(url, "Comment".to_string()).await;
|
||||||
@ -28,10 +39,13 @@ async fn render(username: String, sort: String) -> Result<HttpResponse> {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s))
|
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s))
|
||||||
} else {
|
} else {
|
||||||
|
let posts_unwrapped = posts.unwrap();
|
||||||
|
|
||||||
let s = UserTemplate {
|
let s = UserTemplate {
|
||||||
user: user.unwrap(),
|
user: user.unwrap(),
|
||||||
posts: posts.unwrap().0,
|
posts: posts_unwrapped.0,
|
||||||
sort: sort,
|
sort: sorting,
|
||||||
|
ends: (before, posts_unwrapped.1)
|
||||||
}
|
}
|
||||||
.render()
|
.render()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -41,10 +55,7 @@ async fn render(username: String, sort: String) -> Result<HttpResponse> {
|
|||||||
|
|
||||||
// SERVICES
|
// SERVICES
|
||||||
pub async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
|
pub async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
|
||||||
match ¶ms.sort {
|
render(username, params.sort.clone(), (params.before.clone(), params.after.clone())).await
|
||||||
Some(sort) => render(username, sort.to_string()).await,
|
|
||||||
None => render(username, "hot".to_string()).await,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// USER
|
// USER
|
||||||
|
@ -121,7 +121,7 @@ pub async fn nested_val(j: &serde_json::Value, n: &str, k: &str) -> String {
|
|||||||
// Fetch posts of a user or subreddit
|
// Fetch posts of a user or subreddit
|
||||||
pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Post>, String), &'static str> {
|
pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Post>, String), &'static str> {
|
||||||
// Send a request to the url, receive JSON in response
|
// Send a request to the url, receive JSON in response
|
||||||
let req = request(url).await;
|
let req = request(url.clone()).await;
|
||||||
|
|
||||||
// If the Reddit API returns an error, exit this function
|
// If the Reddit API returns an error, exit this function
|
||||||
if req.is_err() {
|
if req.is_err() {
|
||||||
@ -174,6 +174,8 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Pos
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dbg!(url);
|
||||||
|
|
||||||
Ok((posts, res["data"]["after"].as_str().unwrap_or("").to_string()))
|
Ok((posts, res["data"]["after"].as_str().unwrap_or("").to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,6 +52,15 @@
|
|||||||
</div><br>
|
</div><br>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
<footer>
|
||||||
|
{% if ends.0 != "" %}
|
||||||
|
<a href="?sort={{ sort }}&before={{ ends.0 }}">PREV</a>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% if ends.1 != "" %}
|
||||||
|
<a href="?sort={{ sort }}&after={{ ends.1 }}">NEXT</a>
|
||||||
|
{% endif %}
|
||||||
|
</footer>
|
||||||
</div>
|
</div>
|
||||||
<aside>
|
<aside>
|
||||||
<div class="user">
|
<div class="user">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user