may_fail! macro

Returns an error template if an Option is None, else runs the given block
This commit is contained in:
Bat 2018-06-18 18:28:28 +01:00
parent 7e3cdec0b6
commit 51571d6320
5 changed files with 90 additions and 57 deletions

View File

@ -19,14 +19,16 @@ use utils;
#[get("/~/<name>", rank = 2)]
fn details(name: String, conn: DbConn, user: Option<User>) -> Template {
let blog = Blog::find_by_fqn(&*conn, name).unwrap();
may_fail!(Blog::find_by_fqn(&*conn, name), "Requested blog couldn't be found", |blog| {
let recents = Post::get_recents_for_blog(&*conn, &blog, 5);
Template::render("blogs/details", json!({
"blog": blog,
"account": user,
"is_author": user.map(|x| x.is_author_in(&*conn, blog)),
"recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>()
}))
})
}
#[get("/~/<name>", format = "application/activity+json", rank = 1)]

View File

@ -17,11 +17,12 @@ use safe_string::SafeString;
#[get("/~/<_blog>/<slug>/comment")]
fn new(_blog: String, slug: String, user: User, conn: DbConn) -> Template {
let post = Post::find_by_slug(&*conn, slug).unwrap();
may_fail!(Post::find_by_slug(&*conn, slug), "Couldn't find this post", |post| {
Template::render("comments/new", json!({
"post": post,
"account": user
}))
})
}
#[get("/~/<blog>/<slug>/comment", rank=2)]

View File

@ -1,6 +1,32 @@
use rocket::response::NamedFile;
use std::path::{Path, PathBuf};
macro_rules! may_fail {
($expr:expr, $template:expr, $msg:expr, | $res:ident | $block:block) => {
{
let res = $expr;
if res.is_some() {
let $res = res.unwrap();
$block
} else {
Template::render(concat!("errors/", stringify!($template)), json!({
"error_message": $msg
}))
}
}
};
($expr:expr, $msg:expr, | $res:ident | $block:block) => {
may_fail!($expr, "404", $msg, |$res| {
$block
})
};
($expr:expr, | $res:ident | $block:block) => {
may_fail!($expr, "", |$res| {
$block
})
};
}
pub mod blogs;
pub mod comments;
pub mod errors;

View File

@ -19,8 +19,8 @@ use safe_string::SafeString;
#[get("/~/<blog>/<slug>", rank = 4)]
fn details(blog: String, slug: String, conn: DbConn, user: Option<User>) -> Template {
let blog = Blog::find_by_fqn(&*conn, blog).unwrap();
let post = Post::find_by_slug(&*conn, slug).unwrap();
may_fail!(Blog::find_by_fqn(&*conn, blog), "Couldn't find this blog", |blog| {
may_fail!(Post::find_by_slug(&*conn, slug), "Couldn't find this post", |post| {
let comments = Comment::find_by_post(&*conn, post.id);
Template::render("posts/details", json!({
@ -35,6 +35,8 @@ fn details(blog: String, slug: String, conn: DbConn, user: Option<User>) -> Temp
"account": user,
"date": &post.creation_date.timestamp()
}))
})
})
}
#[get("/~/<_blog>/<slug>", rank = 3, format = "application/activity+json")]

View File

@ -25,7 +25,7 @@ use models::{
use utils;
#[get("/me")]
fn me(user: Option<User>) -> Result<Redirect,Flash<Redirect>> {
fn me(user: Option<User>) -> Result<Redirect, Flash<Redirect>> {
match user {
Some(user) => Ok(Redirect::to(format!("/@/{}/", user.username))),
None => Err(utils::requires_login("", "/me"))
@ -34,7 +34,7 @@ fn me(user: Option<User>) -> Result<Redirect,Flash<Redirect>> {
#[get("/@/<name>", rank = 2)]
fn details(name: String, conn: DbConn, account: Option<User>) -> Template {
let user = User::find_by_fqn(&*conn, name).unwrap();
may_fail!(User::find_by_fqn(&*conn, name), "Couldn't find requested user", |user| {
let recents = Post::get_recents_for_author(&*conn, &user, 6);
let reshares = Reshare::get_recents_for_author(&*conn, &user, 6);
let user_id = user.id.clone();
@ -51,6 +51,7 @@ fn details(name: String, conn: DbConn, account: Option<User>) -> Template {
"is_self": account.map(|a| a.id == user_id).unwrap_or(false),
"n_followers": n_followers
}))
})
}
#[get("/dashboard")]
@ -91,7 +92,7 @@ fn follow_auth(name: String) -> Flash<Redirect> {
#[get("/@/<name>/followers", rank = 2)]
fn followers(name: String, conn: DbConn, account: Option<User>) -> Template {
let user = User::find_by_fqn(&*conn, name.clone()).unwrap();
may_fail!(User::find_by_fqn(&*conn, name.clone()), "Couldn't find requested user", |user| {
let user_id = user.id.clone();
Template::render("users/followers", json!({
@ -104,6 +105,7 @@ fn followers(name: String, conn: DbConn, account: Option<User>) -> Template {
"is_self": account.map(|a| a.id == user_id).unwrap_or(false),
"n_followers": user.get_followers(&*conn).len()
}))
})
}
#[get("/@/<name>", format = "application/activity+json", rank = 1)]