]> Untitled Git - lemmy.git/blob - crates/api_common/src/context.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / api_common / src / context.rs
1 use lemmy_db_schema::{
2   source::secret::Secret,
3   utils::{ActualDbPool, DbPool},
4 };
5 use lemmy_utils::{
6   rate_limit::RateLimitCell,
7   settings::{structs::Settings, SETTINGS},
8 };
9 use reqwest_middleware::ClientWithMiddleware;
10 use std::sync::Arc;
11
12 #[derive(Clone)]
13 pub struct LemmyContext {
14   pool: ActualDbPool,
15   client: Arc<ClientWithMiddleware>,
16   secret: Arc<Secret>,
17   rate_limit_cell: RateLimitCell,
18 }
19
20 impl LemmyContext {
21   pub fn create(
22     pool: ActualDbPool,
23     client: ClientWithMiddleware,
24     secret: Secret,
25     rate_limit_cell: RateLimitCell,
26   ) -> LemmyContext {
27     LemmyContext {
28       pool,
29       client: Arc::new(client),
30       secret: Arc::new(secret),
31       rate_limit_cell,
32     }
33   }
34   pub fn pool(&self) -> DbPool<'_> {
35     DbPool::Pool(&self.pool)
36   }
37   pub fn inner_pool(&self) -> &ActualDbPool {
38     &self.pool
39   }
40   pub fn client(&self) -> &ClientWithMiddleware {
41     &self.client
42   }
43   pub fn settings(&self) -> &'static Settings {
44     &SETTINGS
45   }
46   pub fn secret(&self) -> &Secret {
47     &self.secret
48   }
49   pub fn settings_updated_channel(&self) -> &RateLimitCell {
50     &self.rate_limit_cell
51   }
52 }