add enum containing all successful route returns (#614)
* add enum containing all successful route returns This enum derives `Responder`, so it can be used as route result. We also implement `From`, so it can be converted * disable clippy warning about this enum There's no use trying to chase this warning. The next warning will be about `Redirect`, which itself is 360 byte, according to @fdb-hiroshima (thanks for the research!) So, if anything, we can try to get Rocket to work on that! * refactor routes/posts to only use one level of Result * admin settings error is not an ErrorPage, so dont use Result<> * refactor: use early return to remove indent of main logic This diff is absolutely atrocious. but the resulting readability is worth it * refactor routes/post for early returns & RespondOrRedirect * refactor routes/session for early returns & use RespondOrRedirect * refactor routes/user -- add another field to enum * refactor routes/blogs for early returns & RespondOrRedirect * refactor routes/blogs for early returns & RespondOrRedirect This is a final refactor of `pub fn update()` for readability. We're removing at least one indentation level.
This commit is contained in:
+37
-28
@@ -26,7 +26,9 @@ use plume_models::{
|
||||
users::User,
|
||||
Error, PlumeRocket,
|
||||
};
|
||||
use routes::{comments::NewCommentForm, errors::ErrorPage, ContentLen, RemoteForm};
|
||||
use routes::{
|
||||
comments::NewCommentForm, errors::ErrorPage, ContentLen, RemoteForm, RespondOrRedirect,
|
||||
};
|
||||
use template_utils::{IntoContext, Ructe};
|
||||
|
||||
#[get("/~/<blog>/<slug>?<responding_to>", rank = 4)]
|
||||
@@ -40,17 +42,23 @@ pub fn details(
|
||||
let user = rockets.user.clone();
|
||||
let blog = Blog::find_by_fqn(&rockets, &blog)?;
|
||||
let post = Post::find_by_slug(&*conn, &slug, blog.id)?;
|
||||
if post.published
|
||||
if !(post.published
|
||||
|| post
|
||||
.get_authors(&*conn)?
|
||||
.into_iter()
|
||||
.any(|a| a.id == user.clone().map(|u| u.id).unwrap_or(0))
|
||||
.any(|a| a.id == user.clone().map(|u| u.id).unwrap_or(0)))
|
||||
{
|
||||
let comments = CommentTree::from_post(&*conn, &post, user.as_ref())?;
|
||||
return Ok(render!(errors::not_authorized(
|
||||
&rockets.to_context(),
|
||||
i18n!(rockets.intl.catalog, "This post isn't published yet.")
|
||||
)));
|
||||
}
|
||||
|
||||
let previous = responding_to.and_then(|r| Comment::get(&*conn, r).ok());
|
||||
let comments = CommentTree::from_post(&*conn, &post, user.as_ref())?;
|
||||
|
||||
Ok(render!(posts::details(
|
||||
let previous = responding_to.and_then(|r| Comment::get(&*conn, r).ok());
|
||||
|
||||
Ok(render!(posts::details(
|
||||
&rockets.to_context(),
|
||||
post.clone(),
|
||||
blog,
|
||||
@@ -87,12 +95,6 @@ pub fn details(
|
||||
user.and_then(|u| u.is_following(&*conn, post.get_authors(&*conn).ok()?[0].id).ok()).unwrap_or(false),
|
||||
post.get_authors(&*conn)?[0].clone()
|
||||
)))
|
||||
} else {
|
||||
Ok(render!(errors::not_authorized(
|
||||
&rockets.to_context(),
|
||||
i18n!(rockets.intl.catalog, "This post isn't published yet.")
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/~/<blog>/<slug>", rank = 3)]
|
||||
@@ -219,7 +221,7 @@ pub fn update(
|
||||
cl: ContentLen,
|
||||
form: LenientForm<NewPostForm>,
|
||||
rockets: PlumeRocket,
|
||||
) -> Result<Flash<Redirect>, Ructe> {
|
||||
) -> RespondOrRedirect {
|
||||
let conn = &*rockets.conn;
|
||||
let b = Blog::find_by_fqn(&rockets, &blog).expect("post::update: blog error");
|
||||
let mut post =
|
||||
@@ -255,10 +257,11 @@ pub fn update(
|
||||
.expect("posts::update: is author in error")
|
||||
{
|
||||
// actually it's not "Ok"…
|
||||
Ok(Flash::error(
|
||||
Flash::error(
|
||||
Redirect::to(uri!(super::blogs::details: name = blog, page = _)),
|
||||
i18n!(&intl, "You are not allowed to publish on this blog."),
|
||||
))
|
||||
)
|
||||
.into()
|
||||
} else {
|
||||
let (content, mentions, hashtags) = utils::md_to_html(
|
||||
form.content.to_string().as_ref(),
|
||||
@@ -345,14 +348,15 @@ pub fn update(
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Flash::success(
|
||||
Flash::success(
|
||||
Redirect::to(uri!(details: blog = blog, slug = new_slug, responding_to = _)),
|
||||
i18n!(intl, "Your article has been updated."),
|
||||
))
|
||||
)
|
||||
.into()
|
||||
}
|
||||
} else {
|
||||
let medias = Media::for_user(&*conn, user.id).expect("posts:update: medias error");
|
||||
Err(render!(posts::new(
|
||||
render!(posts::new(
|
||||
&rockets.to_context(),
|
||||
i18n!(intl, "Edit {0}"; &form.title),
|
||||
b,
|
||||
@@ -363,7 +367,8 @@ pub fn update(
|
||||
errors.clone(),
|
||||
medias.clone(),
|
||||
cl.0
|
||||
)))
|
||||
))
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,7 +401,7 @@ pub fn create(
|
||||
form: LenientForm<NewPostForm>,
|
||||
cl: ContentLen,
|
||||
rockets: PlumeRocket,
|
||||
) -> Result<Flash<Redirect>, Result<Ructe, ErrorPage>> {
|
||||
) -> Result<RespondOrRedirect, ErrorPage> {
|
||||
let conn = &*rockets.conn;
|
||||
let blog = Blog::find_by_fqn(&rockets, &blog_name).expect("post::create: blog error");;
|
||||
let slug = form.title.to_string().to_kebab_case();
|
||||
@@ -429,7 +434,8 @@ pub fn create(
|
||||
&rockets.intl.catalog,
|
||||
"You are not allowed to publish on this blog."
|
||||
),
|
||||
));
|
||||
)
|
||||
.into());
|
||||
}
|
||||
|
||||
let (content, mentions, hashtags) = utils::md_to_html(
|
||||
@@ -530,10 +536,11 @@ pub fn create(
|
||||
Ok(Flash::success(
|
||||
Redirect::to(uri!(details: blog = blog_name, slug = slug, responding_to = _)),
|
||||
i18n!(&rockets.intl.catalog, "Your article has been saved."),
|
||||
))
|
||||
)
|
||||
.into())
|
||||
} else {
|
||||
let medias = Media::for_user(&*conn, user.id).expect("posts::create: medias error");
|
||||
Err(Ok(render!(posts::new(
|
||||
Ok(render!(posts::new(
|
||||
&rockets.to_context(),
|
||||
i18n!(rockets.intl.catalog, "New article"),
|
||||
blog,
|
||||
@@ -544,7 +551,8 @@ pub fn create(
|
||||
errors.clone(),
|
||||
medias,
|
||||
cl.0
|
||||
))))
|
||||
))
|
||||
.into())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -627,14 +635,14 @@ pub fn remote_interact_post(
|
||||
blog_name: String,
|
||||
slug: String,
|
||||
remote: LenientForm<RemoteForm>,
|
||||
) -> Result<Result<Ructe, Redirect>, ErrorPage> {
|
||||
) -> Result<RespondOrRedirect, ErrorPage> {
|
||||
let target = Blog::find_by_fqn(&rockets, &blog_name)
|
||||
.and_then(|blog| Post::find_by_slug(&rockets.conn, &slug, blog.id))?;
|
||||
if let Some(uri) = User::fetch_remote_interact_uri(&remote.remote)
|
||||
.ok()
|
||||
.and_then(|uri| rt_format!(uri, uri = target.ap_url).ok())
|
||||
{
|
||||
Ok(Err(Redirect::to(uri)))
|
||||
Ok(Redirect::to(uri).into())
|
||||
} else {
|
||||
let mut errs = ValidationErrors::new();
|
||||
errs.add("remote", ValidationError {
|
||||
@@ -643,13 +651,14 @@ pub fn remote_interact_post(
|
||||
params: HashMap::new(),
|
||||
});
|
||||
//could not get your remote url?
|
||||
Ok(Ok(render!(posts::remote_interact(
|
||||
Ok(render!(posts::remote_interact(
|
||||
&rockets.to_context(),
|
||||
target,
|
||||
super::session::LoginForm::default(),
|
||||
ValidationErrors::default(),
|
||||
remote.clone(),
|
||||
errs
|
||||
))))
|
||||
))
|
||||
.into())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user