2018-07-20 16:42:35 +00:00
|
|
|
use rocket::{
|
|
|
|
http::uri::{FromUriParam, UriDisplay},
|
|
|
|
response::NamedFile
|
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
fmt,
|
|
|
|
path::{Path, PathBuf}
|
|
|
|
};
|
2018-05-10 18:01:16 +00:00
|
|
|
|
2018-06-18 17:28:28 +00:00
|
|
|
macro_rules! may_fail {
|
2018-06-21 22:50:06 +00:00
|
|
|
($account:expr, $expr:expr, $template:expr, $msg:expr, | $res:ident | $block:block) => {
|
2018-06-18 17:28:28 +00:00
|
|
|
{
|
|
|
|
let res = $expr;
|
|
|
|
if res.is_some() {
|
|
|
|
let $res = res.unwrap();
|
|
|
|
$block
|
|
|
|
} else {
|
2018-06-18 17:44:18 +00:00
|
|
|
Template::render(concat!("errors/", $template), json!({
|
2018-06-21 22:50:06 +00:00
|
|
|
"error_message": $msg,
|
|
|
|
"account": $account
|
2018-06-18 17:28:28 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-06-21 22:50:06 +00:00
|
|
|
($account:expr, $expr:expr, $msg:expr, | $res:ident | $block:block) => {
|
|
|
|
may_fail!($account, $expr, "404", $msg, |$res| {
|
2018-06-18 17:28:28 +00:00
|
|
|
$block
|
|
|
|
})
|
|
|
|
};
|
2018-06-21 22:50:06 +00:00
|
|
|
($account:expr, $expr:expr, | $res:ident | $block:block) => {
|
|
|
|
may_fail!($account, $expr, "", |$res| {
|
2018-06-18 17:28:28 +00:00
|
|
|
$block
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:42:35 +00:00
|
|
|
const ITEMS_PER_PAGE: i32 = 10;
|
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
pub struct Page {
|
|
|
|
page: i32
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UriDisplay for Page {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "page={}", &self.page as &UriDisplay)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromUriParam<i32> for Page {
|
|
|
|
type Target = Page;
|
|
|
|
fn from_uri_param(num: i32) -> Page {
|
|
|
|
Page { page: num }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Page {
|
|
|
|
pub fn first() -> Page {
|
|
|
|
Page {
|
|
|
|
page: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn limits(&self) -> (i32, i32) {
|
|
|
|
((self.page - 1) * ITEMS_PER_PAGE, self.page * ITEMS_PER_PAGE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2018-04-22 13:35:37 +00:00
|
|
|
pub mod instance;
|
2018-05-10 16:38:03 +00:00
|
|
|
pub mod likes;
|
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-04-22 18:13:12 +00:00
|
|
|
pub mod user;
|
2018-04-24 08:35:45 +00:00
|
|
|
pub mod well_known;
|
2018-05-10 18:01:16 +00:00
|
|
|
|
|
|
|
#[get("/static/<file..>")]
|
|
|
|
fn static_files(file: PathBuf) -> Option<NamedFile> {
|
|
|
|
NamedFile::open(Path::new("static/").join(file)).ok()
|
|
|
|
}
|