Federated blogging application, thanks to ActivityPub https://joinplu.me
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

88 lines
3.0 KiB

  1. pub use self::module::PlumeRocket;
  2. #[cfg(not(test))]
  3. mod module {
  4. use crate::{db_conn::DbConn, search, users};
  5. use riker::actors::ActorSystem;
  6. use rocket::{
  7. request::{self, FlashMessage, FromRequest, Request},
  8. Outcome, State,
  9. };
  10. use scheduled_thread_pool::ScheduledThreadPool;
  11. use std::sync::Arc;
  12. /// Common context needed by most routes and operations on models
  13. pub struct PlumeRocket {
  14. pub conn: DbConn,
  15. pub intl: rocket_i18n::I18n,
  16. pub user: Option<users::User>,
  17. pub searcher: Arc<search::Searcher>,
  18. pub worker: Arc<ScheduledThreadPool>,
  19. pub flash_msg: Option<(String, String)>,
  20. pub actors: Arc<ActorSystem>,
  21. }
  22. impl<'a, 'r> FromRequest<'a, 'r> for PlumeRocket {
  23. type Error = ();
  24. fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> {
  25. let conn = request.guard::<DbConn>()?;
  26. let intl = request.guard::<rocket_i18n::I18n>()?;
  27. let user = request.guard::<users::User>().succeeded();
  28. let worker = request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>()?;
  29. let searcher = request.guard::<'_, State<'_, Arc<search::Searcher>>>()?;
  30. let flash_msg = request.guard::<FlashMessage<'_, '_>>().succeeded();
  31. let actors = request.guard::<'_, State<'_, Arc<ActorSystem>>>()?;
  32. Outcome::Success(PlumeRocket {
  33. conn,
  34. intl,
  35. user,
  36. flash_msg: flash_msg.map(|f| (f.name().into(), f.msg().into())),
  37. worker: worker.clone(),
  38. searcher: searcher.clone(),
  39. actors: actors.clone(),
  40. })
  41. }
  42. }
  43. }
  44. #[cfg(test)]
  45. mod module {
  46. use crate::{db_conn::DbConn, search, users};
  47. use riker::actors::ActorSystem;
  48. use rocket::{
  49. request::{self, FromRequest, Request},
  50. Outcome, State,
  51. };
  52. use scheduled_thread_pool::ScheduledThreadPool;
  53. use std::sync::Arc;
  54. /// Common context needed by most routes and operations on models
  55. pub struct PlumeRocket {
  56. pub conn: DbConn,
  57. pub user: Option<users::User>,
  58. pub searcher: Arc<search::Searcher>,
  59. pub worker: Arc<ScheduledThreadPool>,
  60. pub actors: Arc<ActorSystem>,
  61. }
  62. impl<'a, 'r> FromRequest<'a, 'r> for PlumeRocket {
  63. type Error = ();
  64. fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> {
  65. let conn = request.guard::<DbConn>()?;
  66. let user = request.guard::<users::User>().succeeded();
  67. let worker = request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>()?;
  68. let searcher = request.guard::<'_, State<'_, Arc<search::Searcher>>>()?;
  69. let actors = request.guard::<'_, State<'_, Arc<ActorSystem>>>()?;
  70. Outcome::Success(PlumeRocket {
  71. conn,
  72. user,
  73. worker: worker.clone(),
  74. searcher: searcher.clone(),
  75. actors: actors.clone(),
  76. })
  77. }
  78. }
  79. }