From ce119ffe50e7237c38fbaf7b5869b8cfdfb8c27a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Sat, 25 Jan 2020 23:37:44 +0100 Subject: [PATCH] start making PlumeRocket async --- plume-models/src/admin.rs | 4 ++-- plume-models/src/plume_rocket.rs | 36 +++++++++++++++++--------------- plume-models/src/users.rs | 22 ++++++++++--------- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/plume-models/src/admin.rs b/plume-models/src/admin.rs index 325198ea..1dd810a9 100644 --- a/plume-models/src/admin.rs +++ b/plume-models/src/admin.rs @@ -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::()); + 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::()); + let user = try_outcome!(User::from_request(request).await); if user.is_moderator() { Outcome::Success(Moderator(user)) } else { diff --git a/plume-models/src/plume_rocket.rs b/plume-models/src/plume_rocket.rs index 0a2b3cec..ca5f05df 100644 --- a/plume-models/src/plume_rocket.rs +++ b/plume-models/src/plume_rocket.rs @@ -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,23 +20,25 @@ 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 { - let conn = request.guard::()?; - let intl = request.guard::()?; - let user = request.guard::().succeeded(); - let worker = request.guard::<'_, State<'_, Arc>>()?; - let searcher = request.guard::<'_, State<'_, Arc>>()?; - let flash_msg = request.guard::>().succeeded(); - Outcome::Success(PlumeRocket { - conn, - intl, - user, - flash_msg: flash_msg.map(|f| (f.name().into(), f.msg().into())), - worker: worker.clone(), - searcher: searcher.clone(), + 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>>()?; + let searcher = request.guard::<'_, State<'_, Arc>>()?; + let flash_msg = request.guard::>().succeeded(); + Outcome::Success(PlumeRocket { + conn, + intl, + user, + flash_msg: flash_msg.map(|f| (f.name().into(), f.msg().into())), + worker: worker.clone(), + searcher: searcher.clone(), + }) }) } } @@ -64,7 +66,7 @@ mod module { type Error = (); fn from_request(request: &'a Request<'r>) -> request::Outcome { - let conn = request.guard::()?; + let conn = DbConn::from_request(request).await; let user = request.guard::().succeeded(); let worker = request.guard::<'_, State<'_, Arc>>()?; let searcher = request.guard::<'_, State<'_, Arc>>()?; diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index 7da04d4a..5d1dd5fa 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -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 { - let conn = request.guard::()?; - request - .cookies() - .get_private(AUTH_COOKIE) - .and_then(|cookie| cookie.value().parse().ok()) - .and_then(|id| User::get(&*conn, id).ok()) - .or_forward(()) + 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(()) + }) } }