aboutsummaryrefslogtreecommitdiff
path: root/src/app/mod.rs
blob: 96481463fd6b868d912cac828c076038e2f5ed7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use crate::{post::Post, signalboost::Person};
use chrono::prelude::*;
use color_eyre::eyre::Result;
use std::{path::PathBuf, sync::Arc};
use tracing::{error, instrument};

pub mod config;
pub mod poke;

pub use config::*;

#[instrument]
async fn patrons() -> Result<Option<patreon::Users>> {
    let mut p = dirs::home_dir().unwrap_or(".".into());
    p.push(".patreon.json");
    if !p.exists() {
        info!("{:?} does not exist", p);
        return Ok(None);
    }

    let mut cli = patreon::Client::new()?;

    if let Err(why) = cli.refresh_token().await {
        error!("error getting refresh token: {}", why);
    }

    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) => {
                    error!("error getting pledges: {}", why);
                    Ok(None)
                }
            }
        }
        Err(why) => {
            error!("error getting patreon campaign: {}", why);
            Ok(None)
        }
    }
}

pub const ICON: &str = "https://xeiaso.net/static/img/avatar.png";

pub struct State {
    pub cfg: Arc<Config>,
    pub signalboost: Vec<Person>,
    pub blog: Vec<Post>,
    pub gallery: Vec<Post>,
    pub talks: Vec<Post>,
    pub everything: Vec<Post>,
    pub jf: xe_jsonfeed::Feed,
    pub sitemap: Vec<u8>,
    pub patrons: Option<patreon::Users>,
    pub mi: mi::Client,
}

pub async fn init(cfg: PathBuf) -> Result<State> {
    let cfg: Arc<Config> = Arc::new(serde_dhall::from_file(cfg).parse()?);
    let sb = cfg.signalboost.clone();
    let mi = mi::Client::new(
        cfg.clone().mi_token.clone(),
        crate::APPLICATION_NAME.to_string(),
    )?;
    let blog = crate::post::load("blog").await?;
    let gallery = crate::post::load("gallery").await?;
    let talks = crate::post::load("talks").await?;
    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();

    let today = Utc::now().date_naive();
    let everything: Vec<Post> = everything
        .into_iter()
        .filter(|p| today.num_days_from_ce() >= p.date.num_days_from_ce())
        .take(5)
        .collect();

    let mut jfb = xe_jsonfeed::Feed::builder()
        .title("Xe's Blog")
        .description("My blog posts and rants about various technology things.")
        .author(
            xe_jsonfeed::Author::new()
                .name("Xe")
                .url("https://xeiaso.net")
                .avatar(ICON),
        )
        .feed_url("https://xeiaso.net/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://xeiaso.net")
        .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://xeiaso.net/resume",
        "https://xeiaso.net/contact",
        "https://xeiaso.net/",
        "https://xeiaso.net/blog",
        "https://xeiaso.net/signalboost",
    ] {
        urlwriter.url(*url)?;
    }

    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 {
        urlwriter.url(format!("https://xeiaso.net/{}", post.link))?;
    }

    urlwriter.end()?;

    Ok(State {
        mi,
        cfg,
        signalboost: sb,
        blog,
        gallery,
        talks,
        everything,
        jf: jfb.build(),
        sitemap: sm,
        patrons: patrons().await?,
    })
}

#[cfg(test)]
mod tests {
    use color_eyre::eyre::Result;
    #[tokio::test]
    async fn init() -> Result<()> {
        super::init("./config.dhall".into()).await?;
        Ok(())
    }
}