about summary refs log tree commit diff
path: root/src/frontend/rss_conversion.rs
blob: 3448bf031767696ddd971495bb46bfd323ae4a4d (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
//! Conversion of MF2-JSON posts into RSS items.

/// TTL of the RSS feeds generated by Kittybox. Compliant RSS
/// consumers must avoid refetching the feed before its TTL expires.
const RSS_TTL: std::time::Duration = std::time::Duration::from_secs(600);

/// Convert a single MF2 post to an RSS item.
///
/// Posts must have a UID.
pub fn post_to_rss(mut post: serde_json::Value) -> Option<rss::Item> {
    let mut builder = rss::ItemBuilder::default();

    let date = match post["properties"]["published"][0].take() {
        serde_json::Value::String(d) => chrono::DateTime::<chrono::FixedOffset>::parse_from_rfc3339(&d).ok(),
        _ => None
    };
    if let Some(date) = date {
        builder.pub_date(date.to_rfc2822());
    }
    if let serde_json::Value::String(title) = post["properties"]["title"][0].take() {
        builder.title(title);
    }
    if let serde_json::Value::String(summary) = post["properties"]["summary"][0].take() {
        builder.description(summary);
    }
    // Do not emit posts that do not have GUIDs.
    match post["properties"]["uid"][0].take() {
        serde_json::Value::String(uid) => {
            builder.guid(rss::GuidBuilder::default().value(uid).permalink(true).build());
        },
        _ => { return None; },
    }
    // TODO: enclosures for u-photo, u-video, u-audio
    // requires knowing MIME type for the included media file
    // can enrich from media endpoint

    let item = builder.build();
    if item.title.is_some() || item.description.is_some() {
        Some(item)
    } else {
        None
    }
}

/// Convert an entire MF2 h-feed into an RSS channel.
/// Fairly opinionated. Provided MF2-JSON object must be a feed.
pub fn feed_to_rss(mut post: serde_json::Value) -> rss::Channel {
    let children: Vec<serde_json::Value> = match post["children"].take() {
        serde_json::Value::Array(children) => children,
        _ => Vec::default(),
    };
    rss::ChannelBuilder::default()
        .title(post["properties"]["name"][0].as_str().unwrap())
        .link(post["properties"]["uid"][0].as_str().unwrap())
        .generator(Some(concat!(
            env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")
        ).to_string()))
        .ttl((RSS_TTL.as_secs() / 60).to_string())
        .items(children
            .into_iter()
            .filter_map(post_to_rss)
            .collect::<Vec<rss::Item>>()
        )
        .build()
}