2020-07-16 19:32:30 +00:00
|
|
|
use crate::{post::Post, signalboost::Person};
|
2022-05-05 14:58:23 +00:00
|
|
|
use chrono::prelude::*;
|
2022-06-14 19:04:17 +00:00
|
|
|
use color_eyre::eyre::Result;
|
2022-06-20 12:47:11 +00:00
|
|
|
use std::{fs, path::PathBuf, sync::Arc};
|
2020-11-18 17:18:24 +00:00
|
|
|
use tracing::{error, instrument};
|
2020-07-16 19:32:30 +00:00
|
|
|
|
2022-06-20 12:47:11 +00:00
|
|
|
pub mod config;
|
2020-09-19 15:33:46 +00:00
|
|
|
pub mod markdown;
|
2021-01-17 02:38:22 +00:00
|
|
|
pub mod poke;
|
2020-09-19 15:33:46 +00:00
|
|
|
|
2022-06-20 12:47:11 +00:00
|
|
|
pub use config::*;
|
2020-07-16 19:32:30 +00:00
|
|
|
|
2020-10-02 22:36:57 +00:00
|
|
|
#[instrument]
|
2020-07-16 19:32:30 +00:00
|
|
|
async fn patrons() -> Result<Option<patreon::Users>> {
|
2022-05-19 00:17:48 +00:00
|
|
|
let mut p = dirs::home_dir().unwrap_or(".".into());
|
|
|
|
p.push(".patreon.json");
|
2022-04-06 01:02:14 +00:00
|
|
|
if !p.exists() {
|
|
|
|
info!("{:?} does not exist", p);
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2022-04-06 00:35:07 +00:00
|
|
|
let mut cli = patreon::Client::new()?;
|
2022-04-02 16:15:10 +00:00
|
|
|
|
2022-04-06 00:53:47 +00:00
|
|
|
if let Err(why) = cli.refresh_token().await {
|
|
|
|
error!("error getting refresh token: {}", why);
|
|
|
|
}
|
2020-07-16 19:32:30 +00:00
|
|
|
|
|
|
|
match cli.campaign().await {
|
|
|
|
Ok(camp) => {
|
|
|
|
let id = camp.data[0].id.clone();
|
|
|
|
|
|
|
|
match cli.pledges(id).await {
|
|
|
|
Ok(users) => Ok(Some(users)),
|
|
|
|
Err(why) => {
|
2020-10-02 22:36:57 +00:00
|
|
|
error!("error getting pledges: {}", why);
|
2020-07-16 19:32:30 +00:00
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(why) => {
|
2020-10-02 22:36:57 +00:00
|
|
|
error!("error getting patreon campaign: {}", why);
|
2020-07-16 19:32:30 +00:00
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 13:17:01 +00:00
|
|
|
pub const ICON: &'static str = "https://xeiaso.net/static/img/avatar.png";
|
2020-07-16 19:32:30 +00:00
|
|
|
|
|
|
|
pub struct State {
|
2022-06-14 19:04:17 +00:00
|
|
|
pub cfg: Arc<Config>,
|
2020-07-16 19:32:30 +00:00
|
|
|
pub signalboost: Vec<Person>,
|
|
|
|
pub resume: String,
|
|
|
|
pub blog: Vec<Post>,
|
|
|
|
pub gallery: Vec<Post>,
|
|
|
|
pub talks: Vec<Post>,
|
|
|
|
pub everything: Vec<Post>,
|
2022-07-04 14:44:00 +00:00
|
|
|
pub jf: xe_jsonfeed::Feed,
|
2020-07-16 19:32:30 +00:00
|
|
|
pub sitemap: Vec<u8>,
|
|
|
|
pub patrons: Option<patreon::Users>,
|
2020-12-02 21:16:58 +00:00
|
|
|
pub mi: mi::Client,
|
2020-07-16 19:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn init(cfg: PathBuf) -> Result<State> {
|
2022-06-14 19:04:17 +00:00
|
|
|
let cfg: Arc<Config> = Arc::new(serde_dhall::from_file(cfg).parse()?);
|
2020-07-16 19:32:30 +00:00
|
|
|
let sb = cfg.signalboost.clone();
|
2022-06-14 19:04:17 +00:00
|
|
|
let resume = fs::read_to_string(cfg.clone().resume_fname.clone())?;
|
|
|
|
let resume: String = markdown::render(cfg.clone(), &resume)?;
|
|
|
|
let mi = mi::Client::new(
|
|
|
|
cfg.clone().mi_token.clone(),
|
|
|
|
crate::APPLICATION_NAME.to_string(),
|
|
|
|
)?;
|
|
|
|
let blog = crate::post::load(cfg.clone(), "blog").await?;
|
|
|
|
let gallery = crate::post::load(cfg.clone(), "gallery").await?;
|
|
|
|
let talks = crate::post::load(cfg.clone(), "talks").await?;
|
2020-07-16 19:32:30 +00:00
|
|
|
let mut everything: Vec<Post> = vec![];
|
|
|
|
|
|
|
|
{
|
|
|
|
let blog = blog.clone();
|
|
|
|
let gallery = gallery.clone();
|
|
|
|
let talks = talks.clone();
|
|
|
|
everything.extend(blog.iter().cloned());
|
|
|
|
everything.extend(gallery.iter().cloned());
|
|
|
|
everything.extend(talks.iter().cloned());
|
|
|
|
};
|
|
|
|
|
|
|
|
everything.sort();
|
|
|
|
everything.reverse();
|
|
|
|
|
2022-05-05 14:58:23 +00:00
|
|
|
let today = Utc::today();
|
|
|
|
let everything: Vec<Post> = everything
|
|
|
|
.into_iter()
|
|
|
|
.filter(|p| today.num_days_from_ce() >= p.date.num_days_from_ce())
|
|
|
|
.take(5)
|
|
|
|
.collect();
|
2022-06-14 19:04:17 +00:00
|
|
|
|
2022-07-04 14:44:00 +00:00
|
|
|
let mut jfb = xe_jsonfeed::Feed::builder()
|
2021-11-06 09:48:16 +00:00
|
|
|
.title("Xe's Blog")
|
2020-07-16 19:32:30 +00:00
|
|
|
.description("My blog posts and rants about various technology things.")
|
|
|
|
.author(
|
2022-07-04 14:44:00 +00:00
|
|
|
xe_jsonfeed::Author::new()
|
2021-11-06 09:48:16 +00:00
|
|
|
.name("Xe")
|
2022-05-28 13:17:01 +00:00
|
|
|
.url("https://xeiaso.net")
|
2020-07-16 19:32:30 +00:00
|
|
|
.avatar(ICON),
|
|
|
|
)
|
2022-05-28 13:17:01 +00:00
|
|
|
.feed_url("https://xeiaso.net/blog.json")
|
2020-07-16 19:32:30 +00:00
|
|
|
.user_comment("This is a JSON feed of my blogposts. For more information read: https://jsonfeed.org/version/1")
|
2022-05-28 13:17:01 +00:00
|
|
|
.home_page_url("https://xeiaso.net")
|
2020-07-16 19:32:30 +00:00
|
|
|
.icon(ICON)
|
|
|
|
.favicon(ICON);
|
|
|
|
|
|
|
|
for post in &everything {
|
|
|
|
let post = post.clone();
|
|
|
|
jfb = jfb.item(post.clone().into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut sm: Vec<u8> = vec![];
|
|
|
|
let smw = sitemap::writer::SiteMapWriter::new(&mut sm);
|
|
|
|
let mut urlwriter = smw.start_urlset()?;
|
|
|
|
for url in &[
|
2022-05-28 13:17:01 +00:00
|
|
|
"https://xeiaso.net/resume",
|
|
|
|
"https://xeiaso.net/contact",
|
|
|
|
"https://xeiaso.net/",
|
|
|
|
"https://xeiaso.net/blog",
|
|
|
|
"https://xeiaso.net/signalboost",
|
2020-07-16 19:32:30 +00:00
|
|
|
] {
|
|
|
|
urlwriter.url(*url)?;
|
|
|
|
}
|
|
|
|
|
2022-05-28 13:50:27 +00:00
|
|
|
for post in &blog {
|
|
|
|
urlwriter.url(format!("https://xeiaso.net/{}", post.link))?;
|
|
|
|
}
|
|
|
|
for post in &gallery {
|
|
|
|
urlwriter.url(format!("https://xeiaso.net/{}", post.link))?;
|
|
|
|
}
|
|
|
|
for post in &talks {
|
2022-05-28 13:17:01 +00:00
|
|
|
urlwriter.url(format!("https://xeiaso.net/{}", post.link))?;
|
2020-07-16 19:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
urlwriter.end()?;
|
|
|
|
|
|
|
|
Ok(State {
|
2021-04-17 15:53:42 +00:00
|
|
|
mi,
|
|
|
|
cfg,
|
2020-07-16 19:32:30 +00:00
|
|
|
signalboost: sb,
|
2021-04-17 15:53:42 +00:00
|
|
|
resume,
|
|
|
|
blog,
|
|
|
|
gallery,
|
|
|
|
talks,
|
|
|
|
everything,
|
2020-07-16 19:32:30 +00:00
|
|
|
jf: jfb.build(),
|
|
|
|
sitemap: sm,
|
|
|
|
patrons: patrons().await?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-09-19 15:33:46 +00:00
|
|
|
use color_eyre::eyre::Result;
|
2020-07-16 19:32:30 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn init() -> Result<()> {
|
|
|
|
super::init("./config.dhall".into()).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|