@ -1,6 +1,9 @@
#![ warn(clippy::too_many_arguments) ]
use crate ::template_utils ::Ructe ;
use atom_syndication ::{ ContentBuilder , Entry , EntryBuilder , LinkBuilder , Person , PersonBuilder } ;
use atom_syndication ::{
ContentBuilder , Entry , EntryBuilder , Feed , FeedBuilder , LinkBuilder , Person , PersonBuilder ,
} ;
use chrono ::naive ::NaiveDateTime ;
use plume_models ::{ posts ::Post , Connection , CONFIG , ITEMS_PER_PAGE } ;
use rocket ::{
http ::{
@ -115,13 +118,46 @@ pub struct RemoteForm {
pub remote : String ,
}
pub fn post_to_atom ( post : Post , conn : & Connection ) -> Entry {
pub fn build_atom_feed (
entries : Vec < Post > ,
uri : & str ,
title : & str ,
default_updated : & NaiveDateTime ,
conn : & Connection ,
) -> Feed {
let updated = if entries . is_empty ( ) {
default_updated
} else {
& entries [ 0 ] . creation_date
} ;
FeedBuilder ::default ( )
. title ( title )
. id ( uri )
. updated ( updated . format ( "%Y-%m-%d %H:%M:%SZ" ) . to_string ( ) )
. entries (
entries
. into_iter ( )
. map ( | p | post_to_atom ( p , conn ) )
. collect ::< Vec < Entry > > ( ) ,
)
. links ( vec! [ LinkBuilder ::default ( )
. href ( uri )
. rel ( "self" )
. mime_type ( "application/atom+xml" . to_string ( ) )
. build ( )
. expect ( "Atom feed: link error" ) ] )
. build ( )
. expect ( "user::atom_feed: Error building Atom feed" )
}
fn post_to_atom ( post : Post , conn : & Connection ) -> Entry {
let formatted_creation_date = post . creation_date . format ( "%Y-%m-%dT%H:%M:%SZ" ) . to_string ( ) ;
EntryBuilder ::default ( )
. title ( format! ( "<![CDATA[{}]]>" , post . title ) )
. content (
ContentBuilder ::default ( )
. value ( format! ( "<![CDATA[{}]]>" , * post . content . get ( ) ) )
. src ( post . ap_url . clone ( ) )
. content_type ( "html" . to_string ( ) )
. build ( )
. expect ( "Atom feed: content error" ) ,
@ -141,8 +177,9 @@ pub fn post_to_atom(post: Post, conn: &Connection) -> Entry {
)
// Using RFC 4287 format, see https://tools.ietf.org/html/rfc4287#section-3.3 for dates
// eg: 2003-12-13T18:30:02Z (Z is here because there is no timezone support with the NaiveDateTime crate)
. published ( post . creation_date . format ( "%Y-%m-%dT%H:%M:%SZ" ) . to_string ( ) )
. id ( post . id . to_string ( ) )
. published ( formatted_creation_date . clone ( ) )
. updated ( formatted_creation_date )
. id ( post . ap_url . clone ( ) )
. links ( vec! [ LinkBuilder ::default ( )
. href ( post . ap_url )
. build ( )