Plume/src/routes/mod.rs

83 lines
2.2 KiB
Rust
Raw Normal View History

2018-09-01 20:08:26 +00:00
use atom_syndication::{ContentBuilder, Entry, EntryBuilder, LinkBuilder, Person, PersonBuilder};
use rocket::{
http::RawStr,
request::FromFormValue,
response::NamedFile,
};
use std::path::{Path, PathBuf};
2018-05-10 18:01:16 +00:00
2018-09-27 21:06:40 +00:00
use plume_models::{Connection, posts::Post};
2018-09-01 20:08:26 +00:00
const ITEMS_PER_PAGE: i32 = 12;
#[derive(Copy, Clone)]
pub struct Page(i32);
impl<'v> FromFormValue<'v> for Page {
type Error = &'v RawStr;
fn from_form_value(form_value: &'v RawStr) -> Result<Page, &'v RawStr> {
match form_value.parse::<i32>() {
Ok(page) => Ok(Page(page)),
_ => Err(form_value),
}
}
}
impl Page {
pub fn first() -> Page {
Page(1)
}
2018-07-25 12:29:34 +00:00
/// Computes the total number of pages needed to display n_items
pub fn total(n_items: i32) -> i32 {
if n_items % ITEMS_PER_PAGE == 0 {
n_items / ITEMS_PER_PAGE
} else {
(n_items / ITEMS_PER_PAGE) + 1
}
}
pub fn limits(&self) -> (i32, i32) {
((self.0 - 1) * ITEMS_PER_PAGE, self.0 * ITEMS_PER_PAGE)
}
}
2018-09-27 21:06:40 +00:00
pub fn post_to_atom(post: Post, conn: &Connection) -> Entry {
2018-09-01 20:08:26 +00:00
EntryBuilder::default()
2018-11-06 09:49:46 +00:00
.title(format!("<![CDATA[{}]]>", post.title))
2018-09-01 20:08:26 +00:00
.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"))
.authors(post.get_authors(&*conn)
.into_iter()
.map(|a| PersonBuilder::default()
.name(a.display_name)
.uri(a.ap_url)
.build().expect("Atom feed: author error"))
.collect::<Vec<Person>>())
.links(vec![LinkBuilder::default().href(post.ap_url).build().expect("Atom feed: link error")])
.build().expect("Atom feed: entry error")
}
2018-04-23 10:54:37 +00:00
pub mod blogs;
2018-05-10 09:44:57 +00:00
pub mod comments;
2018-06-18 15:59:49 +00:00
pub mod errors;
pub mod instance;
2018-05-10 16:38:03 +00:00
pub mod likes;
2018-09-02 20:55:42 +00:00
pub mod medias;
2018-05-13 13:35:55 +00:00
pub mod notifications;
2018-04-23 14:25:39 +00:00
pub mod posts;
2018-05-19 09:51:10 +00:00
pub mod reshares;
2018-04-24 09:21:39 +00:00
pub mod session;
2018-09-06 12:06:04 +00:00
pub mod tags;
2018-04-22 18:13:12 +00:00
pub mod user;
pub mod search;
2018-04-24 08:35:45 +00:00
pub mod well_known;
2018-05-10 18:01:16 +00:00
2018-09-02 20:55:42 +00:00
#[get("/static/<file..>", rank = 2)]
pub fn static_files(file: PathBuf) -> Option<NamedFile> {
2018-05-10 18:01:16 +00:00
NamedFile::open(Path::new("static/").join(file)).ok()
}