diff --git a/schemas/query.graphql b/schemas/query.graphql index b66c3fb..6a1768f 100644 --- a/schemas/query.graphql +++ b/schemas/query.graphql @@ -3,11 +3,18 @@ query GetPost($id: ID!) { title createdAt readingTime + uniqueSlug creator { name id imageId } + previewImage { + id + } + previewContent { + subtitle + } content { bodyModel { paragraphs { diff --git a/schemas/schema.graphql b/schemas/schema.graphql index c63d697..ec6a74e 100644 --- a/schemas/schema.graphql +++ b/schemas/schema.graphql @@ -3,63 +3,84 @@ type Query { } schema { - query: Query + query: Query } -type Paragraphs { - text: String! - type: String! - href: String - layout: String - iframe: IFrame - metadata: MetaData - markups: [MarkUp! ]! +type Paragraphs { + text: String! + type: String! + href: String + layout: String + iframe: IFrame + metadata: MetaData + markups: [MarkUp!]! } type MarkUp { - title: String - type: String! - href: String - userId: String - start: Int! - end: Int! - anchorType: String + title: String + type: String! + href: String + userId: String + start: Int! + end: Int! + anchorType: String } - type IFrame { - mediaResource: MediaResource + mediaResource: MediaResource } -type MediaResource{ - href: String! - iframeSrc: String! - iframeWidth: Int! - iframeHeight: Int +type MediaResource { + href: String! + iframeSrc: String! + iframeWidth: Int! + iframeHeight: Int } type MetaData { - id: String! - originalWidth: Int - originalHeight: Int + id: String! + originalWidth: Int + originalHeight: Int } - -type BodyModel { paragraphs: [Paragraphs! ]! } - -type Content { bodyModel: BodyModel! } - -type User { id: String! name: String! imageId: String! } - -type Post { - id: ID! - readingTime: Float! - title: String! - createdAt: Int! - content: Content! - creator: User! +type BodyModel { + paragraphs: [Paragraphs!]! } -type Data { post: Post } +type Content { + bodyModel: BodyModel! +} -type AutogeneratedMainType { data: Data } +type User { + id: String! + name: String! + imageId: String! +} + +type Post { + id: ID! + readingTime: Float! + title: String! + createdAt: Int! + content: Content! + creator: User! + previewImage: PreviewImage + previewContent: PreviewContent + uniqueSlug: String! +} + +type PreviewImage { + id: String +} + +type PreviewContent { + subtitle: String! +} + +type Data { + post: Post +} + +type AutogeneratedMainType { + data: Data +} diff --git a/src/data.rs b/src/data.rs index 736e99b..2d49398 100644 --- a/src/data.rs +++ b/src/data.rs @@ -26,7 +26,7 @@ use sled::{Db, Tree}; use crate::proxy::StringUtils; use crate::SETTINGS; -const POST_CACHE_VERSION: usize = 1; +const POST_CACHE_VERSION: usize = 2; const GIST_CACHE_VERSION: usize = 1; #[derive(Clone)] @@ -53,6 +53,10 @@ impl PostResp { pub fn get_gist_id<'a>(&self, url: &'a str) -> &'a str { url.split('/').last().unwrap() } + + pub fn get_subtitle(&self) -> &str { + self.preview_content.as_ref().unwrap().subtitle.as_str() + } } #[derive(Deserialize, Serialize)] @@ -111,7 +115,7 @@ impl Data { for (tree, key, current_version) in trees { if let Ok(Some(v)) = tree.get(key) { let version = bincode::deserialize::(&v[..]).unwrap(); - let clean = !(version == current_version); + let clean = version != current_version; if clean { log::info!( diff --git a/src/proxy.rs b/src/proxy.rs index c252673..9e0be4d 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -17,6 +17,7 @@ use std::ops::{Bound, RangeBounds}; use actix_web::{http::header, web, HttpResponse, Responder}; +use chrono::{TimeZone, Utc}; use futures::future::join_all; use reqwest::header::CONTENT_TYPE; use sailfish::TemplateOnce; @@ -110,6 +111,9 @@ impl StringUtils for str { #[template(rm_whitespace = true)] pub struct Post { pub data: PostResp, + pub date: String, + pub preview_img: String, + pub reading_time: usize, pub id: String, pub gists: Option>, } @@ -166,7 +170,7 @@ async fn page(path: web::Path<(String, String)>, data: AppData) -> impl Responde .unwrap() .href; if src.contains("gist.github.com") { - let gist_id = post_data.get_gist_id(&src); + let gist_id = post_data.get_gist_id(src); let fut = data.get_gist(gist_id.to_owned()); futs.push(fut); } @@ -179,10 +183,27 @@ async fn page(path: web::Path<(String, String)>, data: AppData) -> impl Responde Some(x) }; + let date = Utc + .timestamp_millis(post_data.created_at) + .format("%b %e, %Y") + .to_string(); + let reading_time = post_data.reading_time.floor() as usize; + let preview_img = post_data + .preview_image + .as_ref() + .unwrap() + .id + .as_ref() + .unwrap(); + let preview_img = crate::V1_API_ROUTES.proxy.get_medium_asset(preview_img); + let page = Post { id: id.to_owned(), data: post_data, + date, gists, + reading_time, + preview_img, }; let page = page.render_once().unwrap(); diff --git a/templates/gist_insert.html b/templates/gist_insert.html index 3d5d840..858f730 100644 --- a/templates/gist_insert.html +++ b/templates/gist_insert.html @@ -1,4 +1,4 @@ -<. let gist_id = data.get_gist_id(&src); .> +<. let gist_id = data.get_gist_id(src); .> <. let (_, gist)= gists.as_ref().unwrap().iter().find(|(id, _)| id == gist_id).as_ref().unwrap(); .> <. for file in &gist.files {.> <.= file.get_html_content() .> diff --git a/templates/post.html b/templates/post.html index 4a0b31b..4802f49 100644 --- a/templates/post.html +++ b/templates/post.html @@ -1,15 +1,10 @@ - - - <.= data.title .> + <. include!("./post_meta.html"); .>
- <. use chrono::{TimeZone, Utc}; .> - <. let dt = Utc.timestamp_millis(data.created_at); .> - <. let date = dt.format("%b %e, %Y").to_string(); .>

<.= data.title .>

@@ -20,7 +15,7 @@ /> <.= data.creator.name .> - on <.= &date .> · <.= data.reading_time.floor() as usize .> min read + on <.= &date .> · <.= reading_time .> min read

<. let paragraphs = &data.content.body_model.paragraphs; .> diff --git a/templates/post_meta.html b/templates/post_meta.html new file mode 100644 index 0000000..5ea92df --- /dev/null +++ b/templates/post_meta.html @@ -0,0 +1,76 @@ +<.= data.title .> | by <.= data.creator.name .> | <.= &date .> + + + + + + + + + + + + + + + + + + + + + + + + + + + +