aboutsummaryrefslogtreecommitdiffstats
path: root/src/atom.rs
blob: a64a6aa1cceabff39180ee3c782d6b68ce20bfbb (plain) (blame)
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
use atom_syndication::{
    Category, Content as AtomContent, Entry as AtomEntry, Feed, Generator, Link, Person, Text,
    TextType,
};
use chrono::{DateTime, Utc};

use crate::posts::Post;

pub struct Context {
    /// the page for which the feed is being generated for
    pub page_title: String,
    /// the human-readable page the feed is for
    pub page_url: String,
    /// the url to the atom xml document
    pub self_url: String,
    /// current site language
    pub lang: String,
}

pub async fn atom<P: Post + Clone>(ctx: Context, entries: Vec<P>) -> Feed {
    let mut authors = Vec::new();
    let me = Person {
        name: "cel".to_owned(),
        email: Some("cel@blos.sm".to_owned()),
        uri: Some("/contact".to_owned()),
    };
    authors.push(me.clone());
    let page_link = Link {
        href: ctx.page_url.clone(),
        rel: "alternate".into(),
        hreflang: Some(ctx.lang.clone()),
        mime_type: Some("text/html".into()),
        title: Some(ctx.page_title.clone()),
        length: None,
    };
    let home_link = Link {
        // TODO?: localisation
        href: "/".into(),
        rel: "via".into(),
        hreflang: Some(ctx.lang.clone()),
        mime_type: Some("text/html".into()),
        // TODO: localisation
        title: Some("cel's garden".into()),
        length: None,
    };
    let self_link = Link {
        href: ctx.self_url.into(),
        rel: "self".into(),
        hreflang: Some(ctx.lang.clone()),
        mime_type: Some("application/atom+xml".into()),
        // TODO: localisation
        title: Some("atom feed".into()),
        length: None,
    };
    let mut links = Vec::new();
    links.push(page_link);
    links.push(home_link);
    links.push(self_link);
    let mut feed = Feed {
        title: Text {
            value: ctx.page_title,
            base: None,
            lang: Some(ctx.lang.clone()),
            r#type: TextType::Text,
        },
        id: ctx.page_url,
        updated: entries
            .clone()
            .into_iter()
            .fold(DateTime::<Utc>::MIN_UTC, |acc, entry| {
                if let Some(updated_at) = entry.updated_at() {
                    if *updated_at > acc {
                        *updated_at
                    } else {
                        acc
                    }
                } else {
                    let published_at = entry.published_at();
                    if *published_at > acc {
                        *published_at
                    } else {
                        acc
                    }
                }
            })
            .into(),
        authors: authors.clone(),
        categories: Vec::new(),
        contributors: authors.clone(),
        generator: Some(Generator {
            value: "blossom".into(),
            uri: Some("https://bunny.garden/cel/blossom".into()),
            version: None,
        }),
        icon: Some("/favicon.ico".into()),
        links: links.clone(),
        logo: Some("/logo.png".into()),
        entries: Vec::new(),
        // TODO: determine base url from lang
        base: Some("https://en.blos.sm/".into()),
        lang: Some(ctx.lang),
        ..Default::default()
    };
    for entry in entries {
        // TODO: localisation: url from lang + filtering entries translated or not
        let categories = entry
            .tags()
            .into_iter()
            .map(|category| Category {
                term: category.clone(),
                scheme: None,
                label: Some(category.clone()),
            })
            .collect();
        let published = Some(entry.published_at().to_owned().into());
        let title = if let Some(title) = entry.subject() {
            title.to_owned()
        } else {
            entry.content()[..30].to_owned() + "..."
        };
        let entry = AtomEntry {
            title: Text {
                value: title,
                base: None,
                lang: Some(entry.lang().to_owned()),
                r#type: TextType::Text,
            },
            id: entry.link(),
            updated: (*entry.updated_at().unwrap_or_else(|| entry.published_at())).into(),
            authors: authors.clone(),
            categories,
            contributors: authors.clone(),
            links: links.clone(),
            published,
            rights: None,
            source: None,
            summary: None,
            content: Some(AtomContent {
                base: None,
                lang: Some(entry.lang().to_owned()),
                value: Some(entry.content().to_owned()),
                src: Some(entry.link()),
                content_type: Some("html".to_string()),
            }),
            ..Default::default()
        };
        feed.entries.push(entry);
    }
    feed
}