Skip to content

Commit

Permalink
Postlist rendering almost done, only the ts part
Browse files Browse the repository at this point in the history
  • Loading branch information
strawmelonjuice committed Aug 11, 2024
1 parent 6c5f910 commit e100437
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 54 deletions.
4 changes: 4 additions & 0 deletions source/Main/externalpluginservers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ pub(crate) enum EPSRequestBody {
template_path: String,
template_data: crate::renders::PageLikePublicationTemplateData,
},
PostlistRenderRequest {
template_path: String,
template_data: crate::renders::PostListPublicationTemplateData,
},
WebRequest {
page_id: String,
headers: Vec<(String, String)>, // Name, Value
Expand Down
117 changes: 116 additions & 1 deletion source/Main/publications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,116 @@ use serde::{Deserialize, Serialize};
use crate::config::{CynthiaConfClone, CynthiaConfig};

pub(crate) type CynthiaPublicationList = Vec<CynthiaPublication>;
pub(crate) trait PostLists {
fn filter(&self, filter: PostListFilter) -> Vec<PostPublication>;
#[allow(dead_code)]
fn get_by_id(&self, id: String) -> Option<CynthiaPublication>;
}
impl PostLists for CynthiaPostList {
fn filter(&self, filter: PostListFilter) -> Vec<PostPublication> {
match filter {
PostListFilter::Latest => {
let mut p = self.clone();
p.sort_by(|a, b| b.dates.published.cmp(&a.dates.published));
p
}
PostListFilter::Oldest => {
let mut p = self.clone();
p.sort_by(|a, b| a.dates.published.cmp(&b.dates.published));
p
}
PostListFilter::Tag(tag) => self
.iter()
.filter(|x| x.tags.contains(&tag))
.cloned()
.collect(),
PostListFilter::Category(category) => self
.iter()
.filter(|x| x.category == Some(category.clone()))
.cloned()
.collect(),
PostListFilter::Author(author) => self
.iter()
.filter(|x| {
x.author
.as_ref()
.map_or(false, |a| a.name == Some(author.clone()))
})
.cloned()
.collect(),
PostListFilter::Search(search) => self
.iter()
.filter(|x| {
x.title.contains(&search)
|| x.short.as_ref().map_or(false, |s| s.contains(&search))
// || x.postcontent.get_inner().contains(&search)
})
.cloned()
.collect(),
}
}
fn get_by_id(&self, id: String) -> Option<CynthiaPublication> {
let mut a: Option<CynthiaPublication> = None;
for i in self {
if i.id == id {
a = Some(CynthiaPublication::Post {
id: i.id.to_string(),
title: i.title.to_string(),
short: i.short.clone(),
dates: i.dates.clone(),
thumbnail: i.thumbnail.clone(),
category: i.category.clone(),
tags: i.tags.clone(),
author: i.author.clone(),
postcontent: i.postcontent.clone(),
scene_override: i.scene_override.clone(),
})
}
}
a
}
}
pub(crate) type CynthiaPostList = Vec<PostPublication>;
pub(crate) trait CynthiaPublicationListTrait {
fn only_posts(&self) -> CynthiaPostList;
fn get_notfound(&self, config: CynthiaConfClone) -> Option<CynthiaPublication>;
fn get_root(&self) -> Option<CynthiaPublication>;
fn get_by_id(&self, id: String) -> Option<CynthiaPublication>;
fn validate(&self, config: CynthiaConfClone) -> bool;
fn load() -> CynthiaPublicationList;
}
impl CynthiaPublicationListTrait for CynthiaPublicationList {
fn only_posts(&self) -> CynthiaPostList {
let mut p = Vec::new();
for i in self {
if let CynthiaPublication::Post {
id,
title,
short,
dates,
thumbnail,
category,
tags,
author,
postcontent,
scene_override,
} = i {
p.push(PostPublication {
id: id.to_string(),
title: title.to_string(),
short: short.clone(),
dates: dates.clone(),
thumbnail: thumbnail.clone(),
category: category.clone(),
tags: tags.clone(),
author: author.clone(),
postcontent: postcontent.clone(),
scene_override: scene_override.clone(),
});
}
}
p
}
fn get_notfound(&self, config: CynthiaConfClone) -> Option<CynthiaPublication> {
self.iter()
.find(|x| {
Expand Down Expand Up @@ -109,7 +211,7 @@ impl CynthiaPublicationListTrait for CynthiaPublicationList {
valid.push(itemsin);

// Return true if all checks passed
return valid.iter().all(|x| *x);
valid.iter().all(|x| *x)
}
fn load() -> CynthiaPublicationList {
if Path::new("./cynthiaFiles/published.yaml").exists() {
Expand Down Expand Up @@ -145,6 +247,19 @@ impl CynthiaPublicationListTrait for CynthiaPublicationList {
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) struct PostPublication {
id: String,
title: String,
short: Option<String>,
dates: CynthiaPublicationDates,
thumbnail: Option<String>,
category: Option<String>,
tags: Vec<String>,
author: Option<Author>,
postcontent: PublicationContent,
scene_override: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) enum CynthiaPublication {
Expand Down
159 changes: 106 additions & 53 deletions source/Main/renders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use std::sync::Arc;
use tokio::sync::{Mutex, MutexGuard};

use crate::config::CynthiaConfClone;
use crate::publications::{
Author, CynthiaPublicationDates, CynthiaPublicationList, CynthiaPublicationListTrait,
};
use crate::publications::{CynthiaPostList, CynthiaPublicationList, CynthiaPublicationListTrait};
use crate::{LockCallback, ServerContext};
use colored::Colorize;

Expand Down Expand Up @@ -130,20 +128,26 @@ pub(crate) struct PageLikePublicationTemplateData {
content: String,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) struct PostListPublicationTemplateData {
meta: PageLikePublicationTemplateDataMeta,
posts: CynthiaPostList,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
struct PageLikePublicationTemplateDataMeta {
id: String,
title: String,
desc: Option<String>,
category: Option<String>,
tags: Vec<String>,
author: Option<Author>,
dates: CynthiaPublicationDates,
author: Option<crate::publications::Author>,
dates: crate::publications::CynthiaPublicationDates,
thumbnail: Option<String>,
}

mod in_renderer {
use super::*;
use crate::externalpluginservers::EPSRequestBody;
use crate::publications::{CynthiaPostList, CynthiaPublicationListTrait, PostLists};
use crate::{
config::{CynthiaConfig, Scene, SceneCollectionTrait},
publications::{ContentType, CynthiaPublication, PublicationContent},
Expand Down Expand Up @@ -189,7 +193,11 @@ mod in_renderer {
},
};

let pageish_template_data: PageLikePublicationTemplateData = match publication {
let mut pageish_template_data: PageLikePublicationTemplateData =
PageLikePublicationTemplateData::default();
let mut postlist_template_data: PostListPublicationTemplateData =
PostListPublicationTemplateData::default();
match publication {
CynthiaPublication::Page {
pagecontent,
id,
Expand All @@ -198,22 +206,24 @@ mod in_renderer {
description,
dates,
..
} => PageLikePublicationTemplateData {
meta: PageLikePublicationTemplateDataMeta {
id: id.clone(),
title: title.clone(),
desc: description.clone(),
category: None,
author: None,
tags: vec![],
dates: dates.clone(),
thumbnail: thumbnail.clone(),
},
content: match fetch_page_ish_content(pagecontent).await.unwrap_html() {
RenderrerResponse::Ok(s) => s,
_ => return RenderrerResponse::Error,
},
},
} => {
pageish_template_data = PageLikePublicationTemplateData {
meta: PageLikePublicationTemplateDataMeta {
id: id.clone(),
title: title.clone(),
desc: description.clone(),
category: None,
author: None,
tags: vec![],
dates: dates.clone(),
thumbnail: thumbnail.clone(),
},
content: match fetch_page_ish_content(pagecontent).await.unwrap_html() {
RenderrerResponse::Ok(s) => s,
_ => return RenderrerResponse::Error,
},
}
}
CynthiaPublication::Post {
id,
title,
Expand All @@ -225,23 +235,52 @@ mod in_renderer {
postcontent,
tags,
..
} => PageLikePublicationTemplateData {
meta: PageLikePublicationTemplateDataMeta {
id: id.clone(),
title: title.clone(),
desc: short.clone(),
category: category.clone(),
author: author.clone(),
dates: dates.clone(),
thumbnail: thumbnail.clone(),
tags: tags.clone(),
},
content: match fetch_page_ish_content(postcontent).await.unwrap_html() {
RenderrerResponse::Ok(s) => s,
_ => return RenderrerResponse::Error,
},
},
_ => todo!("Implement fetching content for postlists."),
} => {
pageish_template_data = PageLikePublicationTemplateData {
meta: PageLikePublicationTemplateDataMeta {
id: id.clone(),
title: title.clone(),
desc: short.clone(),
category: category.clone(),
author: author.clone(),
dates: dates.clone(),
thumbnail: thumbnail.clone(),
tags: tags.clone(),
},
content: match fetch_page_ish_content(postcontent).await.unwrap_html() {
RenderrerResponse::Ok(s) => s,
_ => return RenderrerResponse::Error,
},
}
}
CynthiaPublication::PostList {
id,
title,
short,
filter,
..
} => {
let publicationlist: CynthiaPublicationList = CynthiaPublicationList::load();
let postlist: CynthiaPostList = publicationlist.only_posts();
let filtered_postlist = postlist.filter(filter);
postlist_template_data = PostListPublicationTemplateData {
meta: PageLikePublicationTemplateDataMeta {
id: id.clone(),
title: title.clone(),
desc: short.clone(),
category: None,
tags: vec![],
author: None,
dates: crate::publications::CynthiaPublicationDates {
altered: 0,
published: 0,
},
thumbnail: None,
},
posts: filtered_postlist,
};
// println!("{}", serde_json::to_string(&postlist_template_data).unwrap());
}
};

let outerhtml: String = {
Expand All @@ -257,8 +296,9 @@ mod in_renderer {
error!("Template file '{}' not found.", template_path.display());
return RenderrerResponse::Error;
}

// A fallback function that uses the builtin handlebars renderer.
let builtin_handlebars = || {
let builtin_handlebars = |data| {
let mut template = Handlebars::new();
// streq helper
// This helper checks if two strings are equal.
Expand All @@ -276,7 +316,7 @@ mod in_renderer {
return RenderrerResponse::Error;
}
};
match template.render("base", &pageish_template_data.meta) {
match template.render("base", &data) {
Ok(a) => RenderrerResponse::Ok(a),
Err(e) => {
error!(
Expand All @@ -290,26 +330,39 @@ mod in_renderer {
};
let mut htmlbody: String = if !cfg!(feature = "js_runtime") {
// Fall back to builtin handlebars if the js_runtime feature is not enabled.
if let RenderrerResponse::Ok(a) = builtin_handlebars() {
if let RenderrerResponse::Ok(a) = builtin_handlebars(pageish_template_data.clone())
{
a
} else {
return RenderrerResponse::Error;
}
} else if let crate::externalpluginservers::EPSResponseBody::OkString { value } =
crate::externalpluginservers::contact_eps(
server_context_mutex.clone(),
EPSRequestBody::ContentRenderRequest {
template_path: template_path.to_string_lossy().parse().unwrap(),
template_data: pageish_template_data.clone(),
},
)
.await
{
} else if let crate::externalpluginservers::EPSResponseBody::OkString { value } = {
if localscene.kind != *"postlist" {
crate::externalpluginservers::contact_eps(
server_context_mutex.clone(),
EPSRequestBody::ContentRenderRequest {
template_path: template_path.to_string_lossy().parse().unwrap(),
template_data: pageish_template_data.clone(),
},
)
.await
} else {
crate::externalpluginservers::contact_eps(
server_context_mutex.clone(),
EPSRequestBody::PostlistRenderRequest {
template_path: template_path.to_string_lossy().parse().unwrap(),
template_data: postlist_template_data.clone(),
},
)
.await
}
} {
value
} else {
warn!("External Javascript Runtime failed to render the content. Retrying with basic builtin rendering.");
// Fall back to builtin handlebars if the external plugin server fails.
if let RenderrerResponse::Ok(a) = builtin_handlebars() {
if let RenderrerResponse::Ok(a) = builtin_handlebars(pageish_template_data.clone())
{
a
} else {
return RenderrerResponse::Error;
Expand Down

0 comments on commit e100437

Please sign in to comment.