90 lines
2.7 KiB
Rust
90 lines
2.7 KiB
Rust
use crate::routes::posts;
|
|
use std::collections::HashMap;
|
|
use xml::escape::escape_str_pcdata;
|
|
|
|
pub struct RssEntry {
|
|
pub title: String,
|
|
pub link: String,
|
|
pub description: Option<String>,
|
|
pub created_at: String,
|
|
pub author: String,
|
|
pub guid: String,
|
|
}
|
|
|
|
impl From<posts::Post> for RssEntry {
|
|
fn from(post: posts::Post) -> Self {
|
|
let web_url =
|
|
std::env::var("BASE_URI_WEB").expect("Environment variable BASE_URI_WEB not found");
|
|
let post_url = format!("{}{}{}", web_url, "/posts/", post.post_id.to_string());
|
|
let author_full_name = format!("{} {}", post.first_name.unwrap(), post.last_name.unwrap());
|
|
|
|
Self {
|
|
title: post.title.clone(),
|
|
link: post_url.clone(),
|
|
description: Some(post.body.clone()),
|
|
created_at: post
|
|
.created_at
|
|
.expect("No timestamp was available")
|
|
.to_rfc2822(),
|
|
author: author_full_name,
|
|
guid: post_url.clone(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl RssEntry {
|
|
pub fn to_item(&self) -> String {
|
|
format!(
|
|
r#"
|
|
<item>
|
|
<title><![CDATA[{}]]></title>
|
|
<description><![CDATA[{}]]></description>
|
|
<pubDate>{}</pubDate>
|
|
<link>{}</link>
|
|
<guid isPermaLink="true">{}</guid>
|
|
</item>
|
|
"#,
|
|
self.title,
|
|
self.description.clone().unwrap_or_default(),
|
|
self.created_at,
|
|
self.guid,
|
|
self.guid
|
|
)
|
|
}
|
|
}
|
|
|
|
pub fn generate_rss(
|
|
title: &str,
|
|
description: &str,
|
|
link: &str,
|
|
posts: &HashMap<String, posts::Post>,
|
|
) -> String {
|
|
let values = posts.clone().into_values();
|
|
let rss_entries = values
|
|
.map(|p| p.into())
|
|
.map(|r: RssEntry| r.to_item())
|
|
.collect::<String>();
|
|
|
|
let safe_title = escape_str_pcdata(title);
|
|
let safe_description = escape_str_pcdata(description);
|
|
|
|
// TODO: change the atom link in this string - it's not correct
|
|
// change it when we know the URL
|
|
format!(
|
|
r#"<?xml version="1.0" encoding="UTF-8"?>
|
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
|
<channel>
|
|
<title>{safe_title}</title>
|
|
<description>{safe_description}</description>
|
|
<link>{link}</link>
|
|
<language>en-us</language>
|
|
<ttl>60</ttl>
|
|
<generator>Kyouma 1.0.0-SE</generator>
|
|
<atom:link href="https://wyattjmiller.com/posts.xml" rel="self" type="application/rss+xml" />
|
|
{}
|
|
</channel>
|
|
</rss>"#,
|
|
rss_entries
|
|
)
|
|
}
|