2020-07-16 19:32:30 +00:00
|
|
|
use crate::{post::Post, signalboost::Person};
|
2020-09-19 15:33:46 +00:00
|
|
|
use color_eyre::eyre::Result;
|
2020-07-16 19:32:30 +00:00
|
|
|
use serde::Deserialize;
|
|
|
|
use std::{fs, path::PathBuf};
|
2020-11-18 17:18:24 +00:00
|
|
|
use tracing::{error, instrument};
|
2020-07-16 19:32:30 +00:00
|
|
|
|
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
|
|
|
|
2020-07-16 19:32:30 +00:00
|
|
|
#[derive(Clone, Deserialize)]
|
|
|
|
pub struct Config {
|
2020-11-18 17:18:24 +00:00
|
|
|
pub(crate) signalboost: Vec<Person>,
|
2020-07-16 19:32:30 +00:00
|
|
|
#[serde(rename = "resumeFname")]
|
2020-11-18 17:18:24 +00:00
|
|
|
pub(crate) resume_fname: PathBuf,
|
2020-12-02 21:16:58 +00:00
|
|
|
#[serde(rename = "miToken")]
|
|
|
|
pub(crate) mi_token: String,
|
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>> {
|
|
|
|
use patreon::*;
|
2020-11-18 17:18:24 +00:00
|
|
|
let creds: Credentials = envy::prefixed("PATREON_")
|
|
|
|
.from_env()
|
|
|
|
.unwrap_or(Credentials::default());
|
2022-04-06 00:01:52 +00:00
|
|
|
|
|
|
|
if creds == Credentials::default() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2022-04-02 16:15:10 +00:00
|
|
|
let mut cli = Client::new(creds)?;
|
|
|
|
|
|
|
|
cli.refresh_token().await?;
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const ICON: &'static str = "https://christine.website/static/img/avatar.png";
|
|
|
|
|
|
|
|
pub struct State {
|
|
|
|
pub cfg: Config,
|
|
|
|
pub signalboost: Vec<Person>,
|
|
|
|
pub resume: String,
|
|
|
|
pub blog: Vec<Post>,
|
|
|
|
pub gallery: Vec<Post>,
|
|
|
|
pub talks: Vec<Post>,
|
|
|
|
pub everything: Vec<Post>,
|
|
|
|
pub jf: jsonfeed::Feed,
|
|
|
|
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> {
|
|
|
|
let cfg: Config = serde_dhall::from_file(cfg).parse()?;
|
|
|
|
let sb = cfg.signalboost.clone();
|
|
|
|
let resume = fs::read_to_string(cfg.resume_fname.clone())?;
|
2020-09-19 15:33:46 +00:00
|
|
|
let resume: String = markdown::render(&resume)?;
|
2020-12-02 21:16:58 +00:00
|
|
|
let mi = mi::Client::new(cfg.mi_token.clone(), crate::APPLICATION_NAME.to_string())?;
|
2021-02-15 20:09:25 +00:00
|
|
|
let blog = crate::post::load("blog").await?;
|
|
|
|
let gallery = crate::post::load("gallery").await?;
|
|
|
|
let talks = crate::post::load("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();
|
|
|
|
|
2021-09-06 16:04:17 +00:00
|
|
|
let everything: Vec<Post> = everything.into_iter().take(5).collect();
|
2021-01-15 03:36:34 +00:00
|
|
|
|
2020-07-16 19:32:30 +00:00
|
|
|
let mut jfb = 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(
|
|
|
|
jsonfeed::Author::new()
|
2021-11-06 09:48:16 +00:00
|
|
|
.name("Xe")
|
2020-07-16 19:32:30 +00:00
|
|
|
.url("https://christine.website")
|
|
|
|
.avatar(ICON),
|
|
|
|
)
|
|
|
|
.feed_url("https://christine.website/blog.json")
|
|
|
|
.user_comment("This is a JSON feed of my blogposts. For more information read: https://jsonfeed.org/version/1")
|
|
|
|
.home_page_url("https://christine.website")
|
|
|
|
.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 &[
|
|
|
|
"https://christine.website/resume",
|
|
|
|
"https://christine.website/contact",
|
|
|
|
"https://christine.website/",
|
|
|
|
"https://christine.website/blog",
|
|
|
|
"https://christine.website/signalboost",
|
|
|
|
] {
|
|
|
|
urlwriter.url(*url)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
for post in &everything {
|
2021-04-18 14:39:53 +00:00
|
|
|
urlwriter.url(format!("https://christine.website/{}", 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(())
|
|
|
|
}
|
|
|
|
}
|