mirror of
https://github.com/redlib-org/redlib.git
synced 2025-05-15 14:32:50 +00:00
Merge branch 'master' into rich-flairs
This commit is contained in:
commit
c6627ceece
8 changed files with 47 additions and 27 deletions
|
@ -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))
|
||||
|
|
|
@ -117,7 +117,7 @@ async fn parse_comments(json: &serde_json::Value) -> Vec<Comment> {
|
|||
// 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<Comment> = Vec::new();
|
||||
|
|
27
src/utils.rs
27
src/utils.rs
|
@ -7,7 +7,7 @@ use base64::encode;
|
|||
use regex::Regex;
|
||||
use serde_json::{from_str, Value};
|
||||
use std::collections::HashMap;
|
||||
use time::OffsetDateTime;
|
||||
use time::{OffsetDateTime, Duration};
|
||||
use url::Url;
|
||||
|
||||
//
|
||||
|
@ -179,10 +179,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";
|
||||
|
@ -222,6 +227,18 @@ pub fn parse_rich_flair(flair_type: String, rich_flair: Option<&Vec<Value>>, tex
|
|||
result
|
||||
}
|
||||
|
||||
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
|
||||
//
|
||||
|
@ -300,7 +317,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
|
|||
stickied: post["data"]["stickied"].as_bool().unwrap_or_default(),
|
||||
},
|
||||
permalink: val(post, "permalink"),
|
||||
time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d '%y"), // %b %e '%y
|
||||
time: time(unix_time),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue