start making PlumeRocket async

This commit is contained in:
Igor Galić 2020-01-25 23:37:44 +01:00
parent 944f8c42fa
commit ce119ffe50
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
3 changed files with 33 additions and 29 deletions

View File

@ -13,7 +13,7 @@ impl<'a, 'r> FromRequestAsync<'a, 'r> for Admin {
fn from_request(request: &'a Request<'r>) -> request::FromRequestFuture<'a, Self, Self::Error> {
Box::pin(async move {
let user = try_outcome!(request.guard::<User>());
let user = try_outcome!(User::from_request(request).await);
if user.is_admin() {
Outcome::Success(Admin(user))
} else {
@ -31,7 +31,7 @@ impl<'a, 'r> FromRequestAsync<'a, 'r> for Moderator {
fn from_request(request: &'a Request<'r>) -> request::FromRequestFuture<'a, Self, Self::Error> {
Box::pin(async move {
let user = try_outcome!(request.guard::<User>());
let user = try_outcome!(User::from_request(request).await);
if user.is_moderator() {
Outcome::Success(Moderator(user))
} else {

View File

@ -4,7 +4,7 @@ pub use self::module::PlumeRocket;
mod module {
use crate::{db_conn::DbConn, search, users};
use rocket::{
request::{self, FlashMessage, FromRequest, Request},
request::{self, FlashMessage, FromRequest, FromRequestAsync, Request},
Outcome, State,
};
use scheduled_thread_pool::ScheduledThreadPool;
@ -20,13 +20,14 @@ mod module {
pub flash_msg: Option<(String, String)>,
}
impl<'a, 'r> FromRequest<'a, 'r> for PlumeRocket {
impl<'a, 'r> FromRequestAsync<'a, 'r> for PlumeRocket {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> {
let conn = request.guard::<DbConn>()?;
let intl = request.guard::<rocket_i18n::I18n>()?;
let user = request.guard::<users::User>().succeeded();
fn from_request(request: &'a Request<'r>) -> request::FromRequestFuture<'a, Self, Self::Error> {
Box::pin(async move {
let conn = try_outcome!(DbConn::from_request(request).await);
let intl = try_outcome!(rocket_i18n::I18n::from_request(request).await);
let user = try_outcome!(users::User::from_request(request).await);
let worker = request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>()?;
let searcher = request.guard::<'_, State<'_, Arc<search::Searcher>>>()?;
let flash_msg = request.guard::<FlashMessage<'_, '_>>().succeeded();
@ -38,6 +39,7 @@ mod module {
worker: worker.clone(),
searcher: searcher.clone(),
})
})
}
}
}
@ -64,7 +66,7 @@ mod module {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> {
let conn = request.guard::<DbConn>()?;
let conn = DbConn::from_request(request).await;
let user = request.guard::<users::User>().succeeded();
let worker = request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>()?;
let searcher = request.guard::<'_, State<'_, Arc<search::Searcher>>>()?;

View File

@ -35,7 +35,7 @@ use reqwest::{
};
use rocket::{
outcome::IntoOutcome,
request::{self, FromRequest, Request},
request::{self, FromRequestAsync, Request},
};
use serde_json;
use std::{
@ -789,17 +789,19 @@ impl User {
}
}
impl<'a, 'r> FromRequest<'a, 'r> for User {
impl<'a, 'r> FromRequestAsync<'a, 'r> for User {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<User, ()> {
let conn = request.guard::<DbConn>()?;
fn from_request(request: &'a Request<'r>) -> request::FromRequestFuture<'a, Self, Self::Error> {
Box::pin(async move {
let conn = try_outcome!(DbConn::from_request(request).await);
request
.cookies()
.get_private(AUTH_COOKIE)
.and_then(|cookie| cookie.value().parse().ok())
.and_then(|id| User::get(&*conn, id).ok())
.or_forward(())
})
}
}